summaryrefslogtreecommitdiff
path: root/src/dbus/server/pim/folks.cpp
blob: 7997b2db536ab10388d7926964729d924c5f3c2f (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*
 * 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
 */

#include <config.h>
#include "folks.h"
#include "full-view.h"
#include "filtered-view.h"
#include "individual-traits.h"
#include "persona-details.h"
#include <boost/bind.hpp>
#include "test.h"
#include <syncevo/BoostHelper.h>
#include <syncevo/LogRedirect.h>

#include <boost/ptr_container/ptr_vector.hpp>

#include <syncevo/declarations.h>
SE_BEGIN_CXX

/**
 * Generic error callback. There really isn't much that can be done if
 * libfolks fails, except for logging the problem.
 */
static void logResult(const GError *gerror, const char *operation)
{
    if (gerror) {
        SE_LOG_ERROR(NULL, "%s: %s", operation, gerror->message);
    } else {
        SE_LOG_DEBUG(NULL, "%s: done", operation);
    }
}

class CompareFormattedName : public IndividualCompare {
    bool m_reversed;
    bool m_firstLast;

public:
    CompareFormattedName(bool reversed = false, bool firstLast = false) :
        m_reversed(reversed),
        m_firstLast(firstLast)
    {
    }

    virtual void createCriteria(FolksIndividual *individual, Criteria_t &criteria) const {
        FolksStructuredName *fn =
            folks_name_details_get_structured_name(FOLKS_NAME_DETAILS(individual));
        if (fn) {
            const char *family = folks_structured_name_get_family_name(fn);
            const char *given = folks_structured_name_get_given_name(fn);
            SE_LOG_DEBUG(NULL, "criteria: formatted name: %s, %s",
                         family, given);
            if (m_firstLast) {
                criteria.push_back(given ? given : "");
                criteria.push_back(family ? family : "");
            } else {
                criteria.push_back(family ? family : "");
                criteria.push_back(given ? given : "");
            }
        } else {
            SE_LOG_DEBUG(NULL, "criteria: no formatted");
        }
    }

    virtual bool compare(const Criteria_t &a, const Criteria_t &b) const {
        return m_reversed ?
            IndividualCompare::compare(b, a) :
            IndividualCompare::compare(a, b);
    }
};

boost::shared_ptr<IndividualCompare> IndividualCompare::defaultCompare()
{
    boost::shared_ptr<IndividualCompare> compare(new CompareFormattedName);
    return compare;
}

void IndividualData::init(const IndividualCompare *compare,
                          const LocaleFactory *locale,
                          FolksIndividual *individual)
{
    m_individual = individual;
    if (compare) {
        m_criteria.clear();
        compare->createCriteria(individual, m_criteria);
    }
    if (locale) {
        locale->precompute(individual, m_precomputed);
    }
}

bool IndividualCompare::compare(const Criteria_t &a, const Criteria_t &b) const
{
    Criteria_t::const_iterator ita = a.begin(),
        itb = b.begin();

    while (itb != b.end()) {
        if (ita == a.end()) {
            // a is shorter
            return true;
        }
        int cmp = ita->compare(*itb);
        if (cmp < 0) {
            // String comparison shows that a is less than b.
            return true;
        } else if (cmp > 0) {
            // Is greater, so definitely not less => don't compare
            // rest of the criteria.
            return false;
        } else {
            // Equal, continue comparing.
            ++ita;
            ++itb;
        }
    }

    // a is not less b
    return false;
}

IndividualAggregator::IndividualAggregator(const boost::shared_ptr<LocaleFactory> &locale) :
    m_locale(locale),
    m_databases(gee_hash_set_new(G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, g_free, NULL, NULL, NULL, NULL, NULL, NULL), false)
{
}

void IndividualAggregator::init(boost::shared_ptr<IndividualAggregator> &self)
{
    m_self = self;
    m_backendStore =
        FolksBackendStoreCXX::steal(folks_backend_store_dup());

    // Ignore some known harmless messages from folks.
    LogRedirect::addIgnoreError("Error preparing Backend 'ofono'");
    LogRedirect::addIgnoreError("Error preparing Backend 'telepathy'");

    SYNCEVO_GLIB_CALL_ASYNC(folks_backend_store_prepare,
                            boost::bind(&IndividualAggregator::storePrepared,
                                        m_self),
                            m_backendStore);

    m_folks =
        FolksIndividualAggregatorCXX::steal(folks_individual_aggregator_new_with_backend_store(m_backendStore));
}

boost::shared_ptr<IndividualAggregator> IndividualAggregator::create(const boost::shared_ptr<LocaleFactory> &locale)
{
    boost::shared_ptr<IndividualAggregator> aggregator(new IndividualAggregator(locale));
    aggregator->init(aggregator);
    return aggregator;
}

std::string IndividualAggregator::dumpDatabases()
{
    std::string res;

    BOOST_FOREACH (const gchar *tmp, GeeStringCollection(GEE_COLLECTION(m_databases.get()))) {
        if (!res.empty()) {
            res += ", ";
        }
        res += tmp;
    }
    return res;
}

void IndividualAggregator::storePrepared()
{
    SE_LOG_DEBUG(NULL, "backend store is prepared");

    // Have to hard-code the list of known backends that we don't want.
    SYNCEVO_GLIB_CALL_ASYNC(folks_backend_store_disable_backend,
                            boost::bind(logResult, (const GError *)NULL,
                                        "folks_backend_store_disable_backend"),
                            m_backendStore, "telepathy");
    SYNCEVO_GLIB_CALL_ASYNC(folks_backend_store_disable_backend,
                            boost::bind(logResult, (const GError *)NULL,
                                        "folks_backend_store_disable_backend"),
                            m_backendStore, "tracker");
    SYNCEVO_GLIB_CALL_ASYNC(folks_backend_store_disable_backend,
                            boost::bind(logResult, (const GError *)NULL,
                                        "folks_backend_store_disable_backend"),
                            m_backendStore, "key-file");
    SYNCEVO_GLIB_CALL_ASYNC(folks_backend_store_disable_backend,
                            boost::bind(logResult, (const GError *)NULL,
                                        "folks_backend_store_disable_backend"),
                            m_backendStore, "libsocialweb");
    // Explicitly enable EDS, just to be sure.
    SYNCEVO_GLIB_CALL_ASYNC(folks_backend_store_enable_backend,
                            boost::bind(logResult, (const GError *)NULL,
                                        "folks_backend_store_enable_backend"),
                            m_backendStore, "eds");

    // Start loading backends right away. Assumes that the
    // asynchronous operations above will be done first.
    SYNCEVO_GLIB_CALL_ASYNC(folks_backend_store_load_backends,
                            boost::bind(&IndividualAggregator::backendsLoaded, m_self),
                            m_backendStore);
}

void IndividualAggregator::backendsLoaded()
{
    SE_LOG_DEBUG(NULL, "backend store has loaded backends");
    GeeCollectionCXX coll(folks_backend_store_list_backends(m_backendStore));
    BOOST_FOREACH (FolksBackend *backend, GeeCollCXX<FolksBackend *>(coll.get())) {
        SE_LOG_DEBUG(NULL, "folks backend: %s", folks_backend_get_name(backend));
    }
    m_eds =
        FolksBackendCXX::steal(folks_backend_store_dup_backend_by_name(m_backendStore, "eds"));
    if (m_eds) {
        // Remember system store, for writing contacts.
        GeeMap *stores = folks_backend_get_persona_stores(m_eds);
        FolksPersonaStore *systemStore = static_cast<FolksPersonaStore *>(gee_map_get(stores, "system-address-book"));
        m_systemStore = systemStore;

        // Tell the backend which databases we want.
        SE_LOG_DEBUG(NULL, "backends loaded: setting EDS persona stores: [%s]",
                     dumpDatabases().c_str());
        folks_backend_set_persona_stores(m_eds, GEE_SET(m_databases.get()));

        if (m_view) {
            // We were started, prepare aggregator.
            SYNCEVO_GLIB_CALL_ASYNC(folks_individual_aggregator_prepare,
                                    boost::bind(logResult, _1,
                                                "folks_individual_aggregator_prepare"),
                                    getFolks());
        }

        // Execute delayed work.
        m_backendsLoadedSignal();
    } else {
        SE_LOG_ERROR(NULL, "EDS backend not active?!");
    }
}

void IndividualAggregator::setDatabases(std::set<std::string> &databases)
{
    gee_collection_clear(GEE_COLLECTION(m_databases.get()));
    BOOST_FOREACH (const std::string &database, databases) {
        gee_collection_add(GEE_COLLECTION(m_databases.get()), database.c_str());
    }

    if (m_eds) {
        // Backend is loaded, tell it about the change.
        SE_LOG_DEBUG(NULL, "backends already loaded: setting EDS persona stores directly: [%s]",
                     dumpDatabases().c_str());
        folks_backend_set_persona_stores(m_eds, GEE_SET(m_databases.get()));
    } else {
        SE_LOG_DEBUG(NULL, "backends not loaded yet: setting EDS persona stores delayed: [%s]",
                     dumpDatabases().c_str());
    }
}

void IndividualAggregator::setCompare(const boost::shared_ptr<IndividualCompare> &compare)
{
    // Don't start main view. Instead rememeber the compare instance
    // for start().
    m_compare = compare;
    if (m_view) {
        m_view->setCompare(compare);
    }
}

void IndividualAggregator::start()
{
    if (!m_view) {
        m_view = FullView::create(m_folks, m_locale);
        if (m_compare) {
            m_view->setCompare(m_compare);
        }
        if (m_eds) {
            // Backend was loaded and configured, we can prepare the aggregator.
            SYNCEVO_GLIB_CALL_ASYNC(folks_individual_aggregator_prepare,
                                    boost::bind(logResult, _1,
                                                "folks_individual_aggregator_prepare"),
                                    getFolks());
        }
    }
}

bool IndividualAggregator::isRunning() const
{
    return m_view;
}

boost::shared_ptr<FullView> IndividualAggregator::getMainView()
{
    if (!m_view) {
        start();
    }
    return m_view;
}

void IndividualAggregator::addContact(const Result<void (const std::string &)> &result,
                                      const PersonaDetails &details)
{
    // Called directly by D-Bus client. Need fully functional system address book.
    runWithAddressBook(boost::bind(&IndividualAggregator::doAddContact,
                                   this,
                                   result,
                                   details),
                       result.getOnError());
}

void IndividualAggregator::doAddContact(const Result<void (const std::string &)> &result,
                                        const PersonaDetails &details)
{
    SYNCEVO_GLIB_CALL_ASYNC(folks_persona_store_add_persona_from_details,
                            boost::bind(&IndividualAggregator::addContactDone,
                                        this,
                                        _2, _1,
                                        result),
                            m_systemStore,
                            details.get());
}

void IndividualAggregator::addContactDone(const GError *gerror,
                                          FolksPersona *persona,
                                          const Result<void (const std::string &)> &result) throw()
{
    try {
        // Handle result of folks_persona_store_add_persona_from_details().
        if (!persona || gerror) {
            GErrorCXX::throwError("add contact", gerror);
        }

        const gchar *uid = folks_persona_get_uid(persona);
        if (uid) {
            gchar *backend, *storeID, *personaID;
            folks_persona_split_uid(uid, &backend, &storeID, &personaID);
            PlainGStr tmp1(backend), tmp2(storeID), tmp3(personaID);
            result.done(personaID);
        } else {
            SE_THROW("new persona has empty UID");
        }
    } catch (...) {
        result.failed();
    }
}

void IndividualAggregator::modifyContact(const Result<void ()> &result,
                                         const std::string &localID,
                                         const PersonaDetails &details)
{
    runWithPersona(boost::bind(&IndividualAggregator::doModifyContact,
                               this,
                               result,
                               _1,
                               details),
                   localID,
                   result.getOnError());
}

void IndividualAggregator::doModifyContact(const Result<void ()> &result,
                                           FolksPersona *persona,
                                           const PersonaDetails &details) throw()
{
    try {
        // Asynchronously modify the persona. This will be turned into
        // EDS updates by folks.
        Details2Persona(result, details, persona);
    } catch (...) {
        result.failed();
    }
}

void IndividualAggregator::removeContact(const Result<void ()> &result,
                                         const std::string &localID)
{
    runWithPersona(boost::bind(&IndividualAggregator::doRemoveContact,
                               this,
                               result,
                               _1),
                   localID,
                   result.getOnError());
}

void IndividualAggregator::doRemoveContact(const Result<void ()> &result,
                                           FolksPersona *persona) throw()
{
    try {
        SYNCEVO_GLIB_CALL_ASYNC(folks_persona_store_remove_persona,
                                boost::bind(&IndividualAggregator::removeContactDone,
                                            this,
                                            _1,
                                            result),
                                m_systemStore,
                                persona);
    } catch (...) {
        result.failed();
    }
}

void IndividualAggregator::removeContactDone(const GError *gerror,
                                             const Result<void ()> &result) throw()
{
    try {
        if (gerror) {
            GErrorCXX::throwError("remove contact", gerror);
        }
        result.done();
    } catch (...) {
        result.failed();
    }
}


void IndividualAggregator::runWithAddressBook(const boost::function<void ()> &operation,
                                              const ErrorCb_t &onError) throw()
{
    try {
        if (m_eds) {
            runWithAddressBookHaveEDS(boost::signals2::connection(),
                                      operation,
                                      onError);
        } else {
            // Do it later.
            m_backendsLoadedSignal.connect_extended(boost::bind(&IndividualAggregator::runWithAddressBookHaveEDS,
                                                                this,
                                                                _1,
                                                                operation,
                                                                onError));
        }
    } catch (...) {
        onError();
    }
}

void IndividualAggregator::runWithAddressBookHaveEDS(const boost::signals2::connection &conn,
                                                     const boost::function<void ()> &operation,
                                                     const ErrorCb_t &onError) throw()
{
    try {
        // Called after we obtained EDS backend. Need system store
        // which is prepared.
        m_backendsLoadedSignal.disconnect(conn);
        if (!m_systemStore) {
            SE_THROW("system address book not found");
        }
        if (folks_persona_store_get_is_prepared(m_systemStore)) {
            runWithAddressBookPrepared(NULL,
                                       operation,
                                       onError);
        } else {
            SYNCEVO_GLIB_CALL_ASYNC(folks_persona_store_prepare,
                                    boost::bind(&IndividualAggregator::runWithAddressBookPrepared,
                                                this,
                                                _1,
                                                operation,
                                                onError),
                                    m_systemStore);
        }
    } catch (...) {
        onError();
    }
}

void IndividualAggregator::runWithAddressBookPrepared(const GError *gerror,
                                                      const boost::function<void ()> &operation,
                                                      const ErrorCb_t &onError) throw()
{
    try {
        // Called after EDS system store is prepared, successfully or unsuccessfully.
        if (gerror) {
            GErrorCXX::throwError("prepare EDS system address book", gerror);
        }
        operation();
    } catch (...) {
        onError();
    }
}

void IndividualAggregator::runWithPersona(const boost::function<void (FolksPersona *)> &operation,
                                          const std::string &localID,
                                          const ErrorCb_t &onError) throw()
{
    try {
        runWithAddressBook(boost::bind(&IndividualAggregator::doRunWithPersona,
                                       this,
                                       operation,
                                       localID,
                                       onError),
                           onError);
    } catch (...) {
        onError();
    }
}

void IndividualAggregator::doRunWithPersona(const boost::function<void (FolksPersona *)> &operation,
                                            const std::string &localID,
                                            const ErrorCb_t &onError) throw()
{
    try {
        GeeMap *personas = folks_persona_store_get_personas(m_systemStore);
        typedef GeeCollCXX< GeeMapEntryWrapper<const gchar *, FolksPersona *> > Coll;
        BOOST_FOREACH (const Coll::value_type &entry, Coll(personas)) {
            // key seems to be <store id>:<persona ID>
            const gchar *key = entry.key();
            const gchar *colon = strchr(key, ':');
            if (colon && localID == colon + 1) {
                operation(entry.value());
                return;
            }
        }
        SE_THROW(StringPrintf("contact with local ID '%s' not found in system address book", localID.c_str()));
    } catch (...) {
        onError();
    }
}

#ifdef ENABLE_UNIT_TESTS

class FolksTest : public CppUnit::TestFixture {
    CPPUNIT_TEST_SUITE(FolksTest);
    CPPUNIT_TEST(open);
    CPPUNIT_TEST(asyncError);
    CPPUNIT_TEST(gvalue);
    CPPUNIT_TEST_SUITE_END();

private:
    static void asyncCB(const GError *gerror, const char *func, bool &failed, bool &done) {
        done = true;
        if (gerror) {
            failed = true;
            SE_LOG_ERROR(NULL, "%s: %s", func, gerror->message);
        }
    }

    void open() {
        FolksIndividualAggregatorCXX aggregator(folks_individual_aggregator_new(), false);
        bool done = false, failed = false;
        SYNCEVO_GLIB_CALL_ASYNC(folks_individual_aggregator_prepare,
                                boost::bind(asyncCB, _1,
                                            "folks_individual_aggregator_prepare",
                                            boost::ref(failed), boost::ref(done)),
                                aggregator);

        while (!done) {
            g_main_context_iteration(NULL, true);
        }
        CPPUNIT_ASSERT(!failed);

        while (!folks_individual_aggregator_get_is_quiescent(aggregator)) {
            g_main_context_iteration(NULL, true);
        }

        GeeMap *individuals = folks_individual_aggregator_get_individuals(aggregator);
        SE_LOG_DEBUG(NULL, "%d individuals", gee_map_get_size(individuals));

        GeeMapIteratorCXX it(gee_map_map_iterator(individuals), false);
        while (gee_map_iterator_next(it)) {
            PlainGStr id(reinterpret_cast<gchar *>(gee_map_iterator_get_key(it)));
            FolksIndividualCXX individual(reinterpret_cast<FolksIndividual *>(gee_map_iterator_get_value(it)),
                                          false);
            GValueStringCXX fullname;
            g_object_get_property(G_OBJECT(individual.get()), "full-name", &fullname);
            SE_LOG_DEBUG(NULL, "map: id %s name %s = %s",
                         id.get(),
                         fullname.toString().c_str(),
                         fullname.get());
        }

        GeeIteratorCXX it2(gee_iterable_iterator(GEE_ITERABLE(individuals)), false);
        while (gee_iterator_next(it2)) {
            GeeMapEntryCXX entry(reinterpret_cast<GeeMapEntry *>(gee_iterator_get(it2)), false);
            gchar *id(reinterpret_cast<gchar *>(const_cast<gpointer>(gee_map_entry_get_key(entry))));
            FolksIndividual *individual(reinterpret_cast<FolksIndividual *>(const_cast<gpointer>(gee_map_entry_get_value(entry))));
            GValueStringCXX fullname;
            g_object_get_property(G_OBJECT(individual), "full-name", &fullname);
            SE_LOG_DEBUG(NULL, "iterable: id %s name %s = %s",
                         id,
                         fullname.toString().c_str(),
                         fullname.get());
        }

        typedef GeeCollCXX< GeeMapEntryWrapper<const gchar *, FolksIndividual *> > Coll;
        Coll coll(individuals);
        Coll::const_iterator curr = coll.begin();
        Coll::const_iterator end = coll.end();
        if (curr != end) {
            const gchar *id = (*curr).key();
            FolksIndividual *individual((*curr).value());
            GValueStringCXX fullname;
            g_object_get_property(G_OBJECT(individual), "full-name", &fullname);

            SE_LOG_DEBUG(NULL, "first: id %s name %s = %s",
                         id,
                         fullname.toString().c_str(),
                         fullname.get());
            ++curr;
        }

        BOOST_FOREACH (Coll::value_type &entry, Coll(individuals)) {
            const gchar *id = entry.key();
            FolksIndividual *individual(entry.value());
            // GValueStringCXX fullname;
            // g_object_get_property(G_OBJECT(individual), "full-name", &fullname);
            const gchar *fullname = folks_name_details_get_full_name(FOLKS_NAME_DETAILS(individual));

            SE_LOG_DEBUG(NULL, "boost: id %s %s name %s",
                         id,
                         fullname ? "has" : "has no",
                         fullname);

            GeeSet *emails = folks_email_details_get_email_addresses(FOLKS_EMAIL_DETAILS(individual));
            SE_LOG_DEBUG(NULL, "     %d emails", gee_collection_get_size(GEE_COLLECTION(emails)));
            typedef GeeCollCXX<FolksEmailFieldDetails *> EmailColl;
            BOOST_FOREACH (FolksEmailFieldDetails *email, EmailColl(emails)) {
                SE_LOG_DEBUG(NULL, "     %s",
                             reinterpret_cast<const gchar *>(folks_abstract_field_details_get_value(FOLKS_ABSTRACT_FIELD_DETAILS(email))));
            }
        }

        aggregator.reset();
    }

    void gvalue() {
        GValueBooleanCXX b(true);
        SE_LOG_DEBUG(NULL, "GValueBooleanCXX(true) = %s", b.toString().c_str());
        GValueBooleanCXX b2(b);
        CPPUNIT_ASSERT_EQUAL(b.get(), b2.get());
        b2.set(false);
        CPPUNIT_ASSERT_EQUAL(b.get(), (gboolean)!b2.get());
        b2 = b;
        CPPUNIT_ASSERT_EQUAL(b.get(), b2.get());

        GValueStringCXX str("foo bar");
        SE_LOG_DEBUG(NULL, "GValueStringCXX(\"foo bar\") = %s", str.toString().c_str());
        CPPUNIT_ASSERT(!strcmp(str.get(), "foo bar"));
        GValueStringCXX str2(str);
        CPPUNIT_ASSERT(!strcmp(str.get(), str2.get()));
        CPPUNIT_ASSERT(str.get() != str2.get());
        str2.set("foo");
        CPPUNIT_ASSERT(strcmp(str.get(), str2.get()));
        CPPUNIT_ASSERT(str.get() != str2.get());
        str2 = str;
        CPPUNIT_ASSERT(!strcmp(str.get(), str2.get()));
        CPPUNIT_ASSERT(str.get() != str2.get());
        str2.take(g_strdup("bar"));
        CPPUNIT_ASSERT(strcmp(str.get(), str2.get()));
        CPPUNIT_ASSERT(str.get() != str2.get());
        const char *fixed = "fixed";
        str2.setStatic(fixed);
        CPPUNIT_ASSERT(!strcmp(str2.get(), fixed));
        CPPUNIT_ASSERT(str2.get() == fixed);
    }

    void asyncError() {
        bool done = false, failed = false;
        SYNCEVO_GLIB_CALL_ASYNC(folks_individual_aggregator_remove_individual,
                                boost::bind(asyncCB, _1,
                                            "folks_individual_aggregator_remove_individual",
                                            boost::ref(failed), boost::ref(done)),
                                NULL, NULL);
        while (!done) {
            g_main_context_iteration(NULL, true);
        }
        // Invalid parameters are not reported!
        CPPUNIT_ASSERT(!failed);

        // using simpler macro
        GErrorCXX gerror;
        SYNCEVO_GLIB_CALL_SYNC(NULL,
                               gerror,
                               folks_individual_aggregator_remove_individual,
                               NULL, NULL);
        // Invalid parameters are not reported!
        CPPUNIT_ASSERT(!gerror);
    }

    static void individualSignal(std::ostringstream &out,
                                 const char *action,
                                 int index,
                                 const IndividualData &data) {
        out << action << ": " << index << " " <<
            folks_name_details_get_full_name(FOLKS_NAME_DETAILS(data.m_individual.get())) <<
            std::endl;
    }

    static void monitorView(IndividualView &view, std::ostringstream &out) {
        view.m_addedSignal.connect(boost::bind(individualSignal, boost::ref(out), "added", _1, _2));
        view.m_removedSignal.connect(boost::bind(individualSignal, boost::ref(out), "removed", _1, _2));
        view.m_modifiedSignal.connect(boost::bind(individualSignal, boost::ref(out), "modified", _1, _2));
    }
};

SYNCEVOLUTION_TEST_SUITE_REGISTRATION(FolksTest);

#endif

SE_END_CXX