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