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