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"
41 using boost::shared_ptr;
42 using boost::lexical_cast;
43 using boost::optional;
45 Config* Config::_instance = 0;
47 /** Construct default configuration */
49 : _num_local_encoding_threads (2)
51 , _reference_scaler (Scaler::from_id (N_("bicubic")))
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 (0)
58 _allowed_dcp_frame_rates.push_back (24);
59 _allowed_dcp_frame_rates.push_back (25);
60 _allowed_dcp_frame_rates.push_back (30);
61 _allowed_dcp_frame_rates.push_back (48);
62 _allowed_dcp_frame_rates.push_back (50);
63 _allowed_dcp_frame_rates.push_back (60);
69 if (!boost::filesystem::exists (file (false))) {
74 cxml::File f (file (false), "Config");
77 _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
78 _default_directory = f.string_child ("DefaultDirectory");
79 _server_port = f.number_child<int> ("ServerPort");
80 c = f.optional_string_child ("ReferenceScaler");
82 _reference_scaler = Scaler::from_id (c.get ());
85 list<shared_ptr<cxml::Node> > filters = f.node_children ("ReferenceFilter");
86 for (list<shared_ptr<cxml::Node> >::iterator i = filters.begin(); i != filters.end(); ++i) {
87 _reference_filters.push_back (Filter::from_id ((*i)->content ()));
90 list<shared_ptr<cxml::Node> > servers = f.node_children ("Server");
91 for (list<shared_ptr<cxml::Node> >::iterator i = servers.begin(); i != servers.end(); ++i) {
92 _servers.push_back (new ServerDescription (*i));
95 _tms_ip = f.string_child ("TMSIP");
96 _tms_path = f.string_child ("TMSPath");
97 _tms_user = f.string_child ("TMSUser");
98 _tms_password = f.string_child ("TMSPassword");
100 c = f.optional_string_child ("SoundProcessor");
102 _sound_processor = SoundProcessor::from_id (c.get ());
105 _language = f.optional_string_child ("Language");
107 c = f.optional_string_child ("DefaultContainer");
109 _default_container = Ratio::from_id (c.get ());
112 c = f.optional_string_child ("DefaultDCPContentType");
114 _default_dcp_content_type = DCPContentType::from_dci_name (c.get ());
117 _dcp_metadata.issuer = f.optional_string_child ("DCPMetadataIssuer").get_value_or ("");
118 _dcp_metadata.creator = f.optional_string_child ("DCPMetadataCreator").get_value_or ("");
120 _default_dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
121 _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
125 Config::read_old_metadata ()
127 ifstream f (file(true).c_str ());
129 while (getline (f, line)) {
134 if (line[0] == '#') {
138 size_t const s = line.find (' ');
139 if (s == string::npos) {
143 string const k = line.substr (0, s);
144 string const v = line.substr (s + 1);
146 if (k == N_("num_local_encoding_threads")) {
147 _num_local_encoding_threads = atoi (v.c_str ());
148 } else if (k == N_("default_directory")) {
149 _default_directory = v;
150 } else if (k == N_("server_port")) {
151 _server_port = atoi (v.c_str ());
152 } else if (k == N_("reference_scaler")) {
153 _reference_scaler = Scaler::from_id (v);
154 } else if (k == N_("reference_filter")) {
155 _reference_filters.push_back (Filter::from_id (v));
156 } else if (k == N_("server")) {
157 _servers.push_back (ServerDescription::create_from_metadata (v));
158 } else if (k == N_("tms_ip")) {
160 } else if (k == N_("tms_path")) {
162 } else if (k == N_("tms_user")) {
164 } else if (k == N_("tms_password")) {
166 } else if (k == N_("sound_processor")) {
167 _sound_processor = SoundProcessor::from_id (v);
168 } else if (k == "language") {
170 } else if (k == "default_container") {
171 _default_container = Ratio::from_id (v);
172 } else if (k == "default_dcp_content_type") {
173 _default_dcp_content_type = DCPContentType::from_dci_name (v);
174 } else if (k == "dcp_metadata_issuer") {
175 _dcp_metadata.issuer = v;
176 } else if (k == "dcp_metadata_creator") {
177 _dcp_metadata.creator = v;
178 } else if (k == "dcp_metadata_issue_date") {
179 _dcp_metadata.issue_date = v;
182 _default_dci_metadata.read_old_metadata (k, v);
186 /** @return Filename to write configuration to */
188 Config::file (bool old) const
190 boost::filesystem::path p;
191 p /= g_get_user_config_dir ();
192 boost::system::error_code ec;
193 boost::filesystem::create_directory (p, ec);
197 p /= ".dcpomatic.xml";
202 /** @return Singleton instance */
206 if (_instance == 0) {
207 _instance = new Config;
211 /* configuration load failed; never mind, just
212 stick with the default.
220 /** Write our configuration to disk */
222 Config::write () const
225 xmlpp::Element* root = doc.create_root_node ("Config");
227 root->add_child("NumLocalEncodingThreads")->add_child_text (lexical_cast<string> (_num_local_encoding_threads));
228 root->add_child("DefaultDirectory")->add_child_text (_default_directory);
229 root->add_child("ServerPort")->add_child_text (lexical_cast<string> (_server_port));
230 if (_reference_scaler) {
231 root->add_child("ReferenceScaler")->add_child_text (_reference_scaler->id ());
234 for (vector<Filter const *>::const_iterator i = _reference_filters.begin(); i != _reference_filters.end(); ++i) {
235 root->add_child("ReferenceFilter")->add_child_text ((*i)->id ());
238 for (vector<ServerDescription*>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
239 (*i)->as_xml (root->add_child ("Server"));
242 root->add_child("TMSIP")->add_child_text (_tms_ip);
243 root->add_child("TMSPath")->add_child_text (_tms_path);
244 root->add_child("TMSUser")->add_child_text (_tms_user);
245 root->add_child("TMSPassword")->add_child_text (_tms_password);
246 if (_sound_processor) {
247 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
250 root->add_child("Language")->add_child_text (_language.get());
252 if (_default_container) {
253 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
255 if (_default_dcp_content_type) {
256 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->dci_name ());
258 root->add_child("DCPMetadataIssuer")->add_child_text (_dcp_metadata.issuer);
259 root->add_child("DCPMetadataCreator")->add_child_text (_dcp_metadata.creator);
261 _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
263 root->add_child("DefaultStillLength")->add_child_text (lexical_cast<string> (_default_still_length));
265 doc.write_to_file_formatted (file (false));
269 Config::default_directory_or (string a) const
271 if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
275 return _default_directory;