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