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