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