2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
23 #include <boost/filesystem.hpp>
24 #include <boost/algorithm/string.hpp>
25 #include <libdcp/colour_matrix.h>
26 #include <libdcp/raw_convert.h>
27 #include <libcxml/cxml.h>
33 #include "dcp_content_type.h"
34 #include "sound_processor.h"
35 #include "colour_conversion.h"
50 using boost::shared_ptr;
51 using boost::optional;
52 using boost::algorithm::is_any_of;
53 using boost::algorithm::split;
54 using libdcp::raw_convert;
56 Config* Config::_instance = 0;
58 /** Construct default configuration */
60 : _num_local_encoding_threads (max (2U, boost::thread::hardware_concurrency()))
61 , _server_port_base (6192)
62 , _use_any_servers (true)
64 , _sound_processor (SoundProcessor::from_id (N_("dolby_cp750")))
65 , _allow_any_dcp_frame_rate (false)
66 , _default_still_length (10)
67 , _default_scale (Ratio::from_id ("185"))
68 , _default_container (Ratio::from_id ("185"))
69 , _default_dcp_content_type (DCPContentType::from_isdcf_name ("TST"))
70 , _default_j2k_bandwidth (100000000)
71 , _default_audio_delay (0)
72 , _check_for_updates (false)
73 , _check_for_test_updates (false)
74 , _maximum_j2k_bandwidth (250000000)
75 , _log_types (Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR)
77 _allowed_dcp_frame_rates.push_back (24);
78 _allowed_dcp_frame_rates.push_back (25);
79 _allowed_dcp_frame_rates.push_back (30);
80 _allowed_dcp_frame_rates.push_back (48);
81 _allowed_dcp_frame_rates.push_back (50);
82 _allowed_dcp_frame_rates.push_back (60);
84 _colour_conversions.push_back (PresetColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::srgb_to_xyz, 2.6));
85 _colour_conversions.push_back (PresetColourConversion (_("sRGB non-linearised"), 2.4, false, libdcp::colour_matrix::srgb_to_xyz, 2.6));
86 _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, libdcp::colour_matrix::rec709_to_xyz, 2.6));
94 if (!boost::filesystem::exists (file (false))) {
99 cxml::Document f ("Config");
100 f.read_file (file (false));
103 optional<int> version = f.optional_number_child<int> ("Version");
105 _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
106 _default_directory = f.string_child ("DefaultDirectory");
108 boost::optional<int> b = f.optional_number_child<int> ("ServerPort");
110 b = f.optional_number_child<int> ("ServerPortBase");
112 _server_port_base = b.get ();
114 boost::optional<bool> u = f.optional_bool_child ("UseAnyServers");
115 _use_any_servers = u.get_value_or (true);
117 list<cxml::NodePtr> servers = f.node_children ("Server");
118 for (list<cxml::NodePtr>::iterator i = servers.begin(); i != servers.end(); ++i) {
119 if ((*i)->node_children("HostName").size() == 1) {
120 _servers.push_back ((*i)->string_child ("HostName"));
122 _servers.push_back ((*i)->content ());
126 _tms_ip = f.string_child ("TMSIP");
127 _tms_path = f.string_child ("TMSPath");
128 _tms_user = f.string_child ("TMSUser");
129 _tms_password = f.string_child ("TMSPassword");
131 c = f.optional_string_child ("SoundProcessor");
133 _sound_processor = SoundProcessor::from_id (c.get ());
136 _language = f.optional_string_child ("Language");
138 c = f.optional_string_child ("DefaultScale");
140 _default_scale = Ratio::from_id (c.get ());
143 c = f.optional_string_child ("DefaultContainer");
145 _default_container = Ratio::from_id (c.get ());
148 c = f.optional_string_child ("DefaultDCPContentType");
150 _default_dcp_content_type = DCPContentType::from_isdcf_name (c.get ());
153 if (f.optional_string_child ("DCPMetadataIssuer")) {
154 _dcp_issuer = f.string_child ("DCPMetadataIssuer");
155 } else if (f.optional_string_child ("DCPIssuer")) {
156 _dcp_issuer = f.string_child ("DCPIssuer");
159 if (version && version.get() >= 2) {
160 _default_isdcf_metadata = ISDCFMetadata (f.node_child ("ISDCFMetadata"));
162 _default_isdcf_metadata = ISDCFMetadata (f.node_child ("DCIMetadata"));
165 _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
166 _default_j2k_bandwidth = f.optional_number_child<int>("DefaultJ2KBandwidth").get_value_or (200000000);
167 _default_audio_delay = f.optional_number_child<int>("DefaultAudioDelay").get_value_or (0);
169 list<cxml::NodePtr> cc = f.node_children ("ColourConversion");
172 _colour_conversions.clear ();
175 for (list<cxml::NodePtr>::iterator i = cc.begin(); i != cc.end(); ++i) {
176 _colour_conversions.push_back (PresetColourConversion (*i));
180 /* Loading version 0 (before Rec. 709 was added as a preset).
183 _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, libdcp::colour_matrix::rec709_to_xyz, 2.6));
186 list<cxml::NodePtr> cin = f.node_children ("Cinema");
187 for (list<cxml::NodePtr>::iterator i = cin.begin(); i != cin.end(); ++i) {
188 /* Slightly grotty two-part construction of Cinema here so that we can use
191 shared_ptr<Cinema> cinema (new Cinema (*i));
192 cinema->read_screens (*i);
193 _cinemas.push_back (cinema);
196 _mail_server = f.string_child ("MailServer");
197 _mail_user = f.optional_string_child("MailUser").get_value_or ("");
198 _mail_password = f.optional_string_child("MailPassword").get_value_or ("");
199 _kdm_subject = f.optional_string_child ("KDMSubject").get_value_or (_("KDM delivery: $CPL_NAME"));
200 _kdm_from = f.string_child ("KDMFrom");
201 _kdm_cc = f.optional_string_child ("KDMCC").get_value_or ("");
202 _kdm_bcc = f.optional_string_child ("KDMBCC").get_value_or ("");
203 _kdm_email = f.string_child ("KDMEmail");
205 _check_for_updates = f.optional_bool_child("CheckForUpdates").get_value_or (false);
206 _check_for_test_updates = f.optional_bool_child("CheckForTestUpdates").get_value_or (false);
208 _maximum_j2k_bandwidth = f.optional_number_child<int> ("MaximumJ2KBandwidth").get_value_or (250000000);
209 _allow_any_dcp_frame_rate = f.optional_bool_child ("AllowAnyDCPFrameRate");
211 _log_types = f.optional_number_child<int> ("LogTypes").get_value_or (Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR);
213 list<cxml::NodePtr> his = f.node_children ("History");
214 for (list<cxml::NodePtr>::const_iterator i = his.begin(); i != his.end(); ++i) {
215 _history.push_back ((*i)->content ());
220 Config::read_old_metadata ()
222 /* XXX: this won't work with non-Latin filenames */
223 ifstream f (file(true).string().c_str ());
226 while (getline (f, line)) {
231 if (line[0] == '#') {
235 size_t const s = line.find (' ');
236 if (s == string::npos) {
240 string const k = line.substr (0, s);
241 string const v = line.substr (s + 1);
243 if (k == N_("num_local_encoding_threads")) {
244 _num_local_encoding_threads = atoi (v.c_str ());
245 } else if (k == N_("default_directory")) {
246 _default_directory = v;
247 } else if (k == N_("server_port")) {
248 _server_port_base = atoi (v.c_str ());
249 } else if (k == N_("server")) {
251 split (b, v, is_any_of (" "));
253 _servers.push_back (b[0]);
255 } else if (k == N_("tms_ip")) {
257 } else if (k == N_("tms_path")) {
259 } else if (k == N_("tms_user")) {
261 } else if (k == N_("tms_password")) {
263 } else if (k == N_("sound_processor")) {
264 _sound_processor = SoundProcessor::from_id (v);
265 } else if (k == "language") {
267 } else if (k == "default_container") {
268 _default_container = Ratio::from_id (v);
269 } else if (k == "default_dcp_content_type") {
270 _default_dcp_content_type = DCPContentType::from_isdcf_name (v);
271 } else if (k == "dcp_metadata_issuer") {
275 _default_isdcf_metadata.read_old_metadata (k, v);
279 /** @return Filename to write configuration to */
280 boost::filesystem::path
281 Config::file (bool old) const
283 boost::filesystem::path p;
284 p /= g_get_user_config_dir ();
285 boost::system::error_code ec;
286 boost::filesystem::create_directory (p, ec);
291 boost::filesystem::create_directory (p, ec);
297 boost::filesystem::path
298 Config::signer_chain_directory () const
300 boost::filesystem::path p;
301 p /= g_get_user_config_dir ();
304 boost::filesystem::create_directories (p);
308 /** @return Singleton instance */
312 if (_instance == 0) {
313 _instance = new Config;
316 } catch (exception& e) {
317 /* configuration load failed; never mind, just
318 stick with the default.
320 cerr << "dcpomatic: failed to load configuration (" << e.what() << ")\n";
322 cerr << "dcpomatic: failed to load configuration\n";
329 /** Write our configuration to disk */
331 Config::write () const
334 xmlpp::Element* root = doc.create_root_node ("Config");
336 root->add_child("Version")->add_child_text ("2");
337 root->add_child("NumLocalEncodingThreads")->add_child_text (raw_convert<string> (_num_local_encoding_threads));
338 root->add_child("DefaultDirectory")->add_child_text (_default_directory.string ());
339 root->add_child("ServerPortBase")->add_child_text (raw_convert<string> (_server_port_base));
340 root->add_child("UseAnyServers")->add_child_text (_use_any_servers ? "1" : "0");
342 for (vector<string>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
343 root->add_child("Server")->add_child_text (*i);
346 root->add_child("TMSIP")->add_child_text (_tms_ip);
347 root->add_child("TMSPath")->add_child_text (_tms_path);
348 root->add_child("TMSUser")->add_child_text (_tms_user);
349 root->add_child("TMSPassword")->add_child_text (_tms_password);
350 if (_sound_processor) {
351 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
354 root->add_child("Language")->add_child_text (_language.get());
356 if (_default_scale) {
357 root->add_child("DefaultScale")->add_child_text (_default_scale->id ());
359 if (_default_container) {
360 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
362 if (_default_dcp_content_type) {
363 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->isdcf_name ());
365 root->add_child("DCPIssuer")->add_child_text (_dcp_issuer);
367 _default_isdcf_metadata.as_xml (root->add_child ("ISDCFMetadata"));
369 root->add_child("DefaultStillLength")->add_child_text (raw_convert<string> (_default_still_length));
370 root->add_child("DefaultJ2KBandwidth")->add_child_text (raw_convert<string> (_default_j2k_bandwidth));
371 root->add_child("DefaultAudioDelay")->add_child_text (raw_convert<string> (_default_audio_delay));
373 for (vector<PresetColourConversion>::const_iterator i = _colour_conversions.begin(); i != _colour_conversions.end(); ++i) {
374 i->as_xml (root->add_child ("ColourConversion"));
377 for (list<shared_ptr<Cinema> >::const_iterator i = _cinemas.begin(); i != _cinemas.end(); ++i) {
378 (*i)->as_xml (root->add_child ("Cinema"));
381 root->add_child("MailServer")->add_child_text (_mail_server);
382 root->add_child("MailUser")->add_child_text (_mail_user);
383 root->add_child("MailPassword")->add_child_text (_mail_password);
384 root->add_child("KDMSubject")->add_child_text (_kdm_subject);
385 root->add_child("KDMFrom")->add_child_text (_kdm_from);
386 root->add_child("KDMCC")->add_child_text (_kdm_cc);
387 root->add_child("KDMBCC")->add_child_text (_kdm_bcc);
388 root->add_child("KDMEmail")->add_child_text (_kdm_email);
390 root->add_child("CheckForUpdates")->add_child_text (_check_for_updates ? "1" : "0");
391 root->add_child("CheckForTestUpdates")->add_child_text (_check_for_test_updates ? "1" : "0");
393 root->add_child("MaximumJ2KBandwidth")->add_child_text (raw_convert<string> (_maximum_j2k_bandwidth));
394 root->add_child("AllowAnyDCPFrameRate")->add_child_text (_allow_any_dcp_frame_rate ? "1" : "0");
395 root->add_child("LogTypes")->add_child_text (raw_convert<string> (_log_types));
397 for (vector<boost::filesystem::path>::const_iterator i = _history.begin(); i != _history.end(); ++i) {
398 root->add_child("History")->add_child_text (i->string ());
401 doc.write_to_file_formatted (file(false).string ());
404 boost::filesystem::path
405 Config::default_directory_or (boost::filesystem::path a) const
407 if (_default_directory.empty()) {
411 boost::system::error_code ec;
412 bool const e = boost::filesystem::exists (_default_directory, ec);
417 return _default_directory;
435 Config::reset_kdm_email ()
438 "Dear Projectionist\n\n"
439 "Please find attached KDMs for $CPL_NAME.\n\n"
440 "Cinema: $CINEMA_NAME\n"
441 "Screen(s): $SCREENS\n\n"
442 "The KDMs are valid from $START_TIME until $END_TIME.\n\n"
443 "Best regards,\nDCP-o-matic"
448 Config::add_to_history (boost::filesystem::path p)
450 /* Remove existing instances of this path in the history */
451 _history.erase (remove (_history.begin(), _history.end(), p), _history.end ());
453 _history.insert (_history.begin (), p);
454 if (_history.size() > HISTORY_SIZE) {
455 _history.pop_back ();