| 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 "NotesBackend.h" |
| 25 |
|
| 26 |
#include <extendedcalendar.h> |
| 27 |
#include <sqlitestorage.h> |
| 28 |
#include <QDir> |
| 29 |
|
| 30 |
#include "SyncMLPluginLogging.h" |
| 31 |
|
| 32 |
#include "SimpleItem.h" |
| 33 |
|
| 34 |
// @todo: handle unicode notes better. For example S60 seems to send only ascii. |
| 35 |
// Ovi.com seems to send latin-1 in base64-encoded form. UTF-8 really should |
| 36 |
// be preferred here, but how would we know which format is given to us as |
| 37 |
// latin-1 and utf-8 are not compatible? |
| 38 |
|
| 39 |
static const QString INCIDENCE_TYPE_JOURNAL( "Journal" ); |
| 40 |
|
| 41 |
NotesBackend::NotesBackend() : iCalendar( 0 ), iStorage( 0 ) |
| 42 |
{ |
| 43 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 44 |
} |
| 45 |
|
| 46 |
NotesBackend::~NotesBackend() |
| 47 |
{ |
| 48 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 49 |
} |
| 50 |
|
| 51 |
bool NotesBackend::init( const QString& aNotebookName, const QString& aUid, |
| 52 |
const QString &aMimeType ) |
| 53 |
{ |
| 54 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 55 |
|
| 56 |
qCDebug(lcSyncMLPlugin) << "Notes backend using notebook" << aNotebookName << "And uuid" << aUid; |
| 57 |
|
| 58 |
if( aNotebookName.isEmpty() ) |
| 59 |
{ |
| 60 |
qCDebug(lcSyncMLPlugin) << "NoteBook Name to Sync is expected. It Cannot be Empty"; |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
iNotebookName = aNotebookName; |
| 65 |
iMimeType = aMimeType; |
| 66 |
|
| 67 |
iCalendar = mKCal::ExtendedCalendar::Ptr( new mKCal::ExtendedCalendar(QTimeZone::systemTimeZone()) ); |
| 68 |
|
| 69 |
qCDebug(lcSyncMLPlugin) << "Creating Default Maemo Storage for Notes"; |
| 70 |
iStorage = iCalendar->defaultStorage( iCalendar ); |
| 71 |
|
| 72 |
bool opened = iStorage->open(); |
| 73 |
|
| 74 |
if (!opened) |
| 75 |
{ |
| 76 |
qCDebug(lcSyncMLPluginTrace) << "Calendar storage open failed"; |
| 77 |
} |
| 78 |
|
| 79 |
mKCal::Notebook::Ptr openedNb; |
| 80 |
|
| 81 |
// If we have an Uid, we try to get the corresponding Notebook |
| 82 |
if (!aUid.isEmpty()) { |
| 83 |
openedNb = iStorage->notebook(aUid); |
| 84 |
|
| 85 |
// If we didn't get one, we create one and set its Uid |
| 86 |
if (!openedNb) { |
| 87 |
openedNb = mKCal::Notebook::Ptr(new mKCal::Notebook(aNotebookName, |
| 88 |
"Synchronization Created Notebook for " + aNotebookName)); |
| 89 |
if (!openedNb.isNull()) { |
| 90 |
openedNb->setUid(aUid); |
| 91 |
if (!iStorage->addNotebook(openedNb)) { |
| 92 |
qCWarning(lcSyncMLPlugin) << "Failed to add notebook to storage"; |
| 93 |
openedNb.clear(); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
// If we didn't have an Uid or the creation above failed, |
| 99 |
// we use the default notebook |
| 100 |
if (openedNb.isNull()) { |
| 101 |
qCDebug(lcSyncMLPlugin) << "Using default notebook"; |
| 102 |
openedNb = iStorage->defaultNotebook(); |
| 103 |
if(openedNb.isNull()) |
| 104 |
{ |
| 105 |
qCDebug(lcSyncMLPlugin) << "No default notebook exists, creating one"; |
| 106 |
openedNb = mKCal::Notebook::Ptr(new mKCal::Notebook("Default", QString())); |
| 107 |
if (!iStorage->setDefaultNotebook(openedNb)) { |
| 108 |
qCWarning(lcSyncMLPlugin) << "Failed to set default notebook of storage"; |
| 109 |
openedNb.clear(); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
bool loaded = false; |
| 115 |
if(opened && openedNb) |
| 116 |
{ |
| 117 |
qCDebug(lcSyncMLPlugin) << "Loading all incidences from::" << openedNb->uid(); |
| 118 |
loaded = iStorage->loadNotebookIncidences(openedNb->uid()); |
| 119 |
if(!loaded) |
| 120 |
{ |
| 121 |
qCWarning(lcSyncMLPlugin) << "Failed to load calendar"; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if (opened && loaded && !openedNb.isNull()) |
| 126 |
{ |
| 127 |
iNotebookName = openedNb->uid(); |
| 128 |
|
| 129 |
qCDebug(lcSyncMLPlugin) << "Calendar initialized for notes"; |
| 130 |
return true; |
| 131 |
} |
| 132 |
else |
| 133 |
{ |
| 134 |
qCWarning(lcSyncMLPlugin) << "Not able to initialize calendar"; |
| 135 |
|
| 136 |
iStorage.clear(); |
| 137 |
|
| 138 |
qCDebug(lcSyncMLPluginTrace) << "Storage deleted"; |
| 139 |
|
| 140 |
iCalendar.clear(); |
| 141 |
|
| 142 |
qCDebug(lcSyncMLPluginTrace) << "Calendar deleted"; |
| 143 |
|
| 144 |
return false; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
bool NotesBackend::uninit() |
| 150 |
{ |
| 151 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 152 |
|
| 153 |
if( iStorage ) { |
| 154 |
iStorage->close(); |
| 155 |
iStorage.clear(); |
| 156 |
} |
| 157 |
|
| 158 |
if( iCalendar ) { |
| 159 |
iCalendar->close(); |
| 160 |
iCalendar.clear(); |
| 161 |
} |
| 162 |
|
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
bool NotesBackend::getAllNotes( QList<Buteo::StorageItem*>& aItems ) |
| 167 |
{ |
| 168 |
|
| 169 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 170 |
|
| 171 |
KCalendarCore::Incidence::List incidences; |
| 172 |
|
| 173 |
if( !iStorage->allIncidences( &incidences, iNotebookName ) ) { |
| 174 |
qCWarning(lcSyncMLPlugin) << "Could not retrieve all notes"; |
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
retrieveNoteItems( incidences, aItems ); |
| 179 |
|
| 180 |
return true; |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
bool NotesBackend::getAllNoteIds( QList<QString>& aItemIds ) |
| 185 |
{ |
| 186 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 187 |
|
| 188 |
KCalendarCore::Incidence::List incidences; |
| 189 |
|
| 190 |
if( !iStorage->allIncidences( &incidences, iNotebookName ) ) { |
| 191 |
qCWarning(lcSyncMLPlugin) << "Could not retrieve all notes"; |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
retrieveNoteIds( incidences, aItemIds ); |
| 196 |
|
| 197 |
return true; |
| 198 |
} |
| 199 |
|
| 200 |
bool NotesBackend::getNewNotes( QList<Buteo::StorageItem*>& aNewItems, const QDateTime& aTime ) |
| 201 |
{ |
| 202 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 203 |
|
| 204 |
KCalendarCore::Incidence::List incidences; |
| 205 |
|
| 206 |
if( !iStorage->insertedIncidences( &incidences, aTime, iNotebookName ) ) { |
| 207 |
qCWarning(lcSyncMLPlugin) << "Could not retrieve new notes"; |
| 208 |
return false; |
| 209 |
} |
| 210 |
|
| 211 |
retrieveNoteItems( incidences, aNewItems ); |
| 212 |
|
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
bool NotesBackend::getNewNoteIds( QList<QString>& aNewItemIds, const QDateTime& aTime ) |
| 217 |
{ |
| 218 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 219 |
|
| 220 |
KCalendarCore::Incidence::List incidences; |
| 221 |
|
| 222 |
if( !iStorage->insertedIncidences( &incidences, aTime, iNotebookName ) ) { |
| 223 |
qCWarning(lcSyncMLPlugin) << "Could not retrieve new notes"; |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
retrieveNoteIds( incidences, aNewItemIds ); |
| 228 |
|
| 229 |
return true; |
| 230 |
} |
| 231 |
|
| 232 |
bool NotesBackend::getModifiedNotes( QList<Buteo::StorageItem*>& aModifiedItems, const QDateTime& aTime ) |
| 233 |
{ |
| 234 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 235 |
|
| 236 |
KCalendarCore::Incidence::List incidences; |
| 237 |
|
| 238 |
if( !iStorage->modifiedIncidences( &incidences, aTime, iNotebookName ) ) { |
| 239 |
qCWarning(lcSyncMLPlugin) << "Could not retrieve modified notes"; |
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
retrieveNoteItems( incidences, aModifiedItems ); |
| 244 |
|
| 245 |
return true; |
| 246 |
} |
| 247 |
|
| 248 |
bool NotesBackend::getModifiedNoteIds( QList<QString>& aModifiedItemIds, const QDateTime& aTime ) |
| 249 |
{ |
| 250 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 251 |
|
| 252 |
KCalendarCore::Incidence::List incidences; |
| 253 |
|
| 254 |
if( !iStorage->modifiedIncidences( &incidences, aTime, iNotebookName ) ) { |
| 255 |
qCWarning(lcSyncMLPlugin) << "Could not retrieve modified notes"; |
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
retrieveNoteIds( incidences, aModifiedItemIds ); |
| 260 |
|
| 261 |
return true; |
| 262 |
} |
| 263 |
|
| 264 |
bool NotesBackend::getDeletedNoteIds( QList<QString>& aDeletedItemIds, const QDateTime& aTime ) |
| 265 |
{ |
| 266 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 267 |
|
| 268 |
KCalendarCore::Incidence::List incidences; |
| 269 |
|
| 270 |
if( !iStorage->deletedIncidences( &incidences, aTime, iNotebookName ) ) { |
| 271 |
qCWarning(lcSyncMLPlugin) << "Could not retrieve modified notes"; |
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
retrieveNoteIds( incidences, aDeletedItemIds ); |
| 276 |
|
| 277 |
return true; |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
Buteo::StorageItem* NotesBackend::newItem() |
| 282 |
{ |
| 283 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 284 |
|
| 285 |
return new SimpleItem; |
| 286 |
} |
| 287 |
|
| 288 |
Buteo::StorageItem* NotesBackend::getItem( const QString& aItemId ) |
| 289 |
{ |
| 290 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 291 |
|
| 292 |
iStorage->load ( aItemId ); |
| 293 |
KCalendarCore::Incidence::Ptr item = iCalendar->incidence( aItemId ); |
| 294 |
|
| 295 |
if( !item ) { |
| 296 |
qCWarning(lcSyncMLPlugin) << "Could not find item:" << aItemId; |
| 297 |
return NULL; |
| 298 |
} |
| 299 |
|
| 300 |
Buteo::StorageItem* storageItem = newItem(); |
| 301 |
storageItem->setId( item->uid() ); |
| 302 |
storageItem->setType(iMimeType); |
| 303 |
storageItem->write( 0, item->description().toUtf8() ); |
| 304 |
|
| 305 |
return storageItem; |
| 306 |
} |
| 307 |
|
| 308 |
bool NotesBackend::addNote( Buteo::StorageItem& aItem, bool aCommitNow ) |
| 309 |
{ |
| 310 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 311 |
|
| 312 |
QByteArray data; |
| 313 |
|
| 314 |
if( !aItem.read( 0, aItem.getSize(), data ) ) { |
| 315 |
qCWarning(lcSyncMLPlugin) << "Reading item data failed"; |
| 316 |
return false; |
| 317 |
} |
| 318 |
|
| 319 |
KCalendarCore::Journal::Ptr journal; |
| 320 |
journal = KCalendarCore::Journal::Ptr( new KCalendarCore::Journal() ); |
| 321 |
|
| 322 |
QString description = QString::fromUtf8( data.constData() ); |
| 323 |
|
| 324 |
journal->setDescription( description ); |
| 325 |
|
| 326 |
// addJournal() takes ownership of journal -> we cannot delete it |
| 327 |
|
| 328 |
if( !iCalendar->addJournal( journal, iNotebookName ) ) { |
| 329 |
qCWarning(lcSyncMLPlugin) << "Could not add note to calendar"; |
| 330 |
journal.clear(); |
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
QString id = journal->uid(); |
| 335 |
|
| 336 |
qCDebug(lcSyncMLPlugin) << "New note added, id:" << id; |
| 337 |
|
| 338 |
aItem.setId( id ); |
| 339 |
|
| 340 |
if( aCommitNow ) |
| 341 |
{ |
| 342 |
if( !commitChanges() ) |
| 343 |
{ |
| 344 |
return false; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
return true; |
| 349 |
|
| 350 |
} |
| 351 |
|
| 352 |
bool NotesBackend::modifyNote( Buteo::StorageItem& aItem, bool aCommitNow ) |
| 353 |
{ |
| 354 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 355 |
|
| 356 |
iStorage->load ( aItem.getId() ); |
| 357 |
KCalendarCore::Incidence::Ptr item = iCalendar->incidence( aItem.getId() ); |
| 358 |
|
| 359 |
if( !item ) { |
| 360 |
qCWarning(lcSyncMLPlugin) << "Could not find item to be modified:" << aItem.getId(); |
| 361 |
return false; |
| 362 |
} |
| 363 |
|
| 364 |
QByteArray data; |
| 365 |
|
| 366 |
if( !aItem.read( 0, aItem.getSize(), data ) ) { |
| 367 |
qCWarning(lcSyncMLPlugin) << "Reading item data failed:" << aItem.getId(); |
| 368 |
return false; |
| 369 |
} |
| 370 |
|
| 371 |
QString description = QString::fromLatin1( data.constData() ); |
| 372 |
|
| 373 |
item->setDescription( description ); |
| 374 |
|
| 375 |
if( aCommitNow ) |
| 376 |
{ |
| 377 |
if( !commitChanges() ) |
| 378 |
{ |
| 379 |
return false; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
qCDebug(lcSyncMLPlugin) << "Note modified, id:" << aItem.getId(); |
| 384 |
|
| 385 |
return true; |
| 386 |
} |
| 387 |
|
| 388 |
bool NotesBackend::deleteNote( const QString& aId, bool aCommitNow ) |
| 389 |
{ |
| 390 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 391 |
|
| 392 |
iStorage->load ( aId ); |
| 393 |
KCalendarCore::Incidence::Ptr journal = iCalendar->incidence( aId ); |
| 394 |
|
| 395 |
if( !journal ) { |
| 396 |
qCWarning(lcSyncMLPlugin) << "Could not find item to be deleted:" << aId; |
| 397 |
return false; |
| 398 |
} |
| 399 |
|
| 400 |
if( !iCalendar->deleteIncidence( journal ) ) { |
| 401 |
qCWarning(lcSyncMLPlugin) << "Could not delete note:" << aId; |
| 402 |
return false; |
| 403 |
} |
| 404 |
|
| 405 |
if( aCommitNow ) |
| 406 |
{ |
| 407 |
if( !commitChanges() ) |
| 408 |
{ |
| 409 |
return false; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
return true; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
bool NotesBackend::commitChanges() |
| 418 |
{ |
| 419 |
bool saved = false; |
| 420 |
|
| 421 |
if( iStorage && iStorage->save() ) |
| 422 |
{ |
| 423 |
saved = true; |
| 424 |
} |
| 425 |
else |
| 426 |
{ |
| 427 |
qCCritical(lcSyncMLPlugin) << "Couldn't save to storage"; |
| 428 |
} |
| 429 |
return saved; |
| 430 |
} |
| 431 |
|
| 432 |
void NotesBackend::retrieveNoteItems( KCalendarCore::Incidence::List& aIncidences, QList<Buteo::StorageItem*>& aItems ) |
| 433 |
{ |
| 434 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 435 |
|
| 436 |
filterIncidences( aIncidences ); |
| 437 |
|
| 438 |
for( int i = 0; i < aIncidences.count(); ++i ) { |
| 439 |
Buteo::StorageItem* item = newItem(); |
| 440 |
item->setId( aIncidences[i]->uid() ); |
| 441 |
item->setType(iMimeType); |
| 442 |
item->write( 0, aIncidences[i]->description().toUtf8() ); |
| 443 |
aItems.append( item ); |
| 444 |
} |
| 445 |
|
| 446 |
} |
| 447 |
|
| 448 |
void NotesBackend::retrieveNoteIds( KCalendarCore::Incidence::List& aIncidences, QList<QString>& aIds ) |
| 449 |
{ |
| 450 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 451 |
|
| 452 |
filterIncidences( aIncidences ); |
| 453 |
|
| 454 |
for( int i = 0; i < aIncidences.count(); ++i ) { |
| 455 |
aIds.append( aIncidences[i]->uid() ); |
| 456 |
} |
| 457 |
|
| 458 |
} |
| 459 |
|
| 460 |
void NotesBackend::filterIncidences( KCalendarCore::Incidence::List& aIncidences ) |
| 461 |
{ |
| 462 |
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace); |
| 463 |
|
| 464 |
QString journal( INCIDENCE_TYPE_JOURNAL ); |
| 465 |
|
| 466 |
int i = 0; |
| 467 |
while( i < aIncidences.count() ) { |
| 468 |
KCalendarCore::Incidence::Ptr incidence = aIncidences[i]; |
| 469 |
|
| 470 |
if( incidence->type() != KCalendarCore::Incidence::TypeJournal ) { |
| 471 |
aIncidences.remove( i, 1 ); |
| 472 |
incidence.clear(); |
| 473 |
} |
| 474 |
else { |
| 475 |
++i; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
} |