Merge master; fix crash on new film.
[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::optional;
41
42 Config* Config::_instance = 0;
43
44 /** Construct default configuration */
45 Config::Config ()
46         : _num_local_encoding_threads (2)
47         , _server_port (6192)
48         , _reference_scaler (Scaler::from_id (N_("bicubic")))
49         , _tms_path (N_("."))
50         , _sound_processor (SoundProcessor::from_id (N_("dolby_cp750")))
51 {
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);
58
59         if (!boost::filesystem::exists (file (false))) {
60                 read_old_metadata ();
61                 return;
62         }
63
64         cxml::File f (file (false), "Config");
65         optional<string> c;
66
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");
71         if (c) {
72                 _reference_scaler = Scaler::from_id (c.get ());
73         }
74
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 ()));
78         }
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         _default_dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
97 }
98
99 void
100 Config::read_old_metadata ()
101 {
102         ifstream f (file(true).c_str ());
103         string line;
104         while (getline (f, line)) {
105                 if (line.empty ()) {
106                         continue;
107                 }
108
109                 if (line[0] == '#') {
110                         continue;
111                 }
112
113                 size_t const s = line.find (' ');
114                 if (s == string::npos) {
115                         continue;
116                 }
117                 
118                 string const k = line.substr (0, s);
119                 string const v = line.substr (s + 1);
120
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")) {
134                         _tms_ip = v;
135                 } else if (k == N_("tms_path")) {
136                         _tms_path = v;
137                 } else if (k == N_("tms_user")) {
138                         _tms_user = v;
139                 } else if (k == N_("tms_password")) {
140                         _tms_password = v;
141                 } else if (k == N_("sound_processor")) {
142                         _sound_processor = SoundProcessor::from_id (v);
143                 } else if (k == "language") {
144                         _language = v;
145                 }
146
147                 _default_dci_metadata.read_old_metadata (k, v);
148         }
149 }
150
151 /** @return Filename to write configuration to */
152 string
153 Config::file (bool old) const
154 {
155         boost::filesystem::path p;
156         p /= g_get_user_config_dir ();
157         if (old) {
158                 p /= ".dvdomatic";
159         } else {
160                 p /= ".dcpomatic.xml";
161         }
162         return p.string ();
163 }
164
165 /** @return Singleton instance */
166 Config *
167 Config::instance ()
168 {
169         if (_instance == 0) {
170                 _instance = new Config;
171         }
172
173         return _instance;
174 }
175
176 /** Write our configuration to disk */
177 void
178 Config::write () const
179 {
180         xmlpp::Document doc;
181         xmlpp::Element* root = doc.create_root_node ("Config");
182
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 ());
188         }
189
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 ());
192         }
193         
194         for (vector<ServerDescription*>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
195                 (*i)->as_xml (root->add_child ("Server"));
196         }
197
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 ());
204         }
205         if (_language) {
206                 root->add_child("Language")->add_child_text (_language.get());
207         }
208
209         _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
210
211         doc.write_to_file_formatted (file (false));
212 }
213
214 string
215 Config::default_directory_or (string a) const
216 {
217         if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
218                 return a;
219         }
220
221         return _default_directory;
222 }
223
224 void
225 Config::drop ()
226 {
227         delete _instance;
228         _instance = 0;
229 }