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