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