the mega-properties/SequenceProperty patch. split is broken at present (right hand...
[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 {
44         _extra_xml = 0;
45         _instant_xml = 0;
46 }
47
48 Stateful::~Stateful ()
49 {
50         delete _properties;
51
52         // Do not delete _extra_xml.  The use of add_child_nocopy() 
53         // means it needs to live on indefinately.
54
55         delete _instant_xml;
56 }
57
58 void
59 Stateful::add_extra_xml (XMLNode& node)
60 {
61         if (_extra_xml == 0) {
62                 _extra_xml = new XMLNode ("Extra");
63         }
64
65         _extra_xml->remove_nodes (node.name());
66         _extra_xml->add_child_nocopy (node);
67 }
68
69 XMLNode *
70 Stateful::extra_xml (const string& str)
71 {
72         if (_extra_xml == 0) {
73                 return 0;
74         }
75
76         const XMLNodeList& nlist = _extra_xml->children();
77         XMLNodeConstIterator i;
78
79         for (i = nlist.begin(); i != nlist.end(); ++i) {
80                 if ((*i)->name() == str) {
81                         return (*i);
82                 }
83         }
84
85         return 0;
86 }
87
88 void
89 Stateful::add_instant_xml (XMLNode& node, const sys::path& directory_path)
90 {
91         sys::create_directories (directory_path); // may throw
92
93         if (_instant_xml == 0) {
94                 _instant_xml = new XMLNode ("instant");
95         }
96
97         _instant_xml->remove_nodes_and_delete (node.name());
98         _instant_xml->add_child_copy (node);
99
100         sys::path instant_xml_path(directory_path);
101
102         instant_xml_path /= "instant.xml";
103         
104         XMLTree tree;
105         tree.set_filename(instant_xml_path.to_string());
106
107         /* Important: the destructor for an XMLTree deletes
108            all of its nodes, starting at _root. We therefore
109            cannot simply hand it our persistent _instant_xml 
110            node as its _root, because we will lose it whenever
111            the Tree goes out of scope.
112
113            So instead, copy the _instant_xml node (which does 
114            a deep copy), and hand that to the tree.
115         */
116
117         XMLNode* copy = new XMLNode (*_instant_xml);
118         tree.set_root (copy);
119
120         if (!tree.write()) {
121                 error << string_compose(_("Error: could not write %1"), instant_xml_path.to_string()) << endmsg;
122         }
123 }
124
125 XMLNode *
126 Stateful::instant_xml (const string& str, const sys::path& directory_path)
127 {
128         if (_instant_xml == 0) {
129
130                 sys::path instant_xml_path(directory_path);
131                 instant_xml_path /= "instant.xml";
132
133                 if (exists(instant_xml_path)) {
134                         XMLTree tree;
135                         if (tree.read(instant_xml_path.to_string())) {
136                                 _instant_xml = new XMLNode(*(tree.root()));
137                         } else {
138                                 warning << string_compose(_("Could not understand XML file %1"), instant_xml_path.to_string()) << endmsg;
139                                 return 0;
140                         }
141                 } else {
142                         return 0;
143                 }
144         }
145
146         const XMLNodeList& nlist = _instant_xml->children();
147         XMLNodeConstIterator i;
148
149         for (i = nlist.begin(); i != nlist.end(); ++i) {
150                 if ((*i)->name() == str) {
151                         return (*i);
152                 }
153         }
154
155         return 0;
156 }
157
158 /** Forget about any old state for this object */       
159 void
160 Stateful::clear_history ()
161 {
162         for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
163                 i->second->clear_history ();
164         }
165 }
166
167 void
168 Stateful::diff (PropertyList& before, PropertyList& after) const
169 {
170         for (OwnedPropertyList::const_iterator i = _properties->begin(); i != _properties->end(); ++i) {
171                 i->second->diff (before, after);
172         }
173 }
174
175 /** Set state of some/all _properties from an XML node.
176  *  @param node Node.
177  *  @return PropertyChanges made.
178  */
179 PropertyChange
180 Stateful::set_properties (XMLNode const & owner_state)
181 {
182         PropertyChange c;
183         
184         for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
185                 if (i->second->set_state_from_owner_state (owner_state)) {
186                         c.add (i->first);
187                 }
188         }
189
190         post_set ();
191
192         return c;
193 }
194
195 PropertyChange
196 Stateful::set_properties (const PropertyList& property_list)
197 {
198         PropertyChange c;
199         PropertyList::const_iterator p;
200
201         DEBUG_TRACE (DEBUG::Stateful, string_compose ("Stateful %1 setting properties from list of %2\n", this, property_list.size()));
202
203         for (PropertyList::const_iterator pp = property_list.begin(); pp != property_list.end(); ++pp) {
204                 DEBUG_TRACE (DEBUG::Stateful, string_compose ("in plist: %1\n", pp->second->property_name()));
205         }
206         
207         for (PropertyList::const_iterator i = property_list.begin(); i != property_list.end(); ++i) {
208                 if ((p = _properties->find (i->first)) != _properties->end()) {
209                         DEBUG_TRACE (DEBUG::Stateful, string_compose ("actually setting property %1\n", p->second->property_name()));
210                         if (set_property (*i->second)) {
211                                 c.add (i->first);
212                         }
213                 } else {
214                         DEBUG_TRACE (DEBUG::Stateful, string_compose ("passed in property %1 not found in own property list\n",
215                                                                       i->second->property_name()));
216                 }
217         }
218         
219         post_set ();
220
221         return c;
222 }
223
224 /** Add property states to an XML node.
225  *  @param node Node.
226  */
227 void
228 Stateful::add_properties (XMLNode& owner_state)
229 {
230         for (OwnedPropertyList::iterator i = _properties->begin(); i != _properties->end(); ++i) {
231                 i->second->add_state_to_owner_state (owner_state);
232         }
233 }
234
235 void
236 Stateful::add_property (PropertyBase& s)
237 {
238         _properties->add (s);
239 }
240
241 } // namespace PBD