Replace use of pbd/filesystem.h with Glib:: equivalents
[ardour.git] / libs / ardour / rc_configuration.cc
1 /*
2     Copyright (C) 1999-2006 Paul Davis
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 <unistd.h>
21 #include <cstdio> /* for snprintf, grrr */
22
23 #include <glib.h>
24 #include <glib/gstdio.h> /* for g_stat() */
25 #include <glibmm/miscutils.h>
26
27 #include "pbd/xml++.h"
28 #include "pbd/filesystem.h"
29 #include "pbd/file_utils.h"
30
31 #include "midi++/manager.h"
32
33 #include "ardour/control_protocol_manager.h"
34 #include "ardour/diskstream.h"
35 #include "ardour/filesystem_paths.h"
36 #include "ardour/rc_configuration.h"
37 #include "ardour/session_metadata.h"
38
39 #include "i18n.h"
40
41 using namespace ARDOUR;
42 using namespace std;
43 using namespace PBD;
44
45 /* this is global so that we do not have to indirect through an object pointer
46    to reference it.
47 */
48
49 namespace ARDOUR {
50     float speed_quietning = 0.251189; // -12dB reduction for ffwd or rewind
51 }
52
53 RCConfiguration::RCConfiguration ()
54         :
55 /* construct variables */
56 #undef  CONFIG_VARIABLE
57 #undef  CONFIG_VARIABLE_SPECIAL
58 #define CONFIG_VARIABLE(Type,var,name,value) var (name,value),
59 #define CONFIG_VARIABLE_SPECIAL(Type,var,name,value,mutator) var (name,value,mutator),
60 #include "ardour/rc_configuration_vars.h"
61 #undef  CONFIG_VARIABLE
62 #undef  CONFIG_VARIABLE_SPECIAL
63         _control_protocol_state (0)
64 {
65 }
66
67
68 RCConfiguration::~RCConfiguration ()
69 {
70         for (list<XMLNode*>::iterator i = _midi_port_states.begin(); i != _midi_port_states.end(); ++i) {
71                 delete *i;
72         }
73
74         delete _control_protocol_state;
75 }
76
77 int
78 RCConfiguration::load_state ()
79 {
80         std::string rcfile;
81         struct stat statbuf;
82
83         /* load system configuration first */
84
85         if (find_file_in_search_path (ardour_config_search_path(), "ardour_system.rc", rcfile)) {
86
87                 /* stupid XML Parser hates empty files */
88
89                 if (g_stat (rcfile.c_str(), &statbuf)) {
90                         return -1;
91                 }
92
93                 if (statbuf.st_size != 0) {
94                         info << string_compose (_("Loading system configuration file %1"), rcfile) << endl;
95
96                         XMLTree tree;
97                         if (!tree.read (rcfile.c_str())) {
98                                 error << string_compose(_("%1: cannot read system configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
99                                 return -1;
100                         }
101
102                         if (set_state (*tree.root(), Stateful::current_state_version)) {
103                                 error << string_compose(_("%1: system configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
104                                 return -1;
105                         }
106                 } else {
107                         error << string_compose (_("your system %1 configuration file is empty. This probably means that there as an error installing %1"), PROGRAM_NAME) << endmsg;
108                 }
109         }
110
111         /* now load configuration file for user */
112
113         if (find_file_in_search_path (ardour_config_search_path(), "ardour.rc", rcfile)) {
114
115                 /* stupid XML parser hates empty files */
116
117                 if (g_stat (rcfile.c_str(), &statbuf)) {
118                         return -1;
119                 }
120
121                 if (statbuf.st_size != 0) {
122                         info << string_compose (_("Loading user configuration file %1"), rcfile) << endl;
123
124                         XMLTree tree;
125                         if (!tree.read (rcfile)) {
126                                 error << string_compose(_("%1: cannot read configuration file \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
127                                 return -1;
128                         }
129
130                         if (set_state (*tree.root(), Stateful::current_state_version)) {
131                                 error << string_compose(_("%1: user configuration file \"%2\" not loaded successfully."), PROGRAM_NAME, rcfile) << endmsg;
132                                 return -1;
133                         }
134                 } else {
135                         warning << string_compose (_("your %1 configuration file is empty. This is not normal."), PROGRAM_NAME) << endmsg;
136                 }
137         }
138
139         return 0;
140 }
141
142 int
143 RCConfiguration::save_state()
144 {
145         const std::string rcfile = Glib::build_filename (user_config_directory(), "ardour.rc");
146
147         // this test seems bogus?
148         if (!rcfile.empty()) {
149                 XMLTree tree;
150                 tree.set_root (&get_state());
151                 if (!tree.write (rcfile.c_str())){
152                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
153                         return -1;
154                 }
155         }
156
157         return 0;
158 }
159
160 void
161 RCConfiguration::add_instant_xml(XMLNode& node)
162 {
163         Stateful::add_instant_xml (node, user_config_directory ());
164 }
165
166 XMLNode*
167 RCConfiguration::instant_xml(const string& node_name)
168 {
169         return Stateful::instant_xml (node_name, user_config_directory ());
170 }
171
172
173 XMLNode&
174 RCConfiguration::get_state ()
175 {
176         XMLNode* root;
177         LocaleGuard lg (X_("POSIX"));
178
179         root = new XMLNode("Ardour");
180
181         MIDI::Manager* mm = MIDI::Manager::instance();
182
183         if (mm) {
184                 boost::shared_ptr<const MIDI::Manager::PortList> ports = mm->get_midi_ports();
185
186                 for (MIDI::Manager::PortList::const_iterator i = ports->begin(); i != ports->end(); ++i) {
187                         root->add_child_nocopy((*i)->get_state());
188                 }
189         }
190
191         root->add_child_nocopy (get_variables ());
192
193         root->add_child_nocopy (SessionMetadata::Metadata()->get_user_state());
194
195         if (_extra_xml) {
196                 root->add_child_copy (*_extra_xml);
197         }
198
199         root->add_child_nocopy (ControlProtocolManager::instance().get_state());
200
201         return *root;
202 }
203
204 XMLNode&
205 RCConfiguration::get_variables ()
206 {
207         XMLNode* node;
208         LocaleGuard lg (X_("POSIX"));
209
210         node = new XMLNode ("Config");
211
212 #undef  CONFIG_VARIABLE
213 #undef  CONFIG_VARIABLE_SPECIAL
214 #define CONFIG_VARIABLE(type,var,Name,value) \
215         var.add_to_node (*node);
216 #define CONFIG_VARIABLE_SPECIAL(type,var,Name,value,mutator) \
217         var.add_to_node (*node);
218 #include "ardour/rc_configuration_vars.h"
219 #undef  CONFIG_VARIABLE
220 #undef  CONFIG_VARIABLE_SPECIAL
221
222         return *node;
223 }
224
225 int
226 RCConfiguration::set_state (const XMLNode& root, int version)
227 {
228         if (root.name() != "Ardour") {
229                 return -1;
230         }
231
232         XMLNodeList nlist = root.children();
233         XMLNodeConstIterator niter;
234         XMLNode *node;
235
236         for (list<XMLNode*>::iterator i = _midi_port_states.begin(); i != _midi_port_states.end(); ++i) {
237                 delete *i;
238         }
239
240         _midi_port_states.clear ();
241
242         Stateful::save_extra_xml (root);
243
244         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
245
246                 node = *niter;
247
248                 if (node->name() == "Config") {
249                         set_variables (*node);
250                 } else if (node->name() == "Metadata") {
251                         SessionMetadata::Metadata()->set_state (*node, version);
252                 } else if (node->name() == ControlProtocolManager::state_node_name) {
253                         _control_protocol_state = new XMLNode (*node);
254                 } else if (node->name() == MIDI::Port::state_node_name) {
255                         _midi_port_states.push_back (new XMLNode (*node));
256                 }
257         }
258
259         Diskstream::set_disk_io_chunk_frames (minimum_disk_io_bytes.get() / sizeof (Sample));
260
261         return 0;
262 }
263
264 void
265 RCConfiguration::set_variables (const XMLNode& node)
266 {
267 #undef  CONFIG_VARIABLE
268 #undef  CONFIG_VARIABLE_SPECIAL
269 #define CONFIG_VARIABLE(type,var,name,value) \
270         if (var.set_from_node (node)) { \
271                 ParameterChanged (name);                  \
272         }
273 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
274         if (var.set_from_node (node)) {    \
275                 ParameterChanged (name);                     \
276         }
277
278 #include "ardour/rc_configuration_vars.h"
279 #undef  CONFIG_VARIABLE
280 #undef  CONFIG_VARIABLE_SPECIAL
281
282 }
283 void
284 RCConfiguration::map_parameters (boost::function<void (std::string)>& functor)
285 {
286 #undef  CONFIG_VARIABLE
287 #undef  CONFIG_VARIABLE_SPECIAL
288 #define CONFIG_VARIABLE(type,var,name,value)                 functor (name);
289 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) functor (name);
290 #include "ardour/rc_configuration_vars.h"
291 #undef  CONFIG_VARIABLE
292 #undef  CONFIG_VARIABLE_SPECIAL
293 }