Imported Upstream version 1.2.99~20120606~SE~ff65aef~SYSYNC~2728cb4
[platform/upstream/syncevolution.git] / src / syncevo / PrefixConfigNode.cpp
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 #include <syncevo/PrefixConfigNode.h>
21 #include <syncevo/SyncContext.h>
22
23 #include <boost/foreach.hpp>
24 #include <boost/algorithm/string/predicate.hpp>
25
26 #include <syncevo/declarations.h>
27 SE_BEGIN_CXX
28
29 PrefixConfigNode::PrefixConfigNode(const string prefix,
30                                    const boost::shared_ptr<ConfigNode> &node) :
31     m_prefix(prefix),
32     m_node(node),
33     m_readOnlyNode(node)
34 {
35 }
36
37 PrefixConfigNode::PrefixConfigNode(const string prefix,
38                                    const boost::shared_ptr<const ConfigNode> &node) :
39     m_prefix(prefix),
40     m_readOnlyNode(node)
41 {
42 }
43
44 InitStateString PrefixConfigNode::readProperty(const string &property) const
45 {
46     return m_readOnlyNode->readProperty(m_prefix + property);
47 }
48
49 void PrefixConfigNode::writeProperty(const string &property,
50                                      const InitStateString &value,
51                                      const string &comment)
52 {
53     m_node->writeProperty(m_prefix + property,
54                           value,
55                           comment);
56 }
57
58 void PrefixConfigNode::readProperties(ConfigProps &props) const
59 {
60     ConfigProps original;
61     m_readOnlyNode->readProperties(original);
62
63     BOOST_FOREACH(const StringPair &prop, original) {
64         string key = prop.first;
65         string value = prop.second;
66
67         if (boost::starts_with(key, m_prefix)) {
68             props[key.substr(m_prefix.size())] = value;
69         }
70     }
71 }
72
73 void PrefixConfigNode::clear()
74 {
75     ConfigProps original;
76     m_readOnlyNode->readProperties(original);
77     BOOST_FOREACH(const StringPair &prop, original) {
78         string key = prop.first;
79         if (boost::starts_with(key, m_prefix)) {
80             m_node->removeProperty(key);
81         }
82     }
83 }
84
85 void PrefixConfigNode::removeProperty(const string &property)
86 {
87     m_node->removeProperty(m_prefix + property);
88 }
89
90 void PrefixConfigNode::flush()
91 {
92     if (!m_node.get()) {
93         SyncContext::throwError(getName() + ": read-only, flushing not allowed");
94     }
95     m_node->flush();
96 }
97
98 SE_END_CXX