2011-07-06 20:37:13 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2011 Paul Davis
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "gui_object.h"
|
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
const string GUIObjectState::xml_node_name (X_("GUIObjectState"));
|
|
|
|
|
2012-01-20 13:02:43 -05:00
|
|
|
GUIObjectState::GUIObjectState ()
|
|
|
|
: _state (X_("GUIObjectState"))
|
2011-07-06 20:37:13 -04:00
|
|
|
{
|
2012-01-20 13:02:43 -05:00
|
|
|
|
2011-07-06 20:37:13 -04:00
|
|
|
}
|
|
|
|
|
2012-01-20 13:02:43 -05:00
|
|
|
XMLNode *
|
2012-01-20 13:02:48 -05:00
|
|
|
GUIObjectState::get_node (const XMLNode* parent, const string& id)
|
2011-07-06 20:37:13 -04:00
|
|
|
{
|
2012-01-20 13:02:48 -05:00
|
|
|
XMLNodeList const & children = parent->children ();
|
2012-01-20 13:02:43 -05:00
|
|
|
for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
|
2012-03-06 21:08:46 -05:00
|
|
|
|
2012-01-20 13:02:43 -05:00
|
|
|
if ((*i)->name() != X_("Object")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLProperty* p = (*i)->property (X_("id"));
|
|
|
|
if (p && p->value() == id) {
|
|
|
|
return *i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2011-07-06 20:37:13 -04:00
|
|
|
}
|
|
|
|
|
2012-01-20 13:02:48 -05:00
|
|
|
XMLNode *
|
|
|
|
GUIObjectState::get_or_add_node (XMLNode* parent, const string& id)
|
|
|
|
{
|
|
|
|
XMLNode* child = get_node (parent, id);
|
|
|
|
if (!child) {
|
|
|
|
child = new XMLNode (X_("Object"));
|
|
|
|
child->add_property (X_("id"), id);
|
|
|
|
parent->add_child_nocopy (*child);
|
|
|
|
}
|
|
|
|
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode *
|
|
|
|
GUIObjectState::get_or_add_node (const string& id)
|
|
|
|
{
|
|
|
|
return get_or_add_node (&_state, id);
|
|
|
|
}
|
|
|
|
|
2012-01-20 13:02:43 -05:00
|
|
|
/** Get a string from our state.
|
|
|
|
* @param id property of Object node to look for.
|
|
|
|
* @param prop_name name of the Object property to return.
|
|
|
|
* @param empty if non-0, filled in with true if the property is currently non-existant, otherwise false.
|
|
|
|
* @return value of property `prop_name', or empty.
|
|
|
|
*/
|
|
|
|
|
|
|
|
string
|
|
|
|
GUIObjectState::get_string (const string& id, const string& prop_name, bool* empty)
|
2011-07-11 16:32:33 -04:00
|
|
|
{
|
2012-01-20 13:02:48 -05:00
|
|
|
XMLNode* child = get_node (&_state, id);
|
2012-03-06 21:08:46 -05:00
|
|
|
|
2012-01-20 13:02:43 -05:00
|
|
|
if (!child) {
|
2011-07-11 16:32:33 -04:00
|
|
|
if (empty) {
|
|
|
|
*empty = true;
|
|
|
|
}
|
2012-01-20 13:02:43 -05:00
|
|
|
return string ();
|
2011-07-11 16:32:33 -04:00
|
|
|
}
|
2012-01-20 13:02:43 -05:00
|
|
|
|
|
|
|
XMLProperty* p = child->property (prop_name);
|
|
|
|
if (!p) {
|
2011-07-11 16:32:33 -04:00
|
|
|
if (empty) {
|
|
|
|
*empty = true;
|
|
|
|
}
|
2012-01-20 13:02:43 -05:00
|
|
|
return string ();
|
2011-07-11 16:32:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty) {
|
|
|
|
*empty = false;
|
|
|
|
}
|
2012-01-20 13:02:43 -05:00
|
|
|
|
|
|
|
return p->value ();
|
2011-07-11 16:32:33 -04:00
|
|
|
}
|
|
|
|
|
2011-07-06 20:37:13 -04:00
|
|
|
XMLNode&
|
|
|
|
GUIObjectState::get_state () const
|
|
|
|
{
|
2012-01-20 13:02:43 -05:00
|
|
|
return *new XMLNode (_state);
|
2011-07-06 20:37:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
GUIObjectState::set_state (const XMLNode& node)
|
|
|
|
{
|
|
|
|
if (node.name() != xml_node_name) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-20 13:02:43 -05:00
|
|
|
_state = node;
|
2011-07-06 20:37:13 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GUIObjectState::load (const XMLNode& node)
|
|
|
|
{
|
|
|
|
(void) set_state (node);
|
|
|
|
}
|
|
|
|
|
2012-01-20 13:02:43 -05:00
|
|
|
std::list<string>
|
|
|
|
GUIObjectState::all_ids () const
|
2011-07-13 13:13:30 -04:00
|
|
|
{
|
2012-01-20 13:02:43 -05:00
|
|
|
std::list<string> ids;
|
|
|
|
XMLNodeList const & children = _state.children ();
|
|
|
|
for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
|
|
|
|
if ((*i)->name() != X_("Object")) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-07-13 13:13:30 -04:00
|
|
|
|
2012-01-20 13:02:43 -05:00
|
|
|
XMLProperty* p = (*i)->property (X_("id"));
|
|
|
|
if (p) {
|
|
|
|
ids.push_back (p->value ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids;
|
2011-07-13 13:13:30 -04:00
|
|
|
}
|
|
|
|
|
2012-01-20 13:02:43 -05:00
|
|
|
|