Imported Upstream version 1.3.99.4
[platform/upstream/syncevolution.git] / src / syncevo / FilterConfigNode.h
1 /*
2  * Copyright (C) 2008-2009 Patrick Ohly <patrick.ohly@gmx.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) version 3.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301  USA
18  */
19
20 #ifndef INCL_EVOLUTION_FILTER_CONFIG_NODE
21 # define INCL_EVOLUTION_FILTER_CONFIG_NODE
22
23 #include <syncevo/ConfigNode.h>
24 #include <boost/shared_ptr.hpp>
25 #include <boost/algorithm/string/case_conv.hpp>
26 #include <boost/algorithm/string.hpp>
27
28 #include <map>
29 #include <utility>
30 #include <vector>
31 #include <string>
32
33 #include <syncevo/declarations.h>
34 SE_BEGIN_CXX
35
36 /**
37  * This class acts as filter between a real config node and its user:
38  * reads which match properties which are set in the filter will
39  * return the value set in the filter. Writes will go to the underlying
40  * node and future reads will return the written value.
41  *
42  * The purpose of this class is temporarily overriding saved values
43  * during one run without having to modify the saved values.
44  */
45 class FilterConfigNode : public ConfigNode {
46  public:
47     /** config filters are the same case-insensitive string to string mapping as property sets */
48     typedef ConfigProps ConfigFilter;
49
50     /** read-write access to underlying node */
51     FilterConfigNode(const boost::shared_ptr<ConfigNode> &node,
52                      const ConfigFilter &filter = ConfigFilter());
53
54     /** read-only access to underlying node */
55     FilterConfigNode(const boost::shared_ptr<const ConfigNode> &node,
56                      const ConfigFilter &filter = ConfigFilter());
57
58     virtual std::string getName() const { return m_readOnlyNode->getName(); }
59     virtual bool isVolatile() const { return m_readOnlyNode->isVolatile(); }
60
61     /** add another entry to the list of filter properties */
62     virtual void addFilter(const std::string &property,
63                            const InitStateString &value);
64
65     /** replace current filter list with new one */
66     virtual void setFilter(const ConfigFilter &filter);
67     virtual const ConfigFilter &getFilter() const { return m_filter; }
68
69     /* ConfigNode API */
70     virtual void flush();
71     virtual InitStateString readProperty(const std::string &property) const;
72     virtual void writeProperty(const std::string &property,
73                                const InitStateString &value,
74                                const std::string &comment = "");
75     virtual void readProperties(ConfigProps &props) const;
76     virtual void removeProperty(const std::string &property);
77     virtual bool exists() const { return m_readOnlyNode->exists(); }
78     virtual bool isReadOnly() const { return !m_node || m_readOnlyNode->isReadOnly(); }
79     virtual void clear();
80
81  private:
82     ConfigFilter m_filter;
83     boost::shared_ptr<ConfigNode> m_node;
84     boost::shared_ptr<const ConfigNode> m_readOnlyNode;
85 };
86
87
88 SE_END_CXX
89 #endif