501673e968b9163ced152633105204e60a520a1a
[ardour.git] / libs / ardour / ardour / configuration_variable.h
1 /*
2     Copyright (C) 2000-2007 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 #ifndef __ardour_configuration_variable_h__
21 #define __ardour_configuration_variable_h__
22
23 #include <sstream>
24 #include <ostream>
25
26 #include <pbd/xml++.h>
27
28 namespace ARDOUR {
29
30 class ConfigVariableBase {
31   public:
32         enum Owner {
33                 Default = 0x1,
34                 System = 0x2,
35                 Config = 0x4,
36                 Session = 0x8,
37                 Interface = 0x10
38         };
39
40         ConfigVariableBase (std::string str) : _name (str), _owner (Default) {}
41         virtual ~ConfigVariableBase() {}
42
43         std::string name() const { return _name; }
44         Owner owner() const { return _owner; }
45
46         virtual void add_to_node (XMLNode& node) = 0;
47         virtual bool set_from_node (const XMLNode& node, Owner owner) = 0;
48
49         void show_stored_value (const std::string&);
50         static void set_show_stored_values (bool yn);
51
52   protected:
53         std::string _name;
54         Owner _owner;
55         static bool show_stores;
56
57         void notify ();
58         void miss ();
59 };
60
61 template<class T>
62 class ConfigVariable : public ConfigVariableBase
63 {
64   public:
65         ConfigVariable (std::string str) : ConfigVariableBase (str) {}
66         ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
67
68         virtual bool set (T val, Owner owner = ARDOUR::ConfigVariableBase::Config) {
69                 if (val == value) {
70                         miss ();
71                         return false;
72                 }
73                 value = val;
74                 _owner = (ConfigVariableBase::Owner)(_owner |owner);
75                 notify ();
76                 return true;
77         }
78
79         T get() const {
80                 return value;
81         }
82
83         void add_to_node (XMLNode& node) {
84                 std::stringstream ss;
85                 if (node.name() == "Canvas") {
86                         ss << std::hex;
87                         ss.fill(0);
88                         ss.width(8);
89                 }
90                 ss << value;
91                 show_stored_value (ss.str());
92                 XMLNode* child = new XMLNode ("Option");
93                 child->add_property ("name", _name);
94                 child->add_property ("value", ss.str());
95                 node.add_child_nocopy (*child);
96         }
97         
98         bool set_from_node (const XMLNode& node, Owner owner) {
99
100                 if (node.name() == "Config" || node.name() == "Canvas") {
101
102                         /* ardour.rc */
103
104                         const XMLProperty* prop;
105                         XMLNodeList nlist;
106                         XMLNodeConstIterator niter;
107                         XMLNode* child;
108                         
109                         nlist = node.children();
110                         
111                         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
112                                 
113                                 child = *niter;
114                                 
115                                 if (child->name() == "Option") {
116                                         if ((prop = child->property ("name")) != 0) {
117                                                 if (prop->value() == _name) {
118                                                         if ((prop = child->property ("value")) != 0) {
119                                                                 std::stringstream ss;
120                                                                 if (node.name() == "Canvas")
121                                                                         ss << std::hex;
122                                                                 ss << prop->value();
123                                                                 ss >> value;
124                                                                 _owner = (ConfigVariableBase::Owner)(_owner |owner);
125                                                                 return true;
126                                                         }
127                                                 }
128                                         }
129                                 }
130                         }
131                         
132                 } else if (node.name() == "Options") {
133
134                         /* session file */
135
136                         XMLNodeList olist;
137                         XMLNodeConstIterator oiter;
138                         XMLNode* option;
139                         const XMLProperty* opt_prop;
140                         
141                         olist = node.children();
142                         
143                         for (oiter = olist.begin(); oiter != olist.end(); ++oiter) {
144                                 
145                                 option = *oiter;
146                                 
147                                 if (option->name() == _name) {
148                                         if ((opt_prop = option->property ("val")) != 0) {
149                                                 std::stringstream ss;
150                                                 ss << opt_prop->value();
151                                                 ss >> value;
152                                                 _owner = (ConfigVariableBase::Owner)(_owner |owner);
153                                                 return true;
154                                         }
155                                 }
156                         }
157                 }
158
159                 return false;
160         }
161
162   protected:
163         virtual T get_for_save() { return value; }
164         T value;
165 };
166
167 template<class T>
168 class ConfigVariableWithMutation : public ConfigVariable<T>
169 {
170   public:
171         ConfigVariableWithMutation (std::string name, T val, T (*m)(T)) 
172                 : ConfigVariable<T> (name, val), mutator (m) {}
173
174         bool set (T val, ConfigVariableBase::Owner owner) {
175                 if (unmutated_value != val) {
176                         unmutated_value = val;
177                         return ConfigVariable<T>::set (mutator (val), owner);
178                 } 
179                 return false;
180         }
181
182   protected:
183         virtual T get_for_save() { return unmutated_value; }
184         T unmutated_value;
185         T (*mutator)(T);
186 };
187
188 }
189
190 #endif /* __ardour_configuration_variable_h__ */