Debian Codegraph @

storageplugins/hcalendar/CalendarStorage.cpp

raw
1 /*
2 * This file is part of buteo-sync-plugins package
3 *
4 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5 * Copyright (C) 2013 - 2021 Jolla Ltd.
6 *
7 * Contact: Sateesh Kavuri <[email protected]>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * version 2.1 as published by the Free Software Foundation.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 */
24
25 #include "CalendarStorage.h"
26
27 #include <QFile>
28 #include <QStringListIterator>
29
30 #include "SimpleItem.h"
31 #include "SyncMLCommon.h"
32 #include "SyncMLConfig.h"
33
34 #include "SyncMLPluginLogging.h"
35
36 // @todo: Because CalendarMaemo does not support batched operations ( or it does
37 // but we can't use it as we cannot retrieve the id's of committed items ),
38 // batched operations are currently done in series.
39
40 const char* CTCAPSFILENAME11 = "CTCaps_calendar_11.xml";
41 const char* CTCAPSFILENAME12 = "CTCaps_calendar_12.xml";
42
43
44 CalendarStorage::CalendarStorage( const QString& aPluginName )
45 : Buteo::StoragePlugin(aPluginName)
46 {
47 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
48
49 iCommitNow = true;
50 iStorageType = VCALENDAR_FORMAT;
51 }
52
53 CalendarStorage::~CalendarStorage()
54 {
55 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
56 }
57
58 bool CalendarStorage::init( const QMap<QString, QString>& aProperties )
59 {
60 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
61
62 iProperties = aProperties;
63
64 // Use remote name (e.g. bt name) as notebook name.
65 if (iProperties.contains(Buteo::KEY_REMOTE_NAME)) {
66 qCDebug(lcSyncMLPlugin) << "Using remote name as notebook name";
67 iProperties[NOTEBOOKNAME] = iProperties.value(Buteo::KEY_REMOTE_NAME);
68 }
69 else if( iProperties.value( NOTEBOOKNAME ).isEmpty() ) {
70 qCWarning(lcSyncMLPlugin) << NOTEBOOKNAME << " property not found" <<
71 "for calendar storage, using default of" <<
72 DEFAULT_NOTEBOOK_NAME;
73 iProperties[NOTEBOOKNAME] = DEFAULT_NOTEBOOK_NAME;
74 }
75
76 qCDebug(lcSyncMLPlugin) << "Initializing calendar, notebook name:" << iProperties[NOTEBOOKNAME];
77
78 if( !iCalendar.init( iProperties[NOTEBOOKNAME], iProperties[Buteo::KEY_UUID] ) ) {
79 return false;
80 }
81
82 if( iProperties[CALENDAR_FORMAT] == CALENDAR_FORMAT_ICAL )
83 {
84 qCDebug(lcSyncMLPlugin) << "The calendar storage is using icalendar format";
85 iStorageType = ICALENDAR_FORMAT;
86 iProperties[STORAGE_DEFAULT_MIME_PROP] = "text/calendar";
87 iProperties[STORAGE_DEFAULT_MIME_VERSION_PROP] = "2.0";
88 }
89 else
90 {
91 qCDebug(lcSyncMLPlugin) << "The calendar storage is using vcalendar format";
92 iStorageType = VCALENDAR_FORMAT;
93 }
94
95 iProperties[STORAGE_SYNCML_CTCAPS_PROP_11] = getCtCaps( CTCAPSFILENAME11 );
96 iProperties[STORAGE_SYNCML_CTCAPS_PROP_12] = getCtCaps( CTCAPSFILENAME12 );
97
98 return true;
99 }
100
101 bool CalendarStorage::uninit()
102 {
103 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
104
105 return iCalendar.uninit();
106 }
107
108 bool CalendarStorage::getAllItems( QList<Buteo::StorageItem*>& aItems )
109 {
110 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
111
112 qCDebug(lcSyncMLPlugin) << "Retrieving all calendar events and todo's";
113
114 KCalendarCore::Incidence::List incidences;
115
116 if( !iCalendar.getAllIncidences( incidences ) ) {
117 qCDebug(lcSyncMLPlugin) << "Could not retrieve all calendar events and todo's";
118 return false;
119 }
120
121 retrieveItems( incidences, aItems );
122
123 qCDebug(lcSyncMLPlugin) << "Found" << aItems.count() << "items";
124
125 return true;
126 }
127
128 bool CalendarStorage::getAllItemIds( QList<QString>& aItemIds )
129 {
130 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
131
132 qCDebug(lcSyncMLPlugin) << "Retrieving all calendar events and todo's";
133
134 KCalendarCore::Incidence::List incidences;
135
136 if( !iCalendar.getAllIncidences( incidences ) ) {
137 qCDebug(lcSyncMLPlugin) << "Could not retrieve all calendar events and todo's";
138 return false;
139 }
140
141 retrieveIds( incidences, aItemIds );
142
143 qCDebug(lcSyncMLPlugin) << "Found" << aItemIds.count() << "items";
144
145 return true;
146 }
147
148 bool CalendarStorage::getNewItems( QList<Buteo::StorageItem*>& aNewItems, const QDateTime& aTime )
149 {
150 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
151
152 qCDebug(lcSyncMLPlugin) << "Retrieving new calendar events and todo's";
153
154 KCalendarCore::Incidence::List incidences;
155
156 if( !iCalendar.getAllNew( incidences, normalizeTime( aTime ) ) ) {
157 qCDebug(lcSyncMLPlugin) << "Could not retrieve new calendar events and todo's";
158 return false;
159 }
160
161 retrieveItems( incidences, aNewItems );
162
163 qCDebug(lcSyncMLPlugin) << "Found" << aNewItems.count() << "new items";
164
165 return true;
166 }
167
168 bool CalendarStorage::getNewItemIds( QList<QString>& aNewItemIds, const QDateTime& aTime )
169 {
170 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
171
172 qCDebug(lcSyncMLPlugin) << "Retrieving new calendar events and todo's";
173
174 KCalendarCore::Incidence::List incidences;
175
176 if( !iCalendar.getAllNew( incidences, normalizeTime( aTime ) ) ) {
177 qCDebug(lcSyncMLPlugin) << "Could not retrieve new calendar events and todo's";
178 return false;
179 }
180
181 retrieveIds( incidences, aNewItemIds );
182
183 qCDebug(lcSyncMLPlugin) << "Found" << aNewItemIds.count() << "new items";
184
185 return true;
186 }
187
188 bool CalendarStorage::getModifiedItems( QList<Buteo::StorageItem*>& aModifiedItems, const QDateTime& aTime )
189 {
190 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
191
192 qCDebug(lcSyncMLPlugin) << "Retrieving modified calendar events and todo's";
193
194 KCalendarCore::Incidence::List incidences;
195
196 if( !iCalendar.getAllModified( incidences, normalizeTime( aTime ) ) ) {
197 qCDebug(lcSyncMLPlugin) << "Could not retrieve modified calendar events and todo's";
198 return false;
199 }
200
201 retrieveItems( incidences, aModifiedItems );
202
203 qCDebug(lcSyncMLPlugin) << "Found" << aModifiedItems.count() << "modified items";
204
205 return true;
206 }
207
208 bool CalendarStorage::getModifiedItemIds( QList<QString>& aModifiedItemIds, const QDateTime& aTime )
209 {
210 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
211
212 qCDebug(lcSyncMLPlugin) << "Retrieving modified calendar events and todo's";
213
214 KCalendarCore::Incidence::List incidences;
215
216 if( !iCalendar.getAllModified( incidences, normalizeTime( aTime ) ) ) {
217 qCDebug(lcSyncMLPlugin) << "Could not retrieve modified calendar events and todo's";
218 return false;
219 }
220
221 retrieveIds( incidences, aModifiedItemIds );
222
223 qCDebug(lcSyncMLPlugin) << "Found" << aModifiedItemIds.count() << "modified items";
224
225 return true;
226 }
227
228 bool CalendarStorage::getDeletedItemIds( QList<QString>& aDeletedItemIds, const QDateTime& aTime )
229 {
230 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
231
232 qCDebug(lcSyncMLPlugin) << "Retrieving deleted calendar events and todo's";
233
234 KCalendarCore::Incidence::List incidences;
235
236 if( !iCalendar.getAllDeleted( incidences, normalizeTime( aTime ) ) ) {
237 qCDebug(lcSyncMLPlugin) << "Could not retrieve deleted calendar events and todo's";
238 return false;
239 }
240
241 retrieveIds( incidences, aDeletedItemIds );
242
243 qCDebug(lcSyncMLPlugin) << "Found" << aDeletedItemIds.count() << "deleted items";
244
245 return true;
246 }
247
248 Buteo::StorageItem* CalendarStorage::newItem()
249 {
250 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
251
252 return new SimpleItem();
253 }
254
255 QList<Buteo::StorageItem*> CalendarStorage::getItems( const QStringList& aItemIdList )
256 {
257 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
258
259 KCalendarCore::Incidence::List incidences;
260 KCalendarCore::Incidence::Ptr item;
261 QList<Buteo::StorageItem*> items;
262 QStringListIterator itr( aItemIdList );
263
264 while( itr.hasNext() )
265 {
266 //TODO Does calendar backend support batch fetch, check!
267 QString id = itr.next();
268 item = iCalendar.getIncidence( id );
269 if( item )
270 {
271 incidences.append( item );
272 }
273 else
274 {
275 qCWarning(lcSyncMLPlugin) << "Could not find item " << id;
276 }
277 }
278
279 retrieveItems( incidences, items );
280
281 return items;
282 }
283
284 Buteo::StorageItem* CalendarStorage::getItem( const QString& aItemId )
285 {
286 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
287
288 KCalendarCore::Incidence::Ptr item = iCalendar.getIncidence( aItemId );
289
290 if( item ) {
291 return retrieveItem( item );
292 }
293 else {
294 qCWarning(lcSyncMLPlugin) << "Could not find item:" << aItemId;
295 return NULL;
296 }
297
298 }
299
300 CalendarStorage::OperationStatus CalendarStorage::addItem( Buteo::StorageItem& aItem )
301 {
302
303 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
304
305 KCalendarCore::Incidence::Ptr item = generateIncidence( aItem );
306
307 if( !item ) {
308 qCWarning(lcSyncMLPlugin) << "Item has invalid format";
309 return STATUS_INVALID_FORMAT;
310 }
311
312 if( !iCalendar.addIncidence( item, iCommitNow ) ) {
313 qCWarning(lcSyncMLPlugin) << "Could not add item";
314 // no need to delete item as item is owned by backend
315 return STATUS_ERROR;
316 }
317
318 if (item->recurrenceId().isValid()) {
319 QString reccurId = QString(ID_SEPARATOR).append(item->recurrenceId().toString());
320 aItem.setId( item->uid().append(reccurId) );
321 } else {
322 aItem.setId( item->uid() );
323 }
324
325 qCDebug(lcSyncMLPlugin) << "Item successfully added:" << aItem.getId();
326
327 return STATUS_OK;
328
329 }
330
331 QList<CalendarStorage::OperationStatus> CalendarStorage::addItems( const QList<Buteo::StorageItem*>& aItems )
332 {
333 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
334
335 QList<OperationStatus> results;
336
337 // Disable auto commit as this is a batch add
338 iCommitNow = false;
339 for( int i = 0; i < aItems.count(); ++i ) {
340 results.append( addItem( *aItems[i] ) );
341 }
342
343 //Do a batch commit now
344 if( iCalendar.commitChanges() )
345 {
346 qCDebug(lcSyncMLPlugin) << "Items successfully added";
347 }
348 iCommitNow = true;
349
350 return results;
351
352 }
353
354 CalendarStorage::OperationStatus CalendarStorage::modifyItem( Buteo::StorageItem& aItem )
355 {
356 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
357
358 KCalendarCore::Incidence::Ptr item = generateIncidence( aItem );
359
360 if( !item ) {
361 qCWarning(lcSyncMLPlugin) << "Item has invalid format";
362 return STATUS_INVALID_FORMAT;
363 }
364
365 if( !iCalendar.modifyIncidence( item, aItem.getId(), iCommitNow ) ) {
366 qCWarning(lcSyncMLPlugin) << "Could not replace item:" << aItem.getId();
367 // no need to delete item as item is owned by backend
368 return STATUS_ERROR;
369 }
370
371 qCDebug(lcSyncMLPlugin) << "Item successfully replaced:" << aItem.getId();
372
373 // modifyIncidence doesn't take ownership of the item, need to delete it.
374 item.clear();
375
376 return STATUS_OK;
377 }
378
379 QList<CalendarStorage::OperationStatus> CalendarStorage::modifyItems( const QList<Buteo::StorageItem*>& aItems )
380 {
381 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
382
383 QList<OperationStatus> results;
384
385 // Disable auto commit as this is a batch add
386 iCommitNow = false;
387 for( int i = 0; i < aItems.count(); ++i ) {
388 results.append( modifyItem( *aItems[i] ) );
389 }
390
391 //Do a batch commit now
392 if( iCalendar.commitChanges() )
393 {
394 qCDebug(lcSyncMLPlugin) << "Items successfully modified";
395 }
396 iCommitNow = true;
397
398 return results;
399 }
400
401 CalendarStorage::OperationStatus CalendarStorage::deleteItem( const QString& aItemId )
402 {
403 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
404
405 CalendarBackend::ErrorStatus error = iCalendar.deleteIncidence( aItemId);
406 CalendarStorage::OperationStatus status = mapErrorStatus(error);
407 return status;
408 }
409
410 QList<CalendarStorage::OperationStatus> CalendarStorage::deleteItems( const QList<QString>& aItemIds )
411 {
412 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
413
414 QList<OperationStatus> results;
415
416 for( int i = 0; i < aItemIds.count(); ++i ) {
417 results.append( deleteItem( aItemIds[i] ) );
418 }
419
420 return results;
421 }
422
423 KCalendarCore::Incidence::Ptr CalendarStorage::generateIncidence( Buteo::StorageItem& aItem )
424 {
425 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
426
427 KCalendarCore::Incidence::Ptr incidence;
428 QByteArray itemData;
429
430 if( !aItem.read( 0, aItem.getSize(), itemData ) ) {
431 qCWarning(lcSyncMLPlugin) << "Could not read item data";
432 return incidence;
433 }
434
435 QString data = QString::fromUtf8( itemData.data() );
436
437 // we are getting a temporary incidence from the calendar
438 if( iStorageType == VCALENDAR_FORMAT )
439 {
440 incidence = iCalendar.getIncidenceFromVcal( data );
441 }
442 else
443 {
444 incidence = iCalendar.getIncidenceFromIcal( data );
445 }
446
447 return incidence;
448 }
449
450 void CalendarStorage::retrieveItems( KCalendarCore::Incidence::List& aIncidences, QList<Buteo::StorageItem*>& aItems )
451 {
452 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
453
454 for( int i = 0; i < aIncidences.count(); ++i ) {
455 Buteo::StorageItem* item = retrieveItem( aIncidences[i] );
456 aItems.append( item );
457 }
458 }
459
460 Buteo::StorageItem* CalendarStorage::retrieveItem( KCalendarCore::Incidence::Ptr& aIncidence )
461 {
462 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
463
464 QString data;
465
466 if(iStorageType == VCALENDAR_FORMAT)
467 {
468 data = iCalendar.getVCalString( aIncidence );
469 }
470 else
471 {
472 data = iCalendar.getICalString( aIncidence);
473 }
474
475 Buteo::StorageItem* item = newItem();
476 QString iId = aIncidence->uid();
477 if (aIncidence->recurrenceId().isValid()) {
478 QString reccurId = QString(ID_SEPARATOR).append(aIncidence->recurrenceId().toString());
479 iId.append(reccurId);
480 }
481 item->setId(iId);
482 item->write( 0, data.toUtf8() );
483 item->setType(iProperties[STORAGE_DEFAULT_MIME_PROP]);
484
485 return item;
486
487 }
488
489 void CalendarStorage::retrieveIds( KCalendarCore::Incidence::List& aIncidences, QList<QString>& aIds )
490 {
491 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
492
493 for( int i = 0; i < aIncidences.count(); ++i ) {
494 QString iID = aIncidences[i]->uid();
495 if (aIncidences[i]->recurrenceId().isValid()) {
496 QString reccurId = QString(ID_SEPARATOR).append(aIncidences[i]->recurrenceId().toString());
497 iID = iID.append(reccurId);
498 }
499 aIds.append( iID );
500 }
501
502 }
503
504 QDateTime CalendarStorage::normalizeTime( const QDateTime& aTime ) const
505 {
506 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
507
508 QDateTime normTime = aTime;
509
510 QTime time = aTime.time();
511 time.setHMS( time.hour(), time.minute(), time.second(), 0 );
512
513 normTime.setTime( time );
514
515 normTime = normTime.toUTC();
516
517 return normTime;
518 }
519
520 QByteArray CalendarStorage::getCtCaps( const QString& aFilename ) const
521 {
522 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
523
524 QFile ctCapsFile( SyncMLConfig::getXmlDataPath() + aFilename );
525 QByteArray ctCaps;
526
527 if( ctCapsFile.open(QIODevice::ReadOnly)) {
528 ctCaps = ctCapsFile.readAll();
529 ctCapsFile.close();
530 } else {
531 qCWarning(lcSyncMLPlugin) << "Failed to open CTCaps file for calendar storage:" << aFilename;
532 }
533
534 return ctCaps;
535
536 }
537
538 CalendarStorage::OperationStatus CalendarStorage::mapErrorStatus\
539 (const CalendarBackend::ErrorStatus &aCalenderError) const
540 {
541 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
542 CalendarStorage::OperationStatus iStorageStatus = STATUS_OK;
543
544 switch(aCalenderError) {
545 case CalendarBackend::STATUS_OK:
546 iStorageStatus = STATUS_OK;
547 break;
548
549 case CalendarBackend::STATUS_ITEM_DUPLICATE:
550 iStorageStatus = STATUS_DUPLICATE;
551 break;
552
553 case CalendarBackend::STATUS_ITEM_NOT_FOUND:
554 iStorageStatus = STATUS_NOT_FOUND;
555 break;
556
557 case CalendarBackend::STATUS_GENERIC_ERROR:
558 iStorageStatus = STATUS_ERROR;
559 break;
560
561 default:
562 iStorageStatus = STATUS_ERROR;
563 break;
564 }
565 return iStorageStatus;
566 }
567
568
569 Buteo::StoragePlugin* CalendarStoragePluginLoader::createPlugin(const QString& aPluginName)
570 {
571 return new CalendarStorage(aPluginName);
572 }