~ubuntu-branches/ubuntu/trusty/syncevolution/trusty-proposed

« back to all changes in this revision

Viewing changes to src/syncevo/ConfigFilter.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Tino Keitel
  • Date: 2011-07-20 16:02:02 UTC
  • mfrom: (3.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110720160202-e8uf7ogw4vh0q0f3
Tags: 1.1.99.5a-1
* New upstream version 1.1.99.5a, first release candiate for 1.2
* Added python-openssl dependency, the HTTP server needs it for HTTPS support
* Added versioned dependency on libsynthesis0 to get required features
* Fixed .orig.tar.gz generation in get-orig-source target
* Added myself to Uploaders:, thanks to David for sponsoring
* Use updated upstream tag for source package generation
* Removed 0001-Replace-with-in-call-to-PKG_CHECK_MODULES.patch, fixed upstream
* Renamed NEWS.Debian to NEWS so that it is actually used
* Updated NEWS for 1.1.99.5a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2008-2009 Patrick Ohly <patrick.ohly@gmx.de>
 
3
 * Copyright (C) 2009 Intel Corporation
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 * 02110-1301  USA
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <syncevo/ConfigFilter.h>
 
24
#include <syncevo/SyncConfig.h>
 
25
 
 
26
SE_BEGIN_CXX
 
27
 
 
28
void ConfigProps::add(const ConfigProps &other)
 
29
{
 
30
    BOOST_FOREACH(const StringPair &entry, other) {
 
31
        std::pair<iterator, bool> res = insert(entry);
 
32
        if (!res.second) {
 
33
            res.first->second = entry.second;
 
34
        }
 
35
    }
 
36
}
 
37
 
 
38
string ConfigProps::get(const string &key, const string &def) const
 
39
{
 
40
    const_iterator it = find(key);
 
41
    if (it == end()) {
 
42
        return def;
 
43
    } else {
 
44
        return it->second;
 
45
    }
 
46
}
 
47
 
 
48
ConfigProps SourceProps::createSourceFilter(const std::string &source) const
 
49
{
 
50
    const_iterator it = find("");
 
51
    ConfigProps filter;
 
52
    if (it != end()) {
 
53
        filter = it->second;
 
54
    }
 
55
    if (!source.empty()) {
 
56
        it = find(source);
 
57
        if (it != end()) {
 
58
            filter.add(it->second);
 
59
        }
 
60
    }
 
61
    return filter;
 
62
}
 
63
 
 
64
ConfigProps FullProps::createSyncFilter(const std::string &config) const
 
65
{
 
66
    const_iterator it = find("");
 
67
    ConfigProps filter;
 
68
    if (it != end()) {
 
69
        // first unset context
 
70
        filter = it->second.m_syncProps;
 
71
    }
 
72
 
 
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);
 
79
        if (it != end()) {
 
80
            filter.add(it->second.m_syncProps);
 
81
        }
 
82
        // finally peer config, if we have one
 
83
        if (!peer.empty()) {
 
84
            it = find(normal);
 
85
            if (it != end()) {
 
86
                filter.add(it->second.m_syncProps);
 
87
            }
 
88
        }
 
89
    }
 
90
    return filter;
 
91
}
 
92
 
 
93
ConfigProps FullProps::createSourceFilter(const std::string &config,
 
94
                                          const std::string &source) const
 
95
{
 
96
    const_iterator it = find("");
 
97
    ConfigProps filter;
 
98
    if (it != end()) {
 
99
        // first unset context
 
100
        filter = it->second.m_sourceProps.createSourceFilter(source);
 
101
    }
 
102
 
 
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);
 
109
        if (it != end()) {
 
110
            filter.add(it->second.m_sourceProps.createSourceFilter(source));
 
111
        }
 
112
        // finally peer config, if we have one
 
113
        if (!peer.empty()) {
 
114
            it = find(normal);
 
115
            if (it != end()) {
 
116
                filter.add(it->second.m_sourceProps.createSourceFilter(source));
 
117
            }
 
118
        }
 
119
    }
 
120
    return filter;
 
121
}
 
122
 
 
123
bool FullProps::hasProperties() const
 
124
{
 
125
    BOOST_FOREACH(const value_type &context, *this) {
 
126
        if (!context.second.m_syncProps.empty()) {
 
127
            return true;
 
128
        }
 
129
        BOOST_FOREACH(const SourceProps::value_type &source, context.second.m_sourceProps) {
 
130
            if (!source.second.empty()) {
 
131
                return true;
 
132
            }
 
133
        }
 
134
    }
 
135
 
 
136
    return false;
 
137
}
 
138
 
 
139
void FullProps::createFilters(const string &context,
 
140
                              const string &config,
 
141
                              const set<string> *sources,
 
142
                              ConfigProps &syncFilter,
 
143
                              SourceProps &sourceFilters)
 
144
{
 
145
    boost::shared_ptr<SyncConfig> shared;
 
146
 
 
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);
 
153
    }
 
154
 
 
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, ""));
 
160
    }
 
161
    if (!config.empty()) {
 
162
        syncFilter.add(createSyncFilter(config));
 
163
        sourceFilters[""].add(createSourceFilter(config, ""));
 
164
    }
 
165
 
 
166
    // build full set of all sources
 
167
    set<string> allSources;
 
168
    if (sources) {
 
169
        allSources = *sources;
 
170
    }
 
171
    if (shared) {
 
172
        std::list<std::string> tmp = shared->getSyncSources();
 
173
        allSources.insert(tmp.begin(), tmp.end());
 
174
    }
 
175
    if (!config.empty()) {
 
176
        std::list<std::string> tmp = SyncConfig(config).getSyncSources();
 
177
        allSources.insert(tmp.begin(), tmp.end());
 
178
    }
 
179
 
 
180
    // explicit filter for all known sources
 
181
    BOOST_FOREACH(std::string source, allSources) {
 
182
        ConfigProps &props = sourceFilters[source];
 
183
        if (shared) {
 
184
            // combine existing properties from context and command line
 
185
            // filter
 
186
            SyncSourceNodes nodes = shared->getSyncSourceNodes(source, "");
 
187
            nodes.getProperties()->readProperties(props);
 
188
 
 
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
 
192
            // the other peers.
 
193
            props.erase("type");
 
194
 
 
195
            props.add(createSourceFilter(context, source));
 
196
        }
 
197
        if (!config.empty()) {
 
198
            props.add(createSourceFilter(config, source));
 
199
        }
 
200
    }
 
201
}
 
202
 
 
203
 
 
204
 
 
205
SE_END_CXX