File: CalendarStorage.cpp

package info (click to toggle)
buteo-sync-plugins 0.8.36-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,076 kB
  • sloc: cpp: 8,130; xml: 755; sh: 207; perl: 82; makefile: 11
file content (572 lines) | stat: -rwxr-xr-x 16,374 bytes parent folder | download
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
/*
 * This file is part of buteo-sync-plugins package
 *
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * Copyright (C) 2013 - 2021 Jolla Ltd.
 *
 * Contact: Sateesh Kavuri <sateesh.kavuri@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include "CalendarStorage.h"

#include <QFile>
#include <QStringListIterator>

#include "SimpleItem.h"
#include "SyncMLCommon.h"
#include "SyncMLConfig.h"

#include "SyncMLPluginLogging.h"

// @todo: Because CalendarMaemo does not support batched operations ( or it does
//        but we can't use it as we cannot retrieve the id's of committed items ),
//        batched operations are currently done in series.

const char* CTCAPSFILENAME11 = "CTCaps_calendar_11.xml";
const char* CTCAPSFILENAME12 = "CTCaps_calendar_12.xml";


CalendarStorage::CalendarStorage( const QString& aPluginName )
: Buteo::StoragePlugin(aPluginName)
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    iCommitNow = true;
    iStorageType = VCALENDAR_FORMAT;
}

CalendarStorage::~CalendarStorage()
{
	FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
}

bool CalendarStorage::init( const QMap<QString, QString>& aProperties )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    iProperties = aProperties;

    // Use remote name (e.g. bt name) as notebook name.
    if (iProperties.contains(Buteo::KEY_REMOTE_NAME)) {
        qCDebug(lcSyncMLPlugin) << "Using remote name as notebook name";
        iProperties[NOTEBOOKNAME] = iProperties.value(Buteo::KEY_REMOTE_NAME);
    }
    else if( iProperties.value( NOTEBOOKNAME ).isEmpty() ) {
        qCWarning(lcSyncMLPlugin) << NOTEBOOKNAME << " property not found" <<
                "for calendar storage, using default of" <<
                DEFAULT_NOTEBOOK_NAME;
        iProperties[NOTEBOOKNAME] = DEFAULT_NOTEBOOK_NAME;
    }

    qCDebug(lcSyncMLPlugin) << "Initializing calendar, notebook name:" <<  iProperties[NOTEBOOKNAME]; 

    if( !iCalendar.init( iProperties[NOTEBOOKNAME], iProperties[Buteo::KEY_UUID] ) ) {
        return false;
    }

    if( iProperties[CALENDAR_FORMAT] == CALENDAR_FORMAT_ICAL )
    {
        qCDebug(lcSyncMLPlugin) << "The calendar storage is using icalendar format";
        iStorageType = ICALENDAR_FORMAT;
        iProperties[STORAGE_DEFAULT_MIME_PROP] = "text/calendar";
        iProperties[STORAGE_DEFAULT_MIME_VERSION_PROP] = "2.0";
    }
    else
    {
        qCDebug(lcSyncMLPlugin) << "The calendar storage is using vcalendar format";
        iStorageType = VCALENDAR_FORMAT;
    }

    iProperties[STORAGE_SYNCML_CTCAPS_PROP_11] = getCtCaps( CTCAPSFILENAME11 );
    iProperties[STORAGE_SYNCML_CTCAPS_PROP_12] = getCtCaps( CTCAPSFILENAME12 );

    return true;
}

bool CalendarStorage::uninit()
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    return iCalendar.uninit();
}

bool CalendarStorage::getAllItems( QList<Buteo::StorageItem*>& aItems )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    qCDebug(lcSyncMLPlugin) << "Retrieving all calendar events and todo's";

    KCalendarCore::Incidence::List incidences;

    if( !iCalendar.getAllIncidences( incidences ) ) {
        qCDebug(lcSyncMLPlugin) << "Could not retrieve all calendar events and todo's";
        return false;
    }

    retrieveItems( incidences, aItems );

    qCDebug(lcSyncMLPlugin) << "Found" << aItems.count() << "items";

    return true;
}

bool CalendarStorage::getAllItemIds( QList<QString>& aItemIds )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    qCDebug(lcSyncMLPlugin) << "Retrieving all calendar events and todo's";

    KCalendarCore::Incidence::List incidences;

    if( !iCalendar.getAllIncidences( incidences ) ) {
        qCDebug(lcSyncMLPlugin) << "Could not retrieve all calendar events and todo's";
        return false;
    }

    retrieveIds( incidences, aItemIds );

    qCDebug(lcSyncMLPlugin) << "Found" << aItemIds.count() << "items";

    return true;
}

bool CalendarStorage::getNewItems( QList<Buteo::StorageItem*>& aNewItems, const QDateTime& aTime )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    qCDebug(lcSyncMLPlugin) << "Retrieving new calendar events and todo's";

    KCalendarCore::Incidence::List incidences;

    if( !iCalendar.getAllNew( incidences, normalizeTime( aTime ) ) ) {
        qCDebug(lcSyncMLPlugin) << "Could not retrieve new calendar events and todo's";
        return false;
    }

    retrieveItems( incidences, aNewItems );

    qCDebug(lcSyncMLPlugin) << "Found" << aNewItems.count() << "new items";

    return true;
}

bool CalendarStorage::getNewItemIds( QList<QString>& aNewItemIds, const QDateTime& aTime )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    qCDebug(lcSyncMLPlugin) << "Retrieving new calendar events and todo's";

    KCalendarCore::Incidence::List incidences;

    if( !iCalendar.getAllNew( incidences, normalizeTime( aTime ) ) ) {
        qCDebug(lcSyncMLPlugin) << "Could not retrieve new calendar events and todo's";
        return false;
    }

    retrieveIds( incidences, aNewItemIds );

    qCDebug(lcSyncMLPlugin) << "Found" << aNewItemIds.count() << "new items";

    return true;
}

bool CalendarStorage::getModifiedItems( QList<Buteo::StorageItem*>& aModifiedItems, const QDateTime& aTime )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    qCDebug(lcSyncMLPlugin) << "Retrieving modified calendar events and todo's";

    KCalendarCore::Incidence::List incidences;

    if( !iCalendar.getAllModified( incidences, normalizeTime( aTime ) ) ) {
        qCDebug(lcSyncMLPlugin) << "Could not retrieve modified calendar events and todo's";
        return false;
    }

    retrieveItems( incidences, aModifiedItems );

    qCDebug(lcSyncMLPlugin) << "Found" << aModifiedItems.count() << "modified items";

    return true;
}

bool CalendarStorage::getModifiedItemIds( QList<QString>& aModifiedItemIds, const QDateTime& aTime )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    qCDebug(lcSyncMLPlugin) << "Retrieving modified calendar events and todo's";

    KCalendarCore::Incidence::List incidences;

    if( !iCalendar.getAllModified( incidences, normalizeTime( aTime ) ) ) {
        qCDebug(lcSyncMLPlugin) << "Could not retrieve modified calendar events and todo's";
        return false;
    }

    retrieveIds( incidences, aModifiedItemIds );

    qCDebug(lcSyncMLPlugin) << "Found" << aModifiedItemIds.count() << "modified items";

    return true;
}

bool CalendarStorage::getDeletedItemIds( QList<QString>& aDeletedItemIds, const QDateTime& aTime )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    qCDebug(lcSyncMLPlugin) << "Retrieving deleted calendar events and todo's";

    KCalendarCore::Incidence::List incidences;

    if( !iCalendar.getAllDeleted( incidences, normalizeTime( aTime ) ) ) {
        qCDebug(lcSyncMLPlugin) << "Could not retrieve deleted calendar events and todo's";
        return false;
    }

    retrieveIds( incidences, aDeletedItemIds );

    qCDebug(lcSyncMLPlugin) << "Found" << aDeletedItemIds.count() << "deleted items";

    return true;
}

Buteo::StorageItem* CalendarStorage::newItem()
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    return new SimpleItem();
}

QList<Buteo::StorageItem*> CalendarStorage::getItems( const QStringList& aItemIdList )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    KCalendarCore::Incidence::List incidences;
    KCalendarCore::Incidence::Ptr item;
    QList<Buteo::StorageItem*> items;
    QStringListIterator itr( aItemIdList );

    while( itr.hasNext() )
    {
        //TODO Does calendar backend support batch fetch, check!
        QString id = itr.next();
        item = iCalendar.getIncidence( id );
        if( item )
        {
            incidences.append( item );
        }
        else
        {
            qCWarning(lcSyncMLPlugin) << "Could not find item " << id;
        }
    }

    retrieveItems( incidences, items );
 
    return items;
}

Buteo::StorageItem* CalendarStorage::getItem( const QString& aItemId )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    KCalendarCore::Incidence::Ptr item = iCalendar.getIncidence( aItemId );

    if( item ) {
        return retrieveItem( item );
    }
    else {
        qCWarning(lcSyncMLPlugin) << "Could not find item:" << aItemId;
        return NULL;
    }

}

CalendarStorage::OperationStatus CalendarStorage::addItem( Buteo::StorageItem& aItem )
{

    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    KCalendarCore::Incidence::Ptr item = generateIncidence( aItem );

    if( !item ) {
        qCWarning(lcSyncMLPlugin) << "Item has invalid format";
        return STATUS_INVALID_FORMAT;
    }

    if( !iCalendar.addIncidence( item, iCommitNow ) ) {
        qCWarning(lcSyncMLPlugin) << "Could not add item";
        // no need to delete item as item is owned by backend
        return STATUS_ERROR;
    }

    if (item->recurrenceId().isValid()) {  
	QString reccurId = QString(ID_SEPARATOR).append(item->recurrenceId().toString());    
       	aItem.setId( item->uid().append(reccurId) );
     } else {
        aItem.setId( item->uid() );
    }

    qCDebug(lcSyncMLPlugin) << "Item successfully added:" << aItem.getId();

    return STATUS_OK;

}

QList<CalendarStorage::OperationStatus> CalendarStorage::addItems( const QList<Buteo::StorageItem*>& aItems )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    QList<OperationStatus> results;

    // Disable auto commit as this is a batch add
    iCommitNow = false; 
    for( int i = 0; i < aItems.count(); ++i ) {
        results.append( addItem( *aItems[i] ) );
    }

    //Do a batch commit now
    if( iCalendar.commitChanges() )
    {
        qCDebug(lcSyncMLPlugin) << "Items successfully added";
    }
    iCommitNow = true; 

    return results;

}

CalendarStorage::OperationStatus CalendarStorage::modifyItem( Buteo::StorageItem& aItem )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    KCalendarCore::Incidence::Ptr item = generateIncidence( aItem );

    if( !item ) {
        qCWarning(lcSyncMLPlugin) << "Item has invalid format";
        return STATUS_INVALID_FORMAT;
    }
    
    if( !iCalendar.modifyIncidence( item, aItem.getId(), iCommitNow ) ) {
        qCWarning(lcSyncMLPlugin) << "Could not replace item:" << aItem.getId();
        // no need to delete item as item is owned by backend
        return STATUS_ERROR;
    }

    qCDebug(lcSyncMLPlugin) << "Item successfully replaced:" << aItem.getId();

    // modifyIncidence doesn't take ownership of the item, need to delete it.
    item.clear();

    return STATUS_OK;
}

QList<CalendarStorage::OperationStatus> CalendarStorage::modifyItems( const QList<Buteo::StorageItem*>& aItems )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    QList<OperationStatus> results;

    // Disable auto commit as this is a batch add
    iCommitNow = false; 
    for( int i = 0; i < aItems.count(); ++i ) {
        results.append( modifyItem( *aItems[i] ) );
    }

    //Do a batch commit now
    if( iCalendar.commitChanges() )
    {
        qCDebug(lcSyncMLPlugin) << "Items successfully modified";
    }
    iCommitNow = true; 

    return results;
}

CalendarStorage::OperationStatus CalendarStorage::deleteItem( const QString& aItemId )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    CalendarBackend::ErrorStatus error =  iCalendar.deleteIncidence( aItemId);
    CalendarStorage::OperationStatus status = mapErrorStatus(error);
    return status;
}

QList<CalendarStorage::OperationStatus> CalendarStorage::deleteItems( const QList<QString>& aItemIds )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    QList<OperationStatus> results;

    for( int i = 0; i < aItemIds.count(); ++i ) {
        results.append( deleteItem( aItemIds[i] ) );
    }

    return results;
}

KCalendarCore::Incidence::Ptr CalendarStorage::generateIncidence( Buteo::StorageItem& aItem )
{
	FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    KCalendarCore::Incidence::Ptr incidence;
    QByteArray itemData;

    if( !aItem.read( 0, aItem.getSize(), itemData ) ) {
        qCWarning(lcSyncMLPlugin) << "Could not read item data";
        return incidence;
    }

    QString data = QString::fromUtf8( itemData.data() );

    // we are getting a temporary incidence from the calendar
    if( iStorageType == VCALENDAR_FORMAT )
    {
        incidence = iCalendar.getIncidenceFromVcal( data );
    }
    else
    {
        incidence = iCalendar.getIncidenceFromIcal( data );
    }

    return incidence;
}

void CalendarStorage::retrieveItems( KCalendarCore::Incidence::List& aIncidences, QList<Buteo::StorageItem*>& aItems )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    for( int i = 0; i < aIncidences.count(); ++i ) {
        Buteo::StorageItem* item = retrieveItem( aIncidences[i] );
        aItems.append( item );
    }
}

Buteo::StorageItem* CalendarStorage::retrieveItem( KCalendarCore::Incidence::Ptr& aIncidence )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    QString data;

    if(iStorageType == VCALENDAR_FORMAT)
    {
        data = iCalendar.getVCalString( aIncidence );
    }
    else
    {
        data = iCalendar.getICalString( aIncidence);
    }

    Buteo::StorageItem* item = newItem();
    QString iId = aIncidence->uid();
    if (aIncidence->recurrenceId().isValid()) {  
	QString reccurId = QString(ID_SEPARATOR).append(aIncidence->recurrenceId().toString());    
       	iId.append(reccurId);
    }  
    item->setId(iId);
    item->write( 0, data.toUtf8() );
    item->setType(iProperties[STORAGE_DEFAULT_MIME_PROP]);

    return item;

}

void CalendarStorage::retrieveIds( KCalendarCore::Incidence::List& aIncidences, QList<QString>& aIds )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    for( int i = 0; i < aIncidences.count(); ++i ) {
        QString iID = aIncidences[i]->uid();
	if (aIncidences[i]->recurrenceId().isValid()) {  
	    QString reccurId = QString(ID_SEPARATOR).append(aIncidences[i]->recurrenceId().toString());    
            iID = iID.append(reccurId);
	}  
	aIds.append( iID );
    }

}

QDateTime CalendarStorage::normalizeTime( const QDateTime& aTime ) const
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    QDateTime normTime = aTime;

    QTime time = aTime.time();
    time.setHMS( time.hour(), time.minute(), time.second(), 0 );

    normTime.setTime( time );

    normTime = normTime.toUTC();

    return normTime;
}

QByteArray CalendarStorage::getCtCaps( const QString& aFilename ) const
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    QFile ctCapsFile( SyncMLConfig::getXmlDataPath() + aFilename  );
    QByteArray ctCaps;

    if( ctCapsFile.open(QIODevice::ReadOnly)) {
       ctCaps = ctCapsFile.readAll();
       ctCapsFile.close();
    } else {
        qCWarning(lcSyncMLPlugin) << "Failed to open CTCaps file for calendar storage:" << aFilename;
    }

    return ctCaps;

}

CalendarStorage::OperationStatus CalendarStorage::mapErrorStatus\
        (const CalendarBackend::ErrorStatus &aCalenderError) const
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
    CalendarStorage::OperationStatus iStorageStatus = STATUS_OK;

    switch(aCalenderError) {
    case CalendarBackend::STATUS_OK:
            iStorageStatus = STATUS_OK;
            break;

    case CalendarBackend::STATUS_ITEM_DUPLICATE:
            iStorageStatus = STATUS_DUPLICATE;
            break;

    case CalendarBackend::STATUS_ITEM_NOT_FOUND:
            iStorageStatus = STATUS_NOT_FOUND;
            break;

    case CalendarBackend::STATUS_GENERIC_ERROR:
            iStorageStatus = STATUS_ERROR;
            break;

    default:
            iStorageStatus = STATUS_ERROR;
            break;
    }
    return iStorageStatus;
}


Buteo::StoragePlugin* CalendarStoragePluginLoader::createPlugin(const QString& aPluginName)
{
    return new CalendarStorage(aPluginName);
}