4b6455f51412ed378633f487217918676cbe156c
[dcpomatic.git] / src / lib / config.cc
1 /*
2     Copyright (C) 2012 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 <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::ifstream;
43 using std::string;
44 using std::ofstream;
45 using std::list;
46 using std::max;
47 using std::exception;
48 using std::cerr;
49 using boost::shared_ptr;
50 using boost::lexical_cast;
51 using boost::optional;
52 using boost::algorithm::is_any_of;
53 using boost::algorithm::split;
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         , _default_still_length (10)
65         , _default_container (Ratio::from_id ("185"))
66         , _default_dcp_content_type (DCPContentType::from_dci_name ("TST"))
67         , _default_j2k_bandwidth (200000000)
68         , _kdm_email (
69                 "Dear Projectionist\n\nPlease find attached KDMs for $CPL_NAME.\n\nBest regards,\nDCP-o-matic"
70                 )
71 {
72         _allowed_dcp_frame_rates.push_back (24);
73         _allowed_dcp_frame_rates.push_back (25);
74         _allowed_dcp_frame_rates.push_back (30);
75         _allowed_dcp_frame_rates.push_back (48);
76         _allowed_dcp_frame_rates.push_back (50);
77         _allowed_dcp_frame_rates.push_back (60);
78
79         _colour_conversions.push_back (PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6));
80         _colour_conversions.push_back (PresetColourConversion (_("sRGB non-linearised"), 2.4, false, libdcp::colour_matrix::srgb_to_xyz, 2.6));
81         _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, libdcp::colour_matrix::rec709_to_xyz, 2.6));
82 }
83
84 void
85 Config::read ()
86 {
87         LocaleGuard lg;
88         
89         if (!boost::filesystem::exists (file (false))) {
90                 read_old_metadata ();
91                 return;
92         }
93
94         cxml::Document f ("Config");
95         f.read_file (file (false));
96         optional<string> c;
97
98         optional<int> version = f.optional_number_child<int> ("Version");
99
100         _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
101         _default_directory = f.string_child ("DefaultDirectory");
102
103         boost::optional<int> b = f.optional_number_child<int> ("ServerPort");
104         if (!b) {
105                 b = f.optional_number_child<int> ("ServerPortBase");
106         }
107         _server_port_base = b.get ();
108
109         boost::optional<bool> u = f.optional_bool_child ("UseAnyServers");
110         _use_any_servers = u.get_value_or (true);
111
112         list<cxml::NodePtr> servers = f.node_children ("Server");
113         for (list<cxml::NodePtr>::iterator i = servers.begin(); i != servers.end(); ++i) {
114                 if ((*i)->node_children("HostName").size() == 1) {
115                         _servers.push_back ((*i)->string_child ("HostName"));
116                 } else {
117                         _servers.push_back ((*i)->content ());
118                 }
119         }
120         
121         _tms_ip = f.string_child ("TMSIP");
122         _tms_path = f.string_child ("TMSPath");
123         _tms_user = f.string_child ("TMSUser");
124         _tms_password = f.string_child ("TMSPassword");
125
126         c = f.optional_string_child ("SoundProcessor");
127         if (c) {
128                 _sound_processor = SoundProcessor::from_id (c.get ());
129         }
130
131         _language = f.optional_string_child ("Language");
132
133         c = f.optional_string_child ("DefaultContainer");
134         if (c) {
135                 _default_container = Ratio::from_id (c.get ());
136         }
137
138         c = f.optional_string_child ("DefaultDCPContentType");
139         if (c) {
140                 _default_dcp_content_type = DCPContentType::from_dci_name (c.get ());
141         }
142
143         _dcp_metadata.issuer = f.optional_string_child ("DCPMetadataIssuer").get_value_or ("");
144         _dcp_metadata.creator = f.optional_string_child ("DCPMetadataCreator").get_value_or ("");
145
146         _default_dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
147         _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
148         _default_j2k_bandwidth = f.optional_number_child<int>("DefaultJ2KBandwidth").get_value_or (200000000);
149
150         list<cxml::NodePtr> cc = f.node_children ("ColourConversion");
151
152         if (!cc.empty ()) {
153                 _colour_conversions.clear ();
154         }
155         
156         for (list<cxml::NodePtr>::iterator i = cc.begin(); i != cc.end(); ++i) {
157                 _colour_conversions.push_back (PresetColourConversion (*i));
158         }
159
160         if (!version) {
161                 /* Loading version 0 (before Rec. 709 was added as a preset).
162                    Add it in.
163                 */
164                 _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, libdcp::colour_matrix::rec709_to_xyz, 2.6));
165         }
166
167         list<cxml::NodePtr> cin = f.node_children ("Cinema");
168         for (list<cxml::NodePtr>::iterator i = cin.begin(); i != cin.end(); ++i) {
169                 /* Slightly grotty two-part construction of Cinema here so that we can use
170                    shared_from_this.
171                 */
172                 shared_ptr<Cinema> cinema (new Cinema (*i));
173                 cinema->read_screens (*i);
174                 _cinemas.push_back (cinema);
175         }
176
177         _mail_server = f.string_child ("MailServer");
178         _kdm_from = f.string_child ("KDMFrom");
179         _kdm_email = f.string_child ("KDMEmail");
180 }
181
182 void
183 Config::read_old_metadata ()
184 {
185         ifstream f (file(true).string().c_str ());
186         string line;
187
188         while (getline (f, line)) {
189                 if (line.empty ()) {
190                         continue;
191                 }
192
193                 if (line[0] == '#') {
194                         continue;
195                 }
196
197                 size_t const s = line.find (' ');
198                 if (s == string::npos) {
199                         continue;
200                 }
201                 
202                 string const k = line.substr (0, s);
203                 string const v = line.substr (s + 1);
204
205                 if (k == N_("num_local_encoding_threads")) {
206                         _num_local_encoding_threads = atoi (v.c_str ());
207                 } else if (k == N_("default_directory")) {
208                         _default_directory = v;
209                 } else if (k == N_("server_port")) {
210                         _server_port_base = atoi (v.c_str ());
211                 } else if (k == N_("server")) {
212                         vector<string> b;
213                         split (b, v, is_any_of (" "));
214                         if (b.size() == 2) {
215                                 _servers.push_back (b[0]);
216                         }
217                 } else if (k == N_("tms_ip")) {
218                         _tms_ip = v;
219                 } else if (k == N_("tms_path")) {
220                         _tms_path = v;
221                 } else if (k == N_("tms_user")) {
222                         _tms_user = v;
223                 } else if (k == N_("tms_password")) {
224                         _tms_password = v;
225                 } else if (k == N_("sound_processor")) {
226                         _sound_processor = SoundProcessor::from_id (v);
227                 } else if (k == "language") {
228                         _language = v;
229                 } else if (k == "default_container") {
230                         _default_container = Ratio::from_id (v);
231                 } else if (k == "default_dcp_content_type") {
232                         _default_dcp_content_type = DCPContentType::from_dci_name (v);
233                 } else if (k == "dcp_metadata_issuer") {
234                         _dcp_metadata.issuer = v;
235                 } else if (k == "dcp_metadata_creator") {
236                         _dcp_metadata.creator = v;
237                 } else if (k == "dcp_metadata_issue_date") {
238                         _dcp_metadata.issue_date = v;
239                 }
240
241                 _default_dci_metadata.read_old_metadata (k, v);
242         }
243 }
244
245 /** @return Filename to write configuration to */
246 boost::filesystem::path
247 Config::file (bool old) const
248 {
249         boost::filesystem::path p;
250         p /= g_get_user_config_dir ();
251         boost::system::error_code ec;
252         boost::filesystem::create_directory (p, ec);
253         if (old) {
254                 p /= ".dvdomatic";
255         } else {
256                 p /= "dcpomatic";
257                 boost::filesystem::create_directory (p, ec);
258                 p /= "config.xml";
259         }
260         return p;
261 }
262
263 boost::filesystem::path
264 Config::signer_chain_directory () const
265 {
266         boost::filesystem::path p;
267         p /= g_get_user_config_dir ();
268         p /= "dcpomatic";
269         p /= "crypt";
270         boost::filesystem::create_directories (p);
271         return p;
272 }
273
274 /** @return Singleton instance */
275 Config *
276 Config::instance ()
277 {
278         if (_instance == 0) {
279                 _instance = new Config;
280                 try {
281                         _instance->read ();
282                 } catch (exception& e) {
283                         /* configuration load failed; never mind, just
284                            stick with the default.
285                         */
286                         cerr << "dcpomatic: failed to load configuration (" << e.what() << ")\n";
287                 } catch (...) {
288                         cerr << "dcpomatic: failed to load configuration\n";
289                 }
290         }
291
292         return _instance;
293 }
294
295 /** Write our configuration to disk */
296 void
297 Config::write () const
298 {
299         LocaleGuard lg;
300         
301         xmlpp::Document doc;
302         xmlpp::Element* root = doc.create_root_node ("Config");
303
304         root->add_child("Version")->add_child_text ("1");
305         root->add_child("NumLocalEncodingThreads")->add_child_text (lexical_cast<string> (_num_local_encoding_threads));
306         root->add_child("DefaultDirectory")->add_child_text (_default_directory.string ());
307         root->add_child("ServerPortBase")->add_child_text (lexical_cast<string> (_server_port_base));
308         root->add_child("UseAnyServers")->add_child_text (_use_any_servers ? "1" : "0");
309         
310         for (vector<string>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
311                 root->add_child("Server")->add_child_text (*i);
312         }
313
314         root->add_child("TMSIP")->add_child_text (_tms_ip);
315         root->add_child("TMSPath")->add_child_text (_tms_path);
316         root->add_child("TMSUser")->add_child_text (_tms_user);
317         root->add_child("TMSPassword")->add_child_text (_tms_password);
318         if (_sound_processor) {
319                 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
320         }
321         if (_language) {
322                 root->add_child("Language")->add_child_text (_language.get());
323         }
324         if (_default_container) {
325                 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
326         }
327         if (_default_dcp_content_type) {
328                 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->dci_name ());
329         }
330         root->add_child("DCPMetadataIssuer")->add_child_text (_dcp_metadata.issuer);
331         root->add_child("DCPMetadataCreator")->add_child_text (_dcp_metadata.creator);
332
333         _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
334
335         root->add_child("DefaultStillLength")->add_child_text (lexical_cast<string> (_default_still_length));
336         root->add_child("DefaultJ2KBandwidth")->add_child_text (lexical_cast<string> (_default_j2k_bandwidth));
337
338         for (vector<PresetColourConversion>::const_iterator i = _colour_conversions.begin(); i != _colour_conversions.end(); ++i) {
339                 i->as_xml (root->add_child ("ColourConversion"));
340         }
341
342         for (list<shared_ptr<Cinema> >::const_iterator i = _cinemas.begin(); i != _cinemas.end(); ++i) {
343                 (*i)->as_xml (root->add_child ("Cinema"));
344         }
345
346         root->add_child("MailServer")->add_child_text (_mail_server);
347         root->add_child("KDMFrom")->add_child_text (_kdm_from);
348         root->add_child("KDMEmail")->add_child_text (_kdm_email);
349
350         doc.write_to_file_formatted (file(false).string ());
351 }
352
353 boost::filesystem::path
354 Config::default_directory_or (boost::filesystem::path a) const
355 {
356         if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
357                 return a;
358         }
359
360         return _default_directory;
361 }
362
363 void
364 Config::drop ()
365 {
366         delete _instance;
367         _instance = 0;
368 }