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