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 $
22 #include <io.h> // Microsoft's nearest equivalent to <unistd.h>
27 #include <glibmm/fileutils.h>
28 #include <glibmm/miscutils.h>
30 #include "pbd/debug.h"
31 #include "pbd/stateful.h"
32 #include "pbd/types_convert.h"
33 #include "pbd/property_list.h"
34 #include "pbd/properties.h"
35 #include "pbd/destructible.h"
36 #include "pbd/xml++.h"
37 #include "pbd/error.h"
45 int Stateful::current_state_version = 0;
46 int Stateful::loading_state_version = 0;
48 Glib::Threads::Private<bool> Stateful::_regenerate_xml_or_string_ids;
53 , _properties (new OwnedPropertyList)
54 , _stateful_frozen (0)
58 Stateful::~Stateful ()
62 // Do not delete _extra_xml. The use of add_child_nocopy()
63 // means it needs to live on indefinately.
69 Stateful::add_extra_xml (XMLNode& node)
71 if (_extra_xml == 0) {
72 _extra_xml = new XMLNode ("Extra");
75 _extra_xml->remove_nodes_and_delete (node.name());
76 _extra_xml->add_child_nocopy (node);
80 Stateful::extra_xml (const string& str, bool add_if_missing)
85 node = _extra_xml->child (str.c_str());
88 if (!node && add_if_missing) {
89 node = new XMLNode (str);
90 add_extra_xml (*node);
97 Stateful::save_extra_xml (const XMLNode& node)
99 /* Looks for the child node called "Extra" and makes _extra_xml
100 point to a copy of it. Will delete any existing node pointed
101 to by _extra_xml if a new Extra node is found, but not
105 const XMLNode* xtra = node.child ("Extra");
109 _extra_xml = new XMLNode (*xtra);
114 Stateful::add_instant_xml (XMLNode& node, const std::string& directory_path)
116 if (!Glib::file_test (directory_path, Glib::FILE_TEST_IS_DIR)) {
117 if (g_mkdir_with_parents (directory_path.c_str(), 0755) != 0) {
118 error << string_compose(_("Error: could not create directory %1"), directory_path) << endmsg;
123 if (_instant_xml == 0) {
124 _instant_xml = new XMLNode ("instant");
127 _instant_xml->remove_nodes_and_delete (node.name());
128 _instant_xml->add_child_copy (node);
130 std::string instant_xml_path = Glib::build_filename (directory_path, "instant.xml");
133 tree.set_filename(instant_xml_path);
135 /* Important: the destructor for an XMLTree deletes
136 all of its nodes, starting at _root. We therefore
137 cannot simply hand it our persistent _instant_xml
138 node as its _root, because we will lose it whenever
139 the Tree goes out of scope.
141 So instead, copy the _instant_xml node (which does
142 a deep copy), and hand that to the tree.
145 XMLNode* copy = new XMLNode (*_instant_xml);
146 tree.set_root (copy);
149 error << string_compose(_("Error: could not write %1"), instant_xml_path) << endmsg;
154 Stateful::instant_xml (const string& str, const std::string& directory_path)
156 if (_instant_xml == 0) {
158 std::string instant_xml_path = Glib::build_filename (directory_path, "instant.xml");
160 if (Glib::file_test (instant_xml_path, Glib::FILE_TEST_EXISTS)) {
162 if (tree.read(instant_xml_path)) {
163 _instant_xml = new XMLNode(*(tree.root()));
165 warning << string_compose(_("Could not understand XML file %1"), instant_xml_path) << endmsg;
173 const XMLNodeList& nlist = _instant_xml->children();
174 XMLNodeConstIterator i;
176 for (i = nlist.begin(); i != nlist.end(); ++i) {
177 if ((*i)->name() == str) {
185 /** Forget about any changes to this object's properties */
187 Stateful::clear_changes ()
189 for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
190 i->second->clear_changes ();
195 Stateful::get_changes_as_properties (Command* cmd) const
197 PropertyList* pl = new PropertyList;
199 for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
200 i->second->get_changes_as_properties (*pl, cmd);
206 /** Set our property values from an XML node.
207 * Derived types can call this from set_state() (or elsewhere)
208 * to get basic property setting done.
209 * @return IDs of properties that were changed.
212 Stateful::set_values (XMLNode const & node)
216 for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
217 if (i->second->set_value (node)) {
228 Stateful::apply_changes (const PropertyList& property_list)
231 PropertyList::const_iterator p;
233 DEBUG_TRACE (DEBUG::Stateful, string_compose ("Stateful %1 setting properties from list of %2\n", this, property_list.size()));
235 for (PropertyList::const_iterator pp = property_list.begin(); pp != property_list.end(); ++pp) {
236 DEBUG_TRACE (DEBUG::Stateful, string_compose ("in plist: %1\n", pp->second->property_name()));
239 for (PropertyList::const_iterator i = property_list.begin(); i != property_list.end(); ++i) {
240 if ((p = _properties->find (i->first)) != _properties->end()) {
244 string_compose ("actually setting property %1 using %2\n", p->second->property_name(), i->second->property_name())
247 if (apply_changes (*i->second)) {
251 DEBUG_TRACE (DEBUG::Stateful, string_compose ("passed in property %1 not found in own property list\n",
252 i->second->property_name()));
263 /** Add property states to an XML node.
264 * @param owner_state Node.
267 Stateful::add_properties (XMLNode& owner_state)
269 for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
270 i->second->get_value (owner_state);
275 Stateful::add_property (PropertyBase& s)
277 _properties->add (s);
281 Stateful::send_change (const PropertyChange& what_changed)
283 if (what_changed.empty()) {
288 Glib::Threads::Mutex::Lock lm (_lock);
289 if (property_changes_suspended ()) {
290 _pending_changed.add (what_changed);
295 PropertyChanged (what_changed);
299 Stateful::suspend_property_changes ()
301 g_atomic_int_add (&_stateful_frozen, 1);
305 Stateful::resume_property_changes ()
307 PropertyChange what_changed;
310 Glib::Threads::Mutex::Lock lm (_lock);
312 if (property_changes_suspended() && g_atomic_int_dec_and_test (&_stateful_frozen) == FALSE) {
316 if (!_pending_changed.empty()) {
317 what_changed = _pending_changed;
318 _pending_changed.clear ();
322 mid_thaw (what_changed);
324 send_change (what_changed);
328 Stateful::changed() const
330 for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
331 if (i->second->changed()) {
340 Stateful::apply_changes (const PropertyBase& prop)
342 OwnedPropertyList::iterator i = _properties->find (prop.property_id());
343 if (i == _properties->end()) {
347 i->second->apply_changes (&prop);
352 Stateful::property_factory (const XMLNode& history_node) const
354 PropertyList* prop_list = new PropertyList;
356 for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
357 PropertyBase* prop = i->second->clone_from_xml (history_node);
360 prop_list->add (prop);
368 Stateful::rdiff (vector<Command*>& cmds) const
370 for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
371 i->second->rdiff (cmds);
376 Stateful::clear_owned_changes ()
378 for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
379 i->second->clear_owned_changes ();
384 Stateful::set_id (const XMLNode& node)
386 bool* regen = _regenerate_xml_or_string_ids.get();
388 if (regen && *regen) {
393 if (node.get_property ("id", _id)) {
401 Stateful::reset_id ()
407 Stateful::set_id (const string& str)
409 bool* regen = _regenerate_xml_or_string_ids.get();
411 if (regen && *regen) {
419 Stateful::regenerate_xml_or_string_ids () const
421 bool* regen = _regenerate_xml_or_string_ids.get();
422 if (regen && *regen) {
430 Stateful::set_regenerate_xml_and_string_ids_in_this_thread (bool yn)
432 bool* val = new bool (yn);
433 _regenerate_xml_or_string_ids.set (val);