Imported Upstream version 0.8
[platform/upstream/syncevolution.git] / src / core / PrefixConfigNode.cpp
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 #include "PrefixConfigNode.h"
21 #include "EvolutionSyncClient.h"
22
23 #include <boost/foreach.hpp>
24 #include <boost/algorithm/string/predicate.hpp>
25
26 PrefixConfigNode::PrefixConfigNode(const string prefix,
27                                    const boost::shared_ptr<ConfigNode> &node) :
28     m_prefix(prefix),
29     m_node(node),
30     m_readOnlyNode(node)
31 {
32 }
33
34 PrefixConfigNode::PrefixConfigNode(const string prefix,
35                                    const boost::shared_ptr<const ConfigNode> &node) :
36     m_prefix(prefix),
37     m_readOnlyNode(node)
38 {
39 }
40
41 string PrefixConfigNode::readProperty(const string &property) const
42 {
43     return m_readOnlyNode->readProperty(m_prefix + property);
44 }
45
46 void PrefixConfigNode::setProperty(const string &property,
47                                  const string &value,
48                                  const string &comment,
49                                  const string *defValue)
50 {
51     m_node->setProperty(m_prefix + property,
52                         value,
53                         comment,
54                         defValue);
55 }
56
57 void PrefixConfigNode::readProperties(map<string, string> &props) const
58 {
59     map<string, string> original;
60     m_readOnlyNode->readProperties(original);
61
62     BOOST_FOREACH(const StringPair &prop, original) {
63         string key = prop.first;
64         string value = prop.second;
65
66         if (boost::starts_with(key, m_prefix)) {
67             props[key.substr(m_prefix.size())] = value;
68         }
69     }
70 }
71
72 void PrefixConfigNode::removeProperty(const string &property)
73 {
74     m_node->removeProperty(m_prefix + property);
75 }
76
77 void PrefixConfigNode::flush()
78 {
79     if (!m_node.get()) {
80         EvolutionSyncClient::throwError(getName() + ": read-only, flushing not allowed");
81     }
82     m_node->flush();
83 }