Merge master.
[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 <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 dcp::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_dci_name ("TST"))
68         , _default_j2k_bandwidth (100000000)
69         , _default_audio_delay (0)
70         , _kdm_email (
71                 _("Dear Projectionist\n\nPlease find attached KDMs for $CPL_NAME.\n\nThe KDMs are valid from $START_TIME until $END_TIME.\n\nBest regards,\nDCP-o-matic")
72                 )
73         , _check_for_updates (false)
74         , _check_for_test_updates (false)
75         , _maximum_j2k_bandwidth (250000000)
76         , _log_types (Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR)
77 {
78         _allowed_dcp_frame_rates.push_back (24);
79         _allowed_dcp_frame_rates.push_back (25);
80         _allowed_dcp_frame_rates.push_back (30);
81         _allowed_dcp_frame_rates.push_back (48);
82         _allowed_dcp_frame_rates.push_back (50);
83         _allowed_dcp_frame_rates.push_back (60);
84
85         _colour_conversions.push_back (PresetColourConversion (_("sRGB"), 2.4, true, dcp::colour_matrix::srgb_to_xyz, 2.6));
86         _colour_conversions.push_back (PresetColourConversion (_("sRGB non-linearised"), 2.4, false, dcp::colour_matrix::srgb_to_xyz, 2.6));
87         _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, dcp::colour_matrix::rec709_to_xyz, 2.6));
88 }
89
90 void
91 Config::read ()
92 {
93         if (!boost::filesystem::exists (file (false))) {
94                 read_old_metadata ();
95                 return;
96         }
97
98         cxml::Document f ("Config");
99         f.read_file (file (false));
100         optional<string> c;
101
102         optional<int> version = f.optional_number_child<int> ("Version");
103
104         _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
105         _default_directory = f.string_child ("DefaultDirectory");
106
107         boost::optional<int> b = f.optional_number_child<int> ("ServerPort");
108         if (!b) {
109                 b = f.optional_number_child<int> ("ServerPortBase");
110         }
111         _server_port_base = b.get ();
112
113         boost::optional<bool> u = f.optional_bool_child ("UseAnyServers");
114         _use_any_servers = u.get_value_or (true);
115
116         list<cxml::NodePtr> servers = f.node_children ("Server");
117         for (list<cxml::NodePtr>::iterator i = servers.begin(); i != servers.end(); ++i) {
118                 if ((*i)->node_children("HostName").size() == 1) {
119                         _servers.push_back ((*i)->string_child ("HostName"));
120                 } else {
121                         _servers.push_back ((*i)->content ());
122                 }
123         }
124         
125         _tms_ip = f.string_child ("TMSIP");
126         _tms_path = f.string_child ("TMSPath");
127         _tms_user = f.string_child ("TMSUser");
128         _tms_password = f.string_child ("TMSPassword");
129
130         c = f.optional_string_child ("SoundProcessor");
131         if (c) {
132                 _sound_processor = SoundProcessor::from_id (c.get ());
133         }
134
135         _language = f.optional_string_child ("Language");
136
137         c = f.optional_string_child ("DefaultContainer");
138         if (c) {
139                 _default_container = Ratio::from_id (c.get ());
140         }
141
142         c = f.optional_string_child ("DefaultDCPContentType");
143         if (c) {
144                 _default_dcp_content_type = DCPContentType::from_dci_name (c.get ());
145         }
146
147         _dcp_metadata.issuer = f.optional_string_child ("DCPMetadataIssuer").get_value_or ("");
148         _dcp_metadata.creator = f.optional_string_child ("DCPMetadataCreator").get_value_or ("");
149
150         _default_dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
151         _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
152         _default_j2k_bandwidth = f.optional_number_child<int>("DefaultJ2KBandwidth").get_value_or (200000000);
153         _default_audio_delay = f.optional_number_child<int>("DefaultAudioDelay").get_value_or (0);
154
155         list<cxml::NodePtr> cc = f.node_children ("ColourConversion");
156
157         if (!cc.empty ()) {
158                 _colour_conversions.clear ();
159         }
160         
161         for (list<cxml::NodePtr>::iterator i = cc.begin(); i != cc.end(); ++i) {
162                 _colour_conversions.push_back (PresetColourConversion (*i));
163         }
164
165         if (!version) {
166                 /* Loading version 0 (before Rec. 709 was added as a preset).
167                    Add it in.
168                 */
169                 _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, dcp::colour_matrix::rec709_to_xyz, 2.6));
170         }
171
172         list<cxml::NodePtr> cin = f.node_children ("Cinema");
173         for (list<cxml::NodePtr>::iterator i = cin.begin(); i != cin.end(); ++i) {
174                 /* Slightly grotty two-part construction of Cinema here so that we can use
175                    shared_from_this.
176                 */
177                 shared_ptr<Cinema> cinema (new Cinema (*i));
178                 cinema->read_screens (*i);
179                 _cinemas.push_back (cinema);
180         }
181
182         _mail_server = f.string_child ("MailServer");
183         _mail_user = f.optional_string_child("MailUser").get_value_or ("");
184         _mail_password = f.optional_string_child("MailPassword").get_value_or ("");
185         _kdm_from = f.string_child ("KDMFrom");
186         _kdm_email = f.string_child ("KDMEmail");
187
188         _check_for_updates = f.optional_bool_child("CheckForUpdates").get_value_or (false);
189         _check_for_test_updates = f.optional_bool_child("CheckForTestUpdates").get_value_or (false);
190
191         _maximum_j2k_bandwidth = f.optional_number_child<int> ("MaximumJ2KBandwidth").get_value_or (250000000);
192         _allow_any_dcp_frame_rate = f.optional_bool_child ("AllowAnyDCPFrameRate");
193
194         _log_types = f.optional_number_child<int> ("LogTypes").get_value_or (Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR);
195 }
196
197 void
198 Config::read_old_metadata ()
199 {
200         /* XXX: this won't work with non-Latin filenames */
201         ifstream f (file(true).string().c_str ());
202         string line;
203
204         while (getline (f, line)) {
205                 if (line.empty ()) {
206                         continue;
207                 }
208
209                 if (line[0] == '#') {
210                         continue;
211                 }
212
213                 size_t const s = line.find (' ');
214                 if (s == string::npos) {
215                         continue;
216                 }
217                 
218                 string const k = line.substr (0, s);
219                 string const v = line.substr (s + 1);
220
221                 if (k == N_("num_local_encoding_threads")) {
222                         _num_local_encoding_threads = atoi (v.c_str ());
223                 } else if (k == N_("default_directory")) {
224                         _default_directory = v;
225                 } else if (k == N_("server_port")) {
226                         _server_port_base = atoi (v.c_str ());
227                 } else if (k == N_("server")) {
228                         vector<string> b;
229                         split (b, v, is_any_of (" "));
230                         if (b.size() == 2) {
231                                 _servers.push_back (b[0]);
232                         }
233                 } else if (k == N_("tms_ip")) {
234                         _tms_ip = v;
235                 } else if (k == N_("tms_path")) {
236                         _tms_path = v;
237                 } else if (k == N_("tms_user")) {
238                         _tms_user = v;
239                 } else if (k == N_("tms_password")) {
240                         _tms_password = v;
241                 } else if (k == N_("sound_processor")) {
242                         _sound_processor = SoundProcessor::from_id (v);
243                 } else if (k == "language") {
244                         _language = v;
245                 } else if (k == "default_container") {
246                         _default_container = Ratio::from_id (v);
247                 } else if (k == "default_dcp_content_type") {
248                         _default_dcp_content_type = DCPContentType::from_dci_name (v);
249                 } else if (k == "dcp_metadata_issuer") {
250                         _dcp_metadata.issuer = v;
251                 } else if (k == "dcp_metadata_creator") {
252                         _dcp_metadata.creator = v;
253                 } else if (k == "dcp_metadata_issue_date") {
254                         _dcp_metadata.issue_date = v;
255                 }
256
257                 _default_dci_metadata.read_old_metadata (k, v);
258         }
259 }
260
261 /** @return Filename to write configuration to */
262 boost::filesystem::path
263 Config::file (bool old) const
264 {
265         boost::filesystem::path p;
266         p /= g_get_user_config_dir ();
267         boost::system::error_code ec;
268         boost::filesystem::create_directory (p, ec);
269         if (old) {
270                 p /= ".dvdomatic";
271         } else {
272                 p /= "dcpomatic";
273                 boost::filesystem::create_directory (p, ec);
274                 p /= "config.xml";
275         }
276         return p;
277 }
278
279 boost::filesystem::path
280 Config::signer_chain_directory () const
281 {
282         boost::filesystem::path p;
283         p /= g_get_user_config_dir ();
284         p /= "dcpomatic";
285         p /= "crypt";
286         boost::filesystem::create_directories (p);
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: failed to load configuration (" << e.what() << ")\n";
303                 } catch (...) {
304                         cerr << "dcpomatic: failed to load configuration\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 ("1");
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("TMSIP")->add_child_text (_tms_ip);
329         root->add_child("TMSPath")->add_child_text (_tms_path);
330         root->add_child("TMSUser")->add_child_text (_tms_user);
331         root->add_child("TMSPassword")->add_child_text (_tms_password);
332         if (_sound_processor) {
333                 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
334         }
335         if (_language) {
336                 root->add_child("Language")->add_child_text (_language.get());
337         }
338         if (_default_container) {
339                 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
340         }
341         if (_default_dcp_content_type) {
342                 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->dci_name ());
343         }
344         root->add_child("DCPMetadataIssuer")->add_child_text (_dcp_metadata.issuer);
345         root->add_child("DCPMetadataCreator")->add_child_text (_dcp_metadata.creator);
346
347         _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
348
349         root->add_child("DefaultStillLength")->add_child_text (raw_convert<string> (_default_still_length));
350         root->add_child("DefaultJ2KBandwidth")->add_child_text (raw_convert<string> (_default_j2k_bandwidth));
351         root->add_child("DefaultAudioDelay")->add_child_text (raw_convert<string> (_default_audio_delay));
352
353         for (vector<PresetColourConversion>::const_iterator i = _colour_conversions.begin(); i != _colour_conversions.end(); ++i) {
354                 i->as_xml (root->add_child ("ColourConversion"));
355         }
356
357         for (list<shared_ptr<Cinema> >::const_iterator i = _cinemas.begin(); i != _cinemas.end(); ++i) {
358                 (*i)->as_xml (root->add_child ("Cinema"));
359         }
360
361         root->add_child("MailServer")->add_child_text (_mail_server);
362         root->add_child("MailUser")->add_child_text (_mail_user);
363         root->add_child("MailPassword")->add_child_text (_mail_password);
364         root->add_child("KDMFrom")->add_child_text (_kdm_from);
365         root->add_child("KDMEmail")->add_child_text (_kdm_email);
366
367         root->add_child("CheckForUpdates")->add_child_text (_check_for_updates ? "1" : "0");
368         root->add_child("CheckForTestUpdates")->add_child_text (_check_for_test_updates ? "1" : "0");
369
370         root->add_child("MaximumJ2KBandwidth")->add_child_text (raw_convert<string> (_maximum_j2k_bandwidth));
371         root->add_child("AllowAnyDCPFrameRate")->add_child_text (_allow_any_dcp_frame_rate ? "1" : "0");
372         root->add_child("LogTypes")->add_child_text (raw_convert<string> (_log_types));
373         
374         doc.write_to_file_formatted (file(false).string ());
375 }
376
377 boost::filesystem::path
378 Config::default_directory_or (boost::filesystem::path a) const
379 {
380         if (_default_directory.empty()) {
381                 return a;
382         }
383
384         boost::system::error_code ec;
385         bool const e = boost::filesystem::exists (_default_directory, ec);
386         if (ec || !e) {
387                 return a;
388         }
389
390         return _default_directory;
391 }
392
393 void
394 Config::drop ()
395 {
396         delete _instance;
397         _instance = 0;
398 }
399
400 void
401 Config::changed ()
402 {
403         write ();
404         Changed ();
405 }