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 "ratio.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 std::max;
42 using boost::shared_ptr;
43 using boost::lexical_cast;
44 using boost::optional;
45
46 Config* Config::_instance = 0;
47
48 /** Construct default configuration */
49 Config::Config ()
50         : _num_local_encoding_threads (max (2U, boost::thread::hardware_concurrency()))
51         , _server_port (6192)
52         , _tms_path (N_("."))
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)
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
66 void
67 Config::read ()
68 {
69         if (!boost::filesystem::exists (file (false))) {
70                 read_old_metadata ();
71                 return;
72         }
73
74         cxml::File f (file (false), "Config");
75         optional<string> c;
76
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         
81         list<shared_ptr<cxml::Node> > servers = f.node_children ("Server");
82         for (list<shared_ptr<cxml::Node> >::iterator i = servers.begin(); i != servers.end(); ++i) {
83                 _servers.push_back (new ServerDescription (*i));
84         }
85
86         _tms_ip = f.string_child ("TMSIP");
87         _tms_path = f.string_child ("TMSPath");
88         _tms_user = f.string_child ("TMSUser");
89         _tms_password = f.string_child ("TMSPassword");
90
91         c = f.optional_string_child ("SoundProcessor");
92         if (c) {
93                 _sound_processor = SoundProcessor::from_id (c.get ());
94         }
95
96         _language = f.optional_string_child ("Language");
97
98         c = f.optional_string_child ("DefaultContainer");
99         if (c) {
100                 _default_container = Ratio::from_id (c.get ());
101         }
102
103         c = f.optional_string_child ("DefaultDCPContentType");
104         if (c) {
105                 _default_dcp_content_type = DCPContentType::from_dci_name (c.get ());
106         }
107
108         _dcp_metadata.issuer = f.optional_string_child ("DCPMetadataIssuer").get_value_or ("");
109         _dcp_metadata.creator = f.optional_string_child ("DCPMetadataCreator").get_value_or ("");
110
111         _default_dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
112         _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
113 }
114
115 void
116 Config::read_old_metadata ()
117 {
118         ifstream f (file(true).c_str ());
119         string line;
120         while (getline (f, line)) {
121                 if (line.empty ()) {
122                         continue;
123                 }
124
125                 if (line[0] == '#') {
126                         continue;
127                 }
128
129                 size_t const s = line.find (' ');
130                 if (s == string::npos) {
131                         continue;
132                 }
133                 
134                 string const k = line.substr (0, s);
135                 string const v = line.substr (s + 1);
136
137                 if (k == N_("num_local_encoding_threads")) {
138                         _num_local_encoding_threads = atoi (v.c_str ());
139                 } else if (k == N_("default_directory")) {
140                         _default_directory = v;
141                 } else if (k == N_("server_port")) {
142                         _server_port = atoi (v.c_str ());
143                 } else if (k == N_("server")) {
144                         _servers.push_back (ServerDescription::create_from_metadata (v));
145                 } else if (k == N_("tms_ip")) {
146                         _tms_ip = v;
147                 } else if (k == N_("tms_path")) {
148                         _tms_path = v;
149                 } else if (k == N_("tms_user")) {
150                         _tms_user = v;
151                 } else if (k == N_("tms_password")) {
152                         _tms_password = v;
153                 } else if (k == N_("sound_processor")) {
154                         _sound_processor = SoundProcessor::from_id (v);
155                 } else if (k == "language") {
156                         _language = v;
157                 } else if (k == "default_container") {
158                         _default_container = Ratio::from_id (v);
159                 } else if (k == "default_dcp_content_type") {
160                         _default_dcp_content_type = DCPContentType::from_dci_name (v);
161                 } else if (k == "dcp_metadata_issuer") {
162                         _dcp_metadata.issuer = v;
163                 } else if (k == "dcp_metadata_creator") {
164                         _dcp_metadata.creator = v;
165                 } else if (k == "dcp_metadata_issue_date") {
166                         _dcp_metadata.issue_date = v;
167                 }
168
169                 _default_dci_metadata.read_old_metadata (k, v);
170         }
171 }
172
173 /** @return Filename to write configuration to */
174 string
175 Config::file (bool old) const
176 {
177         boost::filesystem::path p;
178         p /= g_get_user_config_dir ();
179         boost::system::error_code ec;
180         boost::filesystem::create_directory (p, ec);
181         if (old) {
182                 p /= ".dvdomatic";
183         } else {
184                 p /= ".dcpomatic.xml";
185         }
186         return p.string ();
187 }
188
189 /** @return Singleton instance */
190 Config *
191 Config::instance ()
192 {
193         if (_instance == 0) {
194                 _instance = new Config;
195                 try {
196                         _instance->read ();
197                 } catch (...) {
198                         /* configuration load failed; never mind, just
199                            stick with the default.
200                         */
201                 }
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         
218         for (vector<ServerDescription*>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
219                 (*i)->as_xml (root->add_child ("Server"));
220         }
221
222         root->add_child("TMSIP")->add_child_text (_tms_ip);
223         root->add_child("TMSPath")->add_child_text (_tms_path);
224         root->add_child("TMSUser")->add_child_text (_tms_user);
225         root->add_child("TMSPassword")->add_child_text (_tms_password);
226         if (_sound_processor) {
227                 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
228         }
229         if (_language) {
230                 root->add_child("Language")->add_child_text (_language.get());
231         }
232         if (_default_container) {
233                 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
234         }
235         if (_default_dcp_content_type) {
236                 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->dci_name ());
237         }
238         root->add_child("DCPMetadataIssuer")->add_child_text (_dcp_metadata.issuer);
239         root->add_child("DCPMetadataCreator")->add_child_text (_dcp_metadata.creator);
240
241         _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
242
243         root->add_child("DefaultStillLength")->add_child_text (lexical_cast<string> (_default_still_length));
244
245         doc.write_to_file_formatted (file (false));
246 }
247
248 string
249 Config::default_directory_or (string a) const
250 {
251         if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
252                 return a;
253         }
254
255         return _default_directory;
256 }
257
258 void
259 Config::drop ()
260 {
261         delete _instance;
262         _instance = 0;
263 }