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