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