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