34 #ifndef MKCAL_SQLITESTORAGE_H
35 #define MKCAL_SQLITESTORAGE_H
62 typedef QSharedPointer<SqliteStorage>
Ptr;
74 const QString &databaseName,
75 bool validateNotebooks =
false);
85 QString databaseName()
const;
103 bool load(
const QString &uid,
const KDateTime &recurrenceId = KDateTime());
109 bool load(
const QDate &date);
115 bool load(
const QDate &start,
const QDate &end);
121 bool loadSeries(
const QString &uid);
127 bool loadNotebookIncidences(
const QString ¬ebookUid);
139 bool loadPlainIncidences();
145 bool loadRecurringIncidences();
151 bool loadGeoIncidences();
157 bool loadGeoIncidences(
float geoLatitude,
float geoLongitude,
158 float diffLatitude,
float diffLongitude);
164 bool loadAttendeeIncidences();
170 int loadUncompletedTodos();
176 int loadCompletedTodos(
bool hasDate,
int limit, KDateTime *last);
182 int loadIncidences(
bool hasDate,
int limit, KDateTime *last);
188 int loadFutureIncidences(
int limit, KDateTime *last);
194 int loadGeoIncidences(
bool hasDate,
int limit, KDateTime *last);
200 int loadUnreadInvitationIncidences();
206 int loadOldInvitationIncidences(
int limit, KDateTime *last);
212 KCalCore::Person::List loadContacts();
218 int loadContactIncidences(
const KCalCore::Person::Ptr &person,
int limit, KDateTime *last);
224 int loadJournals(
int limit, KDateTime *last);
230 bool notifyOpened(
const KCalCore::Incidence::Ptr &incidence);
236 bool purgeDeletedIncidences(
const KCalCore::Incidence::List &list);
266 void calendarModified(
bool modified, KCalCore::Calendar *calendar);
272 void calendarIncidenceCreated(
const KCalCore::Incidence::Ptr &incidence);
278 void calendarIncidenceAdded(
const KCalCore::Incidence::Ptr &incidence);
284 void calendarIncidenceChanged(
const KCalCore::Incidence::Ptr &incidence);
290 void calendarIncidenceDeleted(
const KCalCore::Incidence::Ptr &incidence);
296 void calendarIncidenceAdditionCanceled(
const KCalCore::Incidence::Ptr &incidence);
302 bool insertedIncidences(KCalCore::Incidence::List *list,
const KDateTime &after,
303 const QString ¬ebookUid = QString());
309 bool modifiedIncidences(KCalCore::Incidence::List *list,
const KDateTime &after,
310 const QString ¬ebookUid = QString());
316 bool deletedIncidences(KCalCore::Incidence::List *list,
const KDateTime &after,
317 const QString ¬ebookUid = QString());
323 bool allIncidences(KCalCore::Incidence::List *list,
const QString ¬ebookUid = QString());
329 bool duplicateIncidences(KCalCore::Incidence::List *list,
330 const KCalCore::Incidence::Ptr &incidence,
331 const QString ¬ebookUid = QString());
337 KDateTime incidenceDeletedDate(
const KCalCore::Incidence::Ptr &incidence);
361 virtual void virtual_hook(
int id,
void *data);
372 sqlite3_int64 toOriginTime(KDateTime dt);
380 sqlite3_int64 toLocalOriginTime(KDateTime dt);
387 KDateTime fromLocalOriginTime(sqlite3_int64 seconds);
394 KDateTime fromOriginTime(sqlite3_int64 seconds);
402 KDateTime fromOriginTime(sqlite3_int64 seconds, QString zonename);
410 bool initializeDatabase();
413 bool loadNotebooks();
414 bool reloadNotebooks();
425 void fileChanged(
const QString &path);
427 void queryFinished();
430 #define sqlite3_exec( db ) \
433 rv = sqlite3_exec( (db), query, NULL, 0, &errmsg ); \
435 if ( rv != SQLITE_CONSTRAINT ) { \
436 qCWarning(lcMkcal) << "sqlite3_exec error code:" << rv; \
439 if ( rv != SQLITE_CONSTRAINT ) { \
440 qCWarning(lcMkcal) << errmsg; \
442 sqlite3_free( errmsg ); \
445 if ( rv != SQLITE_CONSTRAINT ) { \
451 #define sqlite3_prepare_v2( db, query, qsize, stmt, tail ) \
454 rv = sqlite3_prepare_v2( (db), (query), (qsize), (stmt), (tail) ); \
456 qCWarning(lcMkcal) << "sqlite3_prepare error code:" << rv; \
457 qCWarning(lcMkcal) << sqlite3_errmsg( (db) ); \
462 #define sqlite3_bind_text( stmt, index, value, size, desc ) \
464 rv = sqlite3_bind_text( (stmt), (index), (value), (size), (desc) ); \
466 qCWarning(lcMkcal) << "sqlite3_bind_text error:" << rv << "on index and value:" << index << value; \
472 #define sqlite3_bind_int( stmt, index, value ) \
474 rv = sqlite3_bind_int( (stmt), (index), (value) ); \
476 qCWarning(lcMkcal) << "sqlite3_bind_int error:" << rv << "on index and value:" << index << value; \
482 #define sqlite3_bind_int64( stmt, index, value ) \
484 rv = sqlite3_bind_int64( (stmt), (index), (value) ); \
486 qCWarning(lcMkcal) << "sqlite3_bind_int64 error:" << rv << "on index and value:" << index << value; \
492 #define sqlite3_bind_double( stmt, index, value ) \
494 rv = sqlite3_bind_double( (stmt), (index), (value) ); \
496 qCWarning(lcMkcal) << "sqlite3_bind_int error:" << rv << "on index and value:" << index << value; \
502 #define sqlite3_step( stmt ) \
504 rv = sqlite3_step( (stmt) ); \
505 if ( rv && rv != SQLITE_DONE && rv != SQLITE_ROW ) { \
506 if ( rv != SQLITE_CONSTRAINT ) { \
507 qCWarning(lcMkcal) << "sqlite3_step error:" << rv; \
513 #define CREATE_VERSION \
514 "CREATE TABLE IF NOT EXISTS Version(Major INTEGER, Minor INTEGER)"
515 #define CREATE_TIMEZONES \
516 "CREATE TABLE IF NOT EXISTS Timezones(TzId INTEGER PRIMARY KEY, ICalData TEXT)"
517 #define CREATE_CALENDARS \
518 "CREATE TABLE IF NOT EXISTS Calendars(CalendarId TEXT PRIMARY KEY, Name TEXT, Description TEXT, Color INTEGER, Flags INTEGER, syncDate INTEGER, pluginName TEXT, account TEXT, attachmentSize INTEGER, modifiedDate INTEGER, sharedWith TEXT, syncProfile TEXT, createdDate INTEGER, extra1 STRING, extra2 STRING)"
523 #define CREATE_COMPONENTS \
524 "CREATE TABLE IF NOT EXISTS Components(ComponentId INTEGER PRIMARY KEY AUTOINCREMENT, Notebook TEXT, Type TEXT, Summary TEXT, Category TEXT, DateStart INTEGER, DateStartLocal INTEGER, StartTimeZone TEXT, HasDueDate INTEGER, DateEndDue INTEGER, DateEndDueLocal INTEGER, EndDueTimeZone TEXT, Duration INTEGER, Classification INTEGER, Location TEXT, Description TEXT, Status INTEGER, GeoLatitude REAL, GeoLongitude REAL, Priority INTEGER, Resources TEXT, DateCreated INTEGER, DateStamp INTEGER, DateLastModified INTEGER, Sequence INTEGER, Comments TEXT, Attachments TEXT, Contact TEXT, InvitationStatus INTEGER, RecurId INTEGER, RecurIdLocal INTEGER, RecurIdTimeZone TEXT, RelatedTo TEXT, URL TEXT, UID TEXT, Transparency INTEGER, LocalOnly INTEGER, Percent INTEGER, DateCompleted INTEGER, DateCompletedLocal INTEGER, CompletedTimeZone TEXT, DateDeleted INTEGER, extra1 STRING, extra2 STRING, extra3 INTEGER)"
529 #define CREATE_RDATES \
530 "CREATE TABLE IF NOT EXISTS Rdates(ComponentId INTEGER, Type INTEGER, Date INTEGER, DateLocal INTEGER, TimeZone TEXT)"
531 #define CREATE_CUSTOMPROPERTIES \
532 "CREATE TABLE IF NOT EXISTS Customproperties(ComponentId INTEGER, Name TEXT, Value TEXT, Parameters TEXT)"
533 #define CREATE_RECURSIVE \
534 "CREATE TABLE IF NOT EXISTS Recursive(ComponentId INTEGER, RuleType INTEGER, Frequency INTEGER, Until INTEGER, UntilLocal INTEGER, untilTimeZone TEXT, Count INTEGER, Interval INTEGER, BySecond TEXT, ByMinute TEXT, ByHour TEXT, ByDay TEXT, ByDayPos Text, ByMonthDay TEXT, ByYearDay TEXT, ByWeekNum TEXT, ByMonth TEXT, BySetPos TEXT, WeekStart INTEGER)"
535 #define CREATE_ALARM \
536 "CREATE TABLE IF NOT EXISTS Alarm(ComponentId INTEGER, Action INTEGER, Repeat INTEGER, Duration INTEGER, Offset INTEGER, Relation TEXT, DateTrigger INTEGER, DateTriggerLocal INTEGER, triggerTimeZone TEXT, Description TEXT, Attachment TEXT, Summary TEXT, Address TEXT, CustomProperties TEXT, isEnabled INTEGER)"
537 #define CREATE_ATTENDEE \
538 "CREATE TABLE IF NOT EXISTS Attendee(ComponentId INTEGER, Email TEXT, Name TEXT, IsOrganizer INTEGER, Role INTEGER, PartStat INTEGER, Rsvp INTEGER, DelegatedTo TEXT, DelegatedFrom TEXT)"
539 #define CREATE_CALENDARPROPERTIES \
540 "CREATE TABLE IF NOT EXISTS Calendarproperties(CalendarId REFERENCES Calendars(CalendarId) ON DELETE CASCADE, Name TEXT NOT NULL, Value TEXT, UNIQUE (CalendarId, Name))"
542 #define INDEX_CALENDAR \
543 "CREATE INDEX IF NOT EXISTS IDX_CALENDAR on Calendars(CalendarId)"
544 #define INDEX_INVITATION \
545 "CREATE INDEX IF NOT EXISTS IDX_INVITATION on Invitations(InvitationId)"
546 #define INDEX_COMPONENT \
547 "CREATE INDEX IF NOT EXISTS IDX_COMPONENT on Components(ComponentId, Notebook, DateStart, DateEndDue, DateDeleted)"
548 #define INDEX_COMPONENT_UID \
549 "CREATE UNIQUE INDEX IF NOT EXISTS IDX_COMPONENT_UID on Components(UID, RecurId, DateDeleted)"
550 #define INDEX_COMPONENT_NOTEBOOK \
551 "CREATE INDEX IF NOT EXISTS IDX_COMPONENT_NOTEBOOK on Components(Notebook)"
552 #define INDEX_RDATES \
553 "CREATE INDEX IF NOT EXISTS IDX_RDATES on Rdates(ComponentId)"
554 #define INDEX_CUSTOMPROPERTIES \
555 "CREATE INDEX IF NOT EXISTS IDX_CUSTOMPROPERTIES on Customproperties(ComponentId)"
556 #define INDEX_RECURSIVE \
557 "CREATE INDEX IF NOT EXISTS IDX_RECURSIVE on Recursive(ComponentId)"
558 #define INDEX_ALARM \
559 "CREATE INDEX IF NOT EXISTS IDX_ALARM on Alarm(ComponentId)"
560 #define INDEX_ATTENDEE \
561 "CREATE UNIQUE INDEX IF NOT EXISTS IDX_ATTENDEE on Attendee(ComponentId, Email)"
562 #define INDEX_CALENDARPROPERTIES \
563 "CREATE INDEX IF NOT EXISTS IDX_CALENDARPROPERTIES on Calendarproperties(CalendarId)"
565 #define INSERT_VERSION \
566 "insert into Version values (?, ?)"
567 #define INSERT_TIMEZONES \
568 "insert into Timezones values (1, '')"
569 #define INSERT_CALENDARS \
570 "insert into Calendars values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '', '')"
571 #define INSERT_INVITATIONS \
572 "insert into Invitations values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
573 #define INSERT_COMPONENTS \
574 "insert into Components values (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, '', '', 0)"
575 #define INSERT_CUSTOMPROPERTIES \
576 "insert into Customproperties values (?, ?, ?, ?)"
577 #define INSERT_CALENDARPROPERTIES \
578 "insert into Calendarproperties values (?, ?, ?)"
579 #define INSERT_RDATES \
580 "insert into Rdates values (?, ?, ?, ?, ?)"
581 #define INSERT_RECURSIVE \
582 "insert into Recursive values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
583 #define INSERT_ALARM \
584 "insert into Alarm values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
585 #define INSERT_ATTENDEE \
586 "insert into Attendee values (?, ?, ?, ?, ?, ?, ?, ?, ?)"
588 #define UPDATE_TIMEZONES \
589 "update Timezones set ICalData=? where TzId=1"
590 #define UPDATE_CALENDARS \
591 "update Calendars set Name=?, Description=?, Color=?, Flags=?, syncDate=?, pluginName=?, account=?, attachmentSize=?, modifiedDate=?, sharedWith=?, syncProfile=?, createdDate=? where CalendarId=?"
592 #define UPDATE_COMPONENTS \
593 "update Components set Notebook=?, Type=?, Summary=?, Category=?, DateStart=?, DateStartLocal=?, StartTimeZone=?, HasDueDate=?, DateEndDue=?, DateEndDueLocal=?, EndDueTimeZone=?, Duration=?, Classification=?, Location=?, Description=?, Status=?, GeoLatitude=?, GeoLongitude=?, Priority=?, Resources=?, DateCreated=?, DateStamp=?, DateLastModified=?, Sequence=?, Comments=?, Attachments=?, Contact=?, InvitationStatus=?, RecurId=?, RecurIdLocal=?, RecurIdTimeZone=?, RelatedTo=?, URL=?, UID=?, Transparency=?, LocalOnly=?, DateCompleted=?, DateCompletedLocal=?, CompletedTimeZone=?, Percent=? where ComponentId=?"
594 #define UPDATE_COMPONENTS_AS_DELETED \
595 "update Components set DateDeleted=? where ComponentId=?"
598 #define DELETE_TIMEZONES \
599 "delete from Timezones where TzId=1"
600 #define DELETE_CALENDARS \
601 "delete from Calendars where CalendarId=?"
602 #define DELETE_INVITATIONS \
603 "delete from Invitations where InvitationId=?"
604 #define DELETE_COMPONENTS \
605 "delete from Components where ComponentId=?"
606 #define DELETE_RDATES \
607 "delete from Rdates where ComponentId=?"
608 #define DELETE_CUSTOMPROPERTIES \
609 "delete from Customproperties where ComponentId=?"
610 #define DELETE_CALENDARPROPERTIES \
611 "delete from Calendarproperties where CalendarId=?"
612 #define DELETE_RECURSIVE \
613 "delete from Recursive where ComponentId=?"
614 #define DELETE_ALARM \
615 "delete from Alarm where ComponentId=?"
616 #define DELETE_ATTENDEE \
617 "delete from Attendee where ComponentId=?"
619 #define SELECT_VERSION \
620 "select * from Version"
621 #define SELECT_TIMEZONES \
622 "select * from Timezones where TzId=1"
623 #define SELECT_CALENDARS_ALL \
624 "select * from Calendars order by Name"
625 #define SELECT_INVITATIONS_ALL \
626 "select * from Invitations"
627 #define SELECT_COMPONENTS_ALL \
628 "select * from Components where DateDeleted=0"
629 #define SELECT_COMPONENTS_BY_NOTEBOOK \
630 "select * from Components where Notebook=? and DateDeleted=0"
631 #define SELECT_COMPONENTS_BY_GEO \
632 "select * from Components where GeoLatitude!=255.0 and GeoLongitude!=255.0 and DateDeleted=0"
633 #define SELECT_COMPONENTS_BY_GEO_AREA \
634 "select * from Components where GeoLatitude>=? and GeoLongitude>=? and GeoLatitude<=? and GeoLongitude<=? and DateDeleted=0"
635 #define SELECT_COMPONENTS_BY_JOURNAL \
636 "select * from Components where Type='Journal' and DateDeleted=0"
637 #define SELECT_COMPONENTS_BY_JOURNAL_DATE \
638 "select * from Components where Type='Journal' and DateDeleted=0 and datestart<=? order by DateStart desc, DateCreated desc"
639 #define SELECT_COMPONENTS_BY_PLAIN \
640 "select * from Components where DateStart=0 and DateEndDue=0 and DateDeleted=0"
641 #define SELECT_COMPONENTS_BY_RECURSIVE \
642 "select * from components where ((ComponentId in (select DISTINCT ComponentId from recursive)) or (RecurId!=0)) and DateDeleted=0"
643 #define SELECT_COMPONENTS_BY_ATTENDEE \
644 "select * from components where ComponentId in (select DISTINCT ComponentId from attendee) and DateDeleted=0"
645 #define SELECT_COMPONENTS_BY_DATE_BOTH \
646 "select * from Components where DateStart<=? and DateEndDue>=? and DateDeleted=0"
647 #define SELECT_COMPONENTS_BY_DATE_START \
648 "select * from Components where DateEndDue>=? and DateDeleted=0"
649 #define SELECT_COMPONENTS_BY_DATE_END \
650 "select * from Components where DateStart<=? and DateDeleted=0"
651 #define SELECT_COMPONENTS_BY_UID_AND_RECURID \
652 "select * from Components where UID=? and RecurId=? and DateDeleted=0"
653 #define SELECT_COMPONENTS_BY_UID \
654 "select * from Components where UID=? and DateDeleted=0"
655 #define SELECT_COMPONENTS_BY_NOTEBOOKUID \
656 "select * from Components where Notebook=? and DateDeleted=0"
657 #define SELECT_ROWID_FROM_COMPONENTS_BY_UID_AND_RECURID \
658 "select ComponentId from Components where UID=? and RecurId=? and DateDeleted=0"
659 #define SELECT_COMPONENTS_BY_UNCOMPLETED_TODOS \
660 "select * from Components where Type='Todo' and DateCompleted=0 and DateDeleted=0"
661 #define SELECT_COMPONENTS_BY_COMPLETED_TODOS_AND_DATE \
662 "select * from Components where Type='Todo' and DateCompleted<>0 and DateEndDue<>0 and DateEndDue<=? and DateDeleted=0 order by DateEndDue desc, DateCreated desc"
663 #define SELECT_COMPONENTS_BY_COMPLETED_TODOS_AND_CREATED \
664 "select * from Components where Type='Todo' and DateCompleted<>0 and DateEndDue=0 and DateCreated<=? and DateDeleted=0 order by DateCreated desc"
665 #define SELECT_COMPONENTS_BY_DATE_SMART \
666 "select * from Components where DateEndDue<>0 and DateEndDue<=? and DateDeleted=0 order by DateEndDue desc, DateCreated desc"
668 #define FUTURE_DATE_SMART_FIELD \
669 " (case type when 'Todo' then DateEndDue else DateStart end) "
670 #define SELECT_COMPONENTS_BY_FUTURE_DATE_SMART \
671 "select * from Components where " \
672 FUTURE_DATE_SMART_FIELD ">=? and DateDeleted=0 order by " \
673 FUTURE_DATE_SMART_FIELD " asc, DateCreated asc"
675 #define SELECT_COMPONENTS_BY_CREATED_SMART \
676 "select * from Components where DateEndDue=0 and DateCreated<=? and DateDeleted=0 order by DateCreated desc"
677 #define SELECT_COMPONENTS_BY_GEO_AND_DATE \
678 "select * from Components where GeoLatitude!=255.0 and GeoLongitude!=255.0 and DateEndDue<>0 and DateEndDue<=? and DateDeleted=0 order by DateEndDue desc, DateCreated desc"
679 #define SELECT_COMPONENTS_BY_GEO_AND_CREATED \
680 "select * from Components where GeoLatitude!=255.0 and GeoLongitude!=255.0 and DateEndDue=0 and DateCreated<=? and DateDeleted=0 order by DateCreated desc"
681 #define SELECT_COMPONENTS_BY_INVITATION_UNREAD \
682 "select * from Components where InvitationStatus=1 and DateDeleted=0"
683 #define SELECT_COMPONENTS_BY_INVITATION_AND_CREATED \
684 "select * from Components where InvitationStatus>1 and DateCreated<=? and DateDeleted=0 order by DateCreated desc"
685 #define SELECT_COMPONENTS_BY_ATTENDEE_EMAIL_AND_CREATED \
686 "select * from Components where ComponentId in (select distinct ComponentId from Attendee where email=?) and DateCreated<=? and DateDeleted=0 order by DateCreated desc"
687 #define SELECT_COMPONENTS_BY_ATTENDEE_AND_CREATED \
688 "select * from Components where ComponentId in (select distinct ComponentId from Attendee) and DateCreated<=? and DateDeleted=0 order by DateCreated desc"
689 #define SELECT_RDATES_BY_ID \
690 "select * from Rdates where ComponentId=?"
691 #define SELECT_CUSTOMPROPERTIES_BY_ID \
692 "select * from Customproperties where ComponentId=?"
693 #define SELECT_RECURSIVE_BY_ID \
694 "select * from Recursive where ComponentId=?"
695 #define SELECT_ALARM_BY_ID \
696 "select * from Alarm where ComponentId=?"
697 #define SELECT_ATTENDEE_BY_ID \
698 "select * from Attendee where ComponentId=?"
699 #define SELECT_CALENDARPROPERTIES_BY_ID \
700 "select * from Calendarproperties where CalendarId=?"
701 #define SELECT_COMPONENTS_BY_DUPLICATE \
702 "select * from Components where DateStart=? and Summary=? and DateDeleted=0"
703 #define SELECT_COMPONENTS_BY_DUPLICATE_AND_NOTEBOOK \
704 "select * from Components where DateStart=? and Summary=? and Notebook=? and DateDeleted=0"
705 #define SELECT_COMPONENTS_BY_CREATED \
706 "select * from Components where DateCreated>=? and DateDeleted=0"
707 #define SELECT_COMPONENTS_BY_CREATED_AND_NOTEBOOK \
708 "select * from Components where DateCreated>=? and Notebook=? and DateDeleted=0"
709 #define SELECT_COMPONENTS_BY_LAST_MODIFIED \
710 "select * from Components where DateLastModified>=? and DateCreated<? and DateDeleted=0"
711 #define SELECT_COMPONENTS_BY_LAST_MODIFIED_AND_NOTEBOOK \
712 "select * from Components where DateLastModified>=? and DateCreated<? and Notebook=? and DateDeleted=0"
713 #define SELECT_COMPONENTS_BY_DELETED \
714 "select * from Components where DateDeleted>=? and DateCreated<?"
715 #define SELECT_COMPONENTS_BY_DELETED_AND_NOTEBOOK \
716 "select * from Components where DateDeleted>=? and DateCreated<? and Notebook=?"
717 #define SELECT_COMPONENTS_BY_UID_RECID_AND_DELETED \
718 "select ComponentId, DateDeleted from Components where UID=? and RecurId=? and DateDeleted<>0"
719 #define SELECT_ATTENDEE_AND_COUNT \
720 "select Email, Name, count(Email) from Attendee where Email<>0 group by Email"
721 #define SELECT_EVENT_COUNT \
722 "select count(*) from Components where Type='Event' and DateDeleted=0"
723 #define SELECT_TODO_COUNT \
724 "select count(*) from Components where Type='Todo' and DateDeleted=0"
725 #define SELECT_JOURNAL_COUNT \
726 "select count(*) from Components where Type='Journal' and DateDeleted=0"
728 #define BEGIN_TRANSACTION \
730 #define COMMIT_TRANSACTION \