Use PBD string conversion functions in PBD::ConfigurationVariable
[ardour.git] / libs / ardour / session_configuration.cc
1 /*
2     Copyright (C) 2009 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 <glib.h>
21 #include "pbd/gstdio_compat.h"
22 #include <glibmm/miscutils.h> /* for build_filename() */
23
24 #include "pbd/error.h"
25 #include "pbd/file_utils.h"
26 #include "pbd/locale_guard.h"
27 #include "pbd/pathexpand.h"
28
29 #include "ardour/types.h"
30 #include "ardour/types_convert.h"
31 #include "ardour/filesystem_paths.h"
32 #include "ardour/session_configuration.h"
33 #include "ardour/utils.h"
34 #include "pbd/i18n.h"
35
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 SessionConfiguration::SessionConfiguration ()
40         :
41 /* construct variables */
42 #undef  CONFIG_VARIABLE
43 #undef  CONFIG_VARIABLE_SPECIAL
44 #define CONFIG_VARIABLE(Type,var,name,value) var (name,value),
45 #define CONFIG_VARIABLE_SPECIAL(Type,var,name,value,mutator) var (name,value,mutator),
46 #include "ardour/session_configuration_vars.h"
47 #undef  CONFIG_VARIABLE
48 #undef  CONFIG_VARIABLE_SPECIAL
49         foo (0)
50 {
51
52 }
53
54 XMLNode&
55 SessionConfiguration::get_state ()
56 {
57         XMLNode* root;
58         LocaleGuard lg;
59
60         root = new XMLNode ("Ardour");
61         root->add_child_nocopy (get_variables ());
62
63         return *root;
64 }
65
66
67 XMLNode&
68 SessionConfiguration::get_variables ()
69 {
70         XMLNode* node;
71         LocaleGuard lg;
72
73         node = new XMLNode ("Config");
74
75 #undef  CONFIG_VARIABLE
76 #undef  CONFIG_VARIABLE_SPECIAL
77 #define CONFIG_VARIABLE(type,var,Name,value) \
78         var.add_to_node (*node);
79 #define CONFIG_VARIABLE_SPECIAL(type,var,Name,value,mutator) \
80         var.add_to_node (*node);
81 #include "ardour/session_configuration_vars.h"
82 #undef  CONFIG_VARIABLE
83 #undef  CONFIG_VARIABLE_SPECIAL
84
85         return *node;
86 }
87
88
89 int
90 SessionConfiguration::set_state (XMLNode const& root, int /*version*/)
91 {
92         if (root.name() != "Ardour") {
93                 return -1;
94         }
95
96         for (XMLNodeConstIterator i = root.children().begin(); i != root.children().end(); ++i) {
97                 if ((*i)->name() == "Config") {
98                         set_variables (**i);
99                 }
100         }
101
102         return 0;
103 }
104
105 void
106 SessionConfiguration::set_variables (const XMLNode& node)
107 {
108 #undef  CONFIG_VARIABLE
109 #undef  CONFIG_VARIABLE_SPECIAL
110 #define CONFIG_VARIABLE(type,var,name,value) \
111         if (var.set_from_node (node)) { \
112                 ParameterChanged (name);                  \
113         }
114 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
115         if (var.set_from_node (node)) {    \
116                 ParameterChanged (name);                     \
117         }
118
119 #include "ardour/session_configuration_vars.h"
120 #undef  CONFIG_VARIABLE
121 #undef  CONFIG_VARIABLE_SPECIAL
122
123 }
124 void
125 SessionConfiguration::map_parameters (boost::function<void (std::string)>& functor)
126 {
127 #undef  CONFIG_VARIABLE
128 #undef  CONFIG_VARIABLE_SPECIAL
129 #define CONFIG_VARIABLE(type,var,name,value)                 functor (name);
130 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) functor (name);
131 #include "ardour/session_configuration_vars.h"
132 #undef  CONFIG_VARIABLE
133 #undef  CONFIG_VARIABLE_SPECIAL
134 }
135
136
137 bool
138 SessionConfiguration::load_state ()
139 {
140         std::string rcfile;
141         GStatBuf statbuf;
142         if (find_file (ardour_config_search_path(), "session.rc", rcfile)) {
143                 if (g_stat (rcfile.c_str(), &statbuf)) {
144                         return false;
145                 }
146                 if (statbuf.st_size == 0) {
147                         return false;
148                 }
149                 XMLTree tree;
150                 if (!tree.read (rcfile.c_str())) {
151                         error << string_compose(_("%1: cannot part default session options \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
152                         return false;
153                 }
154
155                 XMLNode& root (*tree.root());
156                 if (root.name() != X_("SessionDefaults")) {
157                         warning << _("Invalid session default XML Root.") << endmsg;
158                         return false;
159                 }
160
161                 XMLNode* node;
162                 if (((node = find_named_node (root, X_("Config"))) != 0)) {
163                         LocaleGuard lg;
164                         set_variables(*node);
165                         info << _("Loaded custom session defaults.") << endmsg;
166                 } else {
167                         warning << _("Found no session defaults in XML file.") << endmsg;
168                         return false;
169                 }
170
171                 /* CUSTOM OVERRIDES */
172                 set_audio_search_path("");
173                 set_midi_search_path("");
174                 set_raid_path("");
175         }
176         return true;
177 }
178
179 bool
180 SessionConfiguration::save_state ()
181 {
182         const std::string rcfile = Glib::build_filename (user_config_directory(), "session.rc");
183         if (rcfile.empty()) {
184                 return false;
185         }
186
187         XMLTree tree;
188         XMLNode* root = new XMLNode(X_("SessionDefaults"));
189         root->add_child_nocopy (get_variables ());
190         tree.set_root (root);
191
192         if (!tree.write (rcfile.c_str())) {
193                 error << _("Could not save session options") << endmsg;
194                 return false;
195         }
196
197         return true;
198 }