2
* Copyright (C) 2008-2009 Patrick Ohly <patrick.ohly@gmx.de>
3
* Copyright (C) 2009 Intel Corporation
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) version 3.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23
#include <syncevo/ConfigFilter.h>
24
#include <syncevo/SyncConfig.h>
28
void ConfigProps::add(const ConfigProps &other)
30
BOOST_FOREACH(const StringPair &entry, other) {
31
std::pair<iterator, bool> res = insert(entry);
33
res.first->second = entry.second;
38
string ConfigProps::get(const string &key, const string &def) const
40
const_iterator it = find(key);
48
ConfigProps SourceProps::createSourceFilter(const std::string &source) const
50
const_iterator it = find("");
55
if (!source.empty()) {
58
filter.add(it->second);
64
ConfigProps FullProps::createSyncFilter(const std::string &config) const
66
const_iterator it = find("");
69
// first unset context
70
filter = it->second.m_syncProps;
73
if (!config.empty()) {
74
std::string normal = SyncConfig::normalizeConfigString(config, SyncConfig::NORMALIZE_LONG_FORMAT);
75
std::string peer, context;
76
SyncConfig::splitConfigString(normal, peer, context);
77
// then overwrite with context config
78
it = find(std::string("@") + context);
80
filter.add(it->second.m_syncProps);
82
// finally peer config, if we have one
86
filter.add(it->second.m_syncProps);
93
ConfigProps FullProps::createSourceFilter(const std::string &config,
94
const std::string &source) const
96
const_iterator it = find("");
99
// first unset context
100
filter = it->second.m_sourceProps.createSourceFilter(source);
103
if (!config.empty()) {
104
std::string normal = SyncConfig::normalizeConfigString(config, SyncConfig::NORMALIZE_LONG_FORMAT);
105
std::string peer, context;
106
SyncConfig::splitConfigString(normal, peer, context);
107
// then overwrite with context config
108
it = find(std::string("@") + context);
110
filter.add(it->second.m_sourceProps.createSourceFilter(source));
112
// finally peer config, if we have one
116
filter.add(it->second.m_sourceProps.createSourceFilter(source));
123
bool FullProps::hasProperties() const
125
BOOST_FOREACH(const value_type &context, *this) {
126
if (!context.second.m_syncProps.empty()) {
129
BOOST_FOREACH(const SourceProps::value_type &source, context.second.m_sourceProps) {
130
if (!source.second.empty()) {
139
void FullProps::createFilters(const string &context,
140
const string &config,
141
const set<string> *sources,
142
ConfigProps &syncFilter,
143
SourceProps &sourceFilters)
145
boost::shared_ptr<SyncConfig> shared;
147
if (!context.empty()) {
148
// Read from context. If it does not exist, we simply set no properties
149
// as filter. Previously there was a check for existance, but that was
150
// flawed because it ignored the global property "defaultPeer".
151
shared.reset(new SyncConfig(context));
152
shared->getProperties()->readProperties(syncFilter);
155
// add command line filters for context or config?
156
if (!context.empty()) {
157
syncFilter.add(createSyncFilter(context));
158
// default for (so far) unknown sources which might be created
159
sourceFilters[""].add(createSourceFilter(context, ""));
161
if (!config.empty()) {
162
syncFilter.add(createSyncFilter(config));
163
sourceFilters[""].add(createSourceFilter(config, ""));
166
// build full set of all sources
167
set<string> allSources;
169
allSources = *sources;
172
std::list<std::string> tmp = shared->getSyncSources();
173
allSources.insert(tmp.begin(), tmp.end());
175
if (!config.empty()) {
176
std::list<std::string> tmp = SyncConfig(config).getSyncSources();
177
allSources.insert(tmp.begin(), tmp.end());
180
// explicit filter for all known sources
181
BOOST_FOREACH(std::string source, allSources) {
182
ConfigProps &props = sourceFilters[source];
184
// combine existing properties from context and command line
186
SyncSourceNodes nodes = shared->getSyncSourceNodes(source, "");
187
nodes.getProperties()->readProperties(props);
189
// Special case "type" property: the value in the context
190
// is not preserved. Every new peer must ensure that
191
// its own value is compatible (= same backend) with
195
props.add(createSourceFilter(context, source));
197
if (!config.empty()) {
198
props.add(createSourceFilter(config, source));