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