Wouldn't it be nice if plugin presets had a description/comment?
[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 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
112         if (var.set_from_node (node)) {    \
113                 ParameterChanged (name);                     \
114         }
115
116 #include "ardour/session_configuration_vars.h"
117 #undef  CONFIG_VARIABLE
118 #undef  CONFIG_VARIABLE_SPECIAL
119
120 }
121 void
122 SessionConfiguration::map_parameters (boost::function<void (std::string)>& functor)
123 {
124 #undef  CONFIG_VARIABLE
125 #undef  CONFIG_VARIABLE_SPECIAL
126 #define CONFIG_VARIABLE(type,var,name,value)                 functor (name);
127 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) functor (name);
128 #include "ardour/session_configuration_vars.h"
129 #undef  CONFIG_VARIABLE
130 #undef  CONFIG_VARIABLE_SPECIAL
131 }
132
133
134 bool
135 SessionConfiguration::load_state ()
136 {
137         std::string rcfile;
138         GStatBuf statbuf;
139         if (find_file (ardour_config_search_path(), "session.rc", rcfile)) {
140                 if (g_stat (rcfile.c_str(), &statbuf)) {
141                         return false;
142                 }
143                 if (statbuf.st_size == 0) {
144                         return false;
145                 }
146                 XMLTree tree;
147                 if (!tree.read (rcfile.c_str())) {
148                         error << string_compose(_("%1: cannot part default session options \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
149                         return false;
150                 }
151
152                 XMLNode& root (*tree.root());
153                 if (root.name() != X_("SessionDefaults")) {
154                         warning << _("Invalid session default XML Root.") << endmsg;
155                         return false;
156                 }
157
158                 XMLNode* node;
159                 if (((node = find_named_node (root, X_("Config"))) != 0)) {
160                         set_variables(*node);
161                         info << _("Loaded custom session defaults.") << endmsg;
162                 } else {
163                         warning << _("Found no session defaults in XML file.") << endmsg;
164                         return false;
165                 }
166
167                 /* CUSTOM OVERRIDES */
168                 set_audio_search_path("");
169                 set_midi_search_path("");
170                 set_raid_path("");
171         }
172         return true;
173 }
174
175 bool
176 SessionConfiguration::save_state ()
177 {
178         const std::string rcfile = Glib::build_filename (user_config_directory(), "session.rc");
179         if (rcfile.empty()) {
180                 return false;
181         }
182
183         XMLTree tree;
184         XMLNode* root = new XMLNode(X_("SessionDefaults"));
185         root->add_child_nocopy (get_variables ());
186         tree.set_root (root);
187
188         if (!tree.write (rcfile.c_str())) {
189                 error << _("Could not save session options") << endmsg;
190                 return false;
191         }
192
193         return true;
194 }