2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
24 #include <boost/filesystem.hpp>
29 #include "sound_processor.h"
37 using boost::shared_ptr;
39 Config* Config::_instance = 0;
41 /** Construct default configuration */
43 : _num_local_encoding_threads (2)
45 , _reference_scaler (Scaler::from_id ("bicubic"))
47 , _sound_processor (SoundProcessor::from_id ("dolby_cp750"))
49 ifstream f (read_file().c_str ());
52 shared_ptr<Cinema> cinema;
53 shared_ptr<Screen> screen;
55 while (getline (f, line)) {
64 size_t const s = line.find (' ');
65 if (s == string::npos) {
69 string const k = line.substr (0, s);
70 string const v = line.substr (s + 1);
72 if (k == "num_local_encoding_threads") {
73 _num_local_encoding_threads = atoi (v.c_str ());
74 } else if (k == "default_directory") {
75 _default_directory = v;
76 } else if (k == "server_port") {
77 _server_port = atoi (v.c_str ());
78 } else if (k == "reference_scaler") {
79 _reference_scaler = Scaler::from_id (v);
80 } else if (k == "reference_filter") {
81 _reference_filters.push_back (Filter::from_id (v));
82 } else if (k == "server") {
83 _servers.push_back (ServerDescription::create_from_metadata (v));
84 } else if (k == "tms_ip") {
86 } else if (k == "tms_path") {
88 } else if (k == "tms_user") {
90 } else if (k == "tms_password") {
92 } else if (k == "sound_processor") {
93 _sound_processor = SoundProcessor::from_id (v);
94 } else if (k == "cinema") {
96 _cinemas.push_back (cinema);
98 cinema.reset (new Cinema (v, ""));
99 } else if (k == "cinema_email") {
102 } else if (k == "screen") {
105 cinema->screens.push_back (screen);
107 screen.reset (new Screen (v, shared_ptr<libdcp::Certificate> ()));
108 } else if (k == "screen_certificate") {
110 shared_ptr<Certificate> c (new libdcp::Certificate);
111 c->set_from_string (v);
116 _cinemas.push_back (cinema);
120 /** @return Filename to write configuration to */
122 Config::write_file () const
124 boost::filesystem::path p;
125 p /= g_get_user_config_dir ();
127 boost::filesystem::create_directory (p);
133 Config::read_file () const
135 if (boost::filesystem::exists (write_file ())) {
136 return write_file ();
139 boost::filesystem::path p;
140 p /= g_get_user_config_dir ();
146 Config::crypt_chain_directory () const
148 boost::filesystem::path p;
149 p /= g_get_user_config_dir ();
152 boost::filesystem::create_directories (p);
156 /** @return Singleton instance */
160 if (_instance == 0) {
161 _instance = new Config;
167 /** Write our configuration to disk */
169 Config::write () const
171 ofstream f (write_file().c_str ());
172 f << "num_local_encoding_threads " << _num_local_encoding_threads << "\n"
173 << "default_directory " << _default_directory << "\n"
174 << "server_port " << _server_port << "\n"
175 << "reference_scaler " << _reference_scaler->id () << "\n";
177 for (vector<Filter const *>::const_iterator i = _reference_filters.begin(); i != _reference_filters.end(); ++i) {
178 f << "reference_filter " << (*i)->id () << "\n";
181 for (vector<ServerDescription*>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
182 f << "server " << (*i)->as_metadata () << "\n";
185 f << "tms_ip " << _tms_ip << "\n";
186 f << "tms_path " << _tms_path << "\n";
187 f << "tms_user " << _tms_user << "\n";
188 f << "tms_password " << _tms_password << "\n";
189 f << "sound_processor " << _sound_processor->id () << "\n";
191 for (list<shared_ptr<Cinema> >::const_iterator i = _cinemas.begin(); i != _cinemas.end(); ++i) {
192 f << "cinema " << (*i)->name << "\n";
193 f << "cinema_email " << (*i)->email << "\n";
194 for (list<shared_ptr<Screen> >::iterator j = (*i)->screens.begin(); j != (*i)->screens.end(); ++j) {
195 f << "screen " << (*j)->name << "\n";
201 Config::default_directory_or (string a) const
203 if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
207 return _default_directory;