Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[ardour.git] / libs / ardour / export_preset.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <ardour/export_preset.h>
22
23 #include <ardour/session.h>
24
25 using namespace ARDOUR;
26
27 ExportPreset::ExportPreset (string filename, Session & s) :
28   session (s), global (filename), local (0)
29 {
30         XMLNode * root;
31         if ((root = global.root())) {
32                 XMLProperty * prop;
33                 if ((prop = root->property ("id"))) {
34                         set_id (prop->value());
35                 }
36                 if ((prop = root->property ("name"))) {
37                         set_name (prop->value());
38                 }
39                 
40                 XMLNode * instant_xml = get_instant_xml ();
41                 if (instant_xml) {
42                         XMLNode * instant_copy = new XMLNode (*instant_xml);
43                         set_local_state (*instant_copy);
44                 }
45         }
46 }
47
48 ExportPreset::~ExportPreset ()
49 {
50         delete local;
51 }
52
53 void
54 ExportPreset::set_name (string const & name)
55 {
56         _name = name;
57
58         XMLNode * node; 
59         if ((node = global.root())) {
60                 node->add_property ("name", name);
61         }
62         if (local) {
63                 local->add_property ("name", name);
64         }
65 }
66
67 void
68 ExportPreset::set_id (string const & id)
69 {
70         _id = id;
71
72         XMLNode * node;
73         if ((node = global.root())) {
74                 node->add_property ("id", id);
75         }
76         if (local) {
77                 local->add_property ("id", id);
78         }
79 }
80
81 void
82 ExportPreset::set_global_state (XMLNode & state)
83 {
84         delete global.root ();
85         global.set_root (&state);
86         
87         set_id (_id.to_s());
88         set_name (_name);
89 }
90
91 void
92 ExportPreset::set_local_state (XMLNode & state)
93 {
94         delete local;
95         local = &state;
96         
97         set_id (_id.to_s());
98         set_name (_name);
99 }
100
101 void
102 ExportPreset::save (std::string const & filename)
103 {
104         save_instant_xml ();
105         if (global.root()) {
106                 global.set_filename (filename);
107                 global.write ();
108         }
109 }
110
111 void
112 ExportPreset::remove_local () const
113 {
114         remove_instant_xml ();
115 }
116
117 XMLNode *
118 ExportPreset::get_instant_xml () const
119 {
120         XMLNode * instant_xml;
121         
122         if ((instant_xml = session.instant_xml ("ExportPresets"))) {
123                 XMLNodeList children = instant_xml->children ("ExportPreset");
124                 for (XMLNodeList::iterator it = children.begin(); it != children.end(); ++it) {
125                         XMLProperty * prop;
126                         if ((prop = (*it)->property ("id")) && _id == PBD::UUID(prop->value())) {
127                                 return *it;
128                         }
129                 }
130         }
131         
132         return 0;
133 }
134
135 void
136 ExportPreset::save_instant_xml () const
137 {
138         if (!local) { return; }
139
140         /* First remove old, then add new */
141         
142         remove_instant_xml ();
143         
144         XMLNode * instant_xml;
145         if ((instant_xml = session.instant_xml ("ExportPresets"))) {
146                 instant_xml->add_child_copy (*local);
147         } else {
148                 instant_xml = new XMLNode ("ExportPresets");
149                 instant_xml->add_child_copy (*local);
150                 session.add_instant_xml (*instant_xml, false);
151         }
152 }
153
154 void
155 ExportPreset::remove_instant_xml () const
156 {
157         XMLNode * instant_xml;
158         if ((instant_xml = session.instant_xml ("ExportPresets"))) {
159                 instant_xml->remove_nodes_and_delete ("id", _id.to_s());
160         }
161 }