Trying to debug config write failures.
[dcpomatic.git] / src / lib / config.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <sstream>
21 #include <cstdlib>
22 #include <fstream>
23 #include <glib.h>
24 #include <boost/filesystem.hpp>
25 #include <libdcp/colour_matrix.h>
26 #include <libcxml/cxml.h>
27 #include "config.h"
28 #include "server.h"
29 #include "scaler.h"
30 #include "filter.h"
31 #include "ratio.h"
32 #include "dcp_content_type.h"
33 #include "sound_processor.h"
34 #include "colour_conversion.h"
35 #include "cinema.h"
36
37 #include "i18n.h"
38
39 using std::vector;
40 using std::ifstream;
41 using std::string;
42 using std::ofstream;
43 using std::list;
44 using std::max;
45 using boost::shared_ptr;
46 using boost::lexical_cast;
47 using boost::optional;
48
49 Config* Config::_instance = 0;
50
51 /** Construct default configuration */
52 Config::Config ()
53         : _num_local_encoding_threads (max (2U, boost::thread::hardware_concurrency()))
54         , _server_port (6192)
55         , _tms_path (".")
56         , _sound_processor (SoundProcessor::from_id (N_("dolby_cp750")))
57         , _default_still_length (10)
58         , _default_container (Ratio::from_id ("185"))
59         , _default_dcp_content_type (DCPContentType::from_dci_name ("TST"))
60         , _default_j2k_bandwidth (200000000)
61         , _kdm_email (
62                 "Dear Projectionist\n\nPlease find attached KDMs for $CPL_NAME.\n\nBest regards,\nDCP-o-matic"
63                 )
64 {
65         _allowed_dcp_frame_rates.push_back (24);
66         _allowed_dcp_frame_rates.push_back (25);
67         _allowed_dcp_frame_rates.push_back (30);
68         _allowed_dcp_frame_rates.push_back (48);
69         _allowed_dcp_frame_rates.push_back (50);
70         _allowed_dcp_frame_rates.push_back (60);
71
72         _colour_conversions.push_back (PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6));
73         _colour_conversions.push_back (PresetColourConversion (_("sRGB non-linearised"), 2.4, false, libdcp::colour_matrix::srgb_to_xyz, 2.6));
74         _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, libdcp::colour_matrix::rec709_to_xyz, 2.6));
75 }
76
77 void
78 Config::read ()
79 {
80         if (!boost::filesystem::exists (file (false))) {
81                 read_old_metadata ();
82                 return;
83         }
84
85         cxml::Document f ("Config");
86         f.read_file (file (false));
87         optional<string> c;
88
89         optional<int> version = f.optional_number_child<int> ("Version");
90
91         _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
92         _default_directory = f.string_child ("DefaultDirectory");
93         _server_port = f.number_child<int> ("ServerPort");
94         
95         list<shared_ptr<cxml::Node> > servers = f.node_children ("Server");
96         for (list<shared_ptr<cxml::Node> >::iterator i = servers.begin(); i != servers.end(); ++i) {
97                 _servers.push_back (ServerDescription (*i));
98         }
99
100         _tms_ip = f.string_child ("TMSIP");
101         _tms_path = f.string_child ("TMSPath");
102         _tms_user = f.string_child ("TMSUser");
103         _tms_password = f.string_child ("TMSPassword");
104
105         c = f.optional_string_child ("SoundProcessor");
106         if (c) {
107                 _sound_processor = SoundProcessor::from_id (c.get ());
108         }
109
110         _language = f.optional_string_child ("Language");
111
112         c = f.optional_string_child ("DefaultContainer");
113         if (c) {
114                 _default_container = Ratio::from_id (c.get ());
115         }
116
117         c = f.optional_string_child ("DefaultDCPContentType");
118         if (c) {
119                 _default_dcp_content_type = DCPContentType::from_dci_name (c.get ());
120         }
121
122         _dcp_metadata.issuer = f.optional_string_child ("DCPMetadataIssuer").get_value_or ("");
123         _dcp_metadata.creator = f.optional_string_child ("DCPMetadataCreator").get_value_or ("");
124
125         _default_dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
126         _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
127         _default_j2k_bandwidth = f.optional_number_child<int>("DefaultJ2KBandwidth").get_value_or (200000000);
128
129         list<shared_ptr<cxml::Node> > cc = f.node_children ("ColourConversion");
130
131         if (!cc.empty ()) {
132                 _colour_conversions.clear ();
133         }
134         
135         for (list<shared_ptr<cxml::Node> >::iterator i = cc.begin(); i != cc.end(); ++i) {
136                 _colour_conversions.push_back (PresetColourConversion (*i));
137         }
138
139         if (!version) {
140                 /* Loading version 0 (before Rec. 709 was added as a preset).
141                    Add it in.
142                 */
143                 _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, libdcp::colour_matrix::rec709_to_xyz, 2.6));
144         }
145
146         list<shared_ptr<cxml::Node> > cin = f.node_children ("Cinema");
147         for (list<shared_ptr<cxml::Node> >::iterator i = cin.begin(); i != cin.end(); ++i) {
148                 /* Slightly grotty two-part construction of Cinema here so that we can use
149                    shared_from_this.
150                 */
151                 shared_ptr<Cinema> cinema (new Cinema (*i));
152                 cinema->read_screens (*i);
153                 _cinemas.push_back (cinema);
154         }
155
156         _mail_server = f.string_child ("MailServer");
157         _kdm_from = f.string_child ("KDMFrom");
158         _kdm_email = f.string_child ("KDMEmail");
159 }
160
161 void
162 Config::read_old_metadata ()
163 {
164         ifstream f (file(true).string().c_str ());
165         string line;
166
167         while (getline (f, line)) {
168                 if (line.empty ()) {
169                         continue;
170                 }
171
172                 if (line[0] == '#') {
173                         continue;
174                 }
175
176                 size_t const s = line.find (' ');
177                 if (s == string::npos) {
178                         continue;
179                 }
180                 
181                 string const k = line.substr (0, s);
182                 string const v = line.substr (s + 1);
183
184                 if (k == N_("num_local_encoding_threads")) {
185                         _num_local_encoding_threads = atoi (v.c_str ());
186                 } else if (k == N_("default_directory")) {
187                         _default_directory = v;
188                 } else if (k == N_("server_port")) {
189                         _server_port = atoi (v.c_str ());
190                 } else if (k == N_("server")) {
191                         optional<ServerDescription> server = ServerDescription::create_from_metadata (v);
192                         if (server) {
193                                 _servers.push_back (server.get ());
194                         }
195                 } else if (k == N_("tms_ip")) {
196                         _tms_ip = v;
197                 } else if (k == N_("tms_path")) {
198                         _tms_path = v;
199                 } else if (k == N_("tms_user")) {
200                         _tms_user = v;
201                 } else if (k == N_("tms_password")) {
202                         _tms_password = v;
203                 } else if (k == N_("sound_processor")) {
204                         _sound_processor = SoundProcessor::from_id (v);
205                 } else if (k == "language") {
206                         _language = v;
207                 } else if (k == "default_container") {
208                         _default_container = Ratio::from_id (v);
209                 } else if (k == "default_dcp_content_type") {
210                         _default_dcp_content_type = DCPContentType::from_dci_name (v);
211                 } else if (k == "dcp_metadata_issuer") {
212                         _dcp_metadata.issuer = v;
213                 } else if (k == "dcp_metadata_creator") {
214                         _dcp_metadata.creator = v;
215                 } else if (k == "dcp_metadata_issue_date") {
216                         _dcp_metadata.issue_date = v;
217                 }
218
219                 _default_dci_metadata.read_old_metadata (k, v);
220         }
221 }
222
223 /** @return Filename to write configuration to */
224 boost::filesystem::path
225 Config::file (bool old) const
226 {
227         boost::filesystem::path p;
228         p /= g_get_user_config_dir ();
229         boost::system::error_code ec;
230         boost::filesystem::create_directory (p, ec);
231         if (old) {
232                 p /= ".dvdomatic";
233         } else {
234                 p /= "dcpomatic";
235                 boost::filesystem::create_directory (p, ec);
236                 p /= "config.xml";
237         }
238         return p;
239 }
240
241 boost::filesystem::path
242 Config::signer_chain_directory () const
243 {
244         boost::filesystem::path p;
245         p /= g_get_user_config_dir ();
246         p /= "dcpomatic";
247         p /= "crypt";
248         boost::filesystem::create_directories (p);
249         return p;
250 }
251
252 /** @return Singleton instance */
253 Config *
254 Config::instance ()
255 {
256         if (_instance == 0) {
257                 _instance = new Config;
258                 try {
259                         _instance->read ();
260                 } catch (...) {
261                         /* configuration load failed; never mind, just
262                            stick with the default.
263                         */
264                 }
265         }
266
267         return _instance;
268 }
269
270 /** Write our configuration to disk */
271 void
272 Config::write () const
273 {
274         xmlpp::Document doc;
275         xmlpp::Element* root = doc.create_root_node ("Config");
276
277         root->add_child("Version")->add_child_text ("1");
278         root->add_child("NumLocalEncodingThreads")->add_child_text (lexical_cast<string> (_num_local_encoding_threads));
279         root->add_child("DefaultDirectory")->add_child_text (_default_directory.string ());
280         root->add_child("ServerPort")->add_child_text (lexical_cast<string> (_server_port));
281         
282         for (vector<ServerDescription>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
283                 i->as_xml (root->add_child ("Server"));
284         }
285
286         root->add_child("TMSIP")->add_child_text (_tms_ip);
287         root->add_child("TMSPath")->add_child_text (_tms_path);
288         root->add_child("TMSUser")->add_child_text (_tms_user);
289         root->add_child("TMSPassword")->add_child_text (_tms_password);
290         if (_sound_processor) {
291                 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
292         }
293         if (_language) {
294                 root->add_child("Language")->add_child_text (_language.get());
295         }
296         if (_default_container) {
297                 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
298         }
299         if (_default_dcp_content_type) {
300                 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->dci_name ());
301         }
302         root->add_child("DCPMetadataIssuer")->add_child_text (_dcp_metadata.issuer);
303         root->add_child("DCPMetadataCreator")->add_child_text (_dcp_metadata.creator);
304
305         _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
306
307         root->add_child("DefaultStillLength")->add_child_text (lexical_cast<string> (_default_still_length));
308         root->add_child("DefaultJ2KBandwidth")->add_child_text (lexical_cast<string> (_default_j2k_bandwidth));
309
310         for (vector<PresetColourConversion>::const_iterator i = _colour_conversions.begin(); i != _colour_conversions.end(); ++i) {
311                 i->as_xml (root->add_child ("ColourConversion"));
312         }
313
314         for (list<shared_ptr<Cinema> >::const_iterator i = _cinemas.begin(); i != _cinemas.end(); ++i) {
315                 (*i)->as_xml (root->add_child ("Cinema"));
316         }
317
318         root->add_child("MailServer")->add_child_text (_mail_server);
319         root->add_child("KDMFrom")->add_child_text (_kdm_from);
320         root->add_child("KDMEmail")->add_child_text (_kdm_email);
321
322         std::cout << "dcpomatic: writing configuration to " << file(false).string() << "\n";
323         doc.write_to_file_formatted (file(false).string ());
324         std::cout << "dcpomatic: wrote configuration to " << file(false).string() << "\n";
325 }
326
327 boost::filesystem::path
328 Config::default_directory_or (boost::filesystem::path a) const
329 {
330         if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
331                 return a;
332         }
333
334         return _default_directory;
335 }
336
337 void
338 Config::drop ()
339 {
340         delete _instance;
341         _instance = 0;
342 }