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