Merge master.
[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 "format.h"
31 #include "dcp_content_type.h"
32 #include "sound_processor.h"
33
34 #include "i18n.h"
35
36 using std::vector;
37 using std::ifstream;
38 using std::string;
39 using std::ofstream;
40 using std::list;
41 using boost::shared_ptr;
42 using boost::lexical_cast;
43 using boost::optional;
44
45 Config* Config::_instance = 0;
46
47 /** Construct default configuration */
48 Config::Config ()
49         : _num_local_encoding_threads (2)
50         , _server_port (6192)
51         , _reference_scaler (Scaler::from_id (N_("bicubic")))
52         , _tms_path (N_("."))
53         , _sound_processor (SoundProcessor::from_id (N_("dolby_cp750")))
54         , _default_still_length (10)
55         , _default_format (0)
56         , _default_dcp_content_type (0)
57 {
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);
64
65         if (!boost::filesystem::exists (file (false))) {
66                 read_old_metadata ();
67                 return;
68         }
69
70         cxml::File f (file (false), "Config");
71         optional<string> c;
72
73         _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
74         _default_directory = f.string_child ("DefaultDirectory");
75         _server_port = f.number_child<int> ("ServerPort");
76         c = f.optional_string_child ("ReferenceScaler");
77         if (c) {
78                 _reference_scaler = Scaler::from_id (c.get ());
79         }
80
81         list<shared_ptr<cxml::Node> > filters = f.node_children ("ReferenceFilter");
82         for (list<shared_ptr<cxml::Node> >::iterator i = filters.begin(); i != filters.end(); ++i) {
83                 _reference_filters.push_back (Filter::from_id ((*i)->content ()));
84         }
85         
86         list<shared_ptr<cxml::Node> > servers = f.node_children ("Server");
87         for (list<shared_ptr<cxml::Node> >::iterator i = servers.begin(); i != servers.end(); ++i) {
88                 _servers.push_back (new ServerDescription (*i));
89         }
90
91         _tms_ip = f.string_child ("TMSIP");
92         _tms_path = f.string_child ("TMSPath");
93         _tms_user = f.string_child ("TMSUser");
94         _tms_password = f.string_child ("TMSPassword");
95
96         c = f.optional_string_child ("SoundProcessor");
97         if (c) {
98                 _sound_processor = SoundProcessor::from_id (c.get ());
99         }
100
101         _language = f.optional_string_child ("Language");
102
103         c = f.optional_string_child ("DefaultFormat");
104         if (c) {
105                 _default_format = Format::from_id (c.get ());
106         }
107
108         c = f.optional_string_child ("DefaultDCPContentType");
109         if (c) {
110                 _default_dcp_content_type = DCPContentType::from_dci_name (c.get ());
111         }
112
113         _dcp_metadata.issuer = f.optional_string_child ("DCPMetadataIssuer").get_value_or ("");
114         _dcp_metadata.creator = f.optional_string_child ("DCPMetadataCreator").get_value_or ("");
115
116         _default_dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
117         _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
118 }
119
120 void
121 Config::read_old_metadata ()
122 {
123         ifstream f (file(true).c_str ());
124         string line;
125         while (getline (f, line)) {
126                 if (line.empty ()) {
127                         continue;
128                 }
129
130                 if (line[0] == '#') {
131                         continue;
132                 }
133
134                 size_t const s = line.find (' ');
135                 if (s == string::npos) {
136                         continue;
137                 }
138                 
139                 string const k = line.substr (0, s);
140                 string const v = line.substr (s + 1);
141
142                 if (k == N_("num_local_encoding_threads")) {
143                         _num_local_encoding_threads = atoi (v.c_str ());
144                 } else if (k == N_("default_directory")) {
145                         _default_directory = v;
146                 } else if (k == N_("server_port")) {
147                         _server_port = atoi (v.c_str ());
148                 } else if (k == N_("reference_scaler")) {
149                         _reference_scaler = Scaler::from_id (v);
150                 } else if (k == N_("reference_filter")) {
151                         _reference_filters.push_back (Filter::from_id (v));
152                 } else if (k == N_("server")) {
153                         _servers.push_back (ServerDescription::create_from_metadata (v));
154                 } else if (k == N_("tms_ip")) {
155                         _tms_ip = v;
156                 } else if (k == N_("tms_path")) {
157                         _tms_path = v;
158                 } else if (k == N_("tms_user")) {
159                         _tms_user = v;
160                 } else if (k == N_("tms_password")) {
161                         _tms_password = v;
162                 } else if (k == N_("sound_processor")) {
163                         _sound_processor = SoundProcessor::from_id (v);
164                 } else if (k == "language") {
165                         _language = v;
166                 } else if (k == "default_format") {
167                         _default_format = Format::from_id (v);
168                 } else if (k == "default_dcp_content_type") {
169                         _default_dcp_content_type = DCPContentType::from_dci_name (v);
170                 } else if (k == "dcp_metadata_issuer") {
171                         _dcp_metadata.issuer = v;
172                 } else if (k == "dcp_metadata_creator") {
173                         _dcp_metadata.creator = v;
174                 } else if (k == "dcp_metadata_issue_date") {
175                         _dcp_metadata.issue_date = v;
176                 }
177
178                 _default_dci_metadata.read_old_metadata (k, v);
179         }
180 }
181
182 /** @return Filename to write configuration to */
183 string
184 Config::file (bool old) const
185 {
186         boost::filesystem::path p;
187         p /= g_get_user_config_dir ();
188         if (old) {
189                 p /= ".dvdomatic";
190         } else {
191                 p /= ".dcpomatic.xml";
192         }
193         return p.string ();
194 }
195
196 /** @return Singleton instance */
197 Config *
198 Config::instance ()
199 {
200         if (_instance == 0) {
201                 _instance = new Config;
202         }
203
204         return _instance;
205 }
206
207 /** Write our configuration to disk */
208 void
209 Config::write () const
210 {
211         xmlpp::Document doc;
212         xmlpp::Element* root = doc.create_root_node ("Config");
213
214         root->add_child("NumLocalEncodingThreads")->add_child_text (lexical_cast<string> (_num_local_encoding_threads));
215         root->add_child("DefaultDirectory")->add_child_text (_default_directory);
216         root->add_child("ServerPort")->add_child_text (lexical_cast<string> (_server_port));
217         if (_reference_scaler) {
218                 root->add_child("ReferenceScaler")->add_child_text (_reference_scaler->id ());
219         }
220
221         for (vector<Filter const *>::const_iterator i = _reference_filters.begin(); i != _reference_filters.end(); ++i) {
222                 root->add_child("ReferenceFilter")->add_child_text ((*i)->id ());
223         }
224         
225         for (vector<ServerDescription*>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
226                 (*i)->as_xml (root->add_child ("Server"));
227         }
228
229         root->add_child("TMSIP")->add_child_text (_tms_ip);
230         root->add_child("TMSPath")->add_child_text (_tms_path);
231         root->add_child("TMSUser")->add_child_text (_tms_user);
232         root->add_child("TMSPassword")->add_child_text (_tms_password);
233         if (_sound_processor) {
234                 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
235         }
236         if (_language) {
237                 root->add_child("Language")->add_child_text (_language.get());
238         }
239         if (_default_format) {
240                 root->add_child("DefaultFormat")->add_child_text (_default_format->id ());
241         }
242         if (_default_dcp_content_type) {
243                 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->dci_name ());
244         }
245         root->add_child("DCPMetadataIssuer")->add_child_text (_dcp_metadata.issuer);
246         root->add_child("DCPMetadataCreator")->add_child_text (_dcp_metadata.creator);
247
248         _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
249
250         root->add_child("DefaultStillLength")->add_child_text (lexical_cast<string> (_default_still_length));
251
252         doc.write_to_file_formatted (file (false));
253 }
254
255 string
256 Config::default_directory_or (string a) const
257 {
258         if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
259                 return a;
260         }
261
262         return _default_directory;
263 }
264
265 void
266 Config::drop ()
267 {
268         delete _instance;
269         _instance = 0;
270 }