2008-03-06 23:23:13 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2007 Funambol, Inc
|
|
|
|
* Copyright (C) 2008 Patrick Ohly
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY, TITLE, NONINFRINGEMENT 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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
* 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCL_EVOLUTION_FILTER_CONFIG_NODE
|
|
|
|
# define INCL_EVOLUTION_FILTER_CONFIG_NODE
|
|
|
|
|
|
|
|
#include <ConfigNode.h>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2008-03-30 15:41:56 +02:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2008-03-06 23:23:13 +01:00
|
|
|
|
2008-03-30 11:02:07 +02:00
|
|
|
#include "SyncEvolutionUtil.h"
|
|
|
|
|
2008-03-06 23:23:13 +01:00
|
|
|
#include <map>
|
|
|
|
#include <utility>
|
2008-03-30 11:02:07 +02:00
|
|
|
#include <vector>
|
2008-03-06 23:23:13 +01:00
|
|
|
#include <string>
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class acts as filter between a real config node and its user:
|
|
|
|
* reads which match properties which are set in the filter will
|
|
|
|
* return the value set in the filter. Writes will go to the underlying
|
|
|
|
* node and future reads will return the written value.
|
|
|
|
*
|
|
|
|
* The purpose of this class is temporarily overriding saved values
|
|
|
|
* during one run without having to modify the saved values.
|
|
|
|
*/
|
|
|
|
class FilterConfigNode : public ConfigNode {
|
|
|
|
public:
|
2008-03-30 15:41:56 +02:00
|
|
|
class ConfigFilter : public map<string, string> {
|
2008-03-18 22:45:34 +01:00
|
|
|
public:
|
|
|
|
/** add the mapping, regardless whether it exists already or not */
|
|
|
|
void set(const string &property, const string &value) {
|
2008-03-30 15:41:56 +02:00
|
|
|
pair<iterator, bool> inserted = insert(make_pair(boost::to_lower_copy(property), value));
|
2008-03-18 22:45:34 +01:00
|
|
|
if (!inserted.second) {
|
|
|
|
inserted.first->second = value;
|
|
|
|
}
|
|
|
|
}
|
2008-03-30 11:02:07 +02:00
|
|
|
|
|
|
|
operator string () const {
|
|
|
|
vector<string> res;
|
|
|
|
|
|
|
|
for (const_iterator it = begin();
|
|
|
|
it != end();
|
|
|
|
++it) {
|
|
|
|
res.push_back(it->first + " = " + it->second);
|
|
|
|
}
|
|
|
|
sort(res.begin(), res.end());
|
|
|
|
return join("\n", res.begin(), res.end());
|
|
|
|
}
|
2008-03-30 15:41:56 +02:00
|
|
|
|
|
|
|
iterator find(const string &property) {
|
|
|
|
return map<string, string>::find(boost::to_lower_copy(property));
|
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator find(const string &property) const {
|
|
|
|
return map<string, string>::find(boost::to_lower_copy(property));
|
|
|
|
}
|
2008-03-18 22:45:34 +01:00
|
|
|
};
|
2008-03-06 23:23:13 +01:00
|
|
|
|
2008-03-20 22:59:59 +01:00
|
|
|
/** read-write access to underlying node */
|
2008-03-06 23:23:13 +01:00
|
|
|
FilterConfigNode(const boost::shared_ptr<ConfigNode> &node,
|
2008-03-20 22:59:59 +01:00
|
|
|
const ConfigFilter &filter = ConfigFilter());
|
2008-03-06 23:23:13 +01:00
|
|
|
|
2008-03-20 22:59:59 +01:00
|
|
|
/** read-only access to underlying node */
|
|
|
|
FilterConfigNode(const boost::shared_ptr<const ConfigNode> &node,
|
|
|
|
const ConfigFilter &filter = ConfigFilter());
|
|
|
|
|
|
|
|
virtual string getName() const { return m_readOnlyNode->getName(); }
|
2008-03-19 12:29:14 +01:00
|
|
|
|
2008-03-06 23:23:13 +01:00
|
|
|
/** add another entry to the list of filter properties */
|
|
|
|
void addFilter(const string &property,
|
|
|
|
const string &value);
|
|
|
|
|
|
|
|
/** replace current filter list with new one */
|
2008-03-18 22:45:34 +01:00
|
|
|
void setFilter(const ConfigFilter &filter);
|
2008-03-30 11:02:07 +02:00
|
|
|
const ConfigFilter &getFilter() const { return m_filter; }
|
2008-03-06 23:23:13 +01:00
|
|
|
|
|
|
|
/* ConfigNode API */
|
2008-03-20 22:59:59 +01:00
|
|
|
virtual void flush();
|
2008-03-06 23:23:13 +01:00
|
|
|
virtual string readProperty(const string &property) const;
|
|
|
|
virtual void setProperty(const string &property,
|
|
|
|
const string &value,
|
2008-03-30 23:50:51 +02:00
|
|
|
const string &comment = "",
|
|
|
|
const string *defValue = NULL);
|
2008-03-20 22:59:59 +01:00
|
|
|
virtual map<string, string> readProperties() const;
|
2008-03-06 23:23:13 +01:00
|
|
|
virtual void removeProperty(const string &property);
|
2008-03-24 22:42:47 +01:00
|
|
|
virtual bool exists() const { return m_readOnlyNode->exists(); }
|
2008-03-06 23:23:13 +01:00
|
|
|
|
|
|
|
private:
|
2008-03-18 22:45:34 +01:00
|
|
|
ConfigFilter m_filter;
|
2008-03-06 23:23:13 +01:00
|
|
|
boost::shared_ptr<ConfigNode> m_node;
|
2008-03-20 22:59:59 +01:00
|
|
|
boost::shared_ptr<const ConfigNode> m_readOnlyNode;
|
2008-03-06 23:23:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|