2 * alarmcalendar.h - KAlarm calendar file access
4 * Copyright © 2001-2012 by David Jarvie <djarvie@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #ifndef ALARMCALENDAR_H
22 #define ALARMCALENDAR_H
24 #include "akonadimodel.h"
27 #include <kalarmcal/kaevent.h>
29 #include <AkonadiCore/collection.h>
30 #include <KCalCore/FileStorage>
31 #include <KCalCore/Event>
37 using namespace KAlarmCal
;
40 /** Provides read and write access to calendar files and resources.
41 * Either vCalendar or iCalendar files may be read, but the calendar is saved
42 * only in iCalendar format to avoid information loss.
44 class AlarmCalendar
: public QObject
48 virtual ~AlarmCalendar();
49 bool valid() const { return (mCalType
== RESOURCES
) || mUrl
.isValid(); }
50 CalEvent::Type
type() const { return (mCalType
== RESOURCES
) ? CalEvent::EMPTY
: mEventType
; }
58 KAEvent
* earliestAlarm() const;
59 void setAlarmPending(KAEvent
*, bool pending
= true);
60 bool haveDisabledAlarms() const { return mHaveDisabledAlarms
; }
61 void disabledChanged(const KAEvent
*);
62 KAEvent::List
atLoginAlarms() const;
63 KCalCore::Event::Ptr
kcalEvent(const QString
& uniqueID
); // if Akonadi, display calendar only
64 KAEvent
* event(const EventId
& uniqueId
, bool checkDuplicates
= false);
65 KAEvent
* templateEvent(const QString
& templateName
);
66 KAEvent::List
events(const QString
& uniqueId
) const;
67 KAEvent::List
events(CalEvent::Types s
= CalEvent::EMPTY
) const { return events(Akonadi::Collection(), s
); }
68 KAEvent::List
events(const Akonadi::Collection
&, CalEvent::Types
= CalEvent::EMPTY
) const;
69 KCalCore::Event::List
kcalEvents(CalEvent::Type s
= CalEvent::EMPTY
); // display calendar only
70 bool eventReadOnly(Akonadi::Item::Id
) const;
71 Akonadi::Collection
collectionForEvent(Akonadi::Item::Id
) const;
72 bool addEvent(KAEvent
&, QWidget
* promptparent
= Q_NULLPTR
, bool useEventID
= false, Akonadi::Collection
* = Q_NULLPTR
, bool noPrompt
= false, bool* cancelled
= Q_NULLPTR
);
73 bool modifyEvent(const EventId
& oldEventId
, KAEvent
& newEvent
);
74 KAEvent
* updateEvent(const KAEvent
&);
75 KAEvent
* updateEvent(const KAEvent
*);
76 bool deleteEvent(const KAEvent
&, bool save
= false);
77 bool deleteDisplayEvent(const QString
& eventID
, bool save
= false);
78 void purgeEvents(const KAEvent::List
&);
80 QString
path() const { return (mCalType
== RESOURCES
) ? QString() : mUrl
.prettyUrl(); }
81 QString
urlString() const { return (mCalType
== RESOURCES
) ? QString() : mUrl
.url(); }
82 void adjustStartOfDay();
84 static bool initialiseCalendars();
85 static void terminateCalendars();
86 static AlarmCalendar
* resources() { return mResourcesCalendar
; }
87 static AlarmCalendar
* displayCalendar() { return mDisplayCalendar
; }
88 static AlarmCalendar
* displayCalendarOpen();
89 static KAEvent
* getEvent(const EventId
&);
90 static bool importAlarms(QWidget
*, Akonadi::Collection
* = Q_NULLPTR
);
91 static bool exportAlarms(const KAEvent::List
&, QWidget
* parent
);
94 void earliestAlarmChanged();
95 void haveDisabledAlarmsChanged(bool haveDisabled
);
96 void atLoginEventAdded(const KAEvent
&);
97 void calendarSaved(AlarmCalendar
*);
100 void setAskResource(bool ask
);
101 void slotCollectionStatusChanged(const Akonadi::Collection
&, AkonadiModel::Change
,
102 const QVariant
& value
, bool inserted
);
103 void slotEventsAdded(const AkonadiModel::EventList
&);
104 void slotEventsToBeRemoved(const AkonadiModel::EventList
&);
105 void slotEventChanged(const AkonadiModel::Event
&);
107 enum CalType
{ RESOURCES
, LOCAL_ICAL
, LOCAL_VCAL
};
108 typedef QMap
<Akonadi::Collection::Id
, KAEvent::List
> ResourceMap
; // id = invalid for display calendar
109 typedef QMap
<Akonadi::Collection::Id
, KAEvent
*> EarliestMap
;
110 typedef QHash
<EventId
, KAEvent
*> KAEventMap
; // indexed by collection and event UID
113 AlarmCalendar(const QString
& file
, CalEvent::Type
);
114 bool saveCal(const QString
& newFile
= QString());
115 bool isValid() const { return mCalType
== RESOURCES
|| mCalendarStorage
; }
116 void addNewEvent(const Akonadi::Collection
&, KAEvent
*, bool replace
= false);
117 CalEvent::Type
deleteEventInternal(const KAEvent
&, bool deleteFromAkonadi
= true);
118 CalEvent::Type
deleteEventInternal(const KAEvent
&, const Akonadi::Collection
&,
119 bool deleteFromAkonadi
= true);
120 CalEvent::Type
deleteEventInternal(const QString
& eventID
, const KAEvent
& = KAEvent(),
121 const Akonadi::Collection
& = Akonadi::Collection(), bool deleteFromAkonadi
= true);
122 void updateDisplayKAEvents();
123 void removeKAEvents(Akonadi::Collection::Id
, bool closing
= false, CalEvent::Types
= CalEvent::ACTIVE
| CalEvent::ARCHIVED
| CalEvent::TEMPLATE
);
124 void findEarliestAlarm(const Akonadi::Collection
&);
125 void findEarliestAlarm(Akonadi::Collection::Id
); //deprecated
126 void checkForDisabledAlarms();
127 void checkForDisabledAlarms(bool oldEnabled
, bool newEnabled
);
129 static AlarmCalendar
* mResourcesCalendar
; // the calendar resources
130 static AlarmCalendar
* mDisplayCalendar
; // the display calendar
132 KCalCore::FileStorage::Ptr mCalendarStorage
; // null pointer for Akonadi
133 ResourceMap mResourceMap
;
134 KAEventMap mEventMap
; // lookup of all events by UID
135 EarliestMap mEarliestAlarm
; // alarm with earliest trigger time, by resource
136 QList
<QString
> mPendingAlarms
; // IDs of alarms which are currently being processed after triggering
137 KUrl mUrl
; // URL of current calendar file
138 KUrl mICalUrl
; // URL of iCalendar file
139 QString mLocalFile
; // calendar file, or local copy if it's a remote file
140 CalType mCalType
; // what type of calendar mCalendar is (resources/ical/vcal)
141 CalEvent::Type mEventType
; // what type of events the calendar file is for
142 bool mOpen
; // true if the calendar file is open
143 int mUpdateCount
; // nesting level of group of calendar update calls
144 bool mUpdateSave
; // save() was called while mUpdateCount > 0
145 bool mHaveDisabledAlarms
; // there is at least one individually disabled alarm
147 using QObject::event
; // prevent "hidden" warning
150 #endif // ALARMCALENDAR_H