Configurable default format and content type (#133).
[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 "config.h"
26 #include "server.h"
27 #include "scaler.h"
28 #include "filter.h"
29 #include "format.h"
30 #include "dcp_content_type.h"
31 #include "sound_processor.h"
32
33 #include "i18n.h"
34
35 using std::vector;
36 using std::ifstream;
37 using std::string;
38 using std::ofstream;
39 using boost::shared_ptr;
40
41 Config* Config::_instance = 0;
42
43 /** Construct default configuration */
44 Config::Config ()
45         : _num_local_encoding_threads (2)
46         , _server_port (6192)
47         , _reference_scaler (Scaler::from_id (N_("bicubic")))
48         , _tms_path (N_("."))
49         , _sound_processor (SoundProcessor::from_id (N_("dolby_cp750")))
50         , _default_format (0)
51         , _default_dcp_content_type (0)
52 {
53         _allowed_dcp_frame_rates.push_back (24);
54         _allowed_dcp_frame_rates.push_back (25);
55         _allowed_dcp_frame_rates.push_back (30);
56         _allowed_dcp_frame_rates.push_back (48);
57         _allowed_dcp_frame_rates.push_back (50);
58         _allowed_dcp_frame_rates.push_back (60);
59         
60         ifstream f (file().c_str ());
61         string line;
62         while (getline (f, line)) {
63                 if (line.empty ()) {
64                         continue;
65                 }
66
67                 if (line[0] == '#') {
68                         continue;
69                 }
70
71                 size_t const s = line.find (' ');
72                 if (s == string::npos) {
73                         continue;
74                 }
75                 
76                 string const k = line.substr (0, s);
77                 string const v = line.substr (s + 1);
78
79                 if (k == N_("num_local_encoding_threads")) {
80                         _num_local_encoding_threads = atoi (v.c_str ());
81                 } else if (k == N_("default_directory")) {
82                         _default_directory = v;
83                 } else if (k == N_("server_port")) {
84                         _server_port = atoi (v.c_str ());
85                 } else if (k == N_("reference_scaler")) {
86                         _reference_scaler = Scaler::from_id (v);
87                 } else if (k == N_("reference_filter")) {
88                         _reference_filters.push_back (Filter::from_id (v));
89                 } else if (k == N_("server")) {
90                         _servers.push_back (ServerDescription::create_from_metadata (v));
91                 } else if (k == N_("tms_ip")) {
92                         _tms_ip = v;
93                 } else if (k == N_("tms_path")) {
94                         _tms_path = v;
95                 } else if (k == N_("tms_user")) {
96                         _tms_user = v;
97                 } else if (k == N_("tms_password")) {
98                         _tms_password = v;
99                 } else if (k == N_("sound_processor")) {
100                         _sound_processor = SoundProcessor::from_id (v);
101                 } else if (k == "language") {
102                         _language = v;
103                 } else if (k == "default_format") {
104                         _default_format = Format::from_metadata (v);
105                 } else if (k == "default_dcp_content_type") {
106                         _default_dcp_content_type = DCPContentType::from_dci_name (v);
107                 }
108
109                 _default_dci_metadata.read (k, v);
110         }
111 }
112
113 /** @return Filename to write configuration to */
114 string
115 Config::file () const
116 {
117         boost::filesystem::path p;
118         p /= g_get_user_config_dir ();
119         p /= N_(".dvdomatic");
120         return p.string ();
121 }
122
123 /** @return Singleton instance */
124 Config *
125 Config::instance ()
126 {
127         if (_instance == 0) {
128                 _instance = new Config;
129         }
130
131         return _instance;
132 }
133
134 /** Write our configuration to disk */
135 void
136 Config::write () const
137 {
138         ofstream f (file().c_str ());
139         f << N_("num_local_encoding_threads ") << _num_local_encoding_threads << N_("\n")
140           << N_("default_directory ") << _default_directory << N_("\n")
141           << N_("server_port ") << _server_port << N_("\n");
142
143         if (_reference_scaler) {
144                 f << "reference_scaler " << _reference_scaler->id () << "\n";
145         }
146
147         for (vector<Filter const *>::const_iterator i = _reference_filters.begin(); i != _reference_filters.end(); ++i) {
148                 f << N_("reference_filter ") << (*i)->id () << N_("\n");
149         }
150         
151         for (vector<ServerDescription*>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
152                 f << N_("server ") << (*i)->as_metadata () << N_("\n");
153         }
154
155         f << N_("tms_ip ") << _tms_ip << N_("\n");
156         f << N_("tms_path ") << _tms_path << N_("\n");
157         f << N_("tms_user ") << _tms_user << N_("\n");
158         f << N_("tms_password ") << _tms_password << N_("\n");
159         if (_sound_processor) {
160                 f << "sound_processor " << _sound_processor->id () << "\n";
161         }
162         if (_language) {
163                 f << "language " << _language.get() << "\n";
164         }
165         if (_default_format) {
166                 f << "default_format " << _default_format->as_metadata() << "\n";
167         }
168         if (_default_dcp_content_type) {
169                 f << "default_dcp_content_type " << _default_dcp_content_type->dci_name() << "\n";
170         }
171
172         _default_dci_metadata.write (f);
173 }
174
175 string
176 Config::default_directory_or (string a) const
177 {
178         if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
179                 return a;
180         }
181
182         return _default_directory;
183 }
184
185 void
186 Config::drop ()
187 {
188         delete _instance;
189         _instance = 0;
190 }