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;
54 while (getline (f, line)) {
63 size_t const s = line.find (' ');
64 if (s == string::npos) {
68 string const k = line.substr (0, s);
69 string const v = line.substr (s + 1);
71 if (k == "num_local_encoding_threads") {
72 _num_local_encoding_threads = atoi (v.c_str ());
73 } else if (k == "default_directory") {
74 _default_directory = v;
75 } else if (k == "server_port") {
76 _server_port = atoi (v.c_str ());
77 } else if (k == "reference_scaler") {
78 _reference_scaler = Scaler::from_id (v);
79 } else if (k == "reference_filter") {
80 _reference_filters.push_back (Filter::from_id (v));
81 } else if (k == "server") {
82 _servers.push_back (ServerDescription::create_from_metadata (v));
83 } else if (k == "tms_ip") {
85 } else if (k == "tms_path") {
87 } else if (k == "tms_user") {
89 } else if (k == "tms_password") {
91 } else if (k == "sound_processor") {
92 _sound_processor = SoundProcessor::from_id (v);
93 } else if (k == "cinema") {
95 _cinemas.push_back (cinema);
97 cinema.reset (new Cinema (v, ""));
98 } else if (k == "cinema_email") {
101 } else if (k == "screen") {
102 shared_ptr<Screen> s (new Screen (v));
103 cinema->screens.push_back (s);
108 _cinemas.push_back (cinema);
112 /** @return Filename to write configuration to */
114 Config::write_file () const
116 boost::filesystem::path p;
117 p /= g_get_user_config_dir ();
119 boost::filesystem::create_directory (p);
125 Config::read_file () const
127 if (boost::filesystem::exists (write_file ())) {
128 return write_file ();
131 boost::filesystem::path p;
132 p /= g_get_user_config_dir ();
138 Config::crypt_chain_directory () const
140 boost::filesystem::path p;
141 p /= g_get_user_config_dir ();
144 boost::filesystem::create_directories (p);
148 /** @return Singleton instance */
152 if (_instance == 0) {
153 _instance = new Config;
159 /** Write our configuration to disk */
161 Config::write () const
163 ofstream f (write_file().c_str ());
164 f << "num_local_encoding_threads " << _num_local_encoding_threads << "\n"
165 << "default_directory " << _default_directory << "\n"
166 << "server_port " << _server_port << "\n"
167 << "reference_scaler " << _reference_scaler->id () << "\n";
169 for (vector<Filter const *>::const_iterator i = _reference_filters.begin(); i != _reference_filters.end(); ++i) {
170 f << "reference_filter " << (*i)->id () << "\n";
173 for (vector<ServerDescription*>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
174 f << "server " << (*i)->as_metadata () << "\n";
177 f << "tms_ip " << _tms_ip << "\n";
178 f << "tms_path " << _tms_path << "\n";
179 f << "tms_user " << _tms_user << "\n";
180 f << "tms_password " << _tms_password << "\n";
181 f << "sound_processor " << _sound_processor->id () << "\n";
183 for (list<shared_ptr<Cinema> >::const_iterator i = _cinemas.begin(); i != _cinemas.end(); ++i) {
184 f << "cinema " << (*i)->name << "\n";
185 f << "cinema_email " << (*i)->email << "\n";
186 for (list<shared_ptr<Screen> >::iterator j = (*i)->screens.begin(); j != (*i)->screens.end(); ++j) {
187 f << "screen " << (*j)->name << "\n";
193 Config::default_directory_or (string a) const
195 if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
199 return _default_directory;