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