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