fix thinko when dealing with non-MIDI tracks
[ardour.git] / libs / ardour / session_configuration.cc
1 /*
2  * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
3  * Copyright (C) 2009-2016 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2016 Tim Mayberry <mojofunk@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include <glib.h>
24 #include "pbd/gstdio_compat.h"
25 #include <glibmm/miscutils.h> /* for build_filename() */
26
27 #include "pbd/error.h"
28 #include "pbd/file_utils.h"
29 #include "pbd/pathexpand.h"
30
31 #include "ardour/types.h"
32 #include "ardour/types_convert.h"
33 #include "ardour/filesystem_paths.h"
34 #include "ardour/session_configuration.h"
35 #include "ardour/utils.h"
36 #include "pbd/i18n.h"
37
38 using namespace ARDOUR;
39 using namespace PBD;
40
41 SessionConfiguration::SessionConfiguration ()
42         :
43 /* construct variables */
44 #undef  CONFIG_VARIABLE
45 #undef  CONFIG_VARIABLE_SPECIAL
46 #define CONFIG_VARIABLE(Type,var,name,value) var (name,value),
47 #define CONFIG_VARIABLE_SPECIAL(Type,var,name,value,mutator) var (name,value,mutator),
48 #include "ardour/session_configuration_vars.h"
49 #undef  CONFIG_VARIABLE
50 #undef  CONFIG_VARIABLE_SPECIAL
51         foo (0)
52 {
53
54 }
55
56 XMLNode&
57 SessionConfiguration::get_state ()
58 {
59         XMLNode* root;
60
61         root = new XMLNode ("Ardour");
62         root->add_child_nocopy (get_variables ());
63
64         return *root;
65 }
66
67
68 XMLNode&
69 SessionConfiguration::get_variables ()
70 {
71         XMLNode* node;
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
115 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) \
116   if (var.set_from_node (node)) {                            \
117     ParameterChanged (name);                                 \
118   }
119
120 #include "ardour/session_configuration_vars.h"
121 #undef  CONFIG_VARIABLE
122 #undef  CONFIG_VARIABLE_SPECIAL
123
124 }
125 void
126 SessionConfiguration::map_parameters (boost::function<void (std::string)>& functor)
127 {
128 #undef  CONFIG_VARIABLE
129 #undef  CONFIG_VARIABLE_SPECIAL
130 #define CONFIG_VARIABLE(type,var,name,value)                 functor (name);
131 #define CONFIG_VARIABLE_SPECIAL(type,var,name,value,mutator) functor (name);
132 #include "ardour/session_configuration_vars.h"
133 #undef  CONFIG_VARIABLE
134 #undef  CONFIG_VARIABLE_SPECIAL
135 }
136
137
138 bool
139 SessionConfiguration::load_state ()
140 {
141         std::string rcfile;
142         GStatBuf statbuf;
143         if (find_file (ardour_config_search_path(), "session.rc", rcfile)) {
144                 if (g_stat (rcfile.c_str(), &statbuf)) {
145                         return false;
146                 }
147                 if (statbuf.st_size == 0) {
148                         return false;
149                 }
150                 XMLTree tree;
151                 if (!tree.read (rcfile.c_str())) {
152                         error << string_compose(_("%1: cannot part default session options \"%2\""), PROGRAM_NAME, rcfile) << endmsg;
153                         return false;
154                 }
155
156                 XMLNode& root (*tree.root());
157                 if (root.name() != X_("SessionDefaults")) {
158                         warning << _("Invalid session default XML Root.") << endmsg;
159                         return false;
160                 }
161
162                 XMLNode* node;
163                 if (((node = find_named_node (root, X_("Config"))) != 0)) {
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 }