ardour
gui_object.cc
Go to the documentation of this file.
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 
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 
78 void
79 GUIObjectState::remove_node (const std::string& id)
80 {
81  _state.remove_nodes_and_delete(X_("id"), id );
82 }
83 
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&
120 {
121  return *new XMLNode (_state);
122 }
123 
124 int
126 {
127  if (node.name() != xml_node_name) {
128  return -1;
129  }
130 
131  _state = node;
132  return 0;
133 }
134 
135 void
137 {
138  (void) set_state (node);
139 }
140 
141 std::list<string>
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 
std::string get_string(const std::string &id, const std::string &prop_name, bool *empty=0)
Definition: gui_object.cc:92
const std::string & value() const
Definition: xml++.h:159
const std::string & name() const
Definition: xml++.h:104
const XMLNodeList & children(const std::string &str=std::string()) const
Definition: xml++.cc:329
static XMLNode * get_node(const XMLNode *, const std::string &)
Definition: gui_object.cc:37
std::list< XMLNode * > XMLNodeList
Definition: xml++.h:44
#define X_(Text)
Definition: i18n.h:13
static const std::string xml_node_name
Definition: gui_object.h:39
XMLProperty * property(const char *)
Definition: xml++.cc:413
XMLNode * get_or_add_node(const std::string &)
void remove_node(const std::string &id)
Definition: gui_object.cc:79
XMLNode _state
Definition: gui_object.h:60
XMLProperty * add_property(const char *name, const std::string &value)
void add_child_nocopy(XMLNode &)
Definition: xml++.cc:357
Definition: xml++.h:95
void load(const XMLNode &)
Definition: gui_object.cc:136
std::list< std::string > all_ids() const
Definition: gui_object.cc:142
void remove_nodes_and_delete(const std::string &)
int set_state(const XMLNode &)
Definition: gui_object.cc:125
XMLNode & get_state() const
Definition: gui_object.cc:119