Large nasty commit in the form of a 5000 line patch chock-full of completely
[ardour.git] / libs / ardour / 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$
19 */
20
21 #include <cstdio>
22 #include <unistd.h>
23
24 #include <ardour/stateful.h>
25 #include <ardour/utils.h>
26 #include <pbd/xml++.h>
27
28 #include "i18n.h"
29
30 using namespace PBD;
31
32 Stateful::Stateful ()
33 {
34         _extra_xml = 0;
35         _instant_xml = 0;
36 }
37
38 Stateful::~Stateful ()
39 {
40         // Do not delete _extra_xml.  The use of add_child_nocopy() 
41         // means it needs to live on indefinately.
42         delete _instant_xml;
43 }
44
45 void
46 Stateful::add_extra_xml (XMLNode& node)
47 {
48         if (_extra_xml == 0) {
49                 _extra_xml = new XMLNode ("extra");
50         }
51
52         _extra_xml->remove_nodes (node.name());
53         _extra_xml->add_child_nocopy (node);
54 }
55
56 XMLNode *
57 Stateful::extra_xml (const string& str)
58 {
59         if (_extra_xml == 0) {
60                 return 0;
61         }
62
63         const XMLNodeList& nlist = _extra_xml->children();
64         XMLNodeConstIterator i;
65
66         for (i = nlist.begin(); i != nlist.end(); ++i) {
67                 if ((*i)->name() == str) {
68                         return (*i);
69                 }
70         }
71
72         return 0;
73 }
74
75 void
76 Stateful::add_instant_xml (XMLNode& node, const string& dir)
77 {
78         if (_instant_xml == 0) {
79                 _instant_xml = new XMLNode ("instant");
80         }
81
82         _instant_xml->remove_nodes_and_delete (node.name());
83         _instant_xml->add_child_copy (node);
84         
85         XMLTree tree;
86         tree.set_filename(dir+"/instant.xml");
87
88         /* Important: the destructor for an XMLTree deletes
89            all of its nodes, starting at _root. We therefore
90            cannot simply hand it our persistent _instant_xml 
91            node as its _root, because we will lose it whenever
92            the Tree goes out of scope.
93
94            So instead, copy the _instant_xml node (which does 
95            a deep copy), and hand that to the tree.
96         */
97
98         XMLNode* copy = new XMLNode (*_instant_xml);
99         tree.set_root (copy);
100
101         if (!tree.write()) {
102                 error << string_compose(_("Error: could not write %1"), dir+"/instant.xml") << endmsg;
103         }
104 }
105
106 XMLNode *
107 Stateful::instant_xml (const string& str, const string& dir)
108 {
109         if (_instant_xml == 0) {
110                 string instant_file = dir + "/instant.xml";
111                 if (access(instant_file.c_str(), F_OK) == 0) {
112                         XMLTree tree;
113                         if (tree.read(dir+"/instant.xml")) {
114                                 _instant_xml = new XMLNode(*(tree.root()));
115                         } else {
116                                 warning << string_compose(_("Could not understand XML file %1"), instant_file) << endmsg;
117                                 return 0;
118                         }
119                 } else {
120                         return 0;
121                 }
122         }
123
124         const XMLNodeList& nlist = _instant_xml->children();
125         XMLNodeConstIterator i;
126
127         for (i = nlist.begin(); i != nlist.end(); ++i) {
128                 if ((*i)->name() == str) {
129                         return (*i);
130                 }
131         }
132
133         return 0;
134 }
135