Imported Upstream version 0.8
[platform/upstream/syncevolution.git] / src / core / PrefixConfigNode.h
1 /*
2  * Copyright (C) 2008 Patrick Ohly
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
12  * PURPOSE.  See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17  * 02111-1307  USA
18  */
19
20 #ifndef INCL_EVOLUTION_PREFIX_CONFIG_NODE
21 # define INCL_EVOLUTION_PREFIX_CONFIG_NODE
22
23 #include <ConfigNode.h>
24 #include <boost/shared_ptr.hpp>
25
26 #include <map>
27 #include <utility>
28 #include <vector>
29 #include <string>
30 using namespace std;
31
32 /**
33  * This class acts as filter between a real config node and its user:
34  * a fixed prefix is added to each key when setting/getting a property.
35  * The list of properties only includes the key/value pairs with
36  * a matching prefix.
37  *
38  * The purpose is to have multiple users accessing the same underlying
39  * node without running into namespace conflicts.
40  */
41 class PrefixConfigNode : public ConfigNode {
42  public:
43     /** read-write access to underlying node */
44     PrefixConfigNode(const string prefix,
45                      const boost::shared_ptr<ConfigNode> &node);
46
47     /** read-only access to underlying node */
48     PrefixConfigNode(const string prefix,
49                      const boost::shared_ptr<const ConfigNode> &node);
50
51     virtual string getName() const { return m_readOnlyNode->getName(); }
52
53     /* ConfigNode API */
54     virtual void flush();
55     virtual string readProperty(const string &property) const;
56     virtual void setProperty(const string &property,
57                              const string &value,
58                              const string &comment = "",
59                              const string *defValue = NULL);
60     virtual void readProperties(map<string, string> &props) const;
61     virtual void removeProperty(const string &property);
62     virtual bool exists() const { return m_readOnlyNode->exists(); }
63
64  private:
65     string m_prefix;
66     boost::shared_ptr<ConfigNode> m_node;
67     boost::shared_ptr<const ConfigNode> m_readOnlyNode;
68 };
69
70 #endif