Debian Codegraph @

storageplugins/hcalendar/CalendarBackend.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 *
6 * Contact: Sateesh Kavuri <[email protected]>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * version 2.1 as published by the Free Software Foundation.
11 *
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 */
23
24 #include "CalendarBackend.h"
25 #include <KCalendarCore/Event>
26 #include <KCalendarCore/Journal>
27 #include "SyncMLPluginLogging.h"
28 #include <QDir>
29 #include <QDebug>
30
31 CalendarBackend::CalendarBackend() : iCalendar( 0 ), iStorage( 0 )
32 {
33 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
34 }
35
36 CalendarBackend::~CalendarBackend()
37 {
38 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
39 }
40
41 bool CalendarBackend::init(const QString &aNotebookName, const QString& aUid)
42 {
43 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
44
45 if( aNotebookName.isEmpty() )
46 {
47 qCDebug(lcSyncMLPlugin) << "NoteBook Name to Sync is expected. It Cannot be Empty";
48 return false;
49 }
50
51 iNotebookStr = aNotebookName;
52
53 iCalendar = mKCal::ExtendedCalendar::Ptr( new mKCal::ExtendedCalendar( QTimeZone::systemTimeZone()) );
54
55 qCDebug(lcSyncMLPlugin) << "Creating Default Maemo Storage";
56 iStorage = iCalendar->defaultStorage( iCalendar );
57
58 bool opened = iStorage->open();
59 if(!opened)
60 {
61 qCDebug(lcSyncMLPluginTrace) << "Calendar storage open failed";
62 }
63
64 mKCal::Notebook::Ptr openedNb;
65
66 // If we have an Uid, we try to get the corresponding Notebook
67 if (!aUid.isEmpty()) {
68 openedNb = iStorage->notebook(aUid);
69
70 // If we didn't get one, we create one and set its Uid
71 if (!openedNb) {
72 openedNb = mKCal::Notebook::Ptr(new mKCal::Notebook(aNotebookName,
73 "Synchronization Created Notebook for " + aNotebookName));
74 if (!openedNb.isNull()) {
75 openedNb->setUid(aUid);
76 if (!iStorage->addNotebook(openedNb)) {
77 qCWarning(lcSyncMLPlugin) << "Failed to add notebook to storage";
78 openedNb.clear();
79 }
80 }
81 }
82 }
83
84 // If we didn't have an Uid or the creation above failed,
85 // we use the default notebook
86 if (openedNb.isNull()) {
87 openedNb = iStorage->defaultNotebook();
88 if(openedNb.isNull()) {
89 qCDebug(lcSyncMLPlugin) << "No default notebook exists, creating one";
90 openedNb = mKCal::Notebook::Ptr(new mKCal::Notebook("Default", QString()));
91 if (!iStorage->setDefaultNotebook(openedNb)) {
92 qCWarning(lcSyncMLPlugin) << "Failed to set default notebook of storage";
93 openedNb.clear();
94 }
95 }
96 }
97
98 bool loaded = false;
99 if(opened && openedNb)
100 {
101 qCDebug(lcSyncMLPlugin) << "Loading all incidences from::" << openedNb->uid();
102 loaded = iStorage->loadNotebookIncidences(openedNb->uid());
103 if(!loaded)
104 {
105 qCWarning(lcSyncMLPlugin) << "Failed to load calendar!";
106 }
107 }
108
109 if (opened && loaded && !openedNb.isNull())
110 {
111 iNotebookStr = openedNb->uid();
112
113 qCDebug(lcSyncMLPlugin) << "Calendar initialized";
114 return true;
115 }
116 else
117 {
118 qCWarning(lcSyncMLPlugin) << "Not able to initialize calendar";
119
120 iStorage.clear();
121
122 qCDebug(lcSyncMLPluginTrace) << "Storage deleted";
123
124 iCalendar.clear();
125
126 qCDebug(lcSyncMLPluginTrace) << "Calendar deleted";
127
128 return false;
129 }
130 }
131
132 bool CalendarBackend::uninit()
133 {
134 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
135
136 if( iStorage ) {
137 qCDebug(lcSyncMLPluginTrace) << "Closing calendar storage...";
138 iStorage->close();
139 qCDebug(lcSyncMLPluginTrace) << "Done";
140 iStorage.clear();
141 }
142
143 if( iCalendar ) {
144 qCDebug(lcSyncMLPluginTrace) << "Closing calendar...";
145 iCalendar->close();
146 qCDebug(lcSyncMLPluginTrace) << "Done";
147 iCalendar.clear();
148 }
149
150 return true;
151 }
152
153 bool CalendarBackend::getAllIncidences( KCalendarCore::Incidence::List& aIncidences )
154 {
155 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
156
157 if( !iStorage ) {
158 return false;
159 }
160
161 if( !iStorage->allIncidences( &aIncidences, iNotebookStr ) ) {
162 qCWarning(lcSyncMLPlugin) << "Error Retrieving ALL Incidences from the Storage ";
163 return false;
164 }
165
166 filterIncidences( aIncidences );
167 return true;
168 }
169
170 void CalendarBackend::filterIncidences(KCalendarCore::Incidence::List& aList)
171 {
172 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
173 QString event(INCIDENCE_TYPE_EVENT);
174 QString todo(INCIDENCE_TYPE_TODO);
175
176 for (int i = 0; i < aList.size(); ++i) {
177 KCalendarCore::Incidence::Ptr incidence = aList.at(i);
178 if ((incidence->type() != KCalendarCore::Incidence::TypeEvent) && (incidence->type() != KCalendarCore::Incidence::TypeTodo)) {
179 qCDebug(lcSyncMLPlugin) << "Removing incidence type" << incidence->typeStr();
180 aList.remove( i, 1);
181 incidence.clear();
182 }
183 }
184 }
185
186 bool CalendarBackend::getAllNew( KCalendarCore::Incidence::List& aIncidences, const QDateTime& aTime )
187 {
188 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
189
190 if( !iStorage ) {
191 return false;
192 }
193
194 if( !iStorage->insertedIncidences( &aIncidences, aTime, iNotebookStr) ) {
195 qCWarning(lcSyncMLPlugin) << "Error Retrieving New Incidences from the Storage";
196 return false;
197 }
198
199 filterIncidences( aIncidences );
200 return true;
201 }
202
203 bool CalendarBackend::getAllModified( KCalendarCore::Incidence::List& aIncidences, const QDateTime& aTime )
204 {
205 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
206
207 if( !iStorage ) {
208 return false;
209 }
210
211 if( !iStorage->modifiedIncidences( &aIncidences, aTime, iNotebookStr ) ) {
212 qCWarning(lcSyncMLPlugin) << " Error retrieving modified Incidences ";
213 return false;
214 }
215
216 filterIncidences( aIncidences );
217 return true;
218 }
219
220 bool CalendarBackend::getAllDeleted( KCalendarCore::Incidence::List& aIncidences, const QDateTime& aTime )
221 {
222 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
223
224 if( !iStorage ) {
225 return false;
226 }
227
228 if( !iStorage->deletedIncidences( &aIncidences, aTime, iNotebookStr ) ) {
229 qCWarning(lcSyncMLPlugin) << " Error retrieving deleted Incidences ";
230 return false;
231 }
232
233 filterIncidences( aIncidences );
234 return true;
235 }
236
237 KCalendarCore::Incidence::Ptr CalendarBackend::getIncidence( const QString& aUID )
238 {
239 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
240
241 QStringList iDs = aUID.split(ID_SEPARATOR);
242 KCalendarCore::Incidence::Ptr incidence;
243 if (iDs.size() == 2) {
244 iStorage->load ( iDs.at(0), QDateTime::fromString(iDs.at(1), Qt::ISODate) );
245 incidence = iCalendar->incidence(iDs.at(0), QDateTime::fromString(iDs.at(1), Qt::ISODate));
246 } else {
247 iStorage->load ( aUID );
248 incidence = iCalendar->incidence( aUID );
249 }
250 return incidence;
251 }
252
253 QString CalendarBackend::getVCalString(KCalendarCore::Incidence::Ptr aInci)
254 {
255 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
256
257 Q_ASSERT( aInci );
258
259 QString vcal;
260
261 KCalendarCore::Incidence::Ptr temp = KCalendarCore::Incidence::Ptr ( aInci->clone() );
262 if(temp) {
263 KCalendarCore::Calendar::Ptr tempCalendar( new KCalendarCore::MemoryCalendar( QTimeZone::utc() ) );
264 tempCalendar->addIncidence(temp);
265 KCalendarCore::VCalFormat vcf;
266 vcal = vcf.toString(tempCalendar);
267 }
268 else {
269 qCWarning(lcSyncMLPlugin) << "Error Cloning the Incidence for VCal String";
270 }
271
272 return vcal;
273 }
274
275 QString CalendarBackend::getICalString(KCalendarCore::Incidence::Ptr aInci)
276 {
277 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
278
279 Q_ASSERT( aInci );
280
281 KCalendarCore::Incidence::Ptr temp = KCalendarCore::Incidence::Ptr ( aInci->clone() );
282
283 QString ical;
284 if( temp ) {
285 KCalendarCore::Calendar::Ptr tempCalendar( new KCalendarCore::MemoryCalendar( QTimeZone::utc() ) );
286 tempCalendar->addIncidence(temp);
287 KCalendarCore::ICalFormat icf;
288 ical = icf.toString(tempCalendar);
289 }
290 else {
291 qCWarning(lcSyncMLPlugin) << "Error Cloning the Incidence for Ical String";
292 }
293
294 return ical;
295 }
296
297 KCalendarCore::Incidence::Ptr CalendarBackend::getIncidenceFromVcal( const QString& aVString )
298 {
299 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
300
301 KCalendarCore::Incidence::Ptr pInci;
302
303 KCalendarCore::Calendar::Ptr tempCalendar( new KCalendarCore::MemoryCalendar( QTimeZone::systemTimeZone()) );
304 KCalendarCore::VCalFormat vcf;
305 vcf.fromString(tempCalendar, aVString);
306 KCalendarCore::Incidence::List lst = tempCalendar->rawIncidences();
307
308 if(!lst.isEmpty()) {
309 pInci = KCalendarCore::Incidence::Ptr ( lst[0]->clone() );
310 }
311 else {
312 qCWarning(lcSyncMLPlugin) << "VCal to Incidence Conversion Failed ";
313 }
314 return pInci;
315 }
316
317 KCalendarCore::Incidence::Ptr CalendarBackend::getIncidenceFromIcal( const QString& aIString )
318 {
319 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
320
321 KCalendarCore::Incidence::Ptr pInci;
322
323 KCalendarCore::Calendar::Ptr tempCalendar( new KCalendarCore::MemoryCalendar( QTimeZone::systemTimeZone()) );
324 KCalendarCore::ICalFormat icf;
325 icf.fromString(tempCalendar, aIString);
326 KCalendarCore::Incidence::List lst = tempCalendar->rawIncidences();
327
328 if(!lst.isEmpty()) {
329 pInci = KCalendarCore::Incidence::Ptr ( lst[0]->clone() );
330 } else {
331 qCWarning(lcSyncMLPlugin) << "ICal to Incidence Conversion Failed ";
332 }
333
334 return pInci;
335 }
336
337 bool CalendarBackend::addIncidence( KCalendarCore::Incidence::Ptr aInci, bool commitNow )
338 {
339 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
340
341 if( !iCalendar || !iStorage ) {
342 return false;
343 }
344
345 switch(aInci->type())
346 {
347 case KCalendarCore::Incidence::TypeEvent:
348 {
349 KCalendarCore::Event::Ptr event = aInci.staticCast<KCalendarCore::Event>();
350 if(!iCalendar->addEvent(event, iNotebookStr))
351 {
352 qCWarning(lcSyncMLPlugin) << "Could not add event";
353 return false;
354 }
355 }
356 break;
357 case KCalendarCore::Incidence::TypeTodo:
358 {
359 KCalendarCore::Todo::Ptr todo = aInci.staticCast<KCalendarCore::Todo>();
360 if(!iCalendar->addTodo(todo, iNotebookStr))
361 {
362 qCWarning(lcSyncMLPlugin) << "Could not add todo";
363 return false;
364 }
365 }
366 break;
367 default:
368 qCWarning(lcSyncMLPlugin) << "Could not add incidence, wrong type" << aInci->type();
369 return false;
370 }
371
372 // if you add an incidence, that incidence will be owned by calendar
373 // so no need to delete it. Committing for each modification, can cause performance
374 // problems.
375
376 if( commitNow ) {
377 if( !iStorage->save() )
378 {
379 qCWarning(lcSyncMLPlugin) << "Could not commit changes to calendar";
380 return false;
381 }
382 qCDebug(lcSyncMLPlugin) << "Single incidence committed";
383 }
384
385 qCDebug(lcSyncMLPlugin) << "Added an item with UID : " << aInci->uid() << "Recurrence Id :" << aInci->recurrenceId().toString();
386
387 return true;
388 }
389
390 bool CalendarBackend::commitChanges()
391 {
392 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
393 bool changesCommitted = false;
394
395 if( !iStorage )
396 {
397 qCWarning(lcSyncMLPlugin) << "No calendar storage!";
398 }
399 else if( iStorage->save() ) {
400 qCDebug(lcSyncMLPlugin) << "Committed changes to calendar";
401 changesCommitted = true;
402 }
403 else
404 {
405 qCDebug(lcSyncMLPlugin) << "Could not commit changes to calendar";
406 }
407 return changesCommitted;
408 }
409
410 bool CalendarBackend::modifyIncidence( KCalendarCore::Incidence::Ptr aInci, const QString& aUID, bool commitNow )
411 {
412 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
413
414 if( !iCalendar || !iStorage ) {
415 return false;
416 }
417
418 KCalendarCore::Incidence::Ptr origInci = getIncidence ( aUID );
419
420 if( !origInci ) {
421 qCWarning(lcSyncMLPlugin) << "Item with UID" << aUID << "does not exist. Cannot modify";
422 return false;
423 }
424
425 if( !modifyIncidence( origInci, aInci ) ) {
426 qCWarning(lcSyncMLPlugin) << "Could not make modifications to incidence";
427 return false;
428 }
429
430 if( commitNow ) {
431 if( !iStorage->save() )
432 {
433 qCWarning(lcSyncMLPlugin) << "Could not commit changes to calendar";
434 return false;
435 }
436 qCDebug(lcSyncMLPlugin) << "Single incidence committed";
437 }
438
439 return true;
440 }
441
442 CalendarBackend::ErrorStatus CalendarBackend::deleteIncidence( const QString& aUID )
443 {
444 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
445 CalendarBackend::ErrorStatus errorCode = CalendarBackend::STATUS_OK;
446
447 if( !iCalendar || !iStorage ) {
448 errorCode = CalendarBackend::STATUS_GENERIC_ERROR;
449 }
450
451 KCalendarCore::Incidence::Ptr incidence = getIncidence( aUID );
452
453 if( !incidence ) {
454 qCWarning(lcSyncMLPlugin) << "Could not find incidence to delete with UID" << aUID;
455 errorCode = CalendarBackend::STATUS_ITEM_NOT_FOUND;
456 }
457
458 if( !iCalendar->deleteIncidence( incidence) )
459 {
460 qCWarning(lcSyncMLPlugin) << "Could not delete incidence with UID" << aUID;
461 errorCode = CalendarBackend::STATUS_GENERIC_ERROR;
462 }
463
464 if( !iStorage->save() ) {
465 qCWarning(lcSyncMLPlugin) << "Could not commit changes to calendar";
466 errorCode = CalendarBackend::STATUS_GENERIC_ERROR;
467 }
468
469 return errorCode;
470 }
471
472 bool CalendarBackend::modifyIncidence( KCalendarCore::Incidence::Ptr aIncidence, KCalendarCore::Incidence::Ptr aIncidenceData )
473 {
474 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
475
476 Q_ASSERT( aIncidence );
477 Q_ASSERT( aIncidenceData );
478
479 // Save critical data from original item
480 aIncidenceData->setUid( aIncidence->uid() );
481 aIncidenceData->setCreated( aIncidence->created() );
482
483 if( aIncidence->type() != aIncidenceData->type() ) {
484 qCWarning(lcSyncMLPlugin) << "Expected incidence type" << aIncidence->typeStr() <<", got" << aIncidenceData->typeStr();
485 return false;
486 }
487
488 if( aIncidence->type() == KCalendarCore::Incidence::TypeEvent || aIncidence->type() == KCalendarCore::Incidence::TypeTodo )
489 {
490 KCalendarCore::IncidenceBase::Ptr inc = aIncidence;
491 KCalendarCore::IncidenceBase::Ptr data = aIncidenceData;
492 *inc = *data;
493 }
494 else {
495 qCWarning(lcSyncMLPlugin) << "Unsupported incidence type:" << aIncidence->typeStr();
496 return false;
497 }
498
499 iCalendar->setNotebook( aIncidence, iNotebookStr );
500
501 return true;
502
503 }