ardour
session_configuration.cc
Go to the documentation of this file.
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/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"
32 #include "ardour/utils.h"
33 #include "i18n.h"
34 
35 using namespace ARDOUR;
36 using namespace PBD;
37 
39  :
40 /* construct variables */
41 #undef CONFIG_VARIABLE
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
48  foo (0)
49 {
50 
51 }
52 
53 XMLNode&
55 {
56  XMLNode* root;
57  LocaleGuard lg (X_("C"));
58 
59  root = new XMLNode ("Ardour");
61 
62  return *root;
63 }
64 
65 
66 XMLNode&
68 {
69  XMLNode* node;
70  LocaleGuard lg (X_("C"));
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);
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
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 
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);
131 #undef CONFIG_VARIABLE
132 #undef CONFIG_VARIABLE_SPECIAL
133 }
134 
135 
136 bool
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 (X_("C"));
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
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 }
bool write() const
Definition: xml++.cc:147
const std::string & name() const
Definition: xml++.h:104
LIBPBD_API Transmitter error
LIBPBD_API Transmitter warning
const XMLNodeList & children(const std::string &str=std::string()) const
Definition: xml++.cc:329
std::ostream & endmsg(std::ostream &ostr)
Definition: transmitter.h:71
bool find_file(const Searchpath &search_path, const string &filename, std::string &result)
Definition: file_utils.cc:187
Definition: xml++.h:55
#define _(Text)
Definition: i18n.h:11
int set_state(XMLNode const &, int version)
LIBARDOUR_API std::string user_config_directory(int version=-1)
#define X_(Text)
Definition: i18n.h:13
#define CONFIG_VARIABLE_SPECIAL(Type, var, name, value, mutator)
#define CONFIG_VARIABLE(Type, var, name, value)
XMLNode * set_root(XMLNode *n)
Definition: xml++.h:63
XMLNode * root() const
Definition: xml++.h:62
Definition: amp.h:29
bool read()
Definition: xml++.h:71
LIBARDOUR_API XMLNode * find_named_node(const XMLNode &node, std::string name)
LIBARDOUR_API PBD::Searchpath ardour_config_search_path()
LIBPBD_API Transmitter info
const char * name
void add_child_nocopy(XMLNode &)
Definition: xml++.cc:357
Definition: xml++.h:95
Definition: debug.h:30
XMLNodeList::const_iterator XMLNodeConstIterator
Definition: xml++.h:49
void map_parameters(boost::function< void(std::string)> &)
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:208