Move things round a bit.
[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 "config.h"
24 #include "server.h"
25 #include "scaler.h"
26 #include "screen.h"
27 #include "filter.h"
28
29 using namespace std;
30 using namespace boost;
31
32 Config* Config::_instance = 0;
33
34 /** Construct default configuration */
35 Config::Config ()
36         : _num_local_encoding_threads (2)
37         , _server_port (6192)
38         , _colour_lut_index (0)
39         , _j2k_bandwidth (250000000)
40         , _reference_scaler (Scaler::from_id ("bicubic"))
41         , _tms_path (".")
42 {
43         ifstream f (file().c_str ());
44         string line;
45         while (getline (f, line)) {
46                 if (line.empty ()) {
47                         continue;
48                 }
49
50                 if (line[0] == '#') {
51                         continue;
52                 }
53
54                 size_t const s = line.find (' ');
55                 if (s == string::npos) {
56                         continue;
57                 }
58                 
59                 string const k = line.substr (0, s);
60                 string const v = line.substr (s + 1);
61
62                 if (k == "num_local_encoding_threads") {
63                         _num_local_encoding_threads = atoi (v.c_str ());
64                 } else if (k == "server_port") {
65                         _server_port = atoi (v.c_str ());
66                 } else if (k == "colour_lut_index") {
67                         _colour_lut_index = atoi (v.c_str ());
68                 } else if (k == "j2k_bandwidth") {
69                         _j2k_bandwidth = atoi (v.c_str ());
70                 } else if (k == "reference_scaler") {
71                         _reference_scaler = Scaler::from_id (v);
72                 } else if (k == "reference_filter") {
73                         _reference_filters.push_back (Filter::from_id (v));
74                 } else if (k == "server") {
75                         _servers.push_back (Server::create_from_metadata (v));
76                 } else if (k == "screen") {
77                         _screens.push_back (Screen::create_from_metadata (v));
78                 } else if (k == "tms_ip") {
79                         _tms_ip = v;
80                 } else if (k == "tms_path") {
81                         _tms_path = v;
82                 } else if (k == "tms_user") {
83                         _tms_user = v;
84                 } else if (k == "tms_password") {
85                         _tms_password = v;
86                 }
87         }
88
89         Changed ();
90 }
91
92 /** @return Filename to write configuration to */
93 string
94 Config::file () const
95 {
96         stringstream s;
97         s << getenv ("HOME") << "/.dvdomatic";
98         return s.str ();
99 }
100
101 /** @return Singleton instance */
102 Config *
103 Config::instance ()
104 {
105         if (_instance == 0) {
106                 _instance = new Config;
107         }
108
109         return _instance;
110 }
111
112 /** Write our configuration to disk */
113 void
114 Config::write () const
115 {
116         ofstream f (file().c_str ());
117         f << "num_local_encoding_threads " << _num_local_encoding_threads << "\n"
118           << "server_port " << _server_port << "\n"
119           << "colour_lut_index " << _colour_lut_index << "\n"
120           << "j2k_bandwidth " << _j2k_bandwidth << "\n"
121           << "reference_scaler " << _reference_scaler->id () << "\n";
122
123         for (vector<Filter const *>::const_iterator i = _reference_filters.begin(); i != _reference_filters.end(); ++i) {
124                 f << "reference_filter " << (*i)->id () << "\n";
125         }
126         
127         for (vector<Server*>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
128                 f << "server " << (*i)->as_metadata () << "\n";
129         }
130
131         for (vector<shared_ptr<Screen> >::const_iterator i = _screens.begin(); i != _screens.end(); ++i) {
132                 f << "screen " << (*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 }