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 <libcxml/cxml.h>
31 #include "dcp_content_type.h"
32 #include "sound_processor.h"
42 using boost::shared_ptr;
43 using boost::lexical_cast;
44 using boost::optional;
46 Config* Config::_instance = 0;
48 /** Construct default configuration */
50 : _num_local_encoding_threads (max (2U, boost::thread::hardware_concurrency()))
53 , _sound_processor (SoundProcessor::from_id (N_("dolby_cp750")))
54 , _default_still_length (10)
55 , _default_container (Ratio::from_id ("185"))
56 , _default_dcp_content_type (DCPContentType::from_dci_name ("TST"))
57 , _default_j2k_bandwidth (200000000)
59 _allowed_dcp_frame_rates.push_back (24);
60 _allowed_dcp_frame_rates.push_back (25);
61 _allowed_dcp_frame_rates.push_back (30);
62 _allowed_dcp_frame_rates.push_back (48);
63 _allowed_dcp_frame_rates.push_back (50);
64 _allowed_dcp_frame_rates.push_back (60);
70 if (!boost::filesystem::exists (file (false))) {
75 cxml::File f (file (false), "Config");
78 _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
79 _default_directory = f.string_child ("DefaultDirectory");
80 _server_port = f.number_child<int> ("ServerPort");
82 list<shared_ptr<cxml::Node> > servers = f.node_children ("Server");
83 for (list<shared_ptr<cxml::Node> >::iterator i = servers.begin(); i != servers.end(); ++i) {
84 _servers.push_back (new ServerDescription (*i));
87 _tms_ip = f.string_child ("TMSIP");
88 _tms_path = f.string_child ("TMSPath");
89 _tms_user = f.string_child ("TMSUser");
90 _tms_password = f.string_child ("TMSPassword");
92 c = f.optional_string_child ("SoundProcessor");
94 _sound_processor = SoundProcessor::from_id (c.get ());
97 _language = f.optional_string_child ("Language");
99 c = f.optional_string_child ("DefaultContainer");
101 _default_container = Ratio::from_id (c.get ());
104 c = f.optional_string_child ("DefaultDCPContentType");
106 _default_dcp_content_type = DCPContentType::from_dci_name (c.get ());
109 _dcp_metadata.issuer = f.optional_string_child ("DCPMetadataIssuer").get_value_or ("");
110 _dcp_metadata.creator = f.optional_string_child ("DCPMetadataCreator").get_value_or ("");
112 _default_dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
113 _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
114 _default_j2k_bandwidth = f.optional_number_child<int>("DefaultJ2KBandwidth").get_value_or (200000000);
118 Config::read_old_metadata ()
120 ifstream f (file(true).c_str ());
122 while (getline (f, line)) {
127 if (line[0] == '#') {
131 size_t const s = line.find (' ');
132 if (s == string::npos) {
136 string const k = line.substr (0, s);
137 string const v = line.substr (s + 1);
139 if (k == N_("num_local_encoding_threads")) {
140 _num_local_encoding_threads = atoi (v.c_str ());
141 } else if (k == N_("default_directory")) {
142 _default_directory = v;
143 } else if (k == N_("server_port")) {
144 _server_port = atoi (v.c_str ());
145 } else if (k == N_("server")) {
146 _servers.push_back (ServerDescription::create_from_metadata (v));
147 } else if (k == N_("tms_ip")) {
149 } else if (k == N_("tms_path")) {
151 } else if (k == N_("tms_user")) {
153 } else if (k == N_("tms_password")) {
155 } else if (k == N_("sound_processor")) {
156 _sound_processor = SoundProcessor::from_id (v);
157 } else if (k == "language") {
159 } else if (k == "default_container") {
160 _default_container = Ratio::from_id (v);
161 } else if (k == "default_dcp_content_type") {
162 _default_dcp_content_type = DCPContentType::from_dci_name (v);
163 } else if (k == "dcp_metadata_issuer") {
164 _dcp_metadata.issuer = v;
165 } else if (k == "dcp_metadata_creator") {
166 _dcp_metadata.creator = v;
167 } else if (k == "dcp_metadata_issue_date") {
168 _dcp_metadata.issue_date = v;
171 _default_dci_metadata.read_old_metadata (k, v);
175 /** @return Filename to write configuration to */
177 Config::file (bool old) const
179 boost::filesystem::path p;
180 p /= g_get_user_config_dir ();
181 boost::system::error_code ec;
182 boost::filesystem::create_directory (p, ec);
186 p /= ".dcpomatic.xml";
191 /** @return Singleton instance */
195 if (_instance == 0) {
196 _instance = new Config;
200 /* configuration load failed; never mind, just
201 stick with the default.
209 /** Write our configuration to disk */
211 Config::write () const
214 xmlpp::Element* root = doc.create_root_node ("Config");
216 root->add_child("NumLocalEncodingThreads")->add_child_text (lexical_cast<string> (_num_local_encoding_threads));
217 root->add_child("DefaultDirectory")->add_child_text (_default_directory);
218 root->add_child("ServerPort")->add_child_text (lexical_cast<string> (_server_port));
220 for (vector<ServerDescription*>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
221 (*i)->as_xml (root->add_child ("Server"));
224 root->add_child("TMSIP")->add_child_text (_tms_ip);
225 root->add_child("TMSPath")->add_child_text (_tms_path);
226 root->add_child("TMSUser")->add_child_text (_tms_user);
227 root->add_child("TMSPassword")->add_child_text (_tms_password);
228 if (_sound_processor) {
229 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
232 root->add_child("Language")->add_child_text (_language.get());
234 if (_default_container) {
235 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
237 if (_default_dcp_content_type) {
238 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->dci_name ());
240 root->add_child("DCPMetadataIssuer")->add_child_text (_dcp_metadata.issuer);
241 root->add_child("DCPMetadataCreator")->add_child_text (_dcp_metadata.creator);
243 _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
245 root->add_child("DefaultStillLength")->add_child_text (lexical_cast<string> (_default_still_length));
246 root->add_child("DefaultJ2KBandwidth")->add_child_text (lexical_cast<string> (_default_j2k_bandwidth));
248 doc.write_to_file_formatted (file (false));
252 Config::default_directory_or (string a) const
254 if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
258 return _default_directory;