Add some tests and hopefully clarify the DCPFrameRate class.
[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         , _reference_scaler (Scaler::from_id ("bicubic"))
44         , _tms_path (".")
45         , _sound_processor (SoundProcessor::from_id ("dolby_cp750"))
46 {
47         _allowed_dcp_frame_rates.push_back (24);
48         _allowed_dcp_frame_rates.push_back (25);
49         _allowed_dcp_frame_rates.push_back (30);
50         
51         ifstream f (file().c_str ());
52         string line;
53         while (getline (f, line)) {
54                 if (line.empty ()) {
55                         continue;
56                 }
57
58                 if (line[0] == '#') {
59                         continue;
60                 }
61
62                 size_t const s = line.find (' ');
63                 if (s == string::npos) {
64                         continue;
65                 }
66                 
67                 string const k = line.substr (0, s);
68                 string const v = line.substr (s + 1);
69
70                 if (k == "num_local_encoding_threads") {
71                         _num_local_encoding_threads = atoi (v.c_str ());
72                 } else if (k == "default_directory") {
73                         _default_directory = v;
74                 } else if (k == "server_port") {
75                         _server_port = atoi (v.c_str ());
76                 } else if (k == "reference_scaler") {
77                         _reference_scaler = Scaler::from_id (v);
78                 } else if (k == "reference_filter") {
79                         _reference_filters.push_back (Filter::from_id (v));
80                 } else if (k == "server") {
81                         _servers.push_back (ServerDescription::create_from_metadata (v));
82                 } else if (k == "tms_ip") {
83                         _tms_ip = v;
84                 } else if (k == "tms_path") {
85                         _tms_path = v;
86                 } else if (k == "tms_user") {
87                         _tms_user = v;
88                 } else if (k == "tms_password") {
89                         _tms_password = v;
90                 } else if (k == "sound_processor") {
91                         _sound_processor = SoundProcessor::from_id (v);
92                 }
93         }
94 }
95
96 /** @return Filename to write configuration to */
97 string
98 Config::file () const
99 {
100         boost::filesystem::path p;
101         p /= g_get_user_config_dir ();
102         p /= ".dvdomatic";
103         return p.string ();
104 }
105
106 /** @return Singleton instance */
107 Config *
108 Config::instance ()
109 {
110         if (_instance == 0) {
111                 _instance = new Config;
112         }
113
114         return _instance;
115 }
116
117 /** Write our configuration to disk */
118 void
119 Config::write () const
120 {
121         ofstream f (file().c_str ());
122         f << "num_local_encoding_threads " << _num_local_encoding_threads << "\n"
123           << "default_directory " << _default_directory << "\n"
124           << "server_port " << _server_port << "\n"
125           << "reference_scaler " << _reference_scaler->id () << "\n";
126
127         for (vector<Filter const *>::const_iterator i = _reference_filters.begin(); i != _reference_filters.end(); ++i) {
128                 f << "reference_filter " << (*i)->id () << "\n";
129         }
130         
131         for (vector<ServerDescription*>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
132                 f << "server " << (*i)->as_metadata () << "\n";
133         }
134
135         f << "tms_ip " << _tms_ip << "\n";
136         f << "tms_path " << _tms_path << "\n";
137         f << "tms_user " << _tms_user << "\n";
138         f << "tms_password " << _tms_password << "\n";
139         f << "sound_processor " << _sound_processor->id ();
140 }
141
142 string
143 Config::default_directory_or (string a) const
144 {
145         if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
146                 return a;
147         }
148
149         return _default_directory;
150 }