2 Copyright (C) 2012 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.
24 #include <boost/filesystem.hpp>
25 #include <libdcp/colour_matrix.h>
26 #include <libcxml/cxml.h>
32 #include "dcp_content_type.h"
33 #include "sound_processor.h"
34 #include "colour_conversion.h"
45 using boost::shared_ptr;
46 using boost::lexical_cast;
47 using boost::optional;
49 Config* Config::_instance = 0;
51 /** Construct default configuration */
53 : _num_local_encoding_threads (max (2U, boost::thread::hardware_concurrency()))
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)
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);
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));
76 if (!boost::filesystem::exists (file (false))) {
81 cxml::Document f ("Config");
82 f.read_file (file (false));
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");
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));
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");
99 c = f.optional_string_child ("SoundProcessor");
101 _sound_processor = SoundProcessor::from_id (c.get ());
104 _language = f.optional_string_child ("Language");
106 c = f.optional_string_child ("DefaultContainer");
108 _default_container = Ratio::from_id (c.get ());
111 c = f.optional_string_child ("DefaultDCPContentType");
113 _default_dcp_content_type = DCPContentType::from_dci_name (c.get ());
116 _dcp_metadata.issuer = f.optional_string_child ("DCPMetadataIssuer").get_value_or ("");
117 _dcp_metadata.creator = f.optional_string_child ("DCPMetadataCreator").get_value_or ("");
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);
123 list<shared_ptr<cxml::Node> > cc = f.node_children ("ColourConversion");
126 _colour_conversions.clear ();
129 for (list<shared_ptr<cxml::Node> >::iterator i = cc.begin(); i != cc.end(); ++i) {
130 _colour_conversions.push_back (PresetColourConversion (*i));
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
138 shared_ptr<Cinema> cinema (new Cinema (*i));
139 cinema->read_screens (*i);
140 _cinemas.push_back (cinema);
143 _mail_server = f.string_child ("MailServer");
144 _kdm_from = f.string_child ("KDMFrom");
148 Config::read_old_metadata ()
150 ifstream f (file(true).c_str ());
153 while (getline (f, line)) {
158 if (line[0] == '#') {
162 size_t const s = line.find (' ');
163 if (s == string::npos) {
167 string const k = line.substr (0, s);
168 string const v = line.substr (s + 1);
170 if (k == N_("num_local_encoding_threads")) {
171 _num_local_encoding_threads = atoi (v.c_str ());
172 } else if (k == N_("default_directory")) {
173 _default_directory = v;
174 } else if (k == N_("server_port")) {
175 _server_port = atoi (v.c_str ());
176 } else if (k == N_("server")) {
177 optional<ServerDescription> server = ServerDescription::create_from_metadata (v);
179 _servers.push_back (server.get ());
181 } else if (k == N_("tms_ip")) {
183 } else if (k == N_("tms_path")) {
185 } else if (k == N_("tms_user")) {
187 } else if (k == N_("tms_password")) {
189 } else if (k == N_("sound_processor")) {
190 _sound_processor = SoundProcessor::from_id (v);
191 } else if (k == "language") {
193 } else if (k == "default_container") {
194 _default_container = Ratio::from_id (v);
195 } else if (k == "default_dcp_content_type") {
196 _default_dcp_content_type = DCPContentType::from_dci_name (v);
197 } else if (k == "dcp_metadata_issuer") {
198 _dcp_metadata.issuer = v;
199 } else if (k == "dcp_metadata_creator") {
200 _dcp_metadata.creator = v;
201 } else if (k == "dcp_metadata_issue_date") {
202 _dcp_metadata.issue_date = v;
205 _default_dci_metadata.read_old_metadata (k, v);
209 /** @return Filename to write configuration to */
210 boost::filesystem::path
211 Config::file (bool old) const
213 boost::filesystem::path p;
214 p /= g_get_user_config_dir ();
215 boost::system::error_code ec;
216 boost::filesystem::create_directory (p, ec);
220 p /= "dcpomatic.xml";
225 boost::filesystem::path
226 Config::signer_chain_directory () const
228 boost::filesystem::path p;
229 p /= g_get_user_config_dir ();
232 boost::filesystem::create_directories (p);
236 /** @return Singleton instance */
240 if (_instance == 0) {
241 _instance = new Config;
245 /* configuration load failed; never mind, just
246 stick with the default.
254 /** Write our configuration to disk */
256 Config::write () const
259 xmlpp::Element* root = doc.create_root_node ("Config");
261 root->add_child("NumLocalEncodingThreads")->add_child_text (lexical_cast<string> (_num_local_encoding_threads));
262 root->add_child("DefaultDirectory")->add_child_text (_default_directory.string ());
263 root->add_child("ServerPort")->add_child_text (lexical_cast<string> (_server_port));
265 for (vector<ServerDescription>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
266 i->as_xml (root->add_child ("Server"));
269 root->add_child("TMSIP")->add_child_text (_tms_ip);
270 root->add_child("TMSPath")->add_child_text (_tms_path);
271 root->add_child("TMSUser")->add_child_text (_tms_user);
272 root->add_child("TMSPassword")->add_child_text (_tms_password);
273 if (_sound_processor) {
274 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
277 root->add_child("Language")->add_child_text (_language.get());
279 if (_default_container) {
280 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
282 if (_default_dcp_content_type) {
283 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->dci_name ());
285 root->add_child("DCPMetadataIssuer")->add_child_text (_dcp_metadata.issuer);
286 root->add_child("DCPMetadataCreator")->add_child_text (_dcp_metadata.creator);
288 _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
290 root->add_child("DefaultStillLength")->add_child_text (lexical_cast<string> (_default_still_length));
291 root->add_child("DefaultJ2KBandwidth")->add_child_text (lexical_cast<string> (_default_j2k_bandwidth));
293 for (vector<PresetColourConversion>::const_iterator i = _colour_conversions.begin(); i != _colour_conversions.end(); ++i) {
294 i->as_xml (root->add_child ("ColourConversion"));
297 for (list<shared_ptr<Cinema> >::const_iterator i = _cinemas.begin(); i != _cinemas.end(); ++i) {
298 (*i)->as_xml (root->add_child ("Cinema"));
301 root->add_child("MailServer")->add_child_text (_mail_server);
302 root->add_child("KDMFrom")->add_child_text (_kdm_from);
304 doc.write_to_file_formatted (file(false).string ());
307 boost::filesystem::path
308 Config::default_directory_or (boost::filesystem::path a) const
310 if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
314 return _default_directory;