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>
30 #include "sound_processor.h"
39 using boost::shared_ptr;
40 using boost::optional;
42 Config* Config::_instance = 0;
44 /** Construct default configuration */
46 : _num_local_encoding_threads (2)
48 , _reference_scaler (Scaler::from_id (N_("bicubic")))
50 , _sound_processor (SoundProcessor::from_id (N_("dolby_cp750")))
52 _allowed_dcp_frame_rates.push_back (24);
53 _allowed_dcp_frame_rates.push_back (25);
54 _allowed_dcp_frame_rates.push_back (30);
55 _allowed_dcp_frame_rates.push_back (48);
56 _allowed_dcp_frame_rates.push_back (50);
57 _allowed_dcp_frame_rates.push_back (60);
59 if (!boost::filesystem::exists (file (false))) {
64 cxml::File f (file (false), "Config");
67 _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
68 _default_directory = f.string_child ("DefaultDirectory");
69 _server_port = f.number_child<int> ("ServerPort");
70 c = f.optional_string_child ("ReferenceScaler");
72 _reference_scaler = Scaler::from_id (c.get ());
75 list<shared_ptr<cxml::Node> > filters = f.node_children ("ReferenceFilter");
76 for (list<shared_ptr<cxml::Node> >::iterator i = filters.begin(); i != filters.end(); ++i) {
77 _reference_filters.push_back (Filter::from_id ((*i)->content ()));
80 list<shared_ptr<cxml::Node> > servers = f.node_children ("Server");
81 for (list<shared_ptr<cxml::Node> >::iterator i = servers.begin(); i != servers.end(); ++i) {
82 _servers.push_back (new ServerDescription (*i));
85 _tms_ip = f.string_child ("TMSIP");
86 _tms_path = f.string_child ("TMSPath");
87 _tms_user = f.string_child ("TMSUser");
88 _tms_password = f.string_child ("TMSPassword");
90 c = f.optional_string_child ("SoundProcessor");
92 _sound_processor = SoundProcessor::from_id (c.get ());
95 _language = f.optional_string_child ("Language");
96 _default_dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
100 Config::read_old_metadata ()
102 ifstream f (file(true).c_str ());
104 while (getline (f, line)) {
109 if (line[0] == '#') {
113 size_t const s = line.find (' ');
114 if (s == string::npos) {
118 string const k = line.substr (0, s);
119 string const v = line.substr (s + 1);
121 if (k == N_("num_local_encoding_threads")) {
122 _num_local_encoding_threads = atoi (v.c_str ());
123 } else if (k == N_("default_directory")) {
124 _default_directory = v;
125 } else if (k == N_("server_port")) {
126 _server_port = atoi (v.c_str ());
127 } else if (k == N_("reference_scaler")) {
128 _reference_scaler = Scaler::from_id (v);
129 } else if (k == N_("reference_filter")) {
130 _reference_filters.push_back (Filter::from_id (v));
131 } else if (k == N_("server")) {
132 _servers.push_back (ServerDescription::create_from_metadata (v));
133 } else if (k == N_("tms_ip")) {
135 } else if (k == N_("tms_path")) {
137 } else if (k == N_("tms_user")) {
139 } else if (k == N_("tms_password")) {
141 } else if (k == N_("sound_processor")) {
142 _sound_processor = SoundProcessor::from_id (v);
143 } else if (k == "language") {
147 _default_dci_metadata.read_old_metadata (k, v);
151 /** @return Filename to write configuration to */
153 Config::file (bool old) const
155 boost::filesystem::path p;
156 p /= g_get_user_config_dir ();
160 p /= ".dvdomatic.xml";
165 /** @return Singleton instance */
169 if (_instance == 0) {
170 _instance = new Config;
176 /** Write our configuration to disk */
178 Config::write () const
181 xmlpp::Element* root = doc.create_root_node ("Config");
183 root->add_child("NumLocalEncodingThreads")->add_child_text (boost::lexical_cast<string> (_num_local_encoding_threads));
184 root->add_child("DefaultDirectory")->add_child_text (_default_directory);
185 root->add_child("ServerPort")->add_child_text (boost::lexical_cast<string> (_server_port));
186 if (_reference_scaler) {
187 root->add_child("ReferenceScaler")->add_child_text (_reference_scaler->id ());
190 for (vector<Filter const *>::const_iterator i = _reference_filters.begin(); i != _reference_filters.end(); ++i) {
191 root->add_child("ReferenceFilter")->add_child_text ((*i)->id ());
194 for (vector<ServerDescription*>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
195 (*i)->as_xml (root->add_child ("Server"));
198 root->add_child("TMSIP")->add_child_text (_tms_ip);
199 root->add_child("TMSPath")->add_child_text (_tms_path);
200 root->add_child("TMSUser")->add_child_text (_tms_user);
201 root->add_child("TMSPassword")->add_child_text (_tms_password);
202 if (_sound_processor) {
203 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
206 root->add_child("Language")->add_child_text (_language.get());
209 _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
211 doc.write_to_file_formatted (file (false));
215 Config::default_directory_or (string a) const
217 if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
221 return _default_directory;