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