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;
|
|
|
|
|
2016-05-03 08:26:25 -04:00
|
|
|
/*static*/ 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) {
|
|
|
|
if ((*i)->name() != X_("Object")) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-03 08:26:25 -04:00
|
|
|
if ((*i)->has_property_with_value(X_("id"), id)) {
|
2012-01-20 13:02:43 -05:00
|
|
|
return *i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2011-07-06 20:37:13 -04:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:26:25 -04:00
|
|
|
/*static*/ XMLNode *
|
2012-01-20 13:02:48 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:26:25 -04:00
|
|
|
|
|
|
|
const string GUIObjectState::xml_node_name (X_("GUIObjectState"));
|
|
|
|
|
|
|
|
GUIObjectState::GUIObjectState ()
|
|
|
|
: _state (X_("GUIObjectState"))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-01-20 13:02:48 -05:00
|
|
|
XMLNode *
|
|
|
|
GUIObjectState::get_or_add_node (const string& id)
|
|
|
|
{
|
2016-05-03 08:26:25 -04:00
|
|
|
std::map <std::string, XMLNode*>::iterator i = object_map.find (id);
|
|
|
|
if (i != object_map.end()) {
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
//assert (get_node (&_state, id) == 0); // XXX
|
|
|
|
XMLNode* child = new XMLNode (X_("Object"));
|
|
|
|
child->add_property (X_("id"), id);
|
|
|
|
_state.add_child_nocopy (*child);
|
|
|
|
object_map[id] = child;
|
|
|
|
return child;
|
2012-01-20 13:02:48 -05:00
|
|
|
}
|
|
|
|
|
2015-03-30 15:27:18 -04:00
|
|
|
void
|
|
|
|
GUIObjectState::remove_node (const std::string& id)
|
|
|
|
{
|
2016-05-03 08:26:25 -04:00
|
|
|
object_map.erase (id);
|
2015-03-30 15:27:18 -04:00
|
|
|
_state.remove_nodes_and_delete(X_("id"), id );
|
|
|
|
}
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
string
|
2012-01-20 13:02:43 -05:00
|
|
|
GUIObjectState::get_string (const string& id, const string& prop_name, bool* empty)
|
2011-07-11 16:32:33 -04:00
|
|
|
{
|
2016-05-03 08:26:25 -04:00
|
|
|
std::map <std::string, XMLNode*>::const_iterator i = object_map.find (id);
|
|
|
|
if (i == object_map.end()) {
|
|
|
|
//assert (get_node (&_state, id) == 0); // XXX
|
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
|
|
|
|
2016-05-04 23:09:37 -04:00
|
|
|
XMLProperty const * p (i->second->property (prop_name));
|
2012-01-20 13:02:43 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:26:25 -04:00
|
|
|
object_map.clear ();
|
2012-01-20 13:02:43 -05:00
|
|
|
_state = node;
|
2016-05-03 08:26:25 -04:00
|
|
|
|
|
|
|
XMLNodeList const & children (_state.children ());
|
|
|
|
for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
|
|
|
|
if ((*i)->name() != X_("Object")) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-04 23:09:37 -04:00
|
|
|
XMLProperty const * prop = (*i)->property (X_("id"));
|
2016-05-03 08:26:25 -04:00
|
|
|
if (!prop) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
object_map[prop->value ()] = *i;
|
|
|
|
}
|
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;
|
2016-05-03 08:26:25 -04:00
|
|
|
for (std::map <std::string, XMLNode*>::const_iterator i = object_map.begin ();
|
|
|
|
i != object_map.end (); ++i) {
|
|
|
|
ids.push_back (i->first);
|
2012-01-20 13:02:43 -05:00
|
|
|
}
|
|
|
|
return ids;
|
2011-07-13 13:13:30 -04:00
|
|
|
}
|