Merge remote-tracking branch 'remotes/origin/master' into windows+cc
[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 /** Get a string from our state.
75  *  @param id property of Object node to look for.
76  *  @param prop_name name of the Object property to return.
77  *  @param empty if non-0, filled in with true if the property is currently non-existant, otherwise false.
78  *  @return value of property `prop_name', or empty.
79  */
80
81 string 
82 GUIObjectState::get_string (const string& id, const string& prop_name, bool* empty)
83 {
84         XMLNode* child = get_node (&_state, id);
85
86         if (!child) {
87                 if (empty) {
88                         *empty = true;
89                 }
90                 return string ();
91         }
92
93         XMLProperty* p = child->property (prop_name);
94         if (!p) {
95                 if (empty) {
96                         *empty = true;
97                 }
98                 return string ();
99         }
100
101         if (empty) {
102                 *empty = false;
103         }
104
105         return p->value ();
106 }
107
108 XMLNode&
109 GUIObjectState::get_state () const
110 {
111         return *new XMLNode (_state);
112 }
113
114 int
115 GUIObjectState::set_state (const XMLNode& node)
116 {
117         if (node.name() != xml_node_name) {
118                 return -1;
119         }
120
121         _state = node;
122         return 0;
123 }
124
125 void
126 GUIObjectState::load (const XMLNode& node)
127 {
128         (void) set_state (node);
129 }
130
131 std::list<string>
132 GUIObjectState::all_ids () const
133 {
134         std::list<string> ids;
135         XMLNodeList const & children = _state.children ();
136         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
137                 if ((*i)->name() != X_("Object")) {
138                         continue;
139                 }
140
141                 XMLProperty* p = (*i)->property (X_("id"));
142                 if (p) {
143                         ids.push_back (p->value ());
144                 }
145         }
146
147         return ids;
148 }
149
150