Imported Upstream version 0.8~alpha1
[platform/upstream/syncevolution.git] / src / 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  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
11  * PURPOSE.  See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
16  * 02111-1307  USA
17  */
18
19 #include "PrefixConfigNode.h"
20 #include "EvolutionSyncClient.h"
21
22 #include <boost/foreach.hpp>
23 #include <boost/algorithm/string/predicate.hpp>
24
25 PrefixConfigNode::PrefixConfigNode(const string prefix,
26                                    const boost::shared_ptr<ConfigNode> &node) :
27     m_prefix(prefix),
28     m_node(node),
29     m_readOnlyNode(node)
30 {
31 }
32
33 PrefixConfigNode::PrefixConfigNode(const string prefix,
34                                    const boost::shared_ptr<const ConfigNode> &node) :
35     m_prefix(prefix),
36     m_readOnlyNode(node)
37 {
38 }
39
40 string PrefixConfigNode::readProperty(const string &property) const
41 {
42     return m_readOnlyNode->readProperty(m_prefix + property);
43 }
44
45 void PrefixConfigNode::setProperty(const string &property,
46                                  const string &value,
47                                  const string &comment,
48                                  const string *defValue)
49 {
50     m_node->setProperty(m_prefix + property,
51                         value,
52                         comment,
53                         defValue);
54 }
55
56 void PrefixConfigNode::readProperties(map<string, string> &props) const
57 {
58     map<string, string> original;
59     m_readOnlyNode->readProperties(original);
60
61     for(map<string, string>::iterator it = original.begin();
62         it != original.end();
63         ++it) {
64         string key = it->first;
65         string value = it->second;
66
67         if (boost::starts_with(key, m_prefix)) {
68             props.insert(make_pair(key.substr(m_prefix.size()), value));
69         }
70     }
71 }
72
73 void PrefixConfigNode::removeProperty(const string &property)
74 {
75     m_node->removeProperty(m_prefix + property);
76 }
77
78 void PrefixConfigNode::flush()
79 {
80     if (!m_node.get()) {
81         EvolutionSyncClient::throwError(getName() + ": read-only, flushing not allowed");
82     }
83     m_node->flush();
84 }