* Added PBD::UUID
[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         if (local) {
51                 delete local;
52         }
53 }
54
55 void
56 ExportPreset::set_name (string const & name)
57 {
58         _name = name;
59
60         XMLNode * node; 
61         if ((node = global.root())) {
62                 node->add_property ("name", name);
63         }
64         if (local) {
65                 local->add_property ("name", name);
66         }
67 }
68
69 void
70 ExportPreset::set_id (string const & id)
71 {
72         _id = id;
73
74         XMLNode * node;
75         if ((node = global.root())) {
76                 node->add_property ("id", id);
77         }
78         if (local) {
79                 local->add_property ("id", id);
80         }
81 }
82
83 void
84 ExportPreset::set_global_state (XMLNode & state)
85 {
86         delete global.root ();
87         global.set_root (&state);
88         
89         set_id (_id.to_s());
90         set_name (_name);
91 }
92
93 void
94 ExportPreset::set_local_state (XMLNode & state)
95 {
96         delete local;
97         local = &state;
98         
99         set_id (_id.to_s());
100         set_name (_name);
101 }
102
103 void
104 ExportPreset::save () const
105 {
106         save_instant_xml ();
107         if (global.root()) {
108                 global.write ();
109         }
110 }
111
112 void
113 ExportPreset::remove_local () const
114 {
115         remove_instant_xml ();
116 }
117
118 XMLNode *
119 ExportPreset::get_instant_xml () const
120 {
121         XMLNode * instant_xml;
122         
123         if ((instant_xml = session.instant_xml ("ExportPresets"))) {
124                 XMLNodeList children = instant_xml->children ("ExportPreset");
125                 for (XMLNodeList::iterator it = children.begin(); it != children.end(); ++it) {
126                         XMLProperty * prop;
127                         if ((prop = (*it)->property ("id")) && _id == PBD::UUID(prop->value())) {
128                                 return *it;
129                         }
130                 }
131         }
132         
133         return 0;
134 }
135
136 void
137 ExportPreset::save_instant_xml () const
138 {
139         if (!local) { return; }
140
141         /* First remove old, then add new */
142         
143         remove_instant_xml ();
144         
145         XMLNode * instant_xml;
146         if ((instant_xml = session.instant_xml ("ExportPresets"))) {
147                 instant_xml->add_child_copy (*local);
148         } else {
149                 instant_xml = new XMLNode ("ExportPresets");
150                 instant_xml->add_child_copy (*local);
151                 session.add_instant_xml (*instant_xml, false);
152         }
153 }
154
155 void
156 ExportPreset::remove_instant_xml () const
157 {
158         XMLNode * instant_xml;
159         if ((instant_xml = session.instant_xml ("ExportPresets"))) {
160                 instant_xml->remove_nodes_and_delete ("id", _id.to_s());
161         }
162 }