2008-03-06 23:23:13 +01:00
|
|
|
/*
|
2009-03-25 15:21:04 +01:00
|
|
|
* Copyright (C) 2008-2009 Patrick Ohly <patrick.ohly@gmx.de>
|
2009-04-30 18:14:03 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) version 3.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301 USA
|
2008-03-06 23:23:13 +01:00
|
|
|
*/
|
|
|
|
|
2009-10-05 14:49:32 +02:00
|
|
|
#include <syncevo/FilterConfigNode.h>
|
|
|
|
#include <syncevo/SyncContext.h>
|
2008-03-06 23:23:13 +01:00
|
|
|
|
2008-07-11 22:25:02 +02:00
|
|
|
#include <boost/foreach.hpp>
|
2009-06-10 17:28:45 +02:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
2008-07-11 22:25:02 +02:00
|
|
|
|
2009-10-05 14:49:32 +02:00
|
|
|
#include <syncevo/declarations.h>
|
2009-10-02 17:23:53 +02:00
|
|
|
SE_BEGIN_CXX
|
|
|
|
|
2008-03-06 23:23:13 +01:00
|
|
|
FilterConfigNode::FilterConfigNode(const boost::shared_ptr<ConfigNode> &node,
|
2008-03-18 22:45:34 +01:00
|
|
|
const ConfigFilter &filter) :
|
2008-03-24 22:42:47 +01:00
|
|
|
m_filter(filter),
|
2008-03-06 23:23:13 +01:00
|
|
|
m_node(node),
|
2008-03-24 22:42:47 +01:00
|
|
|
m_readOnlyNode(node)
|
2008-03-20 22:59:59 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FilterConfigNode::FilterConfigNode(const boost::shared_ptr<const ConfigNode> &node,
|
|
|
|
const ConfigFilter &filter) :
|
2008-03-24 22:42:47 +01:00
|
|
|
m_filter(filter),
|
|
|
|
m_readOnlyNode(node)
|
2008-03-06 23:23:13 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilterConfigNode::addFilter(const string &property,
|
|
|
|
const string &value)
|
|
|
|
{
|
2008-07-06 22:26:23 +02:00
|
|
|
m_filter[property] = value;
|
2008-03-06 23:23:13 +01:00
|
|
|
}
|
|
|
|
|
2008-03-18 22:45:34 +01:00
|
|
|
void FilterConfigNode::setFilter(const ConfigFilter &filter)
|
2008-03-06 23:23:13 +01:00
|
|
|
{
|
|
|
|
m_filter = filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
string FilterConfigNode::readProperty(const string &property) const
|
|
|
|
{
|
2008-03-18 22:45:34 +01:00
|
|
|
ConfigFilter::const_iterator it = m_filter.find(property);
|
2008-03-06 23:23:13 +01:00
|
|
|
|
|
|
|
if (it != m_filter.end()) {
|
|
|
|
return it->second;
|
|
|
|
} else {
|
2008-03-20 22:59:59 +01:00
|
|
|
return m_readOnlyNode->readProperty(property);
|
2008-03-06 23:23:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilterConfigNode::setProperty(const string &property,
|
|
|
|
const string &value,
|
2008-03-30 23:50:51 +02:00
|
|
|
const string &comment,
|
|
|
|
const string *defValue)
|
2008-03-06 23:23:13 +01:00
|
|
|
{
|
2008-03-18 22:45:34 +01:00
|
|
|
ConfigFilter::iterator it = m_filter.find(property);
|
2008-03-06 23:23:13 +01:00
|
|
|
|
2008-03-20 22:59:59 +01:00
|
|
|
if (!m_node.get()) {
|
2009-10-05 14:49:32 +02:00
|
|
|
SyncContext::throwError(getName() + ": read-only, setting properties not allowed");
|
2008-03-20 22:59:59 +01:00
|
|
|
}
|
|
|
|
|
2008-03-06 23:23:13 +01:00
|
|
|
if (it != m_filter.end()) {
|
|
|
|
m_filter.erase(it);
|
|
|
|
}
|
2008-03-30 23:50:51 +02:00
|
|
|
m_node->setProperty(property, value, comment, defValue);
|
2008-03-06 23:23:13 +01:00
|
|
|
}
|
|
|
|
|
2009-11-21 18:10:18 +01:00
|
|
|
void FilterConfigNode::readProperties(ConfigProps &props) const
|
2008-03-06 23:23:13 +01:00
|
|
|
{
|
2008-04-10 21:54:02 +02:00
|
|
|
m_readOnlyNode->readProperties(props);
|
2008-03-06 23:23:13 +01:00
|
|
|
|
2008-07-11 22:25:02 +02:00
|
|
|
BOOST_FOREACH(const StringPair &filter, m_filter) {
|
2009-11-21 17:09:11 +01:00
|
|
|
// overwrite existing values or add new ones
|
|
|
|
props[filter.first] = filter.second;
|
2008-03-06 23:23:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilterConfigNode::removeProperty(const string &property)
|
|
|
|
{
|
2008-03-18 22:45:34 +01:00
|
|
|
ConfigFilter::iterator it = m_filter.find(property);
|
2008-03-06 23:23:13 +01:00
|
|
|
|
2008-03-20 22:59:59 +01:00
|
|
|
if (!m_node.get()) {
|
2009-10-05 14:49:32 +02:00
|
|
|
SyncContext::throwError(getName() + ": read-only, removing properties not allowed");
|
2008-03-20 22:59:59 +01:00
|
|
|
}
|
|
|
|
|
2008-03-06 23:23:13 +01:00
|
|
|
if (it != m_filter.end()) {
|
|
|
|
m_filter.erase(it);
|
|
|
|
}
|
|
|
|
m_node->removeProperty(property);
|
|
|
|
}
|
2008-03-20 22:59:59 +01:00
|
|
|
|
2009-09-29 22:23:31 +02:00
|
|
|
void FilterConfigNode::clear()
|
|
|
|
{
|
|
|
|
m_filter.clear();
|
|
|
|
m_node->clear();
|
|
|
|
}
|
|
|
|
|
2008-03-20 22:59:59 +01:00
|
|
|
void FilterConfigNode::flush()
|
|
|
|
{
|
|
|
|
if (!m_node.get()) {
|
2009-10-05 14:49:32 +02:00
|
|
|
SyncContext::throwError(getName() + ": read-only, flushing not allowed");
|
2008-03-20 22:59:59 +01:00
|
|
|
}
|
|
|
|
m_node->flush();
|
|
|
|
}
|
2008-07-11 22:25:02 +02:00
|
|
|
|
|
|
|
FilterConfigNode::ConfigFilter::operator string () const {
|
|
|
|
vector<string> res;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const StringPair &filter, *this) {
|
|
|
|
res.push_back(filter.first + " = " + filter.second);
|
|
|
|
}
|
|
|
|
sort(res.begin(), res.end());
|
|
|
|
return boost::join(res, "\n");
|
|
|
|
}
|
2009-10-02 17:23:53 +02:00
|
|
|
|
|
|
|
SE_END_CXX
|