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

« back to all changes in this revision

Viewing changes to .pc/0001-patch-from-Patrick-Ohly-convert-explicitly-between-s.patch/src/syncevo/SyncSource.h

  • 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) 2005-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
 
#ifndef INCL_SYNCSOURCE
22
 
#define INCL_SYNCSOURCE
23
 
 
24
 
#include <syncevo/SyncConfig.h>
25
 
#include <syncevo/Logging.h>
26
 
#include <syncevo/SyncML.h>
27
 
 
28
 
#include <synthesis/sync_declarations.h>
29
 
#include <synthesis/syerror.h>
30
 
#include <synthesis/blobs.h>
31
 
 
32
 
#include <boost/function.hpp>
33
 
 
34
 
#include <syncevo/declarations.h>
35
 
SE_BEGIN_CXX
36
 
 
37
 
class SyncSource;
38
 
class SDKInterface;
39
 
 
40
 
/**
41
 
 * This set of parameters always has to be passed when constructing
42
 
 * SyncSource instances.
43
 
 */
44
 
struct SyncSourceParams {
45
 
    /**
46
 
     * @param    name        the name needed by SyncSource
47
 
     * @param    nodes       a set of config nodes to be used by this source
48
 
     */
49
 
    SyncSourceParams(const string &name,
50
 
                     const SyncSourceNodes &nodes = SyncSourceNodes()) :
51
 
        m_name(name),
52
 
        m_nodes(nodes)
53
 
    {}
54
 
 
55
 
    string m_name;
56
 
    SyncSourceNodes m_nodes;
57
 
};
58
 
 
59
 
/**
60
 
 * The SyncEvolution core has no knowledge of existing SyncSource
61
 
 * implementations. Implementations have to register themselves
62
 
 * by instantiating this class exactly once with information
63
 
 * about themselves.
64
 
 *
65
 
 * It is also possible to add configuration options. For that define a
66
 
 * derived class. In its constructor use
67
 
 * SyncSourceConfig::getRegistry() resp. SyncConfig::getRegistry() to
68
 
 * define new configuration properties. The advantage of registering
69
 
 * them is that the user interface will automatically handle them like
70
 
 * the predefined ones. The namespace of these configuration options
71
 
 * is shared by all sources and the core.
72
 
 *
73
 
 * For properties with arbitrary names use the
74
 
 * SyncSourceNodes::m_trackingNode.
75
 
 */
76
 
class RegisterSyncSource
77
 
{
78
 
 public:
79
 
    /**
80
 
     * Users select a SyncSource and its data format via the "type"
81
 
     * config property. Backends have to add this kind of function to
82
 
     * the SourceRegistry_t in order to be considered by the
83
 
     * SyncSource creation mechanism.
84
 
     *
85
 
     * The function will be called to check whether the backend was
86
 
     * meant by the user. It should return a new instance which will
87
 
     * be freed by the caller or NULL if it does not support the
88
 
     * selected type.
89
 
     * 
90
 
     * Inactive sources should return the special InactiveSource
91
 
     * pointer value if they recognize without a doubt that the user
92
 
     * wanted to instantiate them: for example, an inactive
93
 
     * EvolutionContactSource will return NULL for "addressbook" but
94
 
     * InactiveSource for "evolution-contacts".
95
 
     */
96
 
    typedef SyncSource *(*Create_t)(const SyncSourceParams &params);
97
 
 
98
 
    /** special return value of Create_t, not a real sync source! */
99
 
    static SyncSource *const InactiveSource;
100
 
 
101
 
    /**
102
 
     * @param shortDescr     a few words identifying the data to be synchronized,
103
 
     *                       e.g. "Evolution Calendar"
104
 
     * @param enabled        true if the sync source can be instantiated,
105
 
     *                       false if it was not enabled during compilation or is
106
 
     *                       otherwise not functional
107
 
     * @param create         factory function for sync sources of this type
108
 
     * @param typeDescr      multiple lines separated by \n which get appended to
109
 
     *                       the the description of the type property, e.g.
110
 
     *                       "Evolution Memos = memo = evolution-memo\n"
111
 
     *                       "   plain text in UTF-8 (default) = text/plain\n"
112
 
     *                       "   iCalendar 2.0 = text/calendar\n"
113
 
     *                       "   The later format is not tested because none of the\n"
114
 
     *                       "   supported SyncML servers accepts it.\n"
115
 
     * @param typeValues     the config accepts multiple names for the same internal
116
 
     *                       type string; this list here is added to that list of
117
 
     *                       aliases. It should contain at least one unique string
118
 
     *                       the can be used to pick  this sync source among all
119
 
     *                       SyncEvolution sync sources (testing, listing backends, ...).
120
 
     *                       Example: Values() + (Aliases("Evolution Memos") + "evolution-memo")
121
 
     */
122
 
    RegisterSyncSource(const string &shortDescr,
123
 
                       bool enabled,
124
 
                       Create_t create,
125
 
                       const string &typeDescr,
126
 
                       const Values &typeValues);
127
 
 public:
128
 
    const string m_shortDescr;
129
 
    const bool m_enabled;
130
 
    const Create_t m_create;
131
 
    const string m_typeDescr;
132
 
    const Values m_typeValues;
133
 
};
134
 
    
135
 
typedef list<const RegisterSyncSource *> SourceRegistry;
136
 
class ClientTest;
137
 
class TestingSyncSource;
138
 
 
139
 
/**
140
 
 * Information about a data source. For the sake of simplicity all
141
 
 * items pointed to are owned by the ClientTest and must
142
 
 * remain valid throughout a test session. Not setting a pointer
143
 
 * is okay, but it will disable all tests that need the
144
 
 * information.
145
 
 */
146
 
struct ClientTestConfig{
147
 
    /**
148
 
     * The name is used in test names and has to be set.
149
 
     */
150
 
    const char *sourceName;
151
 
 
152
 
    /**
153
 
     * A default URI to be used when creating a client config.
154
 
     */
155
 
    const char *uri;
156
 
 
157
 
    /**
158
 
     * A corresponding source name in the default server template,
159
 
     * this is used to copy corresponding uri set in the server template
160
 
     * instead of the uri field above (which is the same for all servers).
161
 
     */
162
 
    const char *sourceNameServerTemplate;
163
 
 
164
 
    /**
165
 
     * A member function of a subclass which is called to create a
166
 
     * sync source referencing the data. This is used in tests of
167
 
     * the SyncSource API itself as well as in tests which need to
168
 
     * modify or check the data sources used during synchronization.
169
 
     *
170
 
     * The test framework will call beginSync() and then some of
171
 
     * the functions it wants to test. After a successful test it
172
 
     * will call endSync() which is then expected to store all
173
 
     * changes persistently. Creating a sync source again
174
 
     * with the same call should not report any
175
 
     * new/updated/deleted items until such changes are made via
176
 
     * another sync source.
177
 
     *
178
 
     * The instance will be deleted by the caller. Because this
179
 
     * may be in the error case or in an exception handler,
180
 
     * the sync source's desctructor should not thow exceptions.
181
 
     *
182
 
     * @param client    the same instance to which this config belongs
183
 
     * @param source    index of the data source (from 0 to ClientTest::getNumSources() - 1)
184
 
     * @param isSourceA true if the requested SyncSource is the first one accessing that
185
 
     *                  data, otherwise the second
186
 
     */
187
 
    typedef TestingSyncSource *(*createsource_t)(ClientTest &client, int source, bool isSourceA);
188
 
 
189
 
    /**
190
 
     * Creates a sync source which references the primary database;
191
 
     * it may report the same changes as the sync source used during
192
 
     * sync tests.
193
 
     */
194
 
    createsource_t createSourceA;
195
 
 
196
 
    /**
197
 
     * A second sync source also referencing the primary data
198
 
     * source, but configured so that it tracks changes
199
 
     * independently from the the primary sync source.
200
 
     *
201
 
     * In local tests the usage is like this:
202
 
     * - add item via first SyncSource
203
 
     * - iterate over new items in second SyncSource
204
 
     * - check that it lists the added item
205
 
     *
206
 
     * In tests with a server the usage is:
207
 
     * - do a synchronization with the server
208
 
     * - iterate over items in second SyncSource
209
 
     * - check that the total number and number of
210
 
     *   added/updated/deleted items is as expected
211
 
     */
212
 
    createsource_t createSourceB;
213
 
 
214
 
    /**
215
 
     * The framework can generate vCard and vCalendar/iCalendar items
216
 
     * automatically by copying a template item and modifying certain
217
 
     * properties.
218
 
     *
219
 
     * This is the template for these automatically generated items.
220
 
     * It must contain the string <<REVISION>> which will be replaced
221
 
     * with the revision parameter of the createItem() method.
222
 
     */
223
 
    const char *templateItem;
224
 
 
225
 
    /**
226
 
     * This is a colon (:) separated list of properties which need
227
 
     * to be modified in templateItem.
228
 
     */
229
 
    const char *uniqueProperties;
230
 
 
231
 
    /**
232
 
     * the number of items to create during stress tests
233
 
     */
234
 
    int numItems;
235
 
 
236
 
    /**
237
 
     * This is a single property in templateItem which can be extended
238
 
     * to increase the size of generated items.
239
 
     */
240
 
    const char *sizeProperty;
241
 
 
242
 
    /**
243
 
     * Type to be set when importing any of the items into the
244
 
     * corresponding sync sources. Use "" if sync source doesn't
245
 
     * need this information.
246
 
     *
247
 
     * Not currently used! All items are assumed to be in the raw,
248
 
     * internal format (see SyncSourceRaw and SyncSourceSerialize).
249
 
     */
250
 
    const char *itemType;
251
 
 
252
 
    /**
253
 
     * A very simple item that is inserted during basic tests. Ideally
254
 
     * it only contains properties supported by all servers.
255
 
     */
256
 
    const char *insertItem;
257
 
 
258
 
    /**
259
 
     * A slightly modified version of insertItem. If the source has UIDs
260
 
     * embedded into the item data, then both must have the same UID.
261
 
     * Again all servers should better support these modified properties.
262
 
     */
263
 
    const char *updateItem;
264
 
 
265
 
    /**
266
 
     * A more heavily modified version of insertItem. Same UID if necessary,
267
 
     * but can test changes to items only supported by more advanced
268
 
     * servers.
269
 
     */
270
 
    const char *complexUpdateItem;
271
 
 
272
 
    /**
273
 
     * To test merge conflicts two different updates of insertItem are
274
 
     * needed. This is the first such update.
275
 
     */
276
 
    const char *mergeItem1;
277
 
 
278
 
    /**
279
 
     * The second merge update item. To avoid true conflicts it should
280
 
     * update different properties than mergeItem1, but even then servers
281
 
     * usually have problems perfectly merging items. Therefore the
282
 
     * test is run without expecting a certain merge result.
283
 
     */
284
 
    const char *mergeItem2;
285
 
 
286
 
    /**
287
 
     * These two items are related: one is main one, the other is
288
 
     * a subordinate one. The semantic is that the main item is
289
 
     * complete on it its own, while the other normally should only
290
 
     * be used in combination with the main one.
291
 
     *
292
 
     * Because SyncML cannot express such dependencies between items,
293
 
     * a SyncSource has to be able to insert, updated and remove
294
 
     * both items independently. However, operations which violate
295
 
     * the semantic of the related items (like deleting the parent, but
296
 
     * not the child) may have unspecified results (like also deleting
297
 
     * the child). See linkedItemsRelaxedSemantic and sourceKnowsItemSemantic.
298
 
     *
299
 
     * One example for main and subordinate items are a recurring
300
 
     * iCalendar 2.0 event and a detached recurrence.
301
 
     */
302
 
    const char *parentItem, *childItem;
303
 
 
304
 
    /**
305
 
     * Backends atomic modification tests
306
 
     */
307
 
    bool atomicModification;
308
 
 
309
 
    /**
310
 
     * set to false to disable tests which slightly violate the
311
 
     * semantic of linked items by inserting children
312
 
     * before/without their parent
313
 
     */
314
 
    bool linkedItemsRelaxedSemantic;
315
 
 
316
 
    /**
317
 
     * setting this to false disables tests which depend
318
 
     * on the source's support for linked item semantic
319
 
     * (testLinkedItemsInsertParentTwice, testLinkedItemsInsertChildTwice)
320
 
     */
321
 
    bool sourceKnowsItemSemantic;
322
 
 
323
 
    /**
324
 
     * called to dump all items into a file, required by tests which need
325
 
     * to compare items
326
 
     *
327
 
     * ClientTest::dump can be used: it will simply dump all items of the source
328
 
     * with a blank line as separator.
329
 
     *
330
 
     * @param source     sync source A already created and with beginSync() called
331
 
     * @param file       a file name
332
 
     * @return error code, 0 for success
333
 
     */
334
 
    int (*dump)(ClientTest &client, TestingSyncSource &source, const char *file);
335
 
 
336
 
    /**
337
 
     * import test items: which these are is determined entirely by
338
 
     * the implementor, but tests work best if several complex items are
339
 
     * imported
340
 
     *
341
 
     * ClientTest::import can be used if the file contains items separated by
342
 
     * empty lines.
343
 
     *
344
 
     * @param source     sync source A already created and with beginSync() called
345
 
     * @param file       the name of the file to import
346
 
     * @retval realfile  the name of the file that was really imported;
347
 
     *                   this may depend on the current server that is being tested
348
 
     * @return error code, 0 for success
349
 
     */
350
 
    int (*import)(ClientTest &client, TestingSyncSource &source, const char *file, std::string &realfile);
351
 
 
352
 
    /**
353
 
     * a function which compares two files with items in the format used by "dump"
354
 
     *
355
 
     * @param fileA      first file name
356
 
     * @param fileB      second file name
357
 
     * @return true if the content of the files is considered equal
358
 
     */
359
 
    bool (*compare)(ClientTest &client, const char *fileA, const char *fileB);
360
 
 
361
 
    /**
362
 
     * a file with test cases in the format expected by import and compare
363
 
     */
364
 
    const char *testcases;
365
 
 
366
 
    /**
367
 
     * the item type normally used by the source (not used by the tests
368
 
     * themselves; client-test.cpp uses it to initialize source configs)
369
 
     */
370
 
    const char *type;
371
 
 
372
 
    /**
373
 
     * a list of sub configs separated via , if this is a super datastore
374
 
     */
375
 
    const char *subConfigs;
376
 
 
377
 
    /**
378
 
     * TRUE if the source supports recovery from an interrupted
379
 
     * synchronization. Enables the Client::Sync::*::Retry group
380
 
     * of tests.
381
 
     */
382
 
    bool retrySync;
383
 
    bool suspendSync;
384
 
    bool resendSync;
385
 
};
386
 
 
387
 
/**
388
 
 * In addition to registering the sync source itself by creating an
389
 
 * instance of RegisterSyncSource, configurations for testing it can
390
 
 * also be registered. A sync source which supports more than one data
391
 
 * exchange format can register one configuration for each format, but
392
 
 * not registering any configuration is also okay.
393
 
 *
394
 
 * *Using* the registered tests depends on the CPPUnit test framework.
395
 
 * *Registering* does not. Therefore backends should always register *
396
 
 * *themselves for testing and leave it to the test runner
397
 
 * "client-test" whether tests are really executed.
398
 
 *
399
 
 * Unit tests are different. They create hard dependencies on CPPUnit
400
 
 * inside the code that contains them, and thus should be encapsulated
401
 
 * inside #ifdef ENABLE_UNIT_TESTS checks.
402
 
 *
403
 
 * Sync sources have to work stand-alone without a full SyncClient
404
 
 * configuration for all local tests. The minimal configuration prepared
405
 
 * for the source includes:
406
 
 * - a tracking node (as used f.i. by TrackingSyncSource) which
407
 
 *   points towards "~/.config/syncevolution/client-test-changes"
408
 
 * - a unique change ID (as used f.i. by EvolutionContactSource)
409
 
 * - a valid "evolutionsource" property in the config node, starting
410
 
 *   with the CLIENT_TEST_EVOLUTION_PREFIX env variable or (if that
411
 
 *   wasn't set) the "SyncEvolution_Test_" prefix
412
 
 * - "evolutionuser/password" if CLIENT_TEST_EVOLUTION_USER/PASSWORD
413
 
 *   are set
414
 
 *
415
 
 * No other properties are set, which implies that currently sync sources
416
 
 * which require further parameters cannot be tested.
417
 
 *
418
 
 * @warning There is a potential problem with the registration
419
 
 * mechanism. Both the sync source tests as well as the CPPUnit tests
420
 
 * derived from them are registrered when global class instances are
421
 
 * initialized. If the RegisterTestEvolution instance in
422
 
 * client-test-app.cpp is initialized *before* the sync source tests,
423
 
 * then those won't show up in the test list. Currently the right
424
 
 * order seems to be used, so everything works as expected.
425
 
 */
426
 
class RegisterSyncSourceTest
427
 
{
428
 
 public:
429
 
    /**
430
 
     * This call is invoked after setting up the config with default
431
 
     * values for the test cases selected via the constructor's
432
 
     * testCaseName parameter (one of vcard21, vcard30, ical20, itodo20;
433
 
     * see ClientTest in the Funambol client library for the current
434
 
     * list).
435
 
     *
436
 
     * This call can then override any of the values or (if there
437
 
     * are no predefined test cases) add them.
438
 
     *
439
 
     * The "type" property must select your sync source and the
440
 
     * data format for the test.
441
 
     *
442
 
     * @retval config        change any field whose default is not suitable
443
 
     */
444
 
    virtual void updateConfig(ClientTestConfig &config) const = 0;
445
 
 
446
 
    /**
447
 
     * @param configName     a unique string: the predefined names known by
448
 
     *                       ClientTest::getTestData() are already used for the initial
449
 
     *                       set of Evolution sync sources, for new sync sources
450
 
     *                       build a string by combining them with the sync source name
451
 
     *                       (e.g., "sqlite_vcard30")
452
 
     * @param testCaseName   a string recognized by ClientTest::getTestData() or an
453
 
     *                       empty string if there are no predefined test cases
454
 
     */
455
 
    RegisterSyncSourceTest(const string &configName,
456
 
                           const string &testCaseName);
457
 
    virtual ~RegisterSyncSourceTest() {}
458
 
 
459
 
    const string m_configName;
460
 
    const string m_testCaseName;
461
 
};
462
 
 
463
 
class TestRegistry : public vector<const RegisterSyncSourceTest *>
464
 
{
465
 
 public:
466
 
    // TODO: using const RegisterSyncSourceTest * operator [] (int);
467
 
    const RegisterSyncSourceTest * operator [] (const string &configName) const {
468
 
        BOOST_FOREACH(const RegisterSyncSourceTest *test, *this) {
469
 
            if (test->m_configName == configName) {
470
 
                return test;
471
 
            }
472
 
        }
473
 
        throw out_of_range(string("test config registry: ") + configName);
474
 
        return NULL;
475
 
    }
476
 
};
477
 
 
478
 
/**
479
 
 * a container for Synthesis XML config fragments
480
 
 *
481
 
 * Backends can define their own field lists, profiles, datatypes and
482
 
 * remote rules. The name of each of these entities have to be unique:
483
 
 * either prefix each name with the name of the backend or coordinate
484
 
 * with other developers (e.g. regarding shared field lists).
485
 
 *
486
 
 * To add new items, add them to the respective hash in your backend's
487
 
 * getDatastoreXML() or getSynthesisInfo() implementation. Both
488
 
 * methods have default implementations: getSynthesisInfo() is called
489
 
 * by the default getDatastoreXML() to provide some details and
490
 
 * provides them based on the "type" configuration option.
491
 
 *
492
 
 * The default config XML contains several predefined items:
493
 
 * - field lists: contacts, calendar, Note, bookmarks
494
 
 * - profiles: vCard, vCalendar, Note, vBookmark
495
 
 * - datatypes: vCard21, vCard30, vCalendar10, iCalendar20,
496
 
 *              note10/11 (no difference except the versioning!),
497
 
 *              vBookmark10
498
 
 * - remote rule: EVOLUTION
499
 
 *
500
 
 * These items do not appear in the hashes, so avoid picking the same
501
 
 * names. The entries of each hash has to be a well-formed XML
502
 
 * element, their keys the name encoded in each XML element.
503
 
 */
504
 
struct XMLConfigFragments {
505
 
    class mapping : public std::map<std::string, std::string> {
506
 
    public:
507
 
        string join() {
508
 
            string res;
509
 
            size_t len = 0;
510
 
            BOOST_FOREACH(const value_type &entry, *this) {
511
 
                len += entry.second.size() + 1;
512
 
            }
513
 
            res.reserve(len);
514
 
            BOOST_FOREACH(const value_type &entry, *this) {
515
 
                res += entry.second;
516
 
                res += "\n";
517
 
            }
518
 
            return res;
519
 
        }
520
 
    } m_fieldlists,
521
 
        m_profiles,
522
 
        m_datatypes,
523
 
        m_remoterules;
524
 
};
525
 
 
526
 
/**
527
 
 * abstract base class for SyncSource with some common functionality
528
 
 * and no data
529
 
 *
530
 
 * Used to implement and call that functionality in multiple derived
531
 
 * classes, including situations where a derived class is derived from
532
 
 * this base via different intermediate classes, therefore the
533
 
 * need to keep it abstract.
534
 
 */
535
 
class SyncSourceBase : public Logger {
536
 
 public:
537
 
    virtual ~SyncSourceBase() {}
538
 
 
539
 
    /** the unique name of the sync source (for example, "addressbook") */
540
 
    virtual const char *getName() const { return "uninitialized SyncSourceBase"; }
541
 
 
542
 
    /**
543
 
     * Convenience function, to be called inside a catch() block of
544
 
     * (or for) the sync source.
545
 
     *
546
 
     * Rethrows the exception to determine what it is, then logs it
547
 
     * as an error and returns a suitable error code (usually a general
548
 
     * STATUS_DATASTORE_FAILURE).
549
 
     */
550
 
    SyncMLStatus handleException();
551
 
 
552
 
    /**
553
 
     * throw an exception after an operation failed
554
 
     *
555
 
     * output format: <source name>: <action>: <error string>
556
 
     *
557
 
     * @param action   a string describing the operation or object involved
558
 
     * @param error    the errno error code for the failure
559
 
     */
560
 
    void throwError(const string &action, int error);
561
 
 
562
 
    /**
563
 
     * throw an exception after an operation failed and
564
 
     * remember that this instance has failed
565
 
     *
566
 
     * output format: <source name>: <failure>
567
 
     *
568
 
     * @param action     a string describing what was attempted *and* how it failed
569
 
     */
570
 
    void throwError(const string &failure);
571
 
 
572
 
    /**
573
 
     * The Synthesis engine only counts items which are deleted by the
574
 
     * peer. Items deleted locally at the start of a
575
 
     * refresh-from-server sync are not counted (and cannot be counted
576
 
     * in all cases).
577
 
     *
578
 
     * Sync sources which want to have those items included in the
579
 
     * sync statistics should count *all* deleted items using these
580
 
     * methods. SyncContext will use this number for
581
 
     * refresh-from-server syncs.
582
 
     */
583
 
    /**@{*/
584
 
    virtual long getNumDeleted() const = 0;
585
 
    virtual void setNumDeleted(long num) = 0;
586
 
    virtual void incrementNumDeleted() = 0;
587
 
    /**@}*/
588
 
 
589
 
    /**
590
 
     * Return Synthesis <datastore> XML fragment for this sync source.
591
 
     * Must *not* include the <datastore> element; it is created by
592
 
     * the caller.
593
 
     *
594
 
     * The default implementation returns a configuration for the
595
 
     * SynthesisDBPlugin, which invokes SyncSource::Operations. Items
596
 
     * are exchanged with the SyncsSource in the format defined by
597
 
     * getSynthesisInfo(). The format used with the SyncML side is
598
 
     * negotiated via the peer's capabilities, with the type defined
599
 
     * in the configuration being the preferred one of the data store.
600
 
     *
601
 
     * See SyncContext::getConfigXML() for details about
602
 
     * predefined <datatype> entries that can be referenced here.
603
 
     *
604
 
     * @retval xml         put content of <datastore>...</datastore> here
605
 
     * @retval fragments   the necessary definitions for the datastore have to be added here
606
 
     */
607
 
    virtual void getDatastoreXML(string &xml, XMLConfigFragments &fragments);
608
 
 
609
 
    /**
610
 
     * Synthesis <datatype> name which matches the format used
611
 
     * for importing and exporting items (exportData()).
612
 
     * This is not necessarily the same format that is given
613
 
     * to the Synthesis engine. If this internal format doesn't
614
 
     * have a <datatype> in the engine, then an empty string is
615
 
     * returned.
616
 
     */
617
 
    virtual string getNativeDatatypeName();
618
 
 
619
 
    /**
620
 
     * Logging utility code.
621
 
     *
622
 
     * Every sync source adds "<name>" as prefix to its output.
623
 
     * All calls are redirected into SyncContext logger.
624
 
     */
625
 
    virtual void messagev(Level level,
626
 
                          const char *prefix,
627
 
                          const char *file,
628
 
                          int line,
629
 
                          const char *function,
630
 
                          const char *format,
631
 
                          va_list args);
632
 
 
633
 
    /**
634
 
     * return Synthesis API pointer, if one currently is available
635
 
     * (between SyncEvolution_Module_CreateContext() and
636
 
     * SyncEvolution_Module_DeleteContext())
637
 
     */
638
 
    virtual SDKInterface *getSynthesisAPI() const = 0;
639
 
 
640
 
    /**
641
 
     * Prepare the sync source for usage inside a SyncML server.  To
642
 
     * be called directly after creating the source, if at all.
643
 
     */
644
 
    virtual void enableServerMode() = 0;
645
 
    virtual bool serverModeEnabled() const = 0;
646
 
 
647
 
 protected:
648
 
    struct SynthesisInfo {
649
 
        /**
650
 
         * name to use for MAKE/PARSETEXTWITHPROFILE,
651
 
         * leave empty when acessing the field list directly
652
 
         */
653
 
        std::string m_profile;
654
 
 
655
 
        /**
656
 
         * the second parameter for MAKE/PARSETEXTWITHPROFILE
657
 
         * which specifies a remote rule to be applied when
658
 
         * converting to and from the backend
659
 
         */
660
 
        std::string m_backendRule;
661
 
    
662
 
        /** list of supported datatypes in "<use .../>" format */
663
 
        std::string m_datatypes;
664
 
 
665
 
        /** native datatype (see getNativeDatatypeName()) */
666
 
        std::string m_native;
667
 
 
668
 
        /** name of the field list used by the datatypes */
669
 
        std::string m_fieldlist;
670
 
 
671
 
        /**
672
 
         * One or more Synthesis script statements, separated
673
 
         * and terminated with a semicolon. Can be left empty.
674
 
         *
675
 
         * If not empty, then these statements are executed directly
676
 
         * before converting the current item fields into
677
 
         * a single string with MAKETEXTWITHPROFILE() in the sync source's
678
 
         * <beforewritescript> (see SyncSourceBase::getDatastoreXML()).
679
 
         *
680
 
         * This value is currently only used by sync sources which
681
 
         * set m_profile.
682
 
         */
683
 
        std::string m_beforeWriteScript;
684
 
 
685
 
        /**
686
 
         * Same as m_beforeWriteScript, but used directly after
687
 
         * converting a string into fields with PARSETEXTWITHPROFILE()
688
 
         * in <afterreadscript>.
689
 
         */
690
 
        std::string m_afterReadScript;
691
 
    };
692
 
 
693
 
    /**
694
 
     * helper function for getDatastoreXML(): fill in information
695
 
     * as necessary
696
 
     *
697
 
     * @retval fragments   the necessary definitions for the other
698
 
     *                     return values have to be added here
699
 
     */
700
 
    virtual void getSynthesisInfo(SynthesisInfo &info,
701
 
                                  XMLConfigFragments &fragments) = 0;
702
 
 
703
 
    /**
704
 
     * utility code: creates Synthesis <use datatype=...>
705
 
     * statements, using the predefined vCard21/vCard30/vcalendar10/icalendar20
706
 
     * types. Throws an error if no suitable result can be returned (empty or invalid type)
707
 
     *
708
 
     * @param type         the format specifier as used in SyncEvolution configs, with and without version
709
 
     *                     (text/x-vcard:2.1, text/x-vcard, text/x-vcalendar, text/calendar, text/plain, ...);
710
 
     *                     see SourceType::m_format
711
 
     * @param forceFormat  if true, then don't allow alternative formats (like vCard 3.0 in addition to 2.1);
712
 
     *                     see SourceType::m_force
713
 
     * @return generated XML fragment
714
 
     */
715
 
    std::string getDataTypeSupport(const std::string &type,
716
 
                                   bool forceFormat);
717
 
};
718
 
 
719
 
/**
720
 
 * SyncEvolution accesses all sources through this interface.
721
 
 *
722
 
 * Certain functionality is optional or can be implemented in
723
 
 * different ways. These methods are accessed through functors
724
 
 * (function objects) which may be unset. The expected usage is that
725
 
 * derived classes fill in the pieces that they provide by binding the
726
 
 * functors to normal methods. For example, TrackingSyncSource
727
 
 * provides a normal base class with pure virtual functions which have
728
 
 * to be provided by users of that class.
729
 
 *
730
 
 * Error reporting is done via the Log class.
731
 
 */
732
 
class SyncSource : virtual public SyncSourceBase, public SyncSourceConfig, public SyncSourceReport
733
 
{
734
 
 public:
735
 
    SyncSource(const SyncSourceParams &params) :
736
 
        SyncSourceConfig(params.m_name, params.m_nodes),
737
 
        m_numDeleted(0),
738
 
        m_forceSlowSync(false)
739
 
        {
740
 
        }
741
 
    virtual ~SyncSource() {}
742
 
 
743
 
    /**
744
 
     * SyncSource implementations must register themselves here via
745
 
     * RegisterSyncSource
746
 
     */
747
 
    static SourceRegistry &getSourceRegistry();
748
 
 
749
 
    /**
750
 
     * SyncSource tests are registered here by the constructor of
751
 
     * RegisterSyncSourceTest
752
 
     */
753
 
    static TestRegistry &getTestRegistry();
754
 
 
755
 
    struct Database {
756
 
    Database(const string &name, const string &uri, bool isDefault = false) :
757
 
        m_name( name ), m_uri( uri ), m_isDefault(isDefault) {}
758
 
        string m_name;
759
 
        string m_uri;
760
 
        bool m_isDefault;
761
 
    };
762
 
    typedef vector<Database> Databases;
763
 
    
764
 
    /**
765
 
     * returns a list of all know data sources for the kind of items
766
 
     * supported by this sync source
767
 
     */
768
 
    virtual Databases getDatabases() = 0;
769
 
 
770
 
    /**
771
 
     * Actually opens the data source specified in the constructor,
772
 
     * will throw the normal exceptions if that fails. Should
773
 
     * not modify the state of the sync source.
774
 
     *
775
 
     * The expectation is that this call is fairly light-weight, but
776
 
     * does enough checking to determine whether the source is
777
 
     * usable. More expensive operations (like determining changes)
778
 
     * should be done in the m_startDataRead callback (bound to
779
 
     * beginSync() in some of the utility classes).
780
 
     *
781
 
     * In clients, it will be called for all sources before
782
 
     * the sync starts. In servers, it is called for each source once
783
 
     * the client asks for it, but not sooner.
784
 
     */
785
 
    virtual void open() = 0;
786
 
 
787
 
    /**
788
 
     * The optional operations.
789
 
     *
790
 
     * All of them are guaranteed to happen between open() and
791
 
     * close().
792
 
     *
793
 
     * They are all allowed to throw exceptions: the operations called
794
 
     * by SyncEvolution then abort whatever SyncEvolution was doing
795
 
     * and end in the normal exception handling. For the Synthesis
796
 
     * operations, the bridge code in SynthesisDBPlugin code catches
797
 
     * exceptions, logs them and translates them into Synthesis error
798
 
     * codes, which are returned to the Synthesis engine.
799
 
     */
800
 
    struct Operations {
801
 
        /**
802
 
         * The caller determines where item data is stored (m_dirname)
803
 
         * and where meta information about them (m_node). The callee
804
 
         * then can use both arbitrarily. As an additional hint,
805
 
         * m_mode specifies why and when the backup is made, which
806
 
         * is useful to determine whether information can be reused.
807
 
         */
808
 
        struct BackupInfo {
809
 
            enum Mode {
810
 
                BACKUP_BEFORE,   /**< directly at start of sync */
811
 
                BACKUP_AFTER,    /**< directly after sync */
812
 
                BACKUP_OTHER
813
 
            } m_mode;
814
 
            string m_dirname;
815
 
            boost::shared_ptr<ConfigNode> m_node;
816
 
            BackupInfo() {}
817
 
            BackupInfo(Mode mode,
818
 
                       const string &dirname,
819
 
                       const boost::shared_ptr<ConfigNode> &node) :
820
 
                m_mode(mode),
821
 
                m_dirname(dirname),
822
 
                m_node(node)
823
 
            {}
824
 
        };
825
 
        struct ConstBackupInfo {
826
 
            BackupInfo::Mode m_mode;
827
 
            string m_dirname;
828
 
            boost::shared_ptr<const ConfigNode> m_node;
829
 
            ConstBackupInfo() {}
830
 
            ConstBackupInfo(BackupInfo::Mode mode,
831
 
                            const string &dirname,
832
 
                            const boost::shared_ptr<const ConfigNode> &node) :
833
 
                m_mode(mode),
834
 
                m_dirname(dirname),
835
 
                m_node(node)
836
 
            {}
837
 
        };
838
 
 
839
 
        /**
840
 
         * Dump all data from source unmodified into the given backup location.
841
 
         * Information about the created backup is added to the
842
 
         * report.
843
 
         *
844
 
         * Required for the backup/restore functionality in
845
 
         * SyncEvolution, not for syncing itself. But typically it is
846
 
         * called before syncing (can be turned off by users), so
847
 
         * implementations can reuse the information gathered while
848
 
         * making a backup in later operations.
849
 
         *
850
 
         * @param previous     the most recent backup, empty m_dirname if none
851
 
         * @param next         the backup which is to be created, directory and node are empty
852
 
         * @param report       to be filled with information about backup (number of items, etc.)
853
 
         */
854
 
        typedef void (BackupData_t)(const ConstBackupInfo &oldBackup,
855
 
                                    const BackupInfo &newBackup,
856
 
                                    BackupReport &report);
857
 
        boost::function<BackupData_t> m_backupData;
858
 
 
859
 
        /**
860
 
         * Restore database from data stored in backupData().
861
 
         * If possible don't touch items which are the same as in the
862
 
         * backup, to mimimize impact on future incremental syncs.
863
 
         *
864
 
         * @param oldBackup    the backup which is to be restored
865
 
         * @param dryrun       pretend to restore and fill in report, without
866
 
         *                     actually touching backend data
867
 
         * @param report       to be filled with information about restore
868
 
         *                     (number of total items and changes)
869
 
         */
870
 
        typedef void (RestoreData_t)(const ConstBackupInfo &oldBackup,
871
 
                                     bool dryrun,
872
 
                                     SyncSourceReport &report);
873
 
        boost::function<RestoreData_t> m_restoreData;
874
 
 
875
 
        /**
876
 
         * initialize information about local changes and items
877
 
         * as in beginSync() with all parameters set to true,
878
 
         * but without changing the state of the underlying database
879
 
         *
880
 
         * This method will be called to check for local changes without
881
 
         * actually running a sync, so there is no matching end call.
882
 
         *
883
 
         * There might be sources which don't support non-destructive
884
 
         * change tracking (in other words, checking changes permanently
885
 
         * modifies the state of the source and cannot be repeated).
886
 
         * Such sources should leave the functor empty.
887
 
         */
888
 
        typedef void (CheckStatus_t)(SyncSourceReport &local);
889
 
        boost::function<CheckStatus_t> m_checkStatus;
890
 
 
891
 
        /**
892
 
         * A quick check whether the source currently has data.
893
 
         *
894
 
         * If this cannot be determined easily, don't provide the
895
 
         * operation. The information is currently only used to
896
 
         * determine whether a slow sync should be allowed. If
897
 
         * the operation is not provided, the assumption is that
898
 
         * there is local data, which disables the "allow slow
899
 
         * sync for empty databases" heuristic and forces the user
900
 
         * to choose.
901
 
         */
902
 
        typedef bool (IsEmpty_t)();
903
 
        boost::function<IsEmpty_t> m_isEmpty;
904
 
 
905
 
        /**
906
 
         * Synthesis DB API callbacks. For documentation see the
907
 
         * Synthesis API specification (PDF and/or sync_dbapi.h).
908
 
         *
909
 
         * Implementing this is necessary for SyncSources which want
910
 
         * to be part of a sync session.
911
 
         */
912
 
        /**@{*/
913
 
        typedef void (Callback_t)();
914
 
        typedef boost::function<Callback_t> CallbackFunctor_t;
915
 
        typedef std::list<CallbackFunctor_t> Callbacks_t;
916
 
 
917
 
        /** all of these functions will be called before accessing
918
 
            the source's data for the first time, i.e., before m_startDataRead */
919
 
        Callbacks_t m_startAccess;
920
 
 
921
 
        typedef sysync::TSyError (StartDataRead_t)(const char *lastToken, const char *resumeToken);
922
 
        boost::function<StartDataRead_t> m_startDataRead;
923
 
 
924
 
        /** all of these functions will be called directly after
925
 
            m_startDataRead() returned successfully */
926
 
        Callbacks_t m_startSession;
927
 
 
928
 
        typedef sysync::TSyError (EndDataRead_t)();
929
 
        boost::function<EndDataRead_t> m_endDataRead;
930
 
 
931
 
        typedef sysync::TSyError (StartDataWrite_t)();
932
 
        boost::function<StartDataWrite_t> m_startDataWrite;
933
 
 
934
 
        /** all of these functions will be called right
935
 
            before m_endDataWrite() */
936
 
        std::list<CallbackFunctor_t> m_endSession;
937
 
 
938
 
        typedef sysync::TSyError (EndDataWrite_t)(bool success, char **newToken);
939
 
        boost::function<EndDataWrite_t> m_endDataWrite;
940
 
 
941
 
        /** the SynthesisDBPlugin is configured so that this operation
942
 
            doesn't have to (and cannot) return the item data */
943
 
        typedef sysync::TSyError (ReadNextItem_t)(sysync::ItemID aID,
944
 
                                                  sysync::sInt32 *aStatus, bool aFirst);
945
 
        boost::function<ReadNextItem_t> m_readNextItem;
946
 
        
947
 
        typedef sysync::TSyError (ReadItemAsKey_t)(sysync::cItemID aID, sysync::KeyH aItemKey);
948
 
        boost::function<ReadItemAsKey_t> m_readItemAsKey;
949
 
 
950
 
        typedef sysync::TSyError (InsertItemAsKey_t)(sysync::KeyH aItemKey, sysync::ItemID newID);
951
 
        boost::function<InsertItemAsKey_t> m_insertItemAsKey;
952
 
        
953
 
        typedef sysync::TSyError (UpdateItemAsKey_t)(sysync::KeyH aItemKey, sysync::cItemID aID, sysync::ItemID updID);
954
 
        boost::function<UpdateItemAsKey_t> m_updateItemAsKey;
955
 
 
956
 
        typedef sysync::TSyError (DeleteItem_t)(sysync::cItemID aID);
957
 
        boost::function<DeleteItem_t> m_deleteItem;
958
 
        /**@}*/
959
 
 
960
 
 
961
 
        /**
962
 
         * Synthesis administration callbacks. For documentation see the
963
 
         * Synthesis API specification (PDF and/or sync_dbapi.h).
964
 
         *
965
 
         * Implementing this is *optional* in clients. In the Synthesis client
966
 
         * engine, the "binfiles" module provides these calls without SyncEvolution
967
 
         * doing anything.
968
 
         *
969
 
         * In the Synthesis server engine, the
970
 
         * SyncSource::enableServerMode() call must install an
971
 
         * implementation, like the one from SyncSourceAdmin.
972
 
         */
973
 
        /**@{*/
974
 
        typedef sysync::TSyError (LoadAdminData_t)(const char *aLocDB,
975
 
                                                   const char *aRemDB,
976
 
                                                   char **adminData);
977
 
        boost::function<LoadAdminData_t> m_loadAdminData;
978
 
 
979
 
        typedef sysync::TSyError (SaveAdminData_t)(const char *adminData);
980
 
        boost::function<SaveAdminData_t> m_saveAdminData;
981
 
 
982
 
        typedef bool (ReadNextMapItem_t)(sysync::MapID mID, bool aFirst);
983
 
        boost::function<ReadNextMapItem_t> m_readNextMapItem;
984
 
 
985
 
        typedef sysync::TSyError (InsertMapItem_t)(sysync::cMapID mID);
986
 
        boost::function<InsertMapItem_t> m_insertMapItem;
987
 
 
988
 
        typedef sysync::TSyError (UpdateMapItem_t)(sysync::cMapID mID);
989
 
        boost::function<UpdateMapItem_t> m_updateMapItem;
990
 
 
991
 
        typedef sysync::TSyError (DeleteMapItem_t)(sysync::cMapID mID);
992
 
        boost::function<DeleteMapItem_t> m_deleteMapItem;
993
 
 
994
 
        typedef sysync::TSyError (ReadBlob_t)(sysync::cItemID aID, const char *aBlobID,
995
 
                                              void **aBlkPtr, size_t *aBlkSize,
996
 
                                              size_t *aTotSize,
997
 
                                              bool aFirst, bool *aLast);
998
 
        boost::function<ReadBlob_t> m_readBlob;
999
 
 
1000
 
        typedef sysync::TSyError (WriteBlob_t)(sysync::cItemID aID, const char *aBlobID,
1001
 
                                               void *aBlkPtr, size_t aBlkSize,
1002
 
                                               size_t aTotSize,
1003
 
                                               bool aFirst, bool aLast);
1004
 
        boost::function<WriteBlob_t> m_writeBlob;
1005
 
 
1006
 
        typedef sysync::TSyError (DeleteBlob_t)(sysync::cItemID aID, const char *aBlobID);
1007
 
        boost::function<DeleteBlob_t> m_deleteBlob;
1008
 
        /**@}*/
1009
 
    };
1010
 
    const Operations &getOperations() { return m_operations; }
1011
 
 
1012
 
    /**
1013
 
     * outside users of the source are only allowed to add callbacks,
1014
 
     * not overwrite arbitrary operations
1015
 
     */
1016
 
    void addCallback(Operations::CallbackFunctor_t callback, Operations::Callbacks_t Operations::* where) { (m_operations.*where).push_back(callback); }
1017
 
        
1018
 
    /**
1019
 
     * closes the data source so that it can be reopened
1020
 
     *
1021
 
     * Just as open() it should not affect the state of
1022
 
     * the database unless some previous action requires
1023
 
     * it.
1024
 
     */
1025
 
    virtual void close() = 0;
1026
 
 
1027
 
    /**
1028
 
     * return Synthesis API pointer, if one currently is available
1029
 
     * (between SyncEvolution_Module_CreateContext() and
1030
 
     * SyncEvolution_Module_DeleteContext())
1031
 
     */
1032
 
    virtual SDKInterface *getSynthesisAPI() const;
1033
 
 
1034
 
    /**
1035
 
     * change the Synthesis API that is used by the source
1036
 
     */
1037
 
    void pushSynthesisAPI(sysync::SDK_InterfaceType *synthesisAPI);
1038
 
 
1039
 
    /**
1040
 
     * remove latest Synthesis API and return to previous one (if any)
1041
 
     */
1042
 
    void popSynthesisAPI();
1043
 
 
1044
 
    /**
1045
 
     * factory function for a SyncSource that provides the
1046
 
     * source type specified in the params.m_nodes.m_configNode
1047
 
     *
1048
 
     * @param error    throw a runtime error describing what the problem is if no matching source is found
1049
 
     * @param config   optional, needed for intantiating virtual sources
1050
 
     * @return valid instance, NULL if no source can handle the given type (only when error==false)
1051
 
     */
1052
 
    static SyncSource *createSource(const SyncSourceParams &params,
1053
 
                                    bool error = true,
1054
 
                                    SyncConfig *config = NULL);
1055
 
 
1056
 
    /**
1057
 
     * Factory function for a SyncSource with the given name
1058
 
     * and handling the kind of data specified by "type" (e.g.
1059
 
     * "Evolution Contacts:text/x-vcard").
1060
 
     *
1061
 
     * The source is instantiated with dummy configuration nodes under
1062
 
     * the pseudo server name "testing". This function is used for
1063
 
     * testing sync sources, not for real syncs. If the prefix is set,
1064
 
     * then <prefix>_<name>_1 is used as database, just as in the
1065
 
     * Client::Sync and Client::Source tests. Otherwise the default
1066
 
     * database is used.
1067
 
     *
1068
 
     * @param error    throw a runtime error describing what the problem is if no matching source is found
1069
 
     * @return NULL if no source can handle the given type
1070
 
     */
1071
 
    static SyncSource *createTestingSource(const string &name, const string &type, bool error,
1072
 
                                           const char *prefix = getenv("CLIENT_TEST_EVOLUTION_PREFIX"));
1073
 
 
1074
 
    /**
1075
 
     * Some information about available backends.
1076
 
     * Multiple lines, formatted for users of the
1077
 
     * command line.
1078
 
     */
1079
 
    static string backendsInfo();
1080
 
    /**
1081
 
     * Debug information about backends.
1082
 
     */
1083
 
    static string backendsDebug();
1084
 
 
1085
 
    /**
1086
 
     * Mime type a backend communicates with the remote peer by default,
1087
 
     * this is used to alert the remote peer in SAN during server alerted sync.
1088
 
     */
1089
 
    virtual const char *getPeerMimeType() const =0;
1090
 
 
1091
 
    /* implementation of SyncSourceBase */
1092
 
    virtual const char * getName() const { return SyncSourceConfig::getName(); }
1093
 
    virtual long getNumDeleted() const { return m_numDeleted; }
1094
 
    virtual void setNumDeleted(long num) { m_numDeleted = num; }
1095
 
    virtual void incrementNumDeleted() { m_numDeleted++; }
1096
 
 
1097
 
    /**
1098
 
     * Set to true in SyncContext::initSAN() when a SyncML server has
1099
 
     * to force a client into slow sync mode. This is necessary because
1100
 
     * the server cannot request that mode (missing in the standard).
1101
 
     * Forcing the slow sync mode is done via a FORCESLOWSYNC() macro
1102
 
     * call in an <alertscript>.
1103
 
     */
1104
 
    void setForceSlowSync(bool forceSlowSync) { m_forceSlowSync = forceSlowSync; }
1105
 
    bool getForceSlowSync() const { return m_forceSlowSync; }
1106
 
 
1107
 
 protected:
1108
 
    Operations m_operations;
1109
 
 
1110
 
  private:
1111
 
    /**
1112
 
     * Counter for items deleted in the source. Has to be incremented
1113
 
     * by RemoveAllItems() and DeleteItem(). This counter is used to
1114
 
     * update the Synthesis engine counter in those cases where the
1115
 
     * engine does not (refresh from server) or cannot
1116
 
     * (RemoveAllItems()) count the removals itself.
1117
 
     */
1118
 
    long m_numDeleted;
1119
 
 
1120
 
    bool m_forceSlowSync;
1121
 
 
1122
 
    /**
1123
 
     * Interface pointer for this sync source, allocated for us by the
1124
 
     * Synthesis engine and registered here by
1125
 
     * SyncEvolution_Module_CreateContext(). Only valid until
1126
 
     * SyncEvolution_Module_DeleteContext(), in other words, while
1127
 
     * the engine is running.
1128
 
     */
1129
 
    std::vector<sysync::SDK_InterfaceType *> m_synthesisAPI;
1130
 
};
1131
 
 
1132
 
/**
1133
 
 * A SyncSource with no pure virtual functions.
1134
 
 */
1135
 
class DummySyncSource : public SyncSource
1136
 
{
1137
 
 public:
1138
 
    DummySyncSource(const SyncSourceParams &params) :
1139
 
       SyncSource(params) {}
1140
 
 
1141
 
    DummySyncSource(const std::string &name) :
1142
 
       SyncSource(SyncSourceParams(name)) {}
1143
 
 
1144
 
    virtual Databases getDatabases() { return Databases(); }
1145
 
    virtual void open() {}
1146
 
    virtual void close() {}
1147
 
    virtual void getSynthesisInfo(SynthesisInfo &info,
1148
 
                                  XMLConfigFragments &fragments) {}
1149
 
    virtual void enableServerMode() {}
1150
 
    virtual bool serverModeEnabled() const { return false; }
1151
 
    virtual const char *getPeerMimeType() const {return "";} 
1152
 
};
1153
 
 
1154
 
/**
1155
 
 * A special source which combines one or more real sources.
1156
 
 * Most of the special handling for that is in SyncContext.cpp.
1157
 
 *
1158
 
 * This class can be instantiated, opened and closed if and only if
1159
 
 * the underlying sources also support that.
1160
 
 */
1161
 
class VirtualSyncSource : public DummySyncSource 
1162
 
{
1163
 
    std::vector< boost::shared_ptr<SyncSource> > m_sources;
1164
 
 
1165
 
public:
1166
 
    /**
1167
 
     * @param config   optional: when given, the constructor will instantiate the
1168
 
     *                 referenced underlying sources and check them in open()
1169
 
     */
1170
 
    VirtualSyncSource(const SyncSourceParams &params, SyncConfig *config = NULL);
1171
 
 
1172
 
    /** opens underlying sources and checks config by calling getDataTypeSupport() */
1173
 
    virtual void open();
1174
 
    virtual void close();
1175
 
 
1176
 
    /**
1177
 
     * returns array with source names that are referenced by this
1178
 
     * virtual source
1179
 
     */
1180
 
    std::vector<std::string> getMappedSources();
1181
 
 
1182
 
    /**
1183
 
     * returns <use datatype=...> statements for XML config,
1184
 
     * throws error if not configured correctly
1185
 
     */
1186
 
    std::string getDataTypeSupport();
1187
 
    using SyncSourceBase::getDataTypeSupport;
1188
 
 
1189
 
 
1190
 
   /*
1191
 
    * If any of the sub datasource has no databases associated, return an empty
1192
 
    * database list to indicate a possibly error condition; otherwise return a
1193
 
    * dummy database to identify "calendar+todo" combined datasource.
1194
 
    **/
1195
 
    virtual Databases getDatabases();
1196
 
};
1197
 
 
1198
 
/**
1199
 
 * Hooks up the Synthesis DB Interface start sync (BeginDataRead) and
1200
 
 * end sync (EndDataWrite) calls with virtual methods. Ensures that
1201
 
 * sleepSinceModification() is called.
1202
 
 *
1203
 
 * Inherit from this class in your sync source and call the init()
1204
 
 * method to use it.
1205
 
 */
1206
 
class SyncSourceSession : virtual public SyncSourceBase {
1207
 
 public:
1208
 
    /**
1209
 
     * called before Synthesis engine starts to ask for changes and item data
1210
 
     *
1211
 
     * See BeingDataRead for details.
1212
 
     *
1213
 
     * @param lastToken     identifies the last completed sync
1214
 
     * @param resumeToken   identifies a more recent sync which needs to be resumed;
1215
 
     *                      if not empty, then report changes made after that sync
1216
 
     *                      instead of the last completed sync
1217
 
     */
1218
 
    virtual void beginSync(const std::string &lastToken, const std::string &resumeToken) = 0;
1219
 
 
1220
 
    /**
1221
 
     * called after completing or suspending the current sync
1222
 
     *
1223
 
     * See EndDataWrite for details.
1224
 
     *
1225
 
     * @return a token identifying this sync session for a future beginSync()
1226
 
     */
1227
 
    virtual std::string endSync(bool success) = 0;
1228
 
 
1229
 
    /** set Synthesis DB Interface operations */
1230
 
    void init(SyncSource::Operations &ops);
1231
 
 private:
1232
 
    sysync::TSyError startDataRead(const char *lastToken, const char *resumeToken);
1233
 
    sysync::TSyError endDataWrite(bool success, char **newToken);
1234
 
};
1235
 
 
1236
 
 
1237
 
/**
1238
 
 * Implements the Synthesis DB Interface for reporting item changes
1239
 
 * (ReadNextItemAsKey) *without* actually delivering the item data.
1240
 
 */
1241
 
class SyncSourceChanges : virtual public SyncSourceBase {
1242
 
 public:
1243
 
    SyncSourceChanges();
1244
 
 
1245
 
    enum State {
1246
 
        ANY,
1247
 
        NEW,
1248
 
        UPDATED,
1249
 
        DELETED,
1250
 
        MAX
1251
 
    };
1252
 
 
1253
 
    /**
1254
 
     * Add the LUID of a NEW/UPDATED/DELETED item.
1255
 
     * If unspecified, the luid is added to the list of
1256
 
     * all items. This must be done *in addition* to adding
1257
 
     * the luid with a specific state.
1258
 
     *
1259
 
     * For example, the luid of an updated item should be added with
1260
 
     * addItem(luid [, ANY]) and again with addItem(luid, DELETED).
1261
 
     *
1262
 
     * The Synthesis engine does not need the list of deleted items
1263
 
     * and does not distinguish between added and updated items, so
1264
 
     * for syncing, adding DELETED items is optional and all items
1265
 
     * which are different from the last sync can be added as
1266
 
     * UPDATED. The client-test program expects that the information
1267
 
     * is provided precisely.
1268
 
     *
1269
 
     * @return true if the luid was already listed
1270
 
     */
1271
 
    bool addItem(const string &luid, State state = ANY);
1272
 
 
1273
 
    typedef std::set<std::string> Items_t;
1274
 
    const Items_t &getItems(State state) { return m_items[state]; }
1275
 
    const Items_t &getAllItems() const { return m_items[ANY]; }
1276
 
    const Items_t &getNewItems() const { return m_items[NEW]; }
1277
 
    const Items_t &getUpdatedItems() const { return m_items[UPDATED]; }
1278
 
    const Items_t &getDeletedItems() const { return m_items[DELETED]; }
1279
 
 
1280
 
    /** set Synthesis DB Interface operations */
1281
 
    void init(SyncSource::Operations &ops);
1282
 
 
1283
 
 private:
1284
 
    Items_t m_items[MAX];
1285
 
    bool m_first;
1286
 
    Items_t::const_iterator m_it;
1287
 
 
1288
 
    sysync::TSyError iterate(sysync::ItemID aID,
1289
 
                             sysync::sInt32 *aStatus,
1290
 
                             bool aFirst);
1291
 
};
1292
 
 
1293
 
/**
1294
 
 * Implements the Synthesis DB Interface for deleting an item
1295
 
 * (DeleteItem). Increments number of deleted items in
1296
 
 * SyncSourceBase.
1297
 
 */
1298
 
class SyncSourceDelete : virtual public SyncSourceBase {
1299
 
 public:
1300
 
    virtual void deleteItem(const string &luid) = 0;
1301
 
 
1302
 
    /** set Synthesis DB Interface operations */
1303
 
    void init(SyncSource::Operations &ops);
1304
 
 
1305
 
 private:
1306
 
    sysync::TSyError deleteItemSynthesis(sysync::cItemID aID);
1307
 
};
1308
 
 
1309
 
/**
1310
 
 * an interface for reading and writing items in the internal
1311
 
 * format; see SyncSourceSerialize for an explanation
1312
 
 */
1313
 
class SyncSourceRaw : virtual public SyncSourceBase {
1314
 
 public:
1315
 
    class InsertItemResult {
1316
 
    public:
1317
 
        InsertItemResult() :
1318
 
            m_merged(false)
1319
 
            {}
1320
 
        
1321
 
        /**
1322
 
         * @param luid      the LUID after the operation; during an update the LUID must
1323
 
         *                  not be changed, so return the original one here
1324
 
         * @param revision  the revision string after the operation; leave empty if not used
1325
 
         * @param merged    set this to true if an existing item was updated instead of adding it
1326
 
         */
1327
 
        InsertItemResult(const string &luid,
1328
 
                         const string &revision,
1329
 
                         bool merged) :
1330
 
        m_luid(luid),
1331
 
            m_revision(revision),
1332
 
            m_merged(merged)
1333
 
            {}
1334
 
 
1335
 
        string m_luid;
1336
 
        string m_revision;
1337
 
        bool m_merged;
1338
 
    };
1339
 
 
1340
 
    /** same as SyncSourceSerialize::insertItem(), but with internal format */
1341
 
    virtual InsertItemResult insertItemRaw(const std::string &luid, const std::string &item) = 0;
1342
 
 
1343
 
    /** same as SyncSourceSerialize::readItem(), but with internal format */
1344
 
    virtual void readItemRaw(const std::string &luid, std::string &item) = 0;
1345
 
};
1346
 
 
1347
 
/**
1348
 
 * Implements the Synthesis DB Interface for importing/exporting item
1349
 
 * data (ReadItemAsKey, InsertItemAsKey, UpdateItemAsKey) in such a
1350
 
 * way that the sync source only has to deal with a text
1351
 
 * representation of an item.
1352
 
 *
1353
 
 * There may be two such representations:
1354
 
 * - "engine format" is the one exchanged with the Synthesis engine
1355
 
 * - "internal or raw format" is a format that might better capture
1356
 
 *   the internal representation and can be used for backup/restore
1357
 
 *   and testing
1358
 
 *
1359
 
 * To give an example, the EvolutionMemoSource uses plain text as
1360
 
 * engine format and iCalendar 2.0 as raw format.
1361
 
 *
1362
 
 * The BackupData_t and RestoreData_t operations are implemented by
1363
 
 * this class using the internal format.
1364
 
 *
1365
 
 * The engine format must be something that the Synthesis engine can
1366
 
 * parse and generate, in other words, there must be a corresponding
1367
 
 * profile in the XML configuration. This class uses information
1368
 
 * provided by the sync source (mime type and version) and from the
1369
 
 * configuration (format selected by user) to generate the required
1370
 
 * XML configuration parts for common configurations (vCard,
1371
 
 * vCalendar, iCalendar, text). Special representations can be added
1372
 
 * to the global XML configuration by overriding default
1373
 
 * implementations provided in this class.
1374
 
 *
1375
 
 * InsertItemAsKey and UpdateItemAsKey are mapped to the same
1376
 
 * insertItem() call because in practice it can happen that a request
1377
 
 * to add an item must be turned into an update. For example, a
1378
 
 * meeting was imported both into the server and the client. A request
1379
 
 * to add the item again should be treated as an update, based on the
1380
 
 * unique iCalendar 2.0 LUID.
1381
 
 */
1382
 
class SyncSourceSerialize : virtual public SyncSourceBase, virtual public SyncSourceRaw {
1383
 
 public:
1384
 
    /**
1385
 
     * Returns the preferred mime type of the items handled by the sync source.
1386
 
     * Example: "text/x-vcard"
1387
 
     */
1388
 
    virtual const char *getMimeType() const = 0;
1389
 
 
1390
 
    /**
1391
 
     * Returns the version of the mime type used by client.
1392
 
     * Example: "2.1"
1393
 
     */
1394
 
    virtual const char *getMimeVersion() const = 0;
1395
 
 
1396
 
    /**
1397
 
     * returns the backend selection and configuration
1398
 
     */
1399
 
    virtual SourceType getSourceType() const = 0;
1400
 
 
1401
 
    /**
1402
 
     * Create or modify an item.
1403
 
     *
1404
 
     * The sync source should be flexible: if the LUID is non-empty, it
1405
 
     * shall modify the item referenced by the LUID. If the LUID is
1406
 
     * empty, the normal operation is to add it. But if the item
1407
 
     * already exists (e.g., a calendar event which was imported
1408
 
     * by the user manually), then the existing item should be
1409
 
     * updated also in the second case.
1410
 
     *
1411
 
     * Passing a LUID of an item which does not exist is an error.
1412
 
     * This error should be reported instead of covering it up by
1413
 
     * (re)creating the item.
1414
 
     *
1415
 
     * Errors are signaled by throwing an exception. Returning empty
1416
 
     * strings in the result is an error which triggers an "item could
1417
 
     * not be stored" error.
1418
 
     *
1419
 
     * @param luid     identifies the item to be modified, empty for creating
1420
 
     * @param item     contains the new content of the item, using the engine format
1421
 
     * @return the result of inserting the item
1422
 
     */
1423
 
    virtual InsertItemResult insertItem(const std::string &luid, const std::string &item) = 0;
1424
 
 
1425
 
    /**
1426
 
     * Return item data in engine format.
1427
 
     *
1428
 
     * @param luid     identifies the item
1429
 
     * @retval item    item data
1430
 
     */
1431
 
    virtual void readItem(const std::string &luid, std::string &item) = 0;
1432
 
 
1433
 
    /* implement SyncSourceRaw under the assumption that the internal and engine format are identical */
1434
 
    virtual InsertItemResult insertItemRaw(const std::string &luid, const std::string &item) { return insertItem(luid, item); }
1435
 
    virtual void readItemRaw(const std::string &luid, std::string &item) { return readItem(luid, item); }
1436
 
 
1437
 
    /** set Synthesis DB Interface operations */
1438
 
    void init(SyncSource::Operations &ops);
1439
 
 
1440
 
 protected:
1441
 
    /**
1442
 
     * used getMimeType(), getMimeVersion() and getSourceType()
1443
 
     * to provide the information necessary for automatic
1444
 
     * conversion to the sync source's internal item representation
1445
 
     */
1446
 
    virtual void getSynthesisInfo(SynthesisInfo &info,
1447
 
                                  XMLConfigFragments &fragments);
1448
 
 private:
1449
 
    sysync::TSyError readItemAsKey(sysync::cItemID aID, sysync::KeyH aItemKey);
1450
 
    sysync::TSyError insertItemAsKey(sysync::KeyH aItemKey, sysync::cItemID aID, sysync::ItemID newID);
1451
 
};
1452
 
 
1453
 
/**
1454
 
 * Implements change tracking based on a "revision" string, a string
1455
 
 * which is guaranteed to change automatically each time an item is
1456
 
 * modified. Backup/restore is optionally implemented by this class if
1457
 
 * pointers to SyncSourceRaw and SyncSourceDelete interfaces are
1458
 
 * passed to init(). For backup only the former is needed, for restore
1459
 
 * both.
1460
 
 *
1461
 
 * Potential implementations of the revision string are:
1462
 
 * - a modification time stamp
1463
 
 * - a hash value of a textual representation of the item
1464
 
 *   (beware, such a hash might change as the textual representation
1465
 
 *    changes even though the item is unchanged)
1466
 
 *
1467
 
 * Sync sources which want to use this functionality have to provide
1468
 
 * the following functionality by implementing the pure virtual
1469
 
 * functions below:
1470
 
 * - enumerate all existing items
1471
 
 * - provide LUID and the revision string
1472
 
 *   The LUID must remain *constant* when making changes to an item,
1473
 
 *   whereas the revision string must *change* each time the item is
1474
 
 *   changed by anyone.
1475
 
 *   Both can be arbitrary strings, but keeping them simple (printable
1476
 
 *   ASCII, no white spaces, no equal sign) makes debugging simpler
1477
 
 *   because they can be stored as they are as key/value pairs in the
1478
 
 *   sync source's change tracking config node (the .other.ini files when
1479
 
 *   using file-based configuration). More complex strings use escape
1480
 
 *   sequences introduced with an exclamation mark for unsafe characters.
1481
 
 *
1482
 
 * Most of the functionality of this class must be activated
1483
 
 * explicitly as part of the life cycle of the sync source instance by
1484
 
 * calling detectChanges(), updateRevision() and deleteRevision().
1485
 
 *
1486
 
 * If the required interfaces are provided to init(), then backup/restore
1487
 
 * operations are set. init() also hooks into the session life cycle
1488
 
 * with an end callback that ensures that enough time passes at the end
1489
 
 * of the sync. This is important for sync sources which use time stamps
1490
 
 * as revision string. "enough time" is defined by a parameter to the
1491
 
 * init call.
1492
 
 */
1493
 
class SyncSourceRevisions : virtual public SyncSourceChanges, virtual public SyncSourceBase {
1494
 
 public:
1495
 
    typedef map<string, string> RevisionMap_t;
1496
 
 
1497
 
    /**
1498
 
     * fills the complete mapping from UID to revision string of all
1499
 
     * currently existing items
1500
 
     *
1501
 
     * Usually both UID and revision string must be non-empty. The
1502
 
     * only exception is a refresh-from-client: in that case the
1503
 
     * revision string may be empty. The implementor of this call
1504
 
     * cannot know whether empty strings are allowed, therefore it
1505
 
     * should not throw errors when it cannot create a non-empty
1506
 
     * string. The caller of this method will detect situations where
1507
 
     * a non-empty string is necessary and none was provided.
1508
 
     *
1509
 
     * This call is typically only invoked only once during the
1510
 
     * lifetime of a source. The result returned in that invocation is
1511
 
     * used throught the session.
1512
 
     */
1513
 
    virtual void listAllItems(RevisionMap_t &revisions) = 0;
1514
 
 
1515
 
    /**
1516
 
     * calculate changes, call when sync source is ready for
1517
 
     * listAllItems() and before changes are needed
1518
 
     *
1519
 
     * The trackingNode must be provided by the caller. It will
1520
 
     * be updated by each of the calls and must be stored by
1521
 
     * the caller.
1522
 
     *
1523
 
     * @param trackingNode     a config node for exclusive use by this class
1524
 
     */
1525
 
    void detectChanges(ConfigNode &trackingNode);
1526
 
 
1527
 
    /**
1528
 
     * record that an item was added or updated
1529
 
     *
1530
 
     * @param old_luid         empty for add, old LUID otherwise
1531
 
     * @param new_luid         normally LUIDs must not change, but this call allows it
1532
 
     * @param revision         revision string after change
1533
 
     */
1534
 
    void updateRevision(ConfigNode &trackingNode,
1535
 
                        const std::string &old_luid,
1536
 
                        const std::string &new_luid,
1537
 
                        const std::string &revision);
1538
 
 
1539
 
    /**
1540
 
     * record that we deleted an item
1541
 
     *
1542
 
     * @param luid        the obsolete LUID
1543
 
     */
1544
 
    void deleteRevision(ConfigNode &trackingNode,
1545
 
                        const std::string &luid);
1546
 
 
1547
 
    /**
1548
 
     * set Synthesis DB Interface and backup/restore operations
1549
 
     * @param raw           needed for backups; if NULL, no backups are made
1550
 
     * @param del           needed for restores; if NULL, only backups are possible
1551
 
     * @param granularity   time that has to pass between making a modification
1552
 
     *                      and checking for changes; this class ensures that
1553
 
     *                      at least this amount of time has passed before letting
1554
 
     *                      the session terminate. Delays in different source do
1555
 
     *                      not add up.
1556
 
     */
1557
 
    void init(SyncSourceRaw *raw, SyncSourceDelete *del,
1558
 
              int granularity,
1559
 
              SyncSource::Operations &ops);
1560
 
 
1561
 
 private:
1562
 
    SyncSourceRaw *m_raw;
1563
 
    SyncSourceDelete *m_del;
1564
 
    int m_revisionAccuracySeconds;
1565
 
 
1566
 
    /** buffers the result of the initial listAllItems() call */
1567
 
    RevisionMap_t m_revisions;
1568
 
    bool m_revisionsSet;
1569
 
    void initRevisions();
1570
 
 
1571
 
    /**
1572
 
     * Dump all data from source unmodified into the given directory.
1573
 
     * The ConfigNode can be used to store meta information needed for
1574
 
     * restoring that state. Both directory and node are empty.
1575
 
     */
1576
 
    void backupData(const SyncSource::Operations::ConstBackupInfo &oldBackup,
1577
 
                    const SyncSource::Operations::BackupInfo &newBackup,
1578
 
                    BackupReport &report);
1579
 
 
1580
 
    /**
1581
 
     * Restore database from data stored in backupData(). Will be
1582
 
     * called inside open()/close() pair. beginSync() is *not* called.
1583
 
     */
1584
 
    void restoreData(const SyncSource::Operations::ConstBackupInfo &oldBackup,
1585
 
                     bool dryrun,
1586
 
                     SyncSourceReport &report);
1587
 
 
1588
 
    /**
1589
 
     * Increments the time stamp of the latest database modification,
1590
 
     * called automatically whenever revisions change.
1591
 
     */
1592
 
    void databaseModified();
1593
 
 
1594
 
    /** time stamp of latest database modification, for sleepSinceModification() */
1595
 
    time_t m_modTimeStamp;
1596
 
    void sleepSinceModification();
1597
 
};
1598
 
 
1599
 
 
1600
 
/**
1601
 
 * Common logging for sync sources.
1602
 
 *
1603
 
 * This class wraps the Synthesis DB functors that were set before
1604
 
 * calling its init() method. The wrappers then log a single line
1605
 
 * describing what is happening (adding/updating/removing)
1606
 
 * to which item (with a short item specific description extracted
1607
 
 * from the incoming item data or the backend).
1608
 
 */
1609
 
class SyncSourceLogging : public virtual SyncSourceBase
1610
 
{
1611
 
 public:
1612
 
    /**
1613
 
     * wrap Synthesis DB Interface operations
1614
 
     *
1615
 
     * @param fields     list of fields to read in getDescription()
1616
 
     * @param sep        separator between non-empty fields
1617
 
     */
1618
 
    void init(const std::list<std::string> &fields,
1619
 
              const std::string &sep,
1620
 
              SyncSource::Operations &ops);
1621
 
 
1622
 
    /**
1623
 
     * Extract short description from Synthesis item data.
1624
 
     * The default implementation reads a list of fields
1625
 
     * as strings and concatenates the non-empty ones
1626
 
     * with a separator.
1627
 
     *
1628
 
     * @param aItemKey     key for reading fields
1629
 
     * @return description, empty string will cause the ID of the item to be printed
1630
 
     */
1631
 
    virtual std::string getDescription(sysync::KeyH aItemKey);
1632
 
 
1633
 
    /**
1634
 
     * Extract short description from backend.
1635
 
     * Necessary for deleted items. The default implementation
1636
 
     * returns an empty string, so that implementing this
1637
 
     * is optional.
1638
 
     *
1639
 
     * @param luid          LUID of the item to be deleted in the backend
1640
 
     * @return description, empty string will cause the ID of the item to be printed
1641
 
     */
1642
 
    virtual std::string getDescription(const string &luid);
1643
 
 
1644
 
 private:
1645
 
    std::list<std::string> m_fields;
1646
 
    std::string m_sep;
1647
 
 
1648
 
    sysync::TSyError insertItemAsKey(sysync::KeyH aItemKey, sysync::ItemID newID,
1649
 
                                     const boost::function<SyncSource::Operations::InsertItemAsKey_t> &parent);
1650
 
    sysync::TSyError updateItemAsKey(sysync::KeyH aItemKey, sysync::cItemID aID, sysync::ItemID newID,
1651
 
                                     const boost::function<SyncSource::Operations::UpdateItemAsKey_t> &parent);
1652
 
    sysync::TSyError deleteItem(sysync::cItemID aID,
1653
 
                                const boost::function<SyncSource::Operations::DeleteItem_t> &parent);
1654
 
};
1655
 
 
1656
 
/**
1657
 
 * Implements Load/SaveAdminData and MapItem handling in a SyncML
1658
 
 * server. Uses a single property for the admin data in the "internal"
1659
 
 * node and a complete node for the map items.
1660
 
 */
1661
 
class SyncSourceAdmin : public virtual SyncSourceBase
1662
 
{
1663
 
    boost::shared_ptr<ConfigNode> m_configNode;
1664
 
    std::string m_adminPropertyName;
1665
 
    boost::shared_ptr<ConfigNode> m_mappingNode;
1666
 
    bool m_mappingLoaded;
1667
 
 
1668
 
    ConfigProps m_mapping;
1669
 
    ConfigProps::const_iterator m_mappingIterator;
1670
 
 
1671
 
    sysync::TSyError loadAdminData(const char *aLocDB,
1672
 
                                   const char *aRemDB,
1673
 
                                   char **adminData);
1674
 
    sysync::TSyError saveAdminData(const char *adminData);
1675
 
    bool readNextMapItem(sysync::MapID mID, bool aFirst);
1676
 
    sysync::TSyError insertMapItem(sysync::cMapID mID);
1677
 
    sysync::TSyError updateMapItem(sysync::cMapID mID);
1678
 
    sysync::TSyError deleteMapItem(sysync::cMapID mID);
1679
 
    void flush();
1680
 
 
1681
 
    void resetMap();
1682
 
    void mapid2entry(sysync::cMapID mID, string &key, string &value);
1683
 
    void entry2mapid(const string &key, const string &value, sysync::MapID mID);
1684
 
 
1685
 
 public:
1686
 
    /** flexible initialization */
1687
 
    void init(SyncSource::Operations &ops,
1688
 
              const boost::shared_ptr<ConfigNode> &config,
1689
 
              const std::string adminPropertyName,
1690
 
              const boost::shared_ptr<ConfigNode> &mapping);
1691
 
 
1692
 
    /**
1693
 
     * simpler initialization, using the default placement of data
1694
 
     * inside the SyncSourceConfig base class
1695
 
     */
1696
 
    void init(SyncSource::Operations &ops, SyncSource *source);
1697
 
};
1698
 
 
1699
 
/**
1700
 
 * Implements Read/Write/DeleteBlob. Blobs are stored inside a
1701
 
 * configurable directory, which has to be unique for the current
1702
 
 * peer.
1703
 
 */
1704
 
class SyncSourceBlob : public virtual SyncSourceBase
1705
 
{
1706
 
    /**
1707
 
     * Only one blob is active at a time.
1708
 
     * This utility class provides the actual implementation.
1709
 
     */
1710
 
    sysync::TBlob m_blob;
1711
 
 
1712
 
    sysync::TSyError readBlob(sysync::cItemID aID, const char *aBlobID,
1713
 
                              void **aBlkPtr, size_t *aBlkSize,
1714
 
                              size_t *aTotSize,
1715
 
                              bool aFirst, bool *aLast) {
1716
 
        return m_blob.ReadBlob(aID, aBlobID, aBlkPtr, aBlkSize, aTotSize, aFirst, aLast);
1717
 
    }
1718
 
    sysync::TSyError writeBlob(sysync::cItemID aID, const char *aBlobID,
1719
 
                               void *aBlkPtr, size_t aBlkSize,
1720
 
                               size_t aTotSize,
1721
 
                               bool aFirst, bool aLast) {
1722
 
        mkdir_p(m_blob.getBlobPath());
1723
 
        return m_blob.WriteBlob(aID, aBlobID, aBlkPtr, aBlkSize, aTotSize, aFirst, aLast);
1724
 
    }
1725
 
    sysync::TSyError deleteBlob(sysync::cItemID aID, const char *aBlobID) {
1726
 
        return m_blob.DeleteBlob(aID, aBlobID);
1727
 
    }
1728
 
 
1729
 
    sysync::TSyError loadAdminData(sysync::cItemID aID, const char *aBlobID,
1730
 
                                   void **aBlkPtr, size_t *aBlkSize, size_t *aTotSize,
1731
 
                                   bool aFirst, bool *aLast);
1732
 
 
1733
 
 public:
1734
 
    void init(SyncSource::Operations &ops,
1735
 
              const std::string &dir);
1736
 
};
1737
 
 
1738
 
 
1739
 
/**
1740
 
 * This is an interface definition that is expected by the client-test
1741
 
 * program. Part of the reason for this requirement is that the test
1742
 
 * program was originally written for the Funambol SyncSource API.
1743
 
 * The other reason is that the testing is based on importing/exporting
1744
 
 * items in the internal format of the sync source, which has to be
1745
 
 * text based or even MIMEDIR based (for tests involving synccompare).
1746
 
 */
1747
 
class TestingSyncSource : public SyncSource,
1748
 
    virtual public SyncSourceSession,
1749
 
    virtual public SyncSourceChanges,
1750
 
    virtual public SyncSourceDelete,
1751
 
    virtual public SyncSourceSerialize {
1752
 
 public:
1753
 
    TestingSyncSource(const SyncSourceParams &params) :
1754
 
       SyncSource(params)
1755
 
    {
1756
 
        SyncSourceSession::init(m_operations);
1757
 
        SyncSourceChanges::init(m_operations);
1758
 
        SyncSourceDelete::init(m_operations);
1759
 
        SyncSourceSerialize::init(m_operations);
1760
 
    }
1761
 
    ~TestingSyncSource() {}
1762
 
 
1763
 
    virtual SourceType getSourceType() const { return SyncSourceConfig::getSourceType(); }
1764
 
 
1765
 
    void removeAllItems() {
1766
 
        BOOST_FOREACH(const string &luid, getAllItems()) {
1767
 
            deleteItem(luid);
1768
 
        }
1769
 
    }
1770
 
};
1771
 
 
1772
 
 
1773
 
SE_END_CXX
1774
 
#endif // INCL_SYNCSOURCE