summaryrefslogtreecommitdiff
path: root/src/dbus/server/pim/folks.h
blob: 59cf6ace0d2c7774a4b9f7b9f4882c61fa024531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * Copyright (C) 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

/**
 * The classes in this file implement sorting, searching and
 * configuring of the unified address book based on libfolks.
 *
 * This is pure C++ code. The D-Bus IPC binding for it is
 * implemented separately in pim-manager.h/cpp.
 */

#ifndef INCL_SYNCEVO_DBUS_SERVER_IVI_FOLKS
#define INCL_SYNCEVO_DBUS_SERVER_IVI_FOLKS

#include <folks/folks.h>

#include "locale-factory.h"
#include "../dbus-callbacks.h"
#include "../timeout.h"

#include <syncevo/GLibSupport.h>
#include <syncevo/GeeSupport.h>
#include <syncevo/GValueSupport.h>

#include <boost/shared_ptr.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/signals2.hpp>

SE_GOBJECT_TYPE(FolksIndividualAggregator)
SE_GOBJECT_TYPE(FolksIndividual)
SE_GOBJECT_TYPE(FolksEmailFieldDetails)
SE_GOBJECT_TYPE(FolksBackendStore)
SE_GOBJECT_TYPE(FolksBackend)
SE_GOBJECT_TYPE(FolksPersonaStore)
SE_GOBJECT_TYPE(FolksAbstractFieldDetails)
SE_GOBJECT_TYPE(FolksRoleFieldDetails)
SE_GOBJECT_TYPE(FolksRole)
SE_GOBJECT_TYPE(FolksPostalAddress)
SE_GOBJECT_TYPE(FolksNoteFieldDetails)
SE_GOBJECT_TYPE(FolksPostalAddressFieldDetails)
SE_GOBJECT_TYPE(FolksPersona)
SE_GOBJECT_TYPE(GeeCollection)
SE_GOBJECT_TYPE(GeeHashSet)
SE_GLIB_TYPE(GHashTable, g_hash_table)

#include <syncevo/declarations.h>
SE_BEGIN_CXX

class PersonaDetails;

/**
 * Abstract interface for comparing two FolksIndividual instances.
 * The properties of a folks individual may change at any time.
 * Therefore the key properties which determine the sort order
 * must be copied from the individual. They will be updated when
 * the individual changes.
 *
 * The other advantage is that complex, derived keys only need
 * to be computed once instead of each time during a comparison.
 * For example, boost::locale::collator::transform() can be used
 * to generate the keys.
 */
class IndividualCompare
{
 public:
    static boost::shared_ptr<IndividualCompare> defaultCompare();

    virtual ~IndividualCompare() {}

    typedef std::vector<std::string> Criteria_t;

    /**
     * Calculate an ordered set of criteria for comparing
     * individuals. The default comparison will start with the initial
     * criteria and move on until a difference is found.
     *
     * This is necessary because a single string of "Doe, John" is
     * treated differently in a collation than the pair of strings
     * "Doe" and "John".
     *
     * @param individual    the individual for which sort keys are to be created
     * @retval criteria     cleared before the call, filled afterwards
     */
    virtual void createCriteria(FolksIndividual *individual, Criteria_t &criteria) const = 0;

    /**
     * Partial sort order: true if a smaller than b.
     *
     * Default implementation uses normal std::string::compare().
     */
    virtual bool compare(const Criteria_t &a, const Criteria_t &b) const;
};

/**
 * A FolksIndividual plus its sort criteria and search cache.
 */
struct IndividualData
{
    /**
     * Sets all members to match the given individual, using the
     * compare instance to compute values. Both compare and locale may
     * be NULL.
     */
    void init(const IndividualCompare *compare,
              const LocaleFactory *locale,
              FolksIndividual *individual);

    FolksIndividualCXX m_individual;
    IndividualCompare::Criteria_t m_criteria;
    LocaleFactory::Precomputed m_precomputed;
};

/**
 * wraps an IndividualCompare for std algorithms on IndividualData
 */
class IndividualDataCompare : public std::binary_function<IndividualData, IndividualData, bool>
{
    boost::shared_ptr<IndividualCompare> m_compare;

 public:
    IndividualDataCompare(const boost::shared_ptr<IndividualCompare> &compare) :
       m_compare(compare)
    {}
    IndividualDataCompare(const IndividualDataCompare &other) :
       m_compare(other.m_compare)
    {}

    bool operator () (const IndividualData &a, const IndividualData &b) const
    {
        return m_compare->compare(a.m_criteria, b.m_criteria);
    }
};

/**
 * Abstract interface for filtering (aka searching) FolksIndividual
 * instances.
 */
class IndividualFilter
{
    int m_maxResults;

 public:
    IndividualFilter() : m_maxResults(-1) {}
    virtual ~IndividualFilter() {}

    /** Maximum number of results. -1 for unlimited. */
    int getMaxResults() const { return m_maxResults; }
    void setMaxResults(int maxResults) { m_maxResults = maxResults; }

    /**
     * True if within the number of expected results.
     */
    bool isIncluded(size_t index) const { return m_maxResults == -1 || index < (size_t)m_maxResults; }

    /** true if the contact matches the filter */
    virtual bool matches(const IndividualData &data) const = 0;
};

/**
 * A filter which just enforces a maximum number of results,
 * something that FullView cannot do.
 */
class MatchAll : public IndividualFilter
{
 public:
    virtual bool matches(const IndividualData &data) const { return true; }
};

class FullView;

/**
 * Is a normal FolksIndividualAggregator and adds sorting, searching
 * and browsing to it. At least the full sorted view always exists.
 */
class IndividualAggregator
{
    /** empty when not started yet */
    boost::shared_ptr<FullView> m_view;
    boost::shared_ptr<IndividualCompare> m_compare;
    boost::shared_ptr<LocaleFactory> m_locale;
    boost::weak_ptr<IndividualAggregator> m_self;
    FolksIndividualAggregatorCXX m_folks;
    FolksBackendStoreCXX m_backendStore;
    /**
     * NULL when backends haven't been loaded yet.
     * Set by backendsLoaded().
     */
    FolksBackendCXX m_eds;
    /**
     * Set by backendsLoaded(), if possible. If m_eds != NULL
     * and m_systemStore == NULL, no system address book is
     * available. If m_eds == NULL, hook into m_backendsLoadedSignal
     * to be notified.
     */
    FolksPersonaStoreCXX m_systemStore;

    boost::signals2::signal<void()> m_backendsLoadedSignal;

    /**
     * The set of enabled EDS databases, referenced by the UUID.
     * Empty by default.
     */
    GeeHashSetCXX m_databases;
    /** string representation for debugging */
    std::string dumpDatabases();

    IndividualAggregator(const boost::shared_ptr<LocaleFactory> &locale);
    void init(boost::shared_ptr<IndividualAggregator> &self);

    /**
     * Called when all Folks backends are loaded, before the
     * aggregator does its work. Now is the right time to initialize
     * the set of databases and prepare the aggregator, if start() was
     * already called.
     */
    void backendsLoaded();

    /**
     * Executes the given operation when the EDS system address book
     * is prepared. The operation may throw an exception, which (like
     * all other errors) is reported as failure for the asynchronous
     * operation.
     */
    void runWithAddressBook(const boost::function<void ()> &operation,
                            const ErrorCb_t &onError) throw();
    void runWithAddressBookHaveEDS(const boost::signals2::connection &conn,
                                   const boost::function<void ()> &operation,
                                   const ErrorCb_t &onError) throw();
    void runWithAddressBookPrepared(const GError *gerror,
                                    const boost::function<void ()> &operation,
                                    const ErrorCb_t &onError) throw();

    /**
     * Executes the given operation after looking up the FolksPersona
     * in the system address book, which must be prepared and loaded
     * at that point.
     */
    void runWithPersona(const boost::function<void (FolksPersona *)> &operation,
                        const std::string &localID,
                        const ErrorCb_t &onError) throw();
    void doRunWithPersona(const boost::function<void (FolksPersona *)> &operation,
                          const std::string &localID,
                          const ErrorCb_t &onError) throw();

    /** the operation for runWithAddressBook() */
    void doAddContact(const Result<void (const std::string &)> &result,
                      const PersonaDetails &details);
    /** handle result of adding contact */
    void addContactDone(const GError *gerror,
                        FolksPersona *persona,
                        const Result<void (const std::string &)> &result) throw();

   void doModifyContact(const Result<void ()> &result,
                        FolksPersona *persona,
                        const PersonaDetails &details) throw();
   void doRemoveContact(const Result<void ()> &result,
                        FolksPersona *persona) throw();
   void removeContactDone(const GError *gerror,
                          const Result<void ()> &result) throw();

 public:
    /**
     * Creates an idle IndividualAggregator. Configure it and
     * subscribe to signals, then call start().
     */
    static boost::shared_ptr<IndividualAggregator> create(const boost::shared_ptr<LocaleFactory> &locale);

    /**
     * Access to FolksIndividualAggregator which is owned by
     * the aggregator.
     */
    FolksIndividualAggregator *getFolks() const { return m_folks.get(); }

    /**
     * Set sorting without starting the view just yet.
     */
    void setCompare(const boost::shared_ptr<IndividualCompare> &compare);

    /**
     * Starts pulling and sorting of contacts.
     * Creates m_view and starts populating it.
     * Can be called multiple times.
     *
     * See also org.01.pim.contacts.Manager.Start().
     */
    void start();

    /**
     * start() was called, or something caused it to be called.
     */
    bool isRunning() const;

    /**
     * configure active databases
     *
     * @param set of EDS database UUIDs, empty string for the default
     * system address book
     */
    void setDatabases(std::set<std::string> &databases);

    /**
     * Each aggregator has exactly one full view on the data. This
     * method grants access to it and its change signals.
     *
     * @return never empty, start() will be called if necessary
     */
    boost::shared_ptr<FullView> getMainView();

    /**
     * Add contact to system address book. Returns new local ID
     * as result.
     */
   void addContact(const Result<void (const std::string &)> &result,
                   const PersonaDetails &details);

   /**
    * Modify contact in system address book.
    */
   void modifyContact(const Result<void ()> &result,
                      const std::string &localID,
                      const PersonaDetails &details);

   /**
    * Remove contact in system address book.
    */
   void removeContact(const Result<void ()> &result,
                      const std::string &localID);
};


SE_END_CXX

#endif // INCL_SYNCEVO_DBUS_SERVER_IVI_FOLKS