merge fix for tempo branch
[ardour.git] / gtk2_ardour / gui_object.cc
1 /*
2     Copyright (C) 2011 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 */
19
20 #include <iostream>
21 #include <sstream>
22
23 #include "gui_object.h"
24 #include "i18n.h"
25
26 using std::string;
27
28 const string GUIObjectState::xml_node_name (X_("GUIObjectState"));
29
30 GUIObjectState::GUIObjectState ()
31         : _state (X_("GUIObjectState"))
32 {
33
34 }
35
36 XMLNode *
37 GUIObjectState::get_node (const XMLNode* parent, const string& id)
38 {
39         XMLNodeList const & children = parent->children ();
40         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
41
42                 if ((*i)->name() != X_("Object")) {
43                         continue;
44                 }
45
46                 XMLProperty* p = (*i)->property (X_("id"));
47                 if (p && p->value() == id) {
48                         return *i;
49                 }
50         }
51
52         return 0;
53 }
54
55 XMLNode *
56 GUIObjectState::get_or_add_node (XMLNode* parent, const string& id)
57 {
58         XMLNode* child = get_node (parent, id);
59         if (!child) {
60                 child = new XMLNode (X_("Object"));
61                 child->add_property (X_("id"), id);
62                 parent->add_child_nocopy (*child);
63         }
64
65         return child;
66 }
67
68 XMLNode *
69 GUIObjectState::get_or_add_node (const string& id)
70 {
71         return get_or_add_node (&_state, id);
72 }
73
74 /** Remove node with provided id.
75  *  @param id property of Object node to look for.
76  */
77
78 void
79 GUIObjectState::remove_node (const std::string& id)
80 {
81         _state.remove_nodes_and_delete(X_("id"), id );
82 }
83
84 /** Get a string from our state.
85  *  @param id property of Object node to look for.
86  *  @param prop_name name of the Object property to return.
87  *  @param empty if non-0, filled in with true if the property is currently non-existant, otherwise false.
88  *  @return value of property `prop_name', or empty.
89  */
90
91 string 
92 GUIObjectState::get_string (const string& id, const string& prop_name, bool* empty)
93 {
94         XMLNode* child = get_node (&_state, id);
95
96         if (!child) {
97                 if (empty) {
98                         *empty = true;
99                 }
100                 return string ();
101         }
102
103         XMLProperty* p = child->property (prop_name);
104         if (!p) {
105                 if (empty) {
106                         *empty = true;
107                 }
108                 return string ();
109         }
110
111         if (empty) {
112                 *empty = false;
113         }
114
115         return p->value ();
116 }
117
118 XMLNode&
119 GUIObjectState::get_state () const
120 {
121         return *new XMLNode (_state);
122 }
123
124 int
125 GUIObjectState::set_state (const XMLNode& node)
126 {
127         if (node.name() != xml_node_name) {
128                 return -1;
129         }
130
131         _state = node;
132         return 0;
133 }
134
135 void
136 GUIObjectState::load (const XMLNode& node)
137 {
138         (void) set_state (node);
139 }
140
141 std::list<string>
142 GUIObjectState::all_ids () const
143 {
144         std::list<string> ids;
145         XMLNodeList const & children = _state.children ();
146         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
147                 if ((*i)->name() != X_("Object")) {
148                         continue;
149                 }
150
151                 XMLProperty* p = (*i)->property (X_("id"));
152                 if (p) {
153                         ids.push_back (p->value ());
154                 }
155         }
156
157         return ids;
158 }
159
160