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