Remove LocaleGuard and lexical_cast<> in favour of libdcp::raw_convert,
[dcpomatic.git] / src / lib / config.cc
1 /*
2     Copyright (C) 2012-2014 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 <boost/algorithm/string.hpp>
26 #include <libdcp/colour_matrix.h>
27 #include <libdcp/raw_convert.h>
28 #include <libcxml/cxml.h>
29 #include "config.h"
30 #include "server.h"
31 #include "scaler.h"
32 #include "filter.h"
33 #include "ratio.h"
34 #include "dcp_content_type.h"
35 #include "sound_processor.h"
36 #include "colour_conversion.h"
37 #include "cinema.h"
38 #include "util.h"
39
40 #include "i18n.h"
41
42 using std::vector;
43 using std::ifstream;
44 using std::string;
45 using std::list;
46 using std::max;
47 using std::exception;
48 using std::cerr;
49 using boost::shared_ptr;
50 using boost::optional;
51 using boost::algorithm::is_any_of;
52 using boost::algorithm::split;
53 using libdcp::raw_convert;
54
55 Config* Config::_instance = 0;
56
57 /** Construct default configuration */
58 Config::Config ()
59         : _num_local_encoding_threads (max (2U, boost::thread::hardware_concurrency()))
60         , _server_port_base (6192)
61         , _use_any_servers (true)
62         , _tms_path (".")
63         , _sound_processor (SoundProcessor::from_id (N_("dolby_cp750")))
64         , _default_still_length (10)
65         , _default_container (Ratio::from_id ("185"))
66         , _default_dcp_content_type (DCPContentType::from_dci_name ("TST"))
67         , _default_j2k_bandwidth (100000000)
68         , _default_audio_delay (0)
69         , _kdm_email (
70                 _("Dear Projectionist\n\nPlease find attached KDMs for $CPL_NAME.\n\nThe KDMs are valid from $START_TIME until $END_TIME.\n\nBest regards,\nDCP-o-matic")
71                 )
72         , _check_for_updates (false)
73         , _check_for_test_updates (false)
74         , _maximum_j2k_bandwidth (250000000)
75 {
76         _allowed_dcp_frame_rates.push_back (24);
77         _allowed_dcp_frame_rates.push_back (25);
78         _allowed_dcp_frame_rates.push_back (30);
79         _allowed_dcp_frame_rates.push_back (48);
80         _allowed_dcp_frame_rates.push_back (50);
81         _allowed_dcp_frame_rates.push_back (60);
82
83         _colour_conversions.push_back (PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6));
84         _colour_conversions.push_back (PresetColourConversion (_("sRGB non-linearised"), 2.4, false, libdcp::colour_matrix::srgb_to_xyz, 2.6));
85         _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, libdcp::colour_matrix::rec709_to_xyz, 2.6));
86 }
87
88 void
89 Config::read ()
90 {
91         if (!boost::filesystem::exists (file (false))) {
92                 read_old_metadata ();
93                 return;
94         }
95
96         cxml::Document f ("Config");
97         f.read_file (file (false));
98         optional<string> c;
99
100         optional<int> version = f.optional_number_child<int> ("Version");
101
102         _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
103         _default_directory = f.string_child ("DefaultDirectory");
104
105         boost::optional<int> b = f.optional_number_child<int> ("ServerPort");
106         if (!b) {
107                 b = f.optional_number_child<int> ("ServerPortBase");
108         }
109         _server_port_base = b.get ();
110
111         boost::optional<bool> u = f.optional_bool_child ("UseAnyServers");
112         _use_any_servers = u.get_value_or (true);
113
114         list<cxml::NodePtr> servers = f.node_children ("Server");
115         for (list<cxml::NodePtr>::iterator i = servers.begin(); i != servers.end(); ++i) {
116                 if ((*i)->node_children("HostName").size() == 1) {
117                         _servers.push_back ((*i)->string_child ("HostName"));
118                 } else {
119                         _servers.push_back ((*i)->content ());
120                 }
121         }
122         
123         _tms_ip = f.string_child ("TMSIP");
124         _tms_path = f.string_child ("TMSPath");
125         _tms_user = f.string_child ("TMSUser");
126         _tms_password = f.string_child ("TMSPassword");
127
128         c = f.optional_string_child ("SoundProcessor");
129         if (c) {
130                 _sound_processor = SoundProcessor::from_id (c.get ());
131         }
132
133         _language = f.optional_string_child ("Language");
134
135         c = f.optional_string_child ("DefaultContainer");
136         if (c) {
137                 _default_container = Ratio::from_id (c.get ());
138         }
139
140         c = f.optional_string_child ("DefaultDCPContentType");
141         if (c) {
142                 _default_dcp_content_type = DCPContentType::from_dci_name (c.get ());
143         }
144
145         _dcp_metadata.issuer = f.optional_string_child ("DCPMetadataIssuer").get_value_or ("");
146         _dcp_metadata.creator = f.optional_string_child ("DCPMetadataCreator").get_value_or ("");
147
148         _default_dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
149         _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
150         _default_j2k_bandwidth = f.optional_number_child<int>("DefaultJ2KBandwidth").get_value_or (200000000);
151         _default_audio_delay = f.optional_number_child<int>("DefaultAudioDelay").get_value_or (0);
152
153         list<cxml::NodePtr> cc = f.node_children ("ColourConversion");
154
155         if (!cc.empty ()) {
156                 _colour_conversions.clear ();
157         }
158         
159         for (list<cxml::NodePtr>::iterator i = cc.begin(); i != cc.end(); ++i) {
160                 _colour_conversions.push_back (PresetColourConversion (*i));
161         }
162
163         if (!version) {
164                 /* Loading version 0 (before Rec. 709 was added as a preset).
165                    Add it in.
166                 */
167                 _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, libdcp::colour_matrix::rec709_to_xyz, 2.6));
168         }
169
170         list<cxml::NodePtr> cin = f.node_children ("Cinema");
171         for (list<cxml::NodePtr>::iterator i = cin.begin(); i != cin.end(); ++i) {
172                 /* Slightly grotty two-part construction of Cinema here so that we can use
173                    shared_from_this.
174                 */
175                 shared_ptr<Cinema> cinema (new Cinema (*i));
176                 cinema->read_screens (*i);
177                 _cinemas.push_back (cinema);
178         }
179
180         _mail_server = f.string_child ("MailServer");
181         _mail_user = f.optional_string_child("MailUser").get_value_or ("");
182         _mail_password = f.optional_string_child("MailPassword").get_value_or ("");
183         _kdm_from = f.string_child ("KDMFrom");
184         _kdm_email = f.string_child ("KDMEmail");
185
186         _check_for_updates = f.optional_bool_child("CheckForUpdates").get_value_or (false);
187         _check_for_test_updates = f.optional_bool_child("CheckForTestUpdates").get_value_or (false);
188
189         _maximum_j2k_bandwidth = f.optional_number_child<int> ("MaximumJ2KBandwidth").get_value_or (250000000);
190 }
191
192 void
193 Config::read_old_metadata ()
194 {
195         /* XXX: this won't work with non-Latin filenames */
196         ifstream f (file(true).string().c_str ());
197         string line;
198
199         while (getline (f, line)) {
200                 if (line.empty ()) {
201                         continue;
202                 }
203
204                 if (line[0] == '#') {
205                         continue;
206                 }
207
208                 size_t const s = line.find (' ');
209                 if (s == string::npos) {
210                         continue;
211                 }
212                 
213                 string const k = line.substr (0, s);
214                 string const v = line.substr (s + 1);
215
216                 if (k == N_("num_local_encoding_threads")) {
217                         _num_local_encoding_threads = atoi (v.c_str ());
218                 } else if (k == N_("default_directory")) {
219                         _default_directory = v;
220                 } else if (k == N_("server_port")) {
221                         _server_port_base = atoi (v.c_str ());
222                 } else if (k == N_("server")) {
223                         vector<string> b;
224                         split (b, v, is_any_of (" "));
225                         if (b.size() == 2) {
226                                 _servers.push_back (b[0]);
227                         }
228                 } else if (k == N_("tms_ip")) {
229                         _tms_ip = v;
230                 } else if (k == N_("tms_path")) {
231                         _tms_path = v;
232                 } else if (k == N_("tms_user")) {
233                         _tms_user = v;
234                 } else if (k == N_("tms_password")) {
235                         _tms_password = v;
236                 } else if (k == N_("sound_processor")) {
237                         _sound_processor = SoundProcessor::from_id (v);
238                 } else if (k == "language") {
239                         _language = v;
240                 } else if (k == "default_container") {
241                         _default_container = Ratio::from_id (v);
242                 } else if (k == "default_dcp_content_type") {
243                         _default_dcp_content_type = DCPContentType::from_dci_name (v);
244                 } else if (k == "dcp_metadata_issuer") {
245                         _dcp_metadata.issuer = v;
246                 } else if (k == "dcp_metadata_creator") {
247                         _dcp_metadata.creator = v;
248                 } else if (k == "dcp_metadata_issue_date") {
249                         _dcp_metadata.issue_date = v;
250                 }
251
252                 _default_dci_metadata.read_old_metadata (k, v);
253         }
254 }
255
256 /** @return Filename to write configuration to */
257 boost::filesystem::path
258 Config::file (bool old) const
259 {
260         boost::filesystem::path p;
261         p /= g_get_user_config_dir ();
262         boost::system::error_code ec;
263         boost::filesystem::create_directory (p, ec);
264         if (old) {
265                 p /= ".dvdomatic";
266         } else {
267                 p /= "dcpomatic";
268                 boost::filesystem::create_directory (p, ec);
269                 p /= "config.xml";
270         }
271         return p;
272 }
273
274 boost::filesystem::path
275 Config::signer_chain_directory () const
276 {
277         boost::filesystem::path p;
278         p /= g_get_user_config_dir ();
279         p /= "dcpomatic";
280         p /= "crypt";
281         boost::filesystem::create_directories (p);
282         return p;
283 }
284
285 /** @return Singleton instance */
286 Config *
287 Config::instance ()
288 {
289         if (_instance == 0) {
290                 _instance = new Config;
291                 try {
292                         _instance->read ();
293                 } catch (exception& e) {
294                         /* configuration load failed; never mind, just
295                            stick with the default.
296                         */
297                         cerr << "dcpomatic: failed to load configuration (" << e.what() << ")\n";
298                 } catch (...) {
299                         cerr << "dcpomatic: failed to load configuration\n";
300                 }
301         }
302
303         return _instance;
304 }
305
306 /** Write our configuration to disk */
307 void
308 Config::write () const
309 {
310         xmlpp::Document doc;
311         xmlpp::Element* root = doc.create_root_node ("Config");
312
313         root->add_child("Version")->add_child_text ("1");
314         root->add_child("NumLocalEncodingThreads")->add_child_text (raw_convert<string> (_num_local_encoding_threads));
315         root->add_child("DefaultDirectory")->add_child_text (_default_directory.string ());
316         root->add_child("ServerPortBase")->add_child_text (raw_convert<string> (_server_port_base));
317         root->add_child("UseAnyServers")->add_child_text (_use_any_servers ? "1" : "0");
318         
319         for (vector<string>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
320                 root->add_child("Server")->add_child_text (*i);
321         }
322
323         root->add_child("TMSIP")->add_child_text (_tms_ip);
324         root->add_child("TMSPath")->add_child_text (_tms_path);
325         root->add_child("TMSUser")->add_child_text (_tms_user);
326         root->add_child("TMSPassword")->add_child_text (_tms_password);
327         if (_sound_processor) {
328                 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
329         }
330         if (_language) {
331                 root->add_child("Language")->add_child_text (_language.get());
332         }
333         if (_default_container) {
334                 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
335         }
336         if (_default_dcp_content_type) {
337                 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->dci_name ());
338         }
339         root->add_child("DCPMetadataIssuer")->add_child_text (_dcp_metadata.issuer);
340         root->add_child("DCPMetadataCreator")->add_child_text (_dcp_metadata.creator);
341
342         _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
343
344         root->add_child("DefaultStillLength")->add_child_text (raw_convert<string> (_default_still_length));
345         root->add_child("DefaultJ2KBandwidth")->add_child_text (raw_convert<string> (_default_j2k_bandwidth));
346         root->add_child("DefaultAudioDelay")->add_child_text (raw_convert<string> (_default_audio_delay));
347
348         for (vector<PresetColourConversion>::const_iterator i = _colour_conversions.begin(); i != _colour_conversions.end(); ++i) {
349                 i->as_xml (root->add_child ("ColourConversion"));
350         }
351
352         for (list<shared_ptr<Cinema> >::const_iterator i = _cinemas.begin(); i != _cinemas.end(); ++i) {
353                 (*i)->as_xml (root->add_child ("Cinema"));
354         }
355
356         root->add_child("MailServer")->add_child_text (_mail_server);
357         root->add_child("MailUser")->add_child_text (_mail_user);
358         root->add_child("MailPassword")->add_child_text (_mail_password);
359         root->add_child("KDMFrom")->add_child_text (_kdm_from);
360         root->add_child("KDMEmail")->add_child_text (_kdm_email);
361
362         root->add_child("CheckForUpdates")->add_child_text (_check_for_updates ? "1" : "0");
363         root->add_child("CheckForTestUpdates")->add_child_text (_check_for_test_updates ? "1" : "0");
364
365         root->add_child("MaximumJ2KBandwidth")->add_child_text (raw_convert<string> (_maximum_j2k_bandwidth));
366
367         doc.write_to_file_formatted (file(false).string ());
368 }
369
370 boost::filesystem::path
371 Config::default_directory_or (boost::filesystem::path a) const
372 {
373         if (_default_directory.empty()) {
374                 return a;
375         }
376
377         boost::system::error_code ec;
378         bool const e = boost::filesystem::exists (_default_directory, ec);
379         if (ec || !e) {
380                 return a;
381         }
382
383         return _default_directory;
384 }
385
386 void
387 Config::drop ()
388 {
389         delete _instance;
390         _instance = 0;
391 }
392
393 void
394 Config::changed ()
395 {
396         write ();
397         Changed ();
398 }