Remove dot from config .xml name; add non-linearised sRGB to defaults.
[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
36 #include "i18n.h"
37
38 using std::vector;
39 using std::ifstream;
40 using std::string;
41 using std::ofstream;
42 using std::list;
43 using std::max;
44 using boost::shared_ptr;
45 using boost::lexical_cast;
46 using boost::optional;
47
48 Config* Config::_instance = 0;
49
50 /** Construct default configuration */
51 Config::Config ()
52         : _num_local_encoding_threads (max (2U, boost::thread::hardware_concurrency()))
53         , _server_port (6192)
54         , _tms_path (N_("."))
55         , _sound_processor (SoundProcessor::from_id (N_("dolby_cp750")))
56         , _default_still_length (10)
57         , _default_container (Ratio::from_id ("185"))
58         , _default_dcp_content_type (DCPContentType::from_dci_name ("TST"))
59         , _default_j2k_bandwidth (200000000)
60 {
61         _allowed_dcp_frame_rates.push_back (24);
62         _allowed_dcp_frame_rates.push_back (25);
63         _allowed_dcp_frame_rates.push_back (30);
64         _allowed_dcp_frame_rates.push_back (48);
65         _allowed_dcp_frame_rates.push_back (50);
66         _allowed_dcp_frame_rates.push_back (60);
67
68         _colour_conversions.push_back (shared_ptr<ColourConversion> (
69                                                new ColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::xyz_to_rgb, 2.6))
70                 );
71         
72         _colour_conversions.push_back (shared_ptr<ColourConversion> (
73                                                new ColourConversion (_("sRGB non-linearised"), 2.4, false, libdcp::colour_matrix::xyz_to_rgb, 2.6))
74                 );
75 }
76
77 void
78 Config::read ()
79 {
80         if (!boost::filesystem::exists (file (false))) {
81                 read_old_metadata ();
82                 return;
83         }
84
85         cxml::File f (file (false), "Config");
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 (shared_ptr<ServerDescription> (new 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 (shared_ptr<ColourConversion> (new ColourConversion (*i)));
134         }
135 }
136
137 void
138 Config::read_old_metadata ()
139 {
140         ifstream f (file(true).c_str ());
141         string line;
142         while (getline (f, line)) {
143                 if (line.empty ()) {
144                         continue;
145                 }
146
147                 if (line[0] == '#') {
148                         continue;
149                 }
150
151                 size_t const s = line.find (' ');
152                 if (s == string::npos) {
153                         continue;
154                 }
155                 
156                 string const k = line.substr (0, s);
157                 string const v = line.substr (s + 1);
158
159                 if (k == N_("num_local_encoding_threads")) {
160                         _num_local_encoding_threads = atoi (v.c_str ());
161                 } else if (k == N_("default_directory")) {
162                         _default_directory = v;
163                 } else if (k == N_("server_port")) {
164                         _server_port = atoi (v.c_str ());
165                 } else if (k == N_("server")) {
166                         _servers.push_back (ServerDescription::create_from_metadata (v));
167                 } else if (k == N_("tms_ip")) {
168                         _tms_ip = v;
169                 } else if (k == N_("tms_path")) {
170                         _tms_path = v;
171                 } else if (k == N_("tms_user")) {
172                         _tms_user = v;
173                 } else if (k == N_("tms_password")) {
174                         _tms_password = v;
175                 } else if (k == N_("sound_processor")) {
176                         _sound_processor = SoundProcessor::from_id (v);
177                 } else if (k == "language") {
178                         _language = v;
179                 } else if (k == "default_container") {
180                         _default_container = Ratio::from_id (v);
181                 } else if (k == "default_dcp_content_type") {
182                         _default_dcp_content_type = DCPContentType::from_dci_name (v);
183                 } else if (k == "dcp_metadata_issuer") {
184                         _dcp_metadata.issuer = v;
185                 } else if (k == "dcp_metadata_creator") {
186                         _dcp_metadata.creator = v;
187                 } else if (k == "dcp_metadata_issue_date") {
188                         _dcp_metadata.issue_date = v;
189                 }
190
191                 _default_dci_metadata.read_old_metadata (k, v);
192         }
193 }
194
195 /** @return Filename to write configuration to */
196 string
197 Config::file (bool old) const
198 {
199         boost::filesystem::path p;
200         p /= g_get_user_config_dir ();
201         boost::system::error_code ec;
202         boost::filesystem::create_directory (p, ec);
203         if (old) {
204                 p /= ".dvdomatic";
205         } else {
206                 p /= "dcpomatic.xml";
207         }
208         return p.string ();
209 }
210
211 /** @return Singleton instance */
212 Config *
213 Config::instance ()
214 {
215         if (_instance == 0) {
216                 _instance = new Config;
217                 try {
218                         _instance->read ();
219                 } catch (...) {
220                         /* configuration load failed; never mind, just
221                            stick with the default.
222                         */
223                 }
224         }
225
226         return _instance;
227 }
228
229 /** Write our configuration to disk */
230 void
231 Config::write () const
232 {
233         xmlpp::Document doc;
234         xmlpp::Element* root = doc.create_root_node ("Config");
235
236         root->add_child("NumLocalEncodingThreads")->add_child_text (lexical_cast<string> (_num_local_encoding_threads));
237         root->add_child("DefaultDirectory")->add_child_text (_default_directory);
238         root->add_child("ServerPort")->add_child_text (lexical_cast<string> (_server_port));
239         
240         for (vector<shared_ptr<ServerDescription> >::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
241                 (*i)->as_xml (root->add_child ("Server"));
242         }
243
244         root->add_child("TMSIP")->add_child_text (_tms_ip);
245         root->add_child("TMSPath")->add_child_text (_tms_path);
246         root->add_child("TMSUser")->add_child_text (_tms_user);
247         root->add_child("TMSPassword")->add_child_text (_tms_password);
248         if (_sound_processor) {
249                 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
250         }
251         if (_language) {
252                 root->add_child("Language")->add_child_text (_language.get());
253         }
254         if (_default_container) {
255                 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
256         }
257         if (_default_dcp_content_type) {
258                 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->dci_name ());
259         }
260         root->add_child("DCPMetadataIssuer")->add_child_text (_dcp_metadata.issuer);
261         root->add_child("DCPMetadataCreator")->add_child_text (_dcp_metadata.creator);
262
263         _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
264
265         root->add_child("DefaultStillLength")->add_child_text (lexical_cast<string> (_default_still_length));
266         root->add_child("DefaultJ2KBandwidth")->add_child_text (lexical_cast<string> (_default_j2k_bandwidth));
267
268         for (vector<shared_ptr<ColourConversion> >::const_iterator i = _colour_conversions.begin(); i != _colour_conversions.end(); ++i) {
269                 (*i)->as_xml (root->add_child ("ColourConversion"));
270         }
271
272         doc.write_to_file_formatted (file (false));
273 }
274
275 string
276 Config::default_directory_or (string a) const
277 {
278         if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
279                 return a;
280         }
281
282         return _default_directory;
283 }
284
285 void
286 Config::drop ()
287 {
288         delete _instance;
289         _instance = 0;
290 }