Logging improvements to allow prettier displays in the server GUI.
[dcpomatic.git] / src / lib / config.cc
1 /*
2     Copyright (C) 2012-2015 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 "config.h"
21 #include "server.h"
22 #include "filter.h"
23 #include "ratio.h"
24 #include "types.h"
25 #include "log.h"
26 #include "dcp_content_type.h"
27 #include "cinema_sound_processor.h"
28 #include "colour_conversion.h"
29 #include "cinema.h"
30 #include "util.h"
31 #include "cross.h"
32 #include "raw_convert.h"
33 #include <dcp/colour_matrix.h>
34 #include <dcp/certificate_chain.h>
35 #include <libcxml/cxml.h>
36 #include <glib.h>
37 #include <libxml++/libxml++.h>
38 #include <boost/filesystem.hpp>
39 #include <boost/algorithm/string.hpp>
40 #include <boost/foreach.hpp>
41 #include <cstdlib>
42 #include <fstream>
43 #include <iostream>
44
45 #include "i18n.h"
46
47 using std::vector;
48 using std::cout;
49 using std::ifstream;
50 using std::string;
51 using std::list;
52 using std::max;
53 using std::remove;
54 using std::exception;
55 using std::cerr;
56 using boost::shared_ptr;
57 using boost::optional;
58 using boost::algorithm::trim;
59
60 Config* Config::_instance = 0;
61
62 /** Construct default configuration */
63 Config::Config ()
64 {
65         set_defaults ();
66 }
67
68 void
69 Config::set_defaults ()
70 {
71         _num_local_encoding_threads = max (2U, boost::thread::hardware_concurrency ());
72         _server_port_base = 6192;
73         _use_any_servers = true;
74         _servers.clear ();
75         _only_servers_encode = false;
76         _tms_protocol = PROTOCOL_SCP;
77         _tms_ip = "";
78         _tms_path = ".";
79         _tms_user = "";
80         _tms_password = "";
81         _cinema_sound_processor = CinemaSoundProcessor::from_id (N_("dolby_cp750"));
82         _allow_any_dcp_frame_rate = false;
83         _language = optional<string> ();
84         _default_still_length = 10;
85         _default_container = Ratio::from_id ("185");
86         _default_dcp_content_type = DCPContentType::from_isdcf_name ("FTR");
87         _default_j2k_bandwidth = 100000000;
88         _default_audio_delay = 0;
89         _default_interop = false;
90         _mail_server = "";
91         _mail_port = 25;
92         _mail_user = "";
93         _mail_password = "";
94         _kdm_subject = _("KDM delivery");
95         _kdm_from = "";
96         _kdm_cc = "";
97         _kdm_bcc = "";
98         _check_for_updates = false;
99         _check_for_test_updates = false;
100         _maximum_j2k_bandwidth = 250000000;
101         _log_types = LogEntry::TYPE_GENERAL | LogEntry::TYPE_WARNING | LogEntry::TYPE_ERROR;
102         _automatic_audio_analysis = false;
103 #ifdef DCPOMATIC_WINDOWS
104         _win32_console = false;
105 #endif
106
107         _allowed_dcp_frame_rates.clear ();
108         _allowed_dcp_frame_rates.push_back (24);
109         _allowed_dcp_frame_rates.push_back (25);
110         _allowed_dcp_frame_rates.push_back (30);
111         _allowed_dcp_frame_rates.push_back (48);
112         _allowed_dcp_frame_rates.push_back (50);
113         _allowed_dcp_frame_rates.push_back (60);
114
115         set_kdm_email_to_default ();
116 }
117
118 void
119 Config::restore_defaults ()
120 {
121         Config::instance()->set_defaults ();
122         Config::instance()->changed ();
123 }
124
125 void
126 Config::read ()
127 {
128         if (!have_existing ()) {
129                 /* Make a new set of signing certificates and key */
130                 _signer_chain.reset (new dcp::CertificateChain (openssl_path ()));
131                 /* And similar for decryption of KDMs */
132                 _decryption_chain.reset (new dcp::CertificateChain (openssl_path ()));
133                 write ();
134                 return;
135         }
136
137         cxml::Document f ("Config");
138         f.read_file (file ());
139         optional<string> c;
140
141         optional<int> version = f.optional_number_child<int> ("Version");
142
143         _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
144         _default_directory = f.string_child ("DefaultDirectory");
145
146         boost::optional<int> b = f.optional_number_child<int> ("ServerPort");
147         if (!b) {
148                 b = f.optional_number_child<int> ("ServerPortBase");
149         }
150         _server_port_base = b.get ();
151
152         boost::optional<bool> u = f.optional_bool_child ("UseAnyServers");
153         _use_any_servers = u.get_value_or (true);
154
155         list<cxml::NodePtr> servers = f.node_children ("Server");
156         for (list<cxml::NodePtr>::iterator i = servers.begin(); i != servers.end(); ++i) {
157                 if ((*i)->node_children("HostName").size() == 1) {
158                         _servers.push_back ((*i)->string_child ("HostName"));
159                 } else {
160                         _servers.push_back ((*i)->content ());
161                 }
162         }
163
164         _only_servers_encode = f.optional_bool_child ("OnlyServersEncode").get_value_or (false);
165         _tms_protocol = static_cast<Protocol> (f.optional_number_child<int> ("TMSProtocol").get_value_or (static_cast<int> (PROTOCOL_SCP)));
166         _tms_ip = f.string_child ("TMSIP");
167         _tms_path = f.string_child ("TMSPath");
168         _tms_user = f.string_child ("TMSUser");
169         _tms_password = f.string_child ("TMSPassword");
170
171         c = f.optional_string_child ("SoundProcessor");
172         if (c) {
173                 _cinema_sound_processor = CinemaSoundProcessor::from_id (c.get ());
174         }
175         c = f.optional_string_child ("CinemaSoundProcessor");
176         if (c) {
177                 _cinema_sound_processor = CinemaSoundProcessor::from_id (c.get ());
178         }
179
180         _language = f.optional_string_child ("Language");
181
182         c = f.optional_string_child ("DefaultContainer");
183         if (c) {
184                 _default_container = Ratio::from_id (c.get ());
185         }
186
187         c = f.optional_string_child ("DefaultDCPContentType");
188         if (c) {
189                 _default_dcp_content_type = DCPContentType::from_isdcf_name (c.get ());
190         }
191
192         if (f.optional_string_child ("DCPMetadataIssuer")) {
193                 _dcp_issuer = f.string_child ("DCPMetadataIssuer");
194         } else if (f.optional_string_child ("DCPIssuer")) {
195                 _dcp_issuer = f.string_child ("DCPIssuer");
196         }
197
198         _dcp_creator = f.optional_string_child ("DCPCreator").get_value_or ("");
199
200         if (version && version.get() >= 2) {
201                 _default_isdcf_metadata = ISDCFMetadata (f.node_child ("ISDCFMetadata"));
202         } else {
203                 _default_isdcf_metadata = ISDCFMetadata (f.node_child ("DCIMetadata"));
204         }
205
206         _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
207         _default_j2k_bandwidth = f.optional_number_child<int>("DefaultJ2KBandwidth").get_value_or (200000000);
208         _default_audio_delay = f.optional_number_child<int>("DefaultAudioDelay").get_value_or (0);
209         _default_interop = f.optional_bool_child("DefaultInterop").get_value_or (false);
210
211         list<cxml::NodePtr> cin = f.node_children ("Cinema");
212         for (list<cxml::NodePtr>::iterator i = cin.begin(); i != cin.end(); ++i) {
213                 /* Slightly grotty two-part construction of Cinema here so that we can use
214                    shared_from_this.
215                 */
216                 shared_ptr<Cinema> cinema (new Cinema (*i));
217                 cinema->read_screens (*i);
218                 _cinemas.push_back (cinema);
219         }
220
221         _mail_server = f.string_child ("MailServer");
222         _mail_port = f.optional_number_child<int> ("MailPort").get_value_or (25);
223         _mail_user = f.optional_string_child("MailUser").get_value_or ("");
224         _mail_password = f.optional_string_child("MailPassword").get_value_or ("");
225         _kdm_subject = f.optional_string_child ("KDMSubject").get_value_or (_("KDM delivery: $CPL_NAME"));
226         _kdm_from = f.string_child ("KDMFrom");
227         _kdm_cc = f.optional_string_child ("KDMCC").get_value_or ("");
228         _kdm_bcc = f.optional_string_child ("KDMBCC").get_value_or ("");
229         _kdm_email = f.string_child ("KDMEmail");
230
231         _check_for_updates = f.optional_bool_child("CheckForUpdates").get_value_or (false);
232         _check_for_test_updates = f.optional_bool_child("CheckForTestUpdates").get_value_or (false);
233
234         _maximum_j2k_bandwidth = f.optional_number_child<int> ("MaximumJ2KBandwidth").get_value_or (250000000);
235         _allow_any_dcp_frame_rate = f.optional_bool_child ("AllowAnyDCPFrameRate").get_value_or (false);
236
237         _log_types = f.optional_number_child<int> ("LogTypes").get_value_or (LogEntry::TYPE_GENERAL | LogEntry::TYPE_WARNING | LogEntry::TYPE_ERROR);
238         _automatic_audio_analysis = f.optional_bool_child ("AutomaticAudioAnalysis").get_value_or (false);
239 #ifdef DCPOMATIC_WINDOWS
240         _win32_console = f.optional_bool_child ("Win32Console").get_value_or (false);
241 #endif
242
243         list<cxml::NodePtr> his = f.node_children ("History");
244         for (list<cxml::NodePtr>::const_iterator i = his.begin(); i != his.end(); ++i) {
245                 _history.push_back ((*i)->content ());
246         }
247
248         cxml::NodePtr signer = f.optional_node_child ("Signer");
249         if (signer) {
250                 shared_ptr<dcp::CertificateChain> c (new dcp::CertificateChain ());
251                 /* Read the signing certificates and private key in from the config file */
252                 BOOST_FOREACH (cxml::NodePtr i, signer->node_children ("Certificate")) {
253                         c->add (dcp::Certificate (i->content ()));
254                 }
255                 c->set_key (signer->string_child ("PrivateKey"));
256                 _signer_chain = c;
257         } else {
258                 /* Make a new set of signing certificates and key */
259                 _signer_chain.reset (new dcp::CertificateChain (openssl_path ()));
260         }
261
262         cxml::NodePtr decryption = f.optional_node_child ("Decryption");
263         if (decryption) {
264                 shared_ptr<dcp::CertificateChain> c (new dcp::CertificateChain ());
265                 BOOST_FOREACH (cxml::NodePtr i, decryption->node_children ("Certificate")) {
266                         c->add (dcp::Certificate (i->content ()));
267                 }
268                 c->set_key (decryption->string_child ("PrivateKey"));
269                 _decryption_chain = c;
270         } else {
271                 _decryption_chain.reset (new dcp::CertificateChain (openssl_path ()));
272         }
273 }
274
275 /** @return Filename to write configuration to */
276 boost::filesystem::path
277 Config::file (bool create_directories)
278 {
279         boost::filesystem::path p;
280 #ifdef DCPOMATIC_OSX
281         p /= g_get_home_dir ();
282         p /= "Library";
283         p /= "Preferences";
284         p /= "com.dcpomatic";
285         p /= "2";
286 #else
287         p /= g_get_user_config_dir ();
288         p /= "dcpomatic2";
289 #endif
290         boost::system::error_code ec;
291         if (create_directories) {
292                 boost::filesystem::create_directories (p, ec);
293         }
294         p /= "config.xml";
295         return p;
296 }
297
298 /** @return Singleton instance */
299 Config *
300 Config::instance ()
301 {
302         if (_instance == 0) {
303                 _instance = new Config;
304                 try {
305                         _instance->read ();
306                 } catch (exception& e) {
307                         /* configuration load failed; never mind, just
308                            stick with the default.
309                         */
310                         cerr << "dcpomatic: warning: configuration did not load (" << e.what() << "); using defaults\n";
311                 } catch (...) {
312                         cerr << "dcpomatic: warning: configuration did not load; using defaults\n";
313                 }
314         }
315
316         return _instance;
317 }
318
319 /** Write our configuration to disk */
320 void
321 Config::write () const
322 {
323         xmlpp::Document doc;
324         xmlpp::Element* root = doc.create_root_node ("Config");
325
326         root->add_child("Version")->add_child_text ("2");
327         root->add_child("NumLocalEncodingThreads")->add_child_text (raw_convert<string> (_num_local_encoding_threads));
328         root->add_child("DefaultDirectory")->add_child_text (_default_directory.string ());
329         root->add_child("ServerPortBase")->add_child_text (raw_convert<string> (_server_port_base));
330         root->add_child("UseAnyServers")->add_child_text (_use_any_servers ? "1" : "0");
331
332         for (vector<string>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
333                 root->add_child("Server")->add_child_text (*i);
334         }
335
336         root->add_child("OnlyServersEncode")->add_child_text (_only_servers_encode ? "1" : "0");
337         root->add_child("TMSProtocol")->add_child_text (raw_convert<string> (_tms_protocol));
338         root->add_child("TMSIP")->add_child_text (_tms_ip);
339         root->add_child("TMSPath")->add_child_text (_tms_path);
340         root->add_child("TMSUser")->add_child_text (_tms_user);
341         root->add_child("TMSPassword")->add_child_text (_tms_password);
342         if (_cinema_sound_processor) {
343                 root->add_child("CinemaSoundProcessor")->add_child_text (_cinema_sound_processor->id ());
344         }
345         if (_language) {
346                 root->add_child("Language")->add_child_text (_language.get());
347         }
348         if (_default_container) {
349                 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
350         }
351         if (_default_dcp_content_type) {
352                 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->isdcf_name ());
353         }
354         root->add_child("DCPIssuer")->add_child_text (_dcp_issuer);
355         root->add_child("DCPCreator")->add_child_text (_dcp_creator);
356
357         _default_isdcf_metadata.as_xml (root->add_child ("ISDCFMetadata"));
358
359         root->add_child("DefaultStillLength")->add_child_text (raw_convert<string> (_default_still_length));
360         root->add_child("DefaultJ2KBandwidth")->add_child_text (raw_convert<string> (_default_j2k_bandwidth));
361         root->add_child("DefaultAudioDelay")->add_child_text (raw_convert<string> (_default_audio_delay));
362         root->add_child("DefaultInterop")->add_child_text (_default_interop ? "1" : "0");
363
364         for (list<shared_ptr<Cinema> >::const_iterator i = _cinemas.begin(); i != _cinemas.end(); ++i) {
365                 (*i)->as_xml (root->add_child ("Cinema"));
366         }
367
368         root->add_child("MailServer")->add_child_text (_mail_server);
369         root->add_child("MailPort")->add_child_text (raw_convert<string> (_mail_port));
370         root->add_child("MailUser")->add_child_text (_mail_user);
371         root->add_child("MailPassword")->add_child_text (_mail_password);
372         root->add_child("KDMSubject")->add_child_text (_kdm_subject);
373         root->add_child("KDMFrom")->add_child_text (_kdm_from);
374         root->add_child("KDMCC")->add_child_text (_kdm_cc);
375         root->add_child("KDMBCC")->add_child_text (_kdm_bcc);
376         root->add_child("KDMEmail")->add_child_text (_kdm_email);
377
378         root->add_child("CheckForUpdates")->add_child_text (_check_for_updates ? "1" : "0");
379         root->add_child("CheckForTestUpdates")->add_child_text (_check_for_test_updates ? "1" : "0");
380
381         root->add_child("MaximumJ2KBandwidth")->add_child_text (raw_convert<string> (_maximum_j2k_bandwidth));
382         root->add_child("AllowAnyDCPFrameRate")->add_child_text (_allow_any_dcp_frame_rate ? "1" : "0");
383         root->add_child("LogTypes")->add_child_text (raw_convert<string> (_log_types));
384         root->add_child("AutomaticAudioAnalysis")->add_child_text (_automatic_audio_analysis ? "1" : "0");
385 #ifdef DCPOMATIC_WINDOWS
386         root->add_child("Win32Console")->add_child_text (_win32_console ? "1" : "0");
387 #endif
388
389         xmlpp::Element* signer = root->add_child ("Signer");
390         BOOST_FOREACH (dcp::Certificate const & i, _signer_chain->root_to_leaf ()) {
391                 signer->add_child("Certificate")->add_child_text (i.certificate (true));
392         }
393         signer->add_child("PrivateKey")->add_child_text (_signer_chain->key().get ());
394
395         xmlpp::Element* decryption = root->add_child ("Decryption");
396         BOOST_FOREACH (dcp::Certificate const & i, _decryption_chain->root_to_leaf ()) {
397                 decryption->add_child("Certificate")->add_child_text (i.certificate (true));
398         }
399         decryption->add_child("PrivateKey")->add_child_text (_decryption_chain->key().get ());
400
401         for (vector<boost::filesystem::path>::const_iterator i = _history.begin(); i != _history.end(); ++i) {
402                 root->add_child("History")->add_child_text (i->string ());
403         }
404
405         try {
406                 doc.write_to_file_formatted (file().string ());
407         } catch (xmlpp::exception& e) {
408                 string s = e.what ();
409                 trim (s);
410                 throw FileError (s, file ());
411         }
412 }
413
414 boost::filesystem::path
415 Config::default_directory_or (boost::filesystem::path a) const
416 {
417         if (_default_directory.empty()) {
418                 return a;
419         }
420
421         boost::system::error_code ec;
422         bool const e = boost::filesystem::exists (_default_directory, ec);
423         if (ec || !e) {
424                 return a;
425         }
426
427         return _default_directory;
428 }
429
430 void
431 Config::drop ()
432 {
433         delete _instance;
434         _instance = 0;
435 }
436
437 void
438 Config::changed (Property what)
439 {
440         Changed (what);
441 }
442
443 void
444 Config::set_kdm_email_to_default ()
445 {
446         _kdm_email = _(
447                 "Dear Projectionist\n\n"
448                 "Please find attached KDMs for $CPL_NAME.\n\n"
449                 "Cinema: $CINEMA_NAME\n"
450                 "Screen(s): $SCREENS\n\n"
451                 "The KDMs are valid from $START_TIME until $END_TIME.\n\n"
452                 "Best regards,\nDCP-o-matic"
453                 );
454 }
455
456 void
457 Config::reset_kdm_email ()
458 {
459         set_kdm_email_to_default ();
460         changed ();
461 }
462
463 void
464 Config::add_to_history (boost::filesystem::path p)
465 {
466         /* Remove existing instances of this path in the history */
467         _history.erase (remove (_history.begin(), _history.end(), p), _history.end ());
468
469         _history.insert (_history.begin (), p);
470         if (_history.size() > HISTORY_SIZE) {
471                 _history.pop_back ();
472         }
473
474         changed ();
475 }
476
477 bool
478 Config::have_existing ()
479 {
480         return boost::filesystem::exists (file (false));
481 }