Assorted doxygen fixes; no functional changes.
[ardour.git] / libs / pbd / stateful.cc
1 /*
2     Copyright (C) 2000-2001 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     $Id: stateful.cc 629 2006-06-21 23:01:03Z paul $
19 */
20
21 #include <unistd.h>
22
23 #include "pbd/debug.h"
24 #include "pbd/stateful.h"
25 #include "pbd/property_list.h"
26 #include "pbd/properties.h"
27 #include "pbd/destructible.h"
28 #include "pbd/filesystem.h"
29 #include "pbd/xml++.h"
30 #include "pbd/error.h"
31
32 #include "i18n.h"
33
34 using namespace std;
35
36 namespace PBD {
37
38 int Stateful::current_state_version = 0;
39 int Stateful::loading_state_version = 0;
40
41 Stateful::Stateful ()
42         : _frozen (0)
43         , _properties (new OwnedPropertyList)
44 {
45         _extra_xml = 0;
46         _instant_xml = 0;
47 }
48
49 Stateful::~Stateful ()
50 {
51         delete _properties;
52
53         // Do not delete _extra_xml.  The use of add_child_nocopy() 
54         // means it needs to live on indefinately.
55
56         delete _instant_xml;
57 }
58
59 void
60 Stateful::add_extra_xml (XMLNode& node)
61 {
62         if (_extra_xml == 0) {
63                 _extra_xml = new XMLNode ("Extra");
64         }
65
66         _extra_xml->remove_nodes (node.name());
67         _extra_xml->add_child_nocopy (node);
68 }
69
70 XMLNode *
71 Stateful::extra_xml (const string& str)
72 {
73         if (_extra_xml == 0) {
74                 return 0;
75         }
76
77         const XMLNodeList& nlist = _extra_xml->children();
78         XMLNodeConstIterator i;
79
80         for (i = nlist.begin(); i != nlist.end(); ++i) {
81                 if ((*i)->name() == str) {
82                         return (*i);
83                 }
84         }
85
86         return 0;
87 }
88
89 void
90 Stateful::add_instant_xml (XMLNode& node, const sys::path& directory_path)
91 {
92         sys::create_directories (directory_path); // may throw
93
94         if (_instant_xml == 0) {
95                 _instant_xml = new XMLNode ("instant");
96         }
97
98         _instant_xml->remove_nodes_and_delete (node.name());
99         _instant_xml->add_child_copy (node);
100
101         sys::path instant_xml_path(directory_path);
102
103         instant_xml_path /= "instant.xml";
104         
105         XMLTree tree;
106         tree.set_filename(instant_xml_path.to_string());
107
108         /* Important: the destructor for an XMLTree deletes
109            all of its nodes, starting at _root. We therefore
110            cannot simply hand it our persistent _instant_xml 
111            node as its _root, because we will lose it whenever
112            the Tree goes out of scope.
113
114            So instead, copy the _instant_xml node (which does 
115            a deep copy), and hand that to the tree.
116         */
117
118         XMLNode* copy = new XMLNode (*_instant_xml);
119         tree.set_root (copy);
120
121         if (!tree.write()) {
122                 error << string_compose(_("Error: could not write %1"), instant_xml_path.to_string()) << endmsg;
123         }
124 }
125
126 XMLNode *
127 Stateful::instant_xml (const string& str, const sys::path& directory_path)
128 {
129         if (_instant_xml == 0) {
130
131                 sys::path instant_xml_path(directory_path);
132                 instant_xml_path /= "instant.xml";
133
134                 if (exists(instant_xml_path)) {
135                         XMLTree tree;
136                         if (tree.read(instant_xml_path.to_string())) {
137                                 _instant_xml = new XMLNode(*(tree.root()));
138                         } else {
139                                 warning << string_compose(_("Could not understand XML file %1"), instant_xml_path.to_string()) << endmsg;
140                                 return 0;
141                         }
142                 } else {
143                         return 0;
144                 }
145         }
146
147         const XMLNodeList& nlist = _instant_xml->children();
148         XMLNodeConstIterator i;
149
150         for (i = nlist.begin(); i != nlist.end(); ++i) {
151                 if ((*i)->name() == str) {
152                         return (*i);
153                 }
154         }
155
156         return 0;
157 }
158
159 /** Forget about any changes to this object's properties */
160 void
161 Stateful::clear_changes ()
162 {
163         for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
164                 i->second->clear_changes ();
165         }
166 }
167
168 PropertyList *
169 Stateful::get_changes_as_properties (Command* cmd) const
170 {
171         PropertyList* pl = new PropertyList;
172         
173         for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
174                 i->second->get_changes_as_properties (*pl, cmd);
175         }
176
177         return pl;
178 }
179
180 /** Set our property values from an XML node.
181  *  Derived types can call this from set_state() (or elsewhere)
182  *  to get basic property setting done.
183  *  @return IDs of properties that were changed.
184  */
185 PropertyChange
186 Stateful::set_values (XMLNode const & node)
187 {
188         PropertyChange c;
189         
190         for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
191                 if (i->second->set_value (node)) {
192                         c.add (i->first);
193                 }
194         }
195
196         post_set (c);
197
198         return c;
199 }
200
201 PropertyChange
202 Stateful::apply_changes (const PropertyList& property_list)
203 {
204         PropertyChange c;
205         PropertyList::const_iterator p;
206
207         DEBUG_TRACE (DEBUG::Stateful, string_compose ("Stateful %1 setting properties from list of %2\n", this, property_list.size()));
208
209         for (PropertyList::const_iterator pp = property_list.begin(); pp != property_list.end(); ++pp) {
210                 DEBUG_TRACE (DEBUG::Stateful, string_compose ("in plist: %1\n", pp->second->property_name()));
211         }
212         
213         for (PropertyList::const_iterator i = property_list.begin(); i != property_list.end(); ++i) {
214                 if ((p = _properties->find (i->first)) != _properties->end()) {
215
216                         DEBUG_TRACE (
217                                 DEBUG::Stateful,
218                                 string_compose ("actually setting property %1 using %2\n", p->second->property_name(), i->second->property_name())
219                                 );
220                         
221                         if (apply_changes (*i->second)) {
222                                 c.add (i->first);
223                         }
224                 } else {
225                         DEBUG_TRACE (DEBUG::Stateful, string_compose ("passed in property %1 not found in own property list\n",
226                                                                       i->second->property_name()));
227                 }
228         }
229         
230         post_set (c);
231
232         send_change (c);
233
234         return c;
235 }
236
237 /** Add property states to an XML node.
238  *  @param owner_state Node.
239  */
240 void
241 Stateful::add_properties (XMLNode& owner_state)
242 {
243         for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
244                 i->second->get_value (owner_state);
245         }
246 }
247
248 void
249 Stateful::add_property (PropertyBase& s)
250 {
251         _properties->add (s);
252 }
253
254 void
255 Stateful::send_change (const PropertyChange& what_changed)
256 {
257         if (what_changed.empty()) {
258                 return;
259         }
260
261         {
262                 Glib::Mutex::Lock lm (_lock);
263                 if (_frozen) {
264                         _pending_changed.add (what_changed);
265                         return;
266                 }
267         }
268
269         PropertyChanged (what_changed);
270 }
271
272 void
273 Stateful::suspend_property_changes ()
274 {
275         _frozen++;
276 }
277
278 void
279 Stateful::resume_property_changes ()
280 {
281         PropertyChange what_changed;
282
283         {
284                 Glib::Mutex::Lock lm (_lock);
285
286                 if (_frozen && --_frozen > 0) {
287                         return;
288                 }
289
290                 if (!_pending_changed.empty()) {
291                         what_changed = _pending_changed;
292                         _pending_changed.clear ();
293                 }
294         }
295
296         mid_thaw (what_changed);
297
298         send_change (what_changed);
299 }
300
301 bool
302 Stateful::changed() const  
303 {
304         for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
305                 if (i->second->changed()) {
306                         return true;
307                 }
308         }
309
310         return false;
311 }
312
313 bool
314 Stateful::apply_changes (const PropertyBase& prop)
315 {
316         OwnedPropertyList::iterator i = _properties->find (prop.property_id());
317         if (i == _properties->end()) {
318                 return false;
319         }
320
321         i->second->apply_changes (&prop);
322         return true;
323 }
324
325 PropertyList*
326 Stateful::property_factory (const XMLNode& history_node) const
327 {
328         PropertyList* prop_list = new PropertyList;
329
330         for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
331                 PropertyBase* prop = i->second->clone_from_xml (history_node);
332
333                 if (prop) {
334                         prop_list->add (prop);
335                 }
336         }
337
338         return prop_list;
339 }
340
341 void
342 Stateful::rdiff (vector<Command*>& cmds) const
343 {
344         for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
345                 i->second->rdiff (cmds);
346         }
347 }
348
349 void
350 Stateful::clear_owned_changes ()
351 {
352         for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
353                 i->second->clear_owned_changes ();
354         }
355 }
356   
357
358 } // namespace PBD