Clear up confusion with overloads of _frozen and frozen()
[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         : _properties (new OwnedPropertyList)
43         , _stateful_frozen (0)
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, bool add_if_missing)
72 {
73         XMLNode* node = 0;
74
75         if (_extra_xml) {
76                 node = _extra_xml->child (str.c_str());
77         }
78
79         if (!node && add_if_missing) {
80                 node = new XMLNode (str);
81                 add_extra_xml (*node);
82         } 
83
84         return node;
85 }
86
87 void
88 Stateful::save_extra_xml (const XMLNode& node)
89 {
90         /* Looks for the child node called "Extra" and makes _extra_xml 
91            point to a copy of it. Will delete any existing node pointed
92            to by _extra_xml if a new Extra node is found, but not
93            otherwise.
94         */
95         
96         const XMLNode* xtra = node.child ("Extra");
97
98         if (xtra) {
99                 delete _extra_xml;
100                 _extra_xml = new XMLNode (*xtra);
101         }
102 }
103
104 void
105 Stateful::add_instant_xml (XMLNode& node, const sys::path& directory_path)
106 {
107         sys::create_directories (directory_path); // may throw
108
109         if (_instant_xml == 0) {
110                 _instant_xml = new XMLNode ("instant");
111         }
112
113         _instant_xml->remove_nodes_and_delete (node.name());
114         _instant_xml->add_child_copy (node);
115
116         sys::path instant_xml_path(directory_path);
117
118         instant_xml_path /= "instant.xml";
119         
120         XMLTree tree;
121         tree.set_filename(instant_xml_path.to_string());
122
123         /* Important: the destructor for an XMLTree deletes
124            all of its nodes, starting at _root. We therefore
125            cannot simply hand it our persistent _instant_xml 
126            node as its _root, because we will lose it whenever
127            the Tree goes out of scope.
128
129            So instead, copy the _instant_xml node (which does 
130            a deep copy), and hand that to the tree.
131         */
132
133         XMLNode* copy = new XMLNode (*_instant_xml);
134         tree.set_root (copy);
135
136         if (!tree.write()) {
137                 error << string_compose(_("Error: could not write %1"), instant_xml_path.to_string()) << endmsg;
138         }
139 }
140
141 XMLNode *
142 Stateful::instant_xml (const string& str, const sys::path& directory_path)
143 {
144         if (_instant_xml == 0) {
145
146                 sys::path instant_xml_path(directory_path);
147                 instant_xml_path /= "instant.xml";
148
149                 if (exists(instant_xml_path)) {
150                         XMLTree tree;
151                         if (tree.read(instant_xml_path.to_string())) {
152                                 _instant_xml = new XMLNode(*(tree.root()));
153                         } else {
154                                 warning << string_compose(_("Could not understand XML file %1"), instant_xml_path.to_string()) << endmsg;
155                                 return 0;
156                         }
157                 } else {
158                         return 0;
159                 }
160         }
161
162         const XMLNodeList& nlist = _instant_xml->children();
163         XMLNodeConstIterator i;
164
165         for (i = nlist.begin(); i != nlist.end(); ++i) {
166                 if ((*i)->name() == str) {
167                         return (*i);
168                 }
169         }
170
171         return 0;
172 }
173
174 /** Forget about any changes to this object's properties */
175 void
176 Stateful::clear_changes ()
177 {
178         for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
179                 i->second->clear_changes ();
180         }
181 }
182
183 PropertyList *
184 Stateful::get_changes_as_properties (Command* cmd) const
185 {
186         PropertyList* pl = new PropertyList;
187         
188         for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
189                 i->second->get_changes_as_properties (*pl, cmd);
190         }
191
192         return pl;
193 }
194
195 /** Set our property values from an XML node.
196  *  Derived types can call this from set_state() (or elsewhere)
197  *  to get basic property setting done.
198  *  @return IDs of properties that were changed.
199  */
200 PropertyChange
201 Stateful::set_values (XMLNode const & node)
202 {
203         PropertyChange c;
204         
205         for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
206                 if (i->second->set_value (node)) {
207                         c.add (i->first);
208                 }
209         }
210
211         post_set (c);
212
213         return c;
214 }
215
216 PropertyChange
217 Stateful::apply_changes (const PropertyList& property_list)
218 {
219         PropertyChange c;
220         PropertyList::const_iterator p;
221
222         DEBUG_TRACE (DEBUG::Stateful, string_compose ("Stateful %1 setting properties from list of %2\n", this, property_list.size()));
223
224         for (PropertyList::const_iterator pp = property_list.begin(); pp != property_list.end(); ++pp) {
225                 DEBUG_TRACE (DEBUG::Stateful, string_compose ("in plist: %1\n", pp->second->property_name()));
226         }
227         
228         for (PropertyList::const_iterator i = property_list.begin(); i != property_list.end(); ++i) {
229                 if ((p = _properties->find (i->first)) != _properties->end()) {
230
231                         DEBUG_TRACE (
232                                 DEBUG::Stateful,
233                                 string_compose ("actually setting property %1 using %2\n", p->second->property_name(), i->second->property_name())
234                                 );
235                         
236                         if (apply_changes (*i->second)) {
237                                 c.add (i->first);
238                         }
239                 } else {
240                         DEBUG_TRACE (DEBUG::Stateful, string_compose ("passed in property %1 not found in own property list\n",
241                                                                       i->second->property_name()));
242                 }
243         }
244         
245         post_set (c);
246
247         send_change (c);
248
249         return c;
250 }
251
252 /** Add property states to an XML node.
253  *  @param owner_state Node.
254  */
255 void
256 Stateful::add_properties (XMLNode& owner_state)
257 {
258         for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
259                 i->second->get_value (owner_state);
260         }
261 }
262
263 void
264 Stateful::add_property (PropertyBase& s)
265 {
266         _properties->add (s);
267 }
268
269 void
270 Stateful::send_change (const PropertyChange& what_changed)
271 {
272         if (what_changed.empty()) {
273                 return;
274         }
275
276         {
277                 Glib::Mutex::Lock lm (_lock);
278                 if (property_changes_suspended ()) {
279                         _pending_changed.add (what_changed);
280                         return;
281                 }
282         }
283
284         PropertyChanged (what_changed);
285 }
286
287 void
288 Stateful::suspend_property_changes ()
289 {
290         g_atomic_int_add (&_stateful_frozen, 1);
291 }
292
293 void
294 Stateful::resume_property_changes ()
295 {
296         PropertyChange what_changed;
297
298         {
299                 Glib::Mutex::Lock lm (_lock);
300
301                 if (property_changes_suspended() && g_atomic_int_dec_and_test (&_stateful_frozen) == FALSE) {
302                         return;
303                 }
304
305                 if (!_pending_changed.empty()) {
306                         what_changed = _pending_changed;
307                         _pending_changed.clear ();
308                 }
309         }
310
311         mid_thaw (what_changed);
312
313         send_change (what_changed);
314 }
315
316 bool
317 Stateful::changed() const  
318 {
319         for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
320                 if (i->second->changed()) {
321                         return true;
322                 }
323         }
324
325         return false;
326 }
327
328 bool
329 Stateful::apply_changes (const PropertyBase& prop)
330 {
331         OwnedPropertyList::iterator i = _properties->find (prop.property_id());
332         if (i == _properties->end()) {
333                 return false;
334         }
335
336         i->second->apply_changes (&prop);
337         return true;
338 }
339
340 PropertyList*
341 Stateful::property_factory (const XMLNode& history_node) const
342 {
343         PropertyList* prop_list = new PropertyList;
344
345         for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
346                 PropertyBase* prop = i->second->clone_from_xml (history_node);
347
348                 if (prop) {
349                         prop_list->add (prop);
350                 }
351         }
352
353         return prop_list;
354 }
355
356 void
357 Stateful::rdiff (vector<Command*>& cmds) const
358 {
359         for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
360                 i->second->rdiff (cmds);
361         }
362 }
363
364 void
365 Stateful::clear_owned_changes ()
366 {
367         for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
368                 i->second->clear_owned_changes ();
369         }
370 }
371   
372 bool
373 Stateful::set_id (const XMLNode& node) 
374 {
375         const XMLProperty* prop;
376
377         if ((prop = node.property ("id")) != 0) {
378                 _id = prop->value ();
379                 return true;
380         } 
381
382         return false;
383 }
384
385 void
386 Stateful::reset_id ()
387 {
388         _id = ID ();
389 }
390
391 void
392 Stateful::set_id (const string& str)
393 {
394         _id = str;
395 }
396
397 } // namespace PBD