Merge branch '1.0' of /home/carl/git/dvdomatic into 1.0
[dcpomatic.git] / src / lib / config.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <sstream>
21 #include <cstdlib>
22 #include <fstream>
23 #include <glib.h>
24 #include <boost/filesystem.hpp>
25 #include <libcxml/cxml.h>
26 #include "config.h"
27 #include "server.h"
28 #include "scaler.h"
29 #include "filter.h"
30 #include "sound_processor.h"
31
32 #include "i18n.h"
33
34 using std::vector;
35 using std::ifstream;
36 using std::string;
37 using std::ofstream;
38 using std::list;
39 using boost::shared_ptr;
40 using boost::lexical_cast;
41 using boost::optional;
42
43 Config* Config::_instance = 0;
44
45 /** Construct default configuration */
46 Config::Config ()
47         : _num_local_encoding_threads (2)
48         , _server_port (6192)
49         , _reference_scaler (Scaler::from_id (N_("bicubic")))
50         , _tms_path (N_("."))
51         , _sound_processor (SoundProcessor::from_id (N_("dolby_cp750")))
52         , _default_still_length (10)
53 {
54         _allowed_dcp_frame_rates.push_back (24);
55         _allowed_dcp_frame_rates.push_back (25);
56         _allowed_dcp_frame_rates.push_back (30);
57         _allowed_dcp_frame_rates.push_back (48);
58         _allowed_dcp_frame_rates.push_back (50);
59         _allowed_dcp_frame_rates.push_back (60);
60
61         if (!boost::filesystem::exists (file (false))) {
62                 read_old_metadata ();
63                 return;
64         }
65
66         cxml::File f (file (false), "Config");
67         optional<string> c;
68
69         _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
70         _default_directory = f.string_child ("DefaultDirectory");
71         _server_port = f.number_child<int> ("ServerPort");
72         c = f.optional_string_child ("ReferenceScaler");
73         if (c) {
74                 _reference_scaler = Scaler::from_id (c.get ());
75         }
76
77         list<shared_ptr<cxml::Node> > filters = f.node_children ("ReferenceFilter");
78         for (list<shared_ptr<cxml::Node> >::iterator i = filters.begin(); i != filters.end(); ++i) {
79                 _reference_filters.push_back (Filter::from_id ((*i)->content ()));
80         }
81         
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));
85         }
86
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");
91
92         c = f.optional_string_child ("SoundProcessor");
93         if (c) {
94                 _sound_processor = SoundProcessor::from_id (c.get ());
95         }
96
97         _language = f.optional_string_child ("Language");
98         _default_dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
99         _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
100 }
101
102 void
103 Config::read_old_metadata ()
104 {
105         ifstream f (file(true).c_str ());
106         string line;
107         while (getline (f, line)) {
108                 if (line.empty ()) {
109                         continue;
110                 }
111
112                 if (line[0] == '#') {
113                         continue;
114                 }
115
116                 size_t const s = line.find (' ');
117                 if (s == string::npos) {
118                         continue;
119                 }
120                 
121                 string const k = line.substr (0, s);
122                 string const v = line.substr (s + 1);
123
124                 if (k == N_("num_local_encoding_threads")) {
125                         _num_local_encoding_threads = atoi (v.c_str ());
126                 } else if (k == N_("default_directory")) {
127                         _default_directory = v;
128                 } else if (k == N_("server_port")) {
129                         _server_port = atoi (v.c_str ());
130                 } else if (k == N_("reference_scaler")) {
131                         _reference_scaler = Scaler::from_id (v);
132                 } else if (k == N_("reference_filter")) {
133                         _reference_filters.push_back (Filter::from_id (v));
134                 } else if (k == N_("server")) {
135                         _servers.push_back (ServerDescription::create_from_metadata (v));
136                 } else if (k == N_("tms_ip")) {
137                         _tms_ip = v;
138                 } else if (k == N_("tms_path")) {
139                         _tms_path = v;
140                 } else if (k == N_("tms_user")) {
141                         _tms_user = v;
142                 } else if (k == N_("tms_password")) {
143                         _tms_password = v;
144                 } else if (k == N_("sound_processor")) {
145                         _sound_processor = SoundProcessor::from_id (v);
146                 } else if (k == "language") {
147                         _language = v;
148                 }
149
150                 _default_dci_metadata.read_old_metadata (k, v);
151         }
152 }
153
154 /** @return Filename to write configuration to */
155 string
156 Config::file (bool old) const
157 {
158         boost::filesystem::path p;
159         p /= g_get_user_config_dir ();
160         if (old) {
161                 p /= ".dvdomatic";
162         } else {
163                 p /= ".dcpomatic.xml";
164         }
165         return p.string ();
166 }
167
168 /** @return Singleton instance */
169 Config *
170 Config::instance ()
171 {
172         if (_instance == 0) {
173                 _instance = new Config;
174         }
175
176         return _instance;
177 }
178
179 /** Write our configuration to disk */
180 void
181 Config::write () const
182 {
183         xmlpp::Document doc;
184         xmlpp::Element* root = doc.create_root_node ("Config");
185
186         root->add_child("NumLocalEncodingThreads")->add_child_text (lexical_cast<string> (_num_local_encoding_threads));
187         root->add_child("DefaultDirectory")->add_child_text (_default_directory);
188         root->add_child("ServerPort")->add_child_text (lexical_cast<string> (_server_port));
189         if (_reference_scaler) {
190                 root->add_child("ReferenceScaler")->add_child_text (_reference_scaler->id ());
191         }
192
193         for (vector<Filter const *>::const_iterator i = _reference_filters.begin(); i != _reference_filters.end(); ++i) {
194                 root->add_child("ReferenceFilter")->add_child_text ((*i)->id ());
195         }
196         
197         for (vector<ServerDescription*>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
198                 (*i)->as_xml (root->add_child ("Server"));
199         }
200
201         root->add_child("TMSIP")->add_child_text (_tms_ip);
202         root->add_child("TMSPath")->add_child_text (_tms_path);
203         root->add_child("TMSUser")->add_child_text (_tms_user);
204         root->add_child("TMSPassword")->add_child_text (_tms_password);
205         if (_sound_processor) {
206                 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
207         }
208         if (_language) {
209                 root->add_child("Language")->add_child_text (_language.get());
210         }
211
212         _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
213
214         root->add_child("DefaultStillLength")->add_child_text (lexical_cast<string> (_default_still_length));
215
216         doc.write_to_file_formatted (file (false));
217 }
218
219 string
220 Config::default_directory_or (string a) const
221 {
222         if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
223                 return a;
224         }
225
226         return _default_directory;
227 }
228
229 void
230 Config::drop ()
231 {
232         delete _instance;
233         _instance = 0;
234 }