Merge branch 'selection_fixes' of https://github.com/nmains/ardour into cairocanvas
[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 <glib/gstdio.h> /* for g_stat() */
22 #include <glibmm/miscutils.h> /* for build_filename() */
23
24 #include "pbd/file_utils.h"
25 #include "pbd/pathexpand.h"
26
27 #include "ardour/types.h"
28 #include "ardour/filesystem_paths.h"
29 #include "ardour/session_configuration.h"
30 #include "i18n.h"
31
32 using namespace ARDOUR;
33 using namespace PBD;
34
35 SessionConfiguration::SessionConfiguration ()
36         :
37 /* construct variables */
38 #undef  CONFIG_VARIABLE
39 #undef  CONFIG_VARIABLE_SPECIAL
40 #define CONFIG_VARIABLE(Type,var,name,value) var (name,value),
41 #define CONFIG_VARIABLE_SPECIAL(Type,var,name,value,mutator) var (name,value,mutator),
42 #include "ardour/session_configuration_vars.h"
43 #undef  CONFIG_VARIABLE
44 #undef  CONFIG_VARIABLE_SPECIAL
45         foo (0)
46 {
47
48 }
49
50 XMLNode&
51 SessionConfiguration::get_state ()
52 {
53         XMLNode* root;
54         LocaleGuard lg (X_("POSIX"));
55
56         root = new XMLNode ("Ardour");
57         root->add_child_nocopy (get_variables ());
58
59         return *root;
60 }
61
62
63 XMLNode&
64 SessionConfiguration::get_variables ()
65 {
66         XMLNode* node;
67         LocaleGuard lg (X_("POSIX"));
68
69         node = new XMLNode ("Config");
70
71 #undef  CONFIG_VARIABLE
72 #undef  CONFIG_VARIABLE_SPECIAL
73 #define CONFIG_VARIABLE(type,var,Name,value) \
74         var.add_to_node (*node);
75 #define CONFIG_VARIABLE_SPECIAL(type,var,Name,value,mutator) \
76         var.add_to_node (*node);
77 #include "ardour/session_configuration_vars.h"
78 #undef  CONFIG_VARIABLE
79 #undef  CONFIG_VARIABLE_SPECIAL
80
81         return *node;
82 }
83
84
85 int
86 SessionConfiguration::set_state (XMLNode const& root, int /*version*/)
87 {
88         if (root.name() != "Ardour") {
89                 return -1;
90         }
91
92         for (XMLNodeConstIterator i = root.children().begin(); i != root.children().end(); ++i) {
93                 if ((*i)->name() == "Config") {
94                         set_variables (**i);
95                 }
96         }
97
98         return 0;
99 }
100
101 void
102 SessionConfiguration::set_variables (const XMLNode& node)
103 {
104 #undef  CONFIG_VARIABLE
105 #undef  CONFIG_VARIABLE_SPECIAL
106 #define CONFIG_VARIABLE(type,var,name,value) \
107         if (var.set_from_node (node)) { \
108                 ParameterChanged (name);                  \
109         }
110 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
111         if (var.set_from_node (node)) {    \
112                 ParameterChanged (name);                     \
113         }
114
115 #include "ardour/session_configuration_vars.h"
116 #undef  CONFIG_VARIABLE
117 #undef  CONFIG_VARIABLE_SPECIAL
118
119 }
120 void
121 SessionConfiguration::map_parameters (boost::function<void (std::string)>& functor)
122 {
123 #undef  CONFIG_VARIABLE
124 #undef  CONFIG_VARIABLE_SPECIAL
125 #define CONFIG_VARIABLE(type,var,name,value)                 functor (name);
126 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) functor (name);
127 #include "ardour/session_configuration_vars.h"
128 #undef  CONFIG_VARIABLE
129 #undef  CONFIG_VARIABLE_SPECIAL
130 }
131
132
133 bool
134 SessionConfiguration::load_state ()
135 {
136         std::string rcfile;
137         GStatBuf statbuf;
138         if (find_file (ardour_config_search_path(), "session.rc", rcfile)) {
139                 if (g_stat (rcfile.c_str(), &statbuf)) {
140                         return false;
141                 }
142                 if (statbuf.st_size == 0) {
143                         return false;
144                 }
145                 XMLTree tree;
146                 if (!tree.read (rcfile.c_str())) {
147                         error << string_compose(_("%1: cannot part default session options \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
148                         return false;
149                 }
150
151                 XMLNode& root (*tree.root());
152                 if (root.name() != X_("SessionDefaults")) {
153                         warning << _("Invalid session default XML Root.") << endmsg;
154                         return false;
155                 }
156
157                 XMLNode* node;
158                 if (((node = find_named_node (root, X_("Config"))) != 0)) {
159                         LocaleGuard lg (X_("POSIX"));
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 }