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