04f28579ba580ab927e9ac77a58958632e7fa8a9
[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         _dcp_metadata.issuer = f.optional_string_child ("DCPMetadataIssuer").get_value_or ("");
154         _dcp_metadata.creator = f.optional_string_child ("DCPMetadataCreator").get_value_or ("");
155
156         if (version && version.get() >= 2) {
157                 _default_isdcf_metadata = ISDCFMetadata (f.node_child ("ISDCFMetadata"));
158         } else {
159                 _default_isdcf_metadata = ISDCFMetadata (f.node_child ("DCIMetadata"));
160         }
161         
162         _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
163         _default_j2k_bandwidth = f.optional_number_child<int>("DefaultJ2KBandwidth").get_value_or (200000000);
164         _default_audio_delay = f.optional_number_child<int>("DefaultAudioDelay").get_value_or (0);
165
166         list<cxml::NodePtr> cc = f.node_children ("ColourConversion");
167
168         if (!cc.empty ()) {
169                 _colour_conversions.clear ();
170         }
171         
172         for (list<cxml::NodePtr>::iterator i = cc.begin(); i != cc.end(); ++i) {
173                 _colour_conversions.push_back (PresetColourConversion (*i));
174         }
175
176         if (!version) {
177                 /* Loading version 0 (before Rec. 709 was added as a preset).
178                    Add it in.
179                 */
180                 _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, libdcp::colour_matrix::rec709_to_xyz, 2.6));
181         }
182
183         list<cxml::NodePtr> cin = f.node_children ("Cinema");
184         for (list<cxml::NodePtr>::iterator i = cin.begin(); i != cin.end(); ++i) {
185                 /* Slightly grotty two-part construction of Cinema here so that we can use
186                    shared_from_this.
187                 */
188                 shared_ptr<Cinema> cinema (new Cinema (*i));
189                 cinema->read_screens (*i);
190                 _cinemas.push_back (cinema);
191         }
192
193         _mail_server = f.string_child ("MailServer");
194         _mail_user = f.optional_string_child("MailUser").get_value_or ("");
195         _mail_password = f.optional_string_child("MailPassword").get_value_or ("");
196         _kdm_subject = f.optional_string_child ("KDMSubject").get_value_or (_("KDM delivery: $CPL_NAME"));
197         _kdm_from = f.string_child ("KDMFrom");
198         _kdm_cc = f.optional_string_child ("KDMCC").get_value_or ("");
199         _kdm_bcc = f.optional_string_child ("KDMBCC").get_value_or ("");
200         _kdm_email = f.string_child ("KDMEmail");
201
202         _check_for_updates = f.optional_bool_child("CheckForUpdates").get_value_or (false);
203         _check_for_test_updates = f.optional_bool_child("CheckForTestUpdates").get_value_or (false);
204
205         _maximum_j2k_bandwidth = f.optional_number_child<int> ("MaximumJ2KBandwidth").get_value_or (250000000);
206         _allow_any_dcp_frame_rate = f.optional_bool_child ("AllowAnyDCPFrameRate");
207
208         _log_types = f.optional_number_child<int> ("LogTypes").get_value_or (Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR);
209
210         list<cxml::NodePtr> his = f.node_children ("History");
211         for (list<cxml::NodePtr>::const_iterator i = his.begin(); i != his.end(); ++i) {
212                 _history.push_back ((*i)->content ());
213         }
214 }
215
216 void
217 Config::read_old_metadata ()
218 {
219         /* XXX: this won't work with non-Latin filenames */
220         ifstream f (file(true).string().c_str ());
221         string line;
222
223         while (getline (f, line)) {
224                 if (line.empty ()) {
225                         continue;
226                 }
227
228                 if (line[0] == '#') {
229                         continue;
230                 }
231
232                 size_t const s = line.find (' ');
233                 if (s == string::npos) {
234                         continue;
235                 }
236                 
237                 string const k = line.substr (0, s);
238                 string const v = line.substr (s + 1);
239
240                 if (k == N_("num_local_encoding_threads")) {
241                         _num_local_encoding_threads = atoi (v.c_str ());
242                 } else if (k == N_("default_directory")) {
243                         _default_directory = v;
244                 } else if (k == N_("server_port")) {
245                         _server_port_base = atoi (v.c_str ());
246                 } else if (k == N_("server")) {
247                         vector<string> b;
248                         split (b, v, is_any_of (" "));
249                         if (b.size() == 2) {
250                                 _servers.push_back (b[0]);
251                         }
252                 } else if (k == N_("tms_ip")) {
253                         _tms_ip = v;
254                 } else if (k == N_("tms_path")) {
255                         _tms_path = v;
256                 } else if (k == N_("tms_user")) {
257                         _tms_user = v;
258                 } else if (k == N_("tms_password")) {
259                         _tms_password = v;
260                 } else if (k == N_("sound_processor")) {
261                         _sound_processor = SoundProcessor::from_id (v);
262                 } else if (k == "language") {
263                         _language = v;
264                 } else if (k == "default_container") {
265                         _default_container = Ratio::from_id (v);
266                 } else if (k == "default_dcp_content_type") {
267                         _default_dcp_content_type = DCPContentType::from_isdcf_name (v);
268                 } else if (k == "dcp_metadata_issuer") {
269                         _dcp_metadata.issuer = v;
270                 } else if (k == "dcp_metadata_creator") {
271                         _dcp_metadata.creator = v;
272                 } else if (k == "dcp_metadata_issue_date") {
273                         _dcp_metadata.issue_date = v;
274                 }
275
276                 _default_isdcf_metadata.read_old_metadata (k, v);
277         }
278 }
279
280 /** @return Filename to write configuration to */
281 boost::filesystem::path
282 Config::file (bool old) const
283 {
284         boost::filesystem::path p;
285         p /= g_get_user_config_dir ();
286         boost::system::error_code ec;
287         boost::filesystem::create_directory (p, ec);
288         if (old) {
289                 p /= ".dvdomatic";
290         } else {
291                 p /= "dcpomatic";
292                 boost::filesystem::create_directory (p, ec);
293                 p /= "config.xml";
294         }
295         return p;
296 }
297
298 boost::filesystem::path
299 Config::signer_chain_directory () const
300 {
301         boost::filesystem::path p;
302         p /= g_get_user_config_dir ();
303         p /= "dcpomatic";
304         p /= "crypt";
305         boost::filesystem::create_directories (p);
306         return p;
307 }
308
309 /** @return Singleton instance */
310 Config *
311 Config::instance ()
312 {
313         if (_instance == 0) {
314                 _instance = new Config;
315                 try {
316                         _instance->read ();
317                 } catch (exception& e) {
318                         /* configuration load failed; never mind, just
319                            stick with the default.
320                         */
321                         cerr << "dcpomatic: failed to load configuration (" << e.what() << ")\n";
322                 } catch (...) {
323                         cerr << "dcpomatic: failed to load configuration\n";
324                 }
325         }
326
327         return _instance;
328 }
329
330 /** Write our configuration to disk */
331 void
332 Config::write () const
333 {
334         xmlpp::Document doc;
335         xmlpp::Element* root = doc.create_root_node ("Config");
336
337         root->add_child("Version")->add_child_text ("2");
338         root->add_child("NumLocalEncodingThreads")->add_child_text (raw_convert<string> (_num_local_encoding_threads));
339         root->add_child("DefaultDirectory")->add_child_text (_default_directory.string ());
340         root->add_child("ServerPortBase")->add_child_text (raw_convert<string> (_server_port_base));
341         root->add_child("UseAnyServers")->add_child_text (_use_any_servers ? "1" : "0");
342         
343         for (vector<string>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
344                 root->add_child("Server")->add_child_text (*i);
345         }
346
347         root->add_child("TMSIP")->add_child_text (_tms_ip);
348         root->add_child("TMSPath")->add_child_text (_tms_path);
349         root->add_child("TMSUser")->add_child_text (_tms_user);
350         root->add_child("TMSPassword")->add_child_text (_tms_password);
351         if (_sound_processor) {
352                 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
353         }
354         if (_language) {
355                 root->add_child("Language")->add_child_text (_language.get());
356         }
357         if (_default_scale) {
358                 root->add_child("DefaultScale")->add_child_text (_default_scale->id ());
359         }
360         if (_default_container) {
361                 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
362         }
363         if (_default_dcp_content_type) {
364                 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->isdcf_name ());
365         }
366         root->add_child("DCPMetadataIssuer")->add_child_text (_dcp_metadata.issuer);
367         root->add_child("DCPMetadataCreator")->add_child_text (_dcp_metadata.creator);
368
369         _default_isdcf_metadata.as_xml (root->add_child ("ISDCFMetadata"));
370
371         root->add_child("DefaultStillLength")->add_child_text (raw_convert<string> (_default_still_length));
372         root->add_child("DefaultJ2KBandwidth")->add_child_text (raw_convert<string> (_default_j2k_bandwidth));
373         root->add_child("DefaultAudioDelay")->add_child_text (raw_convert<string> (_default_audio_delay));
374
375         for (vector<PresetColourConversion>::const_iterator i = _colour_conversions.begin(); i != _colour_conversions.end(); ++i) {
376                 i->as_xml (root->add_child ("ColourConversion"));
377         }
378
379         for (list<shared_ptr<Cinema> >::const_iterator i = _cinemas.begin(); i != _cinemas.end(); ++i) {
380                 (*i)->as_xml (root->add_child ("Cinema"));
381         }
382
383         root->add_child("MailServer")->add_child_text (_mail_server);
384         root->add_child("MailUser")->add_child_text (_mail_user);
385         root->add_child("MailPassword")->add_child_text (_mail_password);
386         root->add_child("KDMSubject")->add_child_text (_kdm_subject);
387         root->add_child("KDMFrom")->add_child_text (_kdm_from);
388         root->add_child("KDMCC")->add_child_text (_kdm_cc);
389         root->add_child("KDMBCC")->add_child_text (_kdm_bcc);
390         root->add_child("KDMEmail")->add_child_text (_kdm_email);
391
392         root->add_child("CheckForUpdates")->add_child_text (_check_for_updates ? "1" : "0");
393         root->add_child("CheckForTestUpdates")->add_child_text (_check_for_test_updates ? "1" : "0");
394
395         root->add_child("MaximumJ2KBandwidth")->add_child_text (raw_convert<string> (_maximum_j2k_bandwidth));
396         root->add_child("AllowAnyDCPFrameRate")->add_child_text (_allow_any_dcp_frame_rate ? "1" : "0");
397         root->add_child("LogTypes")->add_child_text (raw_convert<string> (_log_types));
398
399         for (vector<boost::filesystem::path>::const_iterator i = _history.begin(); i != _history.end(); ++i) {
400                 root->add_child("History")->add_child_text (i->string ());
401         }
402         
403         doc.write_to_file_formatted (file(false).string ());
404 }
405
406 boost::filesystem::path
407 Config::default_directory_or (boost::filesystem::path a) const
408 {
409         if (_default_directory.empty()) {
410                 return a;
411         }
412
413         boost::system::error_code ec;
414         bool const e = boost::filesystem::exists (_default_directory, ec);
415         if (ec || !e) {
416                 return a;
417         }
418
419         return _default_directory;
420 }
421
422 void
423 Config::drop ()
424 {
425         delete _instance;
426         _instance = 0;
427 }
428
429 void
430 Config::changed ()
431 {
432         write ();
433         Changed ();
434 }
435
436 void
437 Config::reset_kdm_email ()
438 {
439         _kdm_email = _(
440                 "Dear Projectionist\n\n"
441                 "Please find attached KDMs for $CPL_NAME.\n\n"
442                 "Cinema: $CINEMA_NAME\n"
443                 "Screen(s): $SCREENS\n\n"
444                 "The KDMs are valid from $START_TIME until $END_TIME.\n\n"
445                 "Best regards,\nDCP-o-matic"
446                 );
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 }