/
StorageHandler.cpp
667 lines (508 loc) · 20.5 KB
/
StorageHandler.cpp
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
/*
* This file is part of buteo-syncml package
*
* Copyright (C) 2010 Nokia Corporation. All rights reserved.
*
* Contact: Sateesh Kavuri <sateesh.kavuri@nokia.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of Nokia Corporation nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "StorageHandler.h"
#include <QMutableMapIterator>
#include "StoragePlugin.h"
#include "SyncItem.h"
#include "ConflictResolver.h"
#include "SyncMLLogging.h"
using namespace DataSync;
StorageHandler::StorageHandler() :
iLargeObject( NULL ),
iLargeObjectSize(0)
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
}
StorageHandler::~StorageHandler()
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
qDeleteAll(iAddList);
qDeleteAll(iReplaceList);
delete iLargeObject;
iLargeObject = NULL;
}
bool StorageHandler::addItem( const ItemId& aItemId,
StoragePlugin& aPlugin,
const SyncItemKey& aLocalKey,
const SyncItemKey& aParentKey,
const QString& aType,
const QString& aFormat,
const QString& aVersion,
const QString& aData )
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
qCDebug(lcSyncML) << "Processing item for add:" << aItemId.iCmdId <<"/" << aItemId.iItemIndex;
if( iLargeObject ) {
qCCritical(lcSyncML) << "Already processing large object, aborting";
return false;
}
SyncItem* newItem = aPlugin.newItem();
if( !newItem ) {
qCCritical(lcSyncML) << "Could not create new item";
return false;
}
//Setting empty string as we dont have any local key for it.
newItem->setKey( aLocalKey );
newItem->setParentKey( aParentKey );
newItem->setType( aType );
newItem->setFormat( aFormat );
newItem->setVersion( aVersion );
if( !newItem->write( 0, aData.toUtf8() ) ) {
delete newItem;
qCCritical(lcSyncML) << "Could not write to item";
return false;
}
iAddList.insert( aItemId, newItem );
qCDebug(lcSyncML) << "Item queued for addition";
return true;
}
bool StorageHandler::replaceItem( const ItemId& aItemId,
StoragePlugin& aPlugin,
const QString& aLocalKey,
const SyncItemKey& aParentKey,
const QString& aType,
const QString& aFormat,
const QString& aVersion,
const QString& aData )
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
qCDebug(lcSyncML) << "Processing item for replace:" << aItemId.iCmdId <<"/" << aItemId.iItemIndex;
if( iLargeObject ) {
qCCritical(lcSyncML) << "Already processing large object, aborting";
return false;
}
SyncItem* item = NULL;
// If local key is empty, this Replace should be handled as Add (this is allowed by the protocol)
if( !aLocalKey.isEmpty() ) {
item = aPlugin.getSyncItem( aLocalKey );
}
if( !item ) {
qCDebug(lcSyncML) << "Could not find item, processing as Add";
return addItem( aItemId, aPlugin, aLocalKey, aParentKey, aType, aFormat, aVersion, aData );
}
item->setParentKey( aParentKey );
item->setType( aType );
item->setFormat( aFormat );
item->setVersion( aVersion );
if( !item->write( 0, aData.toUtf8() ) ) {
delete item;
qCCritical(lcSyncML) << "Could not write to item";
return false;
}
iReplaceList.insert( aItemId, item );
qCDebug(lcSyncML) << "Item queued for replace";
return true;
}
bool StorageHandler::deleteItem( const ItemId& aItemId,
const SyncItemKey& aLocalKey )
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
qCDebug(lcSyncML) << "Processing item for delete:" << aItemId.iCmdId <<"/" << aItemId.iItemIndex;
if( iLargeObject ) {
qCCritical(lcSyncML) << "Already processing large object, aborting";
return false;
}
iDeleteList.insert( aItemId, aLocalKey );
qCDebug(lcSyncML) << "Item queued for delete";
return true;
}
bool StorageHandler::startLargeObjectAdd( StoragePlugin& aPlugin,
const QString& aRemoteKey,
const SyncItemKey& aParentKey,
const QString& aType,
const QString& aFormat,
const QString& aVersion,
qint64 aSize )
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
if( iLargeObject ) {
qCCritical(lcSyncML) << "Already processing large object, aborting";
return false;
}
SyncItem* newItem = aPlugin.newItem();
if( !newItem ) {
qCCritical(lcSyncML) << "Could not create new item for large object";
return false;
}
//Setting empty string as we dont have any local key for it.
newItem->setKey(QString());
newItem->setParentKey( aParentKey );
newItem->setType( aType );
newItem->setFormat( aFormat );
newItem->setVersion( aVersion );
iLargeObject = newItem;
iLargeObjectSize = aSize;
iLargeObjectKey = aRemoteKey;
qCDebug(lcSyncML) << "Large object created for addition";
return true;
}
bool StorageHandler::startLargeObjectReplace( StoragePlugin& aPlugin,
const QString& aLocalKey,
const SyncItemKey& aParentKey,
const QString& aType,
const QString& aFormat,
const QString& aVersion,
qint64 aSize )
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
if( iLargeObject ) {
qCCritical(lcSyncML) << "Already processing large object, aborting";
return false;
}
SyncItem* item = NULL;
// If local key is empty, this Replace should be handled as Add (this is allowed by the protocol)
if( !aLocalKey.isEmpty() ) {
item = aPlugin.getSyncItem( aLocalKey );
}
if( !item ) {
qCCritical(lcSyncML) << "Could not find item, processing as Add";
return startLargeObjectAdd( aPlugin, aLocalKey, aParentKey, aType, aFormat, aVersion, aSize );
}
item->setParentKey( aParentKey );
item->setType( aType );
item->setFormat( aFormat );
item->setVersion( aVersion );
iLargeObject = item;
iLargeObjectSize = aSize;
iLargeObjectKey = aLocalKey;
if( !iLargeObject->resize(0) )
{
qCDebug(lcSyncML) << "Large object created for replace couldn't be resized";
}
qCDebug(lcSyncML) << "Large object created for replace";
return true;
}
bool StorageHandler::buildingLargeObject() const
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
if( iLargeObject ) {
return true;
}
else {
return false;
}
}
bool StorageHandler::matchesLargeObject( const QString& aKey )
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
if( aKey == iLargeObjectKey ) {
return true;
}
else {
delete iLargeObject;
iLargeObject = NULL;
iLargeObjectSize = 0;
iLargeObjectKey.clear();
return false;
}
}
bool StorageHandler::appendLargeObjectData( const QString& aData )
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
if( !iLargeObject ) {
qCCritical(lcSyncML) << "Not building large object, aborting";
return false;
}
if( iLargeObject->write( iLargeObject->getSize(), aData.toUtf8() ) ) {
return true;
}
else {
delete iLargeObject;
iLargeObject = NULL;
iLargeObjectSize = 0;
iLargeObjectKey.clear();
qCCritical(lcSyncML) << "Could not write to large object";
return false;
}
}
bool StorageHandler::finishLargeObject( const ItemId& aItemId )
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
if( !iLargeObject ) {
qCCritical(lcSyncML) << "Not building large object, aborting";
return false;
}
if(iLargeObject->getKey()->isEmpty()) {
qCDebug(lcSyncML) << "Queuing large object for addition";
iLargeObject->setKey(iLargeObjectKey);
iAddList.insert( aItemId, iLargeObject );
}
else {
qCDebug(lcSyncML) << "Queuing large object for replace";
iLargeObject->setKey(iLargeObjectKey);
iReplaceList.insert( aItemId, iLargeObject );
}
iLargeObject = NULL;
iLargeObjectSize = 0;
iLargeObjectKey.clear();
return true;
}
QMap<ItemId, CommitResult> StorageHandler::resolveConflicts( ConflictResolver* aConflictResolver,
QMap<ItemId, SyncItemKey> &aMap,
CommitStatus aStatus )
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
QMutableMapIterator<ItemId, SyncItemKey> i(aMap);
QMap<ItemId, CommitResult> results;
ItemId iId;
while( i.hasNext() ) {
i.next();
CommitResult result;
result.iItemKey = i.value();
result.iStatus = aStatus;
iId.iCmdId = i.key().iCmdId;
iId.iItemIndex = i.key().iItemIndex;
qCDebug(lcSyncML) << "Checking item" << iId.iCmdId <<"/" << iId.iItemIndex << "for conflict";
if( aConflictResolver && aConflictResolver->isConflict( result.iItemKey, false ) ) {
qCDebug(lcSyncML) << "Conflict detected";
if( aConflictResolver->localSideWins() ) {
qCDebug(lcSyncML) << "Conflict resolved, local side wins";
result.iConflict = CONFLICT_LOCAL_WIN;
aConflictResolver->revertLocalChange ( result.iItemKey, CR_MODIFY_TO_ADD );
i.remove();
}
else {
qCDebug(lcSyncML) << "Conflict resolved, remote side wins";
result.iConflict = CONFLICT_REMOTE_WIN;
aConflictResolver->revertLocalChange ( result.iItemKey, CR_REMOVE_LOCAL );
}
}
else {
qCDebug(lcSyncML) << "No conflict detected";
result.iConflict = CONFLICT_NO_CONFLICT;
}
results.insert( iId, result );
}
return results;
}
QMap<ItemId, CommitResult> StorageHandler::resolveConflicts( ConflictResolver* aConflictResolver,
QMap<ItemId, SyncItem*> &aMap,
CommitStatus aStatus )
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
QMap<ItemId, CommitResult> results;
ItemId iId;
QMutableMapIterator<ItemId, SyncItem*> i(aMap);
while( i.hasNext() ) {
i.next();
CommitResult result;
result.iItemKey = *i.value()->getKey();
result.iStatus = aStatus;
iId.iCmdId = i.key().iCmdId;
iId.iItemIndex = i.key().iItemIndex;
qCDebug(lcSyncML) << "Checking item" << iId.iCmdId <<"/" << iId.iItemIndex << "for conflict";
if( aConflictResolver && aConflictResolver->isConflict( result.iItemKey, false ) ) {
qCDebug(lcSyncML) << "Conflict detected";
if( aConflictResolver->localSideWins() ) {
qCDebug(lcSyncML) << "Conflict resolved, local side wins";
result.iConflict = CONFLICT_LOCAL_WIN;
delete i.value();
i.remove();
}
else {
qCDebug(lcSyncML) << "Conflict resolved, remote side wins";
result.iConflict = CONFLICT_REMOTE_WIN;
aConflictResolver->revertLocalChange ( result.iItemKey, CR_REMOVE_LOCAL );
}
}
else {
qCDebug(lcSyncML) << "No conflict detected";
result.iConflict = CONFLICT_NO_CONFLICT;
}
results.insert( iId, result );
}
return results;
}
QMap<ItemId, CommitResult> StorageHandler::commitAddedItems( StoragePlugin& aPlugin,
ConflictResolver* aConflictResolver )
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
QMap<ItemId, CommitResult> results = resolveConflicts (aConflictResolver, iAddList, COMMIT_INIT_ADD);
QList<ItemId> addIds = iAddList.keys();
QList<SyncItem*> addItems = iAddList.values();
qCDebug(lcSyncML) << "Committing" << addItems.count() << "added items";
QList<StoragePlugin::StoragePluginStatus> addStatus = aPlugin.addItems( addItems );
for( int i = 0; i < addStatus.count(); ++i ) {
CommitResult& result = results[addIds[i]];
result.iItemKey = *addItems[i]->getKey();
qCDebug(lcSyncML) << "Item" << addIds[i].iCmdId << "/" << addIds[i].iItemIndex << "committed";
switch( addStatus[i] )
{
case StoragePlugin::STATUS_OK:
{
qCDebug(lcSyncML) << "Commit result: COMMIT_ADDED";
result.iStatus = COMMIT_ADDED;
emit itemProcessed( MOD_ITEM_ADDED, MOD_LOCAL_DATABASE,
aPlugin.getSourceURI() , addItems[i]->getType(), addItems.count() );
break;
}
case StoragePlugin::STATUS_DUPLICATE:
{
qCDebug(lcSyncML) << "Commit result: COMMIT_DUPLICATE";
result.iStatus = COMMIT_DUPLICATE;
emit itemProcessed( MOD_ITEM_ADDED, MOD_LOCAL_DATABASE,
aPlugin.getSourceURI() , addItems[i]->getType(), addItems.count() );
break;
}
default:
{
result.iStatus = generalStatus( addStatus[i] );
emit itemProcessed( MOD_ITEM_ERROR, MOD_LOCAL_DATABASE,
aPlugin.getSourceURI() , addItems[i]->getType(), addItems.count() );
break;
}
}
results.insert( addIds[i], result );
}
qDeleteAll( addItems );
iAddList.clear();
return results;
}
QMap<ItemId, CommitResult> StorageHandler::commitReplacedItems( StoragePlugin& aPlugin,
ConflictResolver* aConflictResolver )
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
QMap<ItemId, CommitResult> results = resolveConflicts (aConflictResolver, iReplaceList, COMMIT_INIT_REPLACE);
QList<ItemId> replaceIds = iReplaceList.keys();
QList<SyncItem*> replaceItems = iReplaceList.values();
qCDebug(lcSyncML) << "Committing" << replaceItems.count() << "replaced items";
QList<StoragePlugin::StoragePluginStatus> replaceStatus = aPlugin.replaceItems( replaceItems );
for( int i = 0; i < replaceStatus.count(); ++i ) {
CommitResult& result = results[replaceIds[i]];
qCDebug(lcSyncML) << "Item" << replaceIds[i].iCmdId << "/" << replaceIds[i].iItemIndex << "committed";
switch( replaceStatus[i] )
{
case StoragePlugin::STATUS_OK:
{
qCDebug(lcSyncML) << "Commit result: COMMIT_REPLACED";
result.iStatus = COMMIT_REPLACED;
emit itemProcessed( MOD_ITEM_MODIFIED, MOD_LOCAL_DATABASE,
aPlugin.getSourceURI() , replaceItems[i]->getType(), replaceItems.count() );
break;
}
case StoragePlugin::STATUS_DUPLICATE:
{
qCDebug(lcSyncML) << "Commit result: COMMIT_DUPLICATE";
result.iStatus = COMMIT_DUPLICATE;
emit itemProcessed( MOD_ITEM_MODIFIED, MOD_LOCAL_DATABASE,
aPlugin.getSourceURI() , replaceItems[i]->getType(), replaceItems.count() );
break;
}
default:
{
result.iStatus = generalStatus( replaceStatus[i] );
emit itemProcessed( MOD_ITEM_ERROR, MOD_LOCAL_DATABASE,
aPlugin.getSourceURI() , replaceItems[i]->getType(), replaceItems.count() );
break;
}
}
}
qDeleteAll( iReplaceList );
iReplaceList.clear();
return results;
}
QMap<ItemId, CommitResult> StorageHandler::commitDeletedItems( StoragePlugin& aPlugin,
ConflictResolver* aConflictResolver )
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
QMap<ItemId, CommitResult> results = resolveConflicts (aConflictResolver, iDeleteList, COMMIT_INIT_DELETE);
QList<ItemId> deleteIds = iDeleteList.keys();
QList<SyncItemKey> deleteItems = iDeleteList.values();
qCDebug(lcSyncML) << "Committing" << deleteItems.count() << "deleted items";
QList<StoragePlugin::StoragePluginStatus> deleteStatus = aPlugin.deleteItems( deleteItems );
for( int i = 0; i < deleteStatus.count(); ++i ) {
CommitResult& result = results[deleteIds[i]];
qCDebug(lcSyncML) << "Item" << deleteIds[i].iCmdId << "/" << deleteIds[i].iItemIndex << "committed";
switch( deleteStatus[i] )
{
case StoragePlugin::STATUS_OK:
{
qCDebug(lcSyncML) << "Commit result: COMMIT_DELETED";
result.iStatus = COMMIT_DELETED;
emit itemProcessed( MOD_ITEM_DELETED, MOD_LOCAL_DATABASE,
aPlugin.getSourceURI() ,aPlugin.getFormatInfo().getPreferredRx().iType, deleteItems.count() );
break;
}
case StoragePlugin::STATUS_NOT_FOUND:
{
qCDebug(lcSyncML) << "Commit result: COMMIT_NOT_DELETED";
result.iStatus = COMMIT_NOT_DELETED;
emit itemProcessed( MOD_ITEM_DELETED, MOD_LOCAL_DATABASE,
aPlugin.getSourceURI() ,aPlugin.getFormatInfo().getPreferredRx().iType, deleteItems.count() );
break;
}
default:
{
result.iStatus = generalStatus( deleteStatus[i] );
emit itemProcessed( MOD_ITEM_ERROR, MOD_LOCAL_DATABASE,
aPlugin.getSourceURI() ,aPlugin.getFormatInfo().getPreferredRx().iType, deleteItems.count() );
break;
}
}
}
iDeleteList.clear();
return results;
}
CommitStatus StorageHandler::generalStatus( StoragePlugin::StoragePluginStatus aStatus ) const
{
FUNCTION_CALL_TRACE(lcSyncMLTrace);
CommitStatus status;
switch( aStatus )
{
case StoragePlugin::STATUS_OBJECT_TOO_BIG:
{
qCDebug(lcSyncML) << "Commit result: COMMIT_ITEM_TOO_BIG";
status = COMMIT_ITEM_TOO_BIG;
break;
}
case StoragePlugin::STATUS_STORAGE_FULL:
{
qCDebug(lcSyncML) << "Commit result: COMMIT_NOT_ENOUGH_SPACE";
status = COMMIT_NOT_ENOUGH_SPACE;
break;
}
case StoragePlugin::STATUS_INVALID_FORMAT:
{
qCDebug(lcSyncML) << "Commit result: COMMIT_UNSUPPORTED_FORMAT";
status = COMMIT_UNSUPPORTED_FORMAT;
break;
}
default:
{
qCDebug(lcSyncML) << "Commit result: COMMIT_GENERAL_ERROR";
status = COMMIT_GENERAL_ERROR;
break;
}
}
return status;
}