remove silly left-over debugging lines
[ardour.git] / gtk2_ardour / ui_config.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 <cstdlib>
22 #include <cstdio> /* for snprintf, grrr */
23
24 #include <glibmm/miscutils.h>
25
26 #include "pbd/failed_constructor.h"
27 #include "pbd/xml++.h"
28 #include "pbd/filesystem.h"
29 #include "pbd/file_utils.h"
30 #include "pbd/error.h"
31
32 #include "gtkmm2ext/rgb_macros.h"
33
34 #include "ardour/ardour.h"
35 #include "ardour/filesystem_paths.h"
36
37 #include "ui_config.h"
38
39 #include "i18n.h"
40
41 using namespace std;
42 using namespace PBD;
43 using namespace ARDOUR;
44
45 UIConfiguration::UIConfiguration ()
46         :
47 #undef  UI_CONFIG_VARIABLE
48 #undef  CANVAS_VARIABLE
49 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
50 #define CANVAS_VARIABLE(var,name) var (name),
51 #include "ui_config_vars.h"
52 #include "canvas_vars.h"
53 #undef  UI_CONFIG_VARIABLE
54 #undef  CANVAS_VARIABLE
55         _dirty (false)
56 {
57         load_state();
58 }
59
60 UIConfiguration::~UIConfiguration ()
61 {
62 }
63
64 int
65 UIConfiguration::load_defaults ()
66 {
67         int found = 0;
68
69         sys::path default_ui_rc_file;
70         std::string rcfile;
71
72         if (getenv ("ARDOUR_SAE")) {
73                 rcfile = "ardour3_ui_sae.conf";
74         } else {
75                 rcfile = "ardour3_ui_default.conf";
76         }
77
78         if (find_file_in_search_path (ardour_search_path() + system_config_search_path(),
79                                       rcfile, default_ui_rc_file) ) {
80                 XMLTree tree;
81                 found = 1;
82
83                 string rcfile = default_ui_rc_file.to_string();
84
85                 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endl;
86
87                 if (!tree.read (rcfile.c_str())) {
88                         error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
89                         return -1;
90                 }
91
92                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
93                         error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
94                         return -1;
95                 }
96
97                 _dirty = false;
98         }
99
100         return found;
101 }
102
103 int
104 UIConfiguration::load_state ()
105 {
106         bool found = false;
107
108         sys::path default_ui_rc_file;
109
110         if ( find_file_in_search_path (ardour_search_path() + system_config_search_path(),
111                         "ardour3_ui_default.conf", default_ui_rc_file) ) {
112                 XMLTree tree;
113                 found = true;
114
115                 string rcfile = default_ui_rc_file.to_string();
116
117                 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endl;
118
119                 if (!tree.read (rcfile.c_str())) {
120                         error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
121                         return -1;
122                 }
123
124                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
125                         error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
126                         return -1;
127                 }
128         }
129
130         sys::path user_ui_rc_file;
131
132         if (find_file_in_search_path (ardour_search_path() + user_config_directory(),
133                         "ardour3_ui.conf", user_ui_rc_file)) {
134                 XMLTree tree;
135                 found = true;
136
137                 string rcfile = user_ui_rc_file.to_string();
138
139                 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
140
141                 if (!tree.read (rcfile)) {
142                         error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
143                         return -1;
144                 }
145
146                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
147                         error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
148                         return -1;
149                 }
150
151                 _dirty = false;
152         }
153
154         if (!found)
155                 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
156
157         pack_canvasvars();
158
159         return 0;
160 }
161
162 int
163 UIConfiguration::save_state()
164 {
165         XMLTree tree;
166
167         try {
168                 sys::create_directories (user_config_directory ());
169         }
170         catch (const sys::filesystem_error& ex) {
171                 error << "Could not create user configuration directory" << endmsg;
172                 return -1;
173         }
174
175         sys::path rcfile_path(user_config_directory());
176
177         rcfile_path /= "ardour3_ui.conf";
178         const string rcfile = rcfile_path.to_string();
179
180         // this test seems bogus?
181         if (rcfile.length()) {
182                 tree.set_root (&get_state());
183                 if (!tree.write (rcfile.c_str())){
184                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
185                         return -1;
186                 }
187         }
188
189         _dirty = false;
190
191         return 0;
192 }
193
194 XMLNode&
195 UIConfiguration::get_state ()
196 {
197         XMLNode* root;
198         LocaleGuard lg (X_("POSIX"));
199
200         root = new XMLNode("Ardour");
201
202         root->add_child_nocopy (get_variables ("UI"));
203         root->add_child_nocopy (get_variables ("Canvas"));
204
205         if (_extra_xml) {
206                 root->add_child_copy (*_extra_xml);
207         }
208
209         return *root;
210 }
211
212 XMLNode&
213 UIConfiguration::get_variables (std::string which_node)
214 {
215         XMLNode* node;
216         LocaleGuard lg (X_("POSIX"));
217
218         node = new XMLNode(which_node);
219
220 #undef  UI_CONFIG_VARIABLE
221 #undef  CANVAS_VARIABLE
222 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
223 #define CANVAS_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
224 #include "ui_config_vars.h"
225 #include "canvas_vars.h"
226 #undef  UI_CONFIG_VARIABLE
227 #undef  CANVAS_VARIABLE
228
229         return *node;
230 }
231
232 int
233 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
234 {
235         if (root.name() != "Ardour") {
236                 return -1;
237         }
238
239         Stateful::save_extra_xml (root);
240
241         XMLNodeList nlist = root.children();
242         XMLNodeConstIterator niter;
243         XMLNode *node;
244
245         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
246
247                 node = *niter;
248
249                 if (node->name() == "Canvas" ||  node->name() == "UI") {
250                         set_variables (*node);
251
252                 }
253         }
254
255         return 0;
256 }
257
258 void
259 UIConfiguration::set_variables (const XMLNode& node)
260 {
261 #undef  UI_CONFIG_VARIABLE
262 #undef  CANVAS_VARIABLE
263 #define UI_CONFIG_VARIABLE(Type,var,name,val) \
264          if (var.set_from_node (node)) { \
265                  ParameterChanged (name); \
266                  }
267 #define CANVAS_VARIABLE(var,name) \
268          if (var.set_from_node (node)) { \
269                  ParameterChanged (name); \
270                  }
271 #include "ui_config_vars.h"
272 #include "canvas_vars.h"
273 #undef  UI_CONFIG_VARIABLE
274 #undef  CANVAS_VARIABLE
275 }
276
277 void
278 UIConfiguration::pack_canvasvars ()
279 {
280 #undef  CANVAS_VARIABLE
281 #define CANVAS_VARIABLE(var,name) canvas_colors.insert (std::pair<std::string,UIConfigVariable<uint32_t>* >(name,&var));
282 #include "canvas_vars.h"
283 #undef  CANVAS_VARIABLE
284 }
285
286 uint32_t
287 UIConfiguration::color_by_name (const std::string& name)
288 {
289         map<std::string,UIConfigVariable<uint32_t>* >::iterator i = canvas_colors.find (name);
290
291         if (i != canvas_colors.end()) {
292                 return i->second->get();
293         }
294
295         // cerr << string_compose (_("Color %1 not found"), name) << endl;
296         return RGBA_TO_UINT (random()%256,random()%256,random()%256,0xff);
297 }
298
299 void
300 UIConfiguration::set_dirty ()
301 {
302         _dirty = true;
303 }
304
305 bool
306 UIConfiguration::dirty () const
307 {
308         return _dirty;
309 }