2 Copyright (C) 2000-2001 Paul Davis
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.
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.
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.
18 $Id: stateful.cc 629 2006-06-21 23:01:03Z paul $
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"
38 int Stateful::current_state_version = 0;
39 int Stateful::loading_state_version = 0;
43 , _no_property_changes (false)
44 , _properties (new OwnedPropertyList)
50 Stateful::~Stateful ()
54 // Do not delete _extra_xml. The use of add_child_nocopy()
55 // means it needs to live on indefinately.
61 Stateful::add_extra_xml (XMLNode& node)
63 if (_extra_xml == 0) {
64 _extra_xml = new XMLNode ("Extra");
67 _extra_xml->remove_nodes (node.name());
68 _extra_xml->add_child_nocopy (node);
72 Stateful::extra_xml (const string& str)
74 if (_extra_xml == 0) {
78 const XMLNodeList& nlist = _extra_xml->children();
79 XMLNodeConstIterator i;
81 for (i = nlist.begin(); i != nlist.end(); ++i) {
82 if ((*i)->name() == str) {
91 Stateful::add_instant_xml (XMLNode& node, const sys::path& directory_path)
93 sys::create_directories (directory_path); // may throw
95 if (_instant_xml == 0) {
96 _instant_xml = new XMLNode ("instant");
99 _instant_xml->remove_nodes_and_delete (node.name());
100 _instant_xml->add_child_copy (node);
102 sys::path instant_xml_path(directory_path);
104 instant_xml_path /= "instant.xml";
107 tree.set_filename(instant_xml_path.to_string());
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.
115 So instead, copy the _instant_xml node (which does
116 a deep copy), and hand that to the tree.
119 XMLNode* copy = new XMLNode (*_instant_xml);
120 tree.set_root (copy);
123 error << string_compose(_("Error: could not write %1"), instant_xml_path.to_string()) << endmsg;
128 Stateful::instant_xml (const string& str, const sys::path& directory_path)
130 if (_instant_xml == 0) {
132 sys::path instant_xml_path(directory_path);
133 instant_xml_path /= "instant.xml";
135 if (exists(instant_xml_path)) {
137 if (tree.read(instant_xml_path.to_string())) {
138 _instant_xml = new XMLNode(*(tree.root()));
140 warning << string_compose(_("Could not understand XML file %1"), instant_xml_path.to_string()) << endmsg;
148 const XMLNodeList& nlist = _instant_xml->children();
149 XMLNodeConstIterator i;
151 for (i = nlist.begin(); i != nlist.end(); ++i) {
152 if ((*i)->name() == str) {
160 /** Forget about any old state for this object */
162 Stateful::clear_history ()
164 for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
165 i->second->clear_history ();
170 Stateful::diff (PropertyList& before, PropertyList& after, Command* cmd) const
172 for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
173 i->second->diff (before, after, cmd);
177 /** Set our property values from an XML node.
178 * Derived types can call this from ::set_state() (or elsewhere)
179 * to get basic property setting done.
180 * @return IDs of properties that were changed.
183 Stateful::set_values (XMLNode const & node)
187 for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
188 if (i->second->set_value (node)) {
199 Stateful::apply_changes (const PropertyList& property_list)
202 PropertyList::const_iterator p;
204 DEBUG_TRACE (DEBUG::Stateful, string_compose ("Stateful %1 setting properties from list of %2\n", this, property_list.size()));
206 for (PropertyList::const_iterator pp = property_list.begin(); pp != property_list.end(); ++pp) {
207 DEBUG_TRACE (DEBUG::Stateful, string_compose ("in plist: %1\n", pp->second->property_name()));
210 for (PropertyList::const_iterator i = property_list.begin(); i != property_list.end(); ++i) {
211 if ((p = _properties->find (i->first)) != _properties->end()) {
212 DEBUG_TRACE (DEBUG::Stateful, string_compose ("actually setting property %1\n", p->second->property_name()));
213 if (apply_change (*i->second)) {
217 DEBUG_TRACE (DEBUG::Stateful, string_compose ("passed in property %1 not found in own property list\n",
218 i->second->property_name()));
229 /** Add property states to an XML node.
230 * @param owner_state Node.
233 Stateful::add_properties (XMLNode& owner_state)
235 for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
236 i->second->get_value (owner_state);
241 Stateful::add_property (PropertyBase& s)
243 _properties->add (s);
247 Stateful::send_change (const PropertyChange& what_changed)
249 if (what_changed.empty()) {
254 Glib::Mutex::Lock lm (_lock);
256 _pending_changed.add (what_changed);
261 PropertyChanged (what_changed);
265 Stateful::suspend_property_changes ()
271 Stateful::resume_property_changes ()
273 PropertyChange what_changed;
276 Glib::Mutex::Lock lm (_lock);
278 if (_frozen && --_frozen > 0) {
282 if (!_pending_changed.empty()) {
283 what_changed = _pending_changed;
284 _pending_changed.clear ();
288 mid_thaw (what_changed);
290 send_change (what_changed);
294 Stateful::changed() const
296 for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
297 if (i->second->changed()) {
306 Stateful::apply_change (const PropertyBase& prop)
308 OwnedPropertyList::iterator i = _properties->find (prop.property_id());
309 if (i == _properties->end()) {
313 i->second->apply_change (&prop);
318 Stateful::property_factory (const XMLNode& history_node) const
320 PropertyList* prop_list = new PropertyList;
322 for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
323 PropertyBase* prop = i->second->maybe_clone_self_if_found_in_history_node (history_node);
326 prop_list->add (prop);
334 Stateful::rdiff (vector<StatefulDiffCommand*>& cmds) const
336 for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
337 i->second->rdiff (cmds);
342 Stateful::clear_owned_history ()
344 for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
345 i->second->clear_owned_history ();