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