the Properties & 64bit region commit
[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/stateful.h"
24 #include "pbd/destructible.h"
25 #include "pbd/filesystem.h"
26 #include "pbd/xml++.h"
27 #include "pbd/error.h"
28
29 #include "i18n.h"
30
31 using namespace std;
32
33 namespace PBD {
34
35 int Stateful::current_state_version = 0;
36 int Stateful::loading_state_version = 0;
37
38 Stateful::Stateful ()
39 {
40         _extra_xml = 0;
41         _instant_xml = 0;
42 }
43
44 Stateful::~Stateful ()
45 {
46         // Do not delete _extra_xml.  The use of add_child_nocopy() 
47         // means it needs to live on indefinately.
48
49         delete _instant_xml;
50 }
51
52 void
53 Stateful::add_extra_xml (XMLNode& node)
54 {
55         if (_extra_xml == 0) {
56                 _extra_xml = new XMLNode ("Extra");
57         }
58
59         _extra_xml->remove_nodes (node.name());
60         _extra_xml->add_child_nocopy (node);
61 }
62
63 XMLNode *
64 Stateful::extra_xml (const string& str)
65 {
66         if (_extra_xml == 0) {
67                 return 0;
68         }
69
70         const XMLNodeList& nlist = _extra_xml->children();
71         XMLNodeConstIterator i;
72
73         for (i = nlist.begin(); i != nlist.end(); ++i) {
74                 if ((*i)->name() == str) {
75                         return (*i);
76                 }
77         }
78
79         return 0;
80 }
81
82 void
83 Stateful::add_instant_xml (XMLNode& node, const sys::path& directory_path)
84 {
85         sys::create_directories (directory_path); // may throw
86
87         if (_instant_xml == 0) {
88                 _instant_xml = new XMLNode ("instant");
89         }
90
91         _instant_xml->remove_nodes_and_delete (node.name());
92         _instant_xml->add_child_copy (node);
93
94         sys::path instant_xml_path(directory_path);
95
96         instant_xml_path /= "instant.xml";
97         
98         XMLTree tree;
99         tree.set_filename(instant_xml_path.to_string());
100
101         /* Important: the destructor for an XMLTree deletes
102            all of its nodes, starting at _root. We therefore
103            cannot simply hand it our persistent _instant_xml 
104            node as its _root, because we will lose it whenever
105            the Tree goes out of scope.
106
107            So instead, copy the _instant_xml node (which does 
108            a deep copy), and hand that to the tree.
109         */
110
111         XMLNode* copy = new XMLNode (*_instant_xml);
112         tree.set_root (copy);
113
114         if (!tree.write()) {
115                 error << string_compose(_("Error: could not write %1"), instant_xml_path.to_string()) << endmsg;
116         }
117 }
118
119 XMLNode *
120 Stateful::instant_xml (const string& str, const sys::path& directory_path)
121 {
122         if (_instant_xml == 0) {
123
124                 sys::path instant_xml_path(directory_path);
125                 instant_xml_path /= "instant.xml";
126
127                 if (exists(instant_xml_path)) {
128                         XMLTree tree;
129                         if (tree.read(instant_xml_path.to_string())) {
130                                 _instant_xml = new XMLNode(*(tree.root()));
131                         } else {
132                                 warning << string_compose(_("Could not understand XML file %1"), instant_xml_path.to_string()) << endmsg;
133                                 return 0;
134                         }
135                 } else {
136                         return 0;
137                 }
138         }
139
140         const XMLNodeList& nlist = _instant_xml->children();
141         XMLNodeConstIterator i;
142
143         for (i = nlist.begin(); i != nlist.end(); ++i) {
144                 if ((*i)->name() == str) {
145                         return (*i);
146                 }
147         }
148
149         return 0;
150 }
151
152 /** Forget about any old state for this object */       
153 void
154 Stateful::clear_history ()
155 {
156         for (OwnedPropertyList::iterator i = _properties.begin(); i != _properties.end(); ++i) {
157                 i->second->clear_history ();
158         }
159 }
160
161 /** @return A pair of XMLNodes representing state that has changed since the last time clear_history
162  *  was called on this object; the first is the state before, the second the state after.
163  *
164  *  It is the caller's responsibility to delete the returned XMLNodes.
165  */
166 pair<XMLNode *, XMLNode *>
167 Stateful::diff () const
168 {
169         XMLNode* old = new XMLNode (_xml_node_name);
170         XMLNode* current = new XMLNode (_xml_node_name);
171
172         for (OwnedPropertyList::const_iterator i = _properties.begin(); i != _properties.end(); ++i) {
173                 i->second->diff (old, current);
174         }
175
176         return make_pair (old, current);
177 }
178
179 /** Modifies PropertyChange @param c to indicate what properties have changed since the last
180     time clear_history was called on this object. Note that not all properties have change
181     values - if this object has any such Property members, they will never show up in
182     the value of @param c. Note also that @param c is not cleared by this function.
183 */
184 void
185 Stateful::changed (PropertyChange& c) const
186 {
187         for (OwnedPropertyList::const_iterator i = _properties.begin(); i != _properties.end(); ++i) {
188                 i->second->diff (c);
189         }
190 }       
191         
192 /** Set state of some/all _properties from an XML node.
193  *  @param node Node.
194  *  @return PropertyChanges made.
195  */
196 PropertyChange
197 Stateful::set_properties (XMLNode const & node)
198 {
199         PropertyChange c = PropertyChange (0);
200
201         for (OwnedPropertyList::iterator i = _properties.begin(); i != _properties.end(); ++i) {
202                 c = PropertyChange (c | i->second->set_state (node));
203         }
204
205         post_set ();
206
207         return c;
208 }
209
210 PropertyChange
211 Stateful::set_properties (const PropertyList& property_list)
212 {
213         PropertyChange c = PropertyChange (0);
214         PropertyList::const_iterator p;
215
216         for (OwnedPropertyList::iterator i = _properties.begin(); i != _properties.end(); ++i) {
217                 if ((p = property_list.find (i->first)) != property_list.end()) {
218                         c = PropertyChange (c|set_property (*(p->second)));
219                 }
220         }
221         
222         post_set ();
223
224         return c;
225 }
226
227
228 /** Add property states to an XML node.
229  *  @param node Node.
230  */
231 void
232 Stateful::add_properties (XMLNode & node)
233 {
234         for (OwnedPropertyList::iterator i = _properties.begin(); i != _properties.end(); ++i) {
235                 i->second->add_state (node);
236         }
237 }
238
239 } // namespace PBD