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