Imported Upstream version 1.2.99~20120606~SE~ff65aef~SYSYNC~2728cb4
[platform/upstream/syncevolution.git] / src / syncevo / MultiplexConfigNode.cpp
1 /*
2  * Copyright (C) 2009 Intel Corporation
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/MultiplexConfigNode.h>
21 #include <boost/algorithm/string/predicate.hpp>
22
23 SE_BEGIN_CXX
24
25 FilterConfigNode *
26 MultiplexConfigNode::getNode(const std::string &property,
27                              const ConfigProperty **found) const
28 {
29     BOOST_FOREACH(const ConfigProperty *prop, m_registry) {
30         bool match = false;
31         BOOST_FOREACH(const std::string &name, prop->getNames()) {
32             if (name == property) {
33                 match = true;
34                 break;
35             }
36         }
37         if (match) {
38             if (found) {
39                 *found = prop;
40             }
41
42             FilterConfigNode *node = 
43                 m_nodes[prop->isHidden()][prop->getSharing()].get();
44
45             return node;
46         }
47     }
48
49     return NULL;
50 }
51
52 void MultiplexConfigNode::addFilter(const std::string &property,
53                                     const InitStateString &value)
54 {
55     FilterConfigNode::addFilter(property, value);
56     for (int i = 0; i < 2; i++) {
57         for (int e = 0; e < 3; e++) {
58             if (m_nodes[i][e]) {
59                 m_nodes[i][e]->addFilter(property, value);
60             }
61         }
62     }
63 }
64
65 void MultiplexConfigNode::setFilter(const ConfigFilter &filter)
66 {
67     FilterConfigNode::setFilter(filter);
68     for (int i = 0; i < 2; i++) {
69         for (int e = 0; e < 3; e++) {
70             if (m_nodes[i][e]) {
71                 m_nodes[i][e]->setFilter(filter);
72             }
73         }
74     }
75 }
76
77 void MultiplexConfigNode::flush()
78 {
79     for (int i = 0; i < 2; i++) {
80         for (int e = 0; e < 3; e++) {
81             if (m_nodes[i][e]) {
82                 m_nodes[i][e]->flush();
83             }
84         }
85     }
86 }
87
88 InitStateString MultiplexConfigNode::readProperty(const std::string &property) const
89 {
90     FilterConfigNode *node = getNode(property);
91     if (node) {
92         return node->readProperty(property);
93     } else {
94         return InitStateString();
95     }
96 }
97
98 void MultiplexConfigNode::writeProperty(const std::string &property,
99                                         const InitStateString &value,
100                                         const std::string &comment)
101 {
102     const ConfigProperty *prop;
103     FilterConfigNode *node = getNode(property, &prop);
104     if (node) {
105         node->writeProperty(property, value, comment);
106     } else {
107         SE_THROW(property + ": not supported by configuration multiplexer");
108     }
109 }
110
111 void MultiplexConfigNode::readProperties(PropsType &props) const
112 {
113     for (int i = 0; i < 2; i++) {
114         for (int e = 0; e < 3; e++) {
115             if (m_nodes[i][e]) {
116                 m_nodes[i][e]->readProperties(props);
117             }
118         }
119     }
120 }
121
122 void MultiplexConfigNode::removeProperty(const std::string &property)
123 {
124 #if 1
125     SE_THROW(property + ": removing via configuration multiplexer not supported");
126 #else
127     for (int i = 0; i < 2; i++) {
128         for (int e = 0; e < 3; e++) {
129             if (m_nodes[i][e]) {
130                 m_nodes[i][e]->removeProperty(property);
131             }
132         }
133     }
134 #endif
135 }
136
137 void MultiplexConfigNode::clear()
138 {
139 #if 1
140     SE_THROW("configuration multiplexer cannot be cleared");
141 #else
142     for (int i = 0; i < 2; i++) {
143         for (int e = 0; e < 3; e++) {
144             if (m_nodes[i][e]) {
145                 m_nodes[i][e]->clear();
146             }
147         }
148     }
149 #endif
150 }
151
152 bool MultiplexConfigNode::exists() const
153 {
154     for (int i = 0; i < 2; i++) {
155         for (int e = 0; e < 3; e++) {
156             if (m_nodes[i][e] &&
157                 m_nodes[i][e]->exists()) {
158                 return true;
159             }
160         }
161     }
162     return false;
163 }
164
165 SE_END_CXX