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