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
25 #include "akonadimodel.h"
28 #include "alarmresources.h"
31 #include <kalarmcal/kaevent.h>
34 #include <akonadi/collection.h>
35 #include <kcalcore/filestorage.h>
36 #include <kcalcore/event.h>
51 using namespace KAlarmCal
;
54 /** Provides read and write access to calendar files and resources.
55 * Either vCalendar or iCalendar files may be read, but the calendar is saved
56 * only in iCalendar format to avoid information loss.
58 class AlarmCalendar
: public QObject
62 virtual ~AlarmCalendar();
63 bool valid() const { return (mCalType
== RESOURCES
) || mUrl
.isValid(); }
64 CalEvent::Type
type() const { return (mCalType
== RESOURCES
) ? CalEvent::EMPTY
: mEventType
; }
69 void loadResource(AlarmResource
*, QWidget
* parent
);
70 void reloadFromCache(const QString
& resourceID
);
76 KAEvent
* earliestAlarm() const;
77 void setAlarmPending(KAEvent
*, bool pending
= true);
78 bool haveDisabledAlarms() const { return mHaveDisabledAlarms
; }
79 void disabledChanged(const KAEvent
*);
80 KAEvent::List
atLoginAlarms() const;
82 KCalCore::Event::Ptr
kcalEvent(const QString
& uniqueID
); // if Akonadi, display calendar only
83 KAEvent
* event(const EventId
& uniqueId
);
85 KCal::Event
* createKCalEvent(const KAEvent
* e
) const
86 { return createKCalEvent(e
, QString()); }
87 KCal::Event
* kcalEvent(const QString
& uniqueId
); // if Akonadi, display calendar only
88 KAEvent
* event(const QString
& uniqueId
);
90 KAEvent
* templateEvent(const QString
& templateName
);
92 KAEvent::List
events(const QString
& uniqueId
) const;
93 KAEvent::List
events(CalEvent::Types s
= CalEvent::EMPTY
) const { return events(Akonadi::Collection(), s
); }
94 KAEvent::List
events(const Akonadi::Collection
&, CalEvent::Types
= CalEvent::EMPTY
) const;
96 KAEvent::List
events(CalEvent::Types s
= CalEvent::EMPTY
) const { return events(0, s
); }
97 KAEvent::List
events(AlarmResource
*, CalEvent::Types
= CalEvent::EMPTY
) const;
98 KAEvent::List
events(const KDateTime
& from
, const KDateTime
& to
, CalEvent::Types
);
101 KCalCore::Event::List
kcalEvents(CalEvent::Type s
= CalEvent::EMPTY
); // display calendar only
102 bool eventReadOnly(Akonadi::Item::Id
) const;
103 Akonadi::Collection
collectionForEvent(Akonadi::Item::Id
) const;
104 bool addEvent(KAEvent
&, QWidget
* promptParent
= 0, bool useEventID
= false, Akonadi::Collection
* = 0, bool noPrompt
= false, bool* cancelled
= 0);
105 bool modifyEvent(const EventId
& oldEventId
, KAEvent
& newEvent
);
107 KCal::Event::List
kcalEvents(CalEvent::Type s
= CalEvent::EMPTY
) { return kcalEvents(0, s
); }
108 KCal::Event::List
kcalEvents(AlarmResource
*, CalEvent::Type
= CalEvent::EMPTY
);
109 bool eventReadOnly(const QString
& uniqueID
) const;
110 AlarmResource
* resourceForEvent(const QString
& eventID
) const;
111 bool addEvent(KAEvent
*, QWidget
* promptParent
= 0, bool useEventID
= false, AlarmResource
* = 0, bool noPrompt
= false, bool* cancelled
= 0);
112 bool modifyEvent(const QString
& oldEventId
, KAEvent
* newEvent
);
114 KAEvent
* updateEvent(const KAEvent
&);
115 KAEvent
* updateEvent(const KAEvent
*);
117 bool deleteEvent(const KAEvent
&, bool save
= false);
118 bool deleteDisplayEvent(const QString
& eventID
, bool save
= false);
120 bool deleteEvent(const QString
& eventID
, bool save
= false);
122 void purgeEvents(const KAEvent::List
&);
124 QString
path() const { return (mCalType
== RESOURCES
) ? QString() : mUrl
.prettyUrl(); }
125 QString
urlString() const { return (mCalType
== RESOURCES
) ? QString() : mUrl
.url(); }
126 void adjustStartOfDay();
128 static bool initialiseCalendars();
129 static void terminateCalendars();
130 static AlarmCalendar
* resources() { return mResourcesCalendar
; }
131 static AlarmCalendar
* displayCalendar() { return mDisplayCalendar
; }
132 static AlarmCalendar
* displayCalendarOpen();
134 static KAEvent
* getEvent(const EventId
&);
135 static bool importAlarms(QWidget
*, Akonadi::Collection
* = 0);
137 static KAEvent
* getEvent(const QString
& uniqueId
);
138 static bool importAlarms(QWidget
*, AlarmResource
* = 0);
140 static bool exportAlarms(const KAEvent::List
&, QWidget
* parent
);
143 void earliestAlarmChanged();
144 void haveDisabledAlarmsChanged(bool haveDisabled
);
146 void atLoginEventAdded(const KAEvent
&);
148 void calendarSaved(AlarmCalendar
*);
151 void setAskResource(bool ask
);
153 void slotCollectionStatusChanged(const Akonadi::Collection
&, AkonadiModel::Change
,
154 const QVariant
& value
, bool inserted
);
155 void slotEventsAdded(const AkonadiModel::EventList
&);
156 void slotEventsToBeRemoved(const AkonadiModel::EventList
&);
157 void slotEventChanged(const AkonadiModel::Event
&);
159 void slotCacheDownloaded(AlarmResource
*);
160 void slotResourceLoaded(AlarmResource
*, bool success
);
161 void slotResourceChange(AlarmResource
*, AlarmResources::Change
);
165 enum CalType
{ RESOURCES
, LOCAL_ICAL
, LOCAL_VCAL
};
167 typedef QMap
<Akonadi::Collection::Id
, KAEvent::List
> ResourceMap
; // id = invalid for display calendar
168 typedef QMap
<Akonadi::Collection::Id
, KAEvent
*> EarliestMap
;
169 typedef QHash
<EventId
, KAEvent
*> KAEventMap
; // indexed by collection and event UID
171 typedef QMap
<AlarmResource
*, KAEvent::List
> ResourceMap
; // resource = null for display calendar
172 typedef QMap
<AlarmResource
*, KAEvent
*> EarliestMap
;
173 typedef QMap
<QString
, KAEvent
*> KAEventMap
; // indexed by event UID
177 AlarmCalendar(const QString
& file
, CalEvent::Type
);
178 bool saveCal(const QString
& newFile
= QString());
180 bool isValid() const { return mCalType
== RESOURCES
|| mCalendarStorage
; }
181 void addNewEvent(const Akonadi::Collection
&, KAEvent
*, bool replace
= false);
182 CalEvent::Type
deleteEventInternal(const KAEvent
&, bool deleteFromAkonadi
= true);
183 CalEvent::Type
deleteEventInternal(const KAEvent
&, const Akonadi::Collection
&,
184 bool deleteFromAkonadi
= true);
185 CalEvent::Type
deleteEventInternal(const QString
& eventID
, const KAEvent
& = KAEvent(),
186 const Akonadi::Collection
& = Akonadi::Collection(), bool deleteFromAkonadi
= true);
187 void updateDisplayKAEvents();
188 void removeKAEvents(Akonadi::Collection::Id
, bool closing
= false, CalEvent::Types
= CalEvent::ACTIVE
| CalEvent::ARCHIVED
| CalEvent::TEMPLATE
);
189 void findEarliestAlarm(const Akonadi::Collection
&);
190 void findEarliestAlarm(Akonadi::Collection::Id
); //deprecated
192 bool isValid() const { return mCalendar
; }
193 bool addEvent(AlarmResource
*, KAEvent
*);
194 KAEvent
* addEvent(AlarmResource
*, const KCal::Event
*);
195 void addNewEvent(AlarmResource
*, KAEvent
*);
196 CalEvent::Type
deleteEventInternal(const QString
& eventID
);
197 void updateKAEvents(AlarmResource
*, KCal::CalendarLocal
*);
198 static void updateResourceKAEvents(AlarmResource
*, KCal::CalendarLocal
*);
199 void removeKAEvents(AlarmResource
*, bool closing
= false);
200 void findEarliestAlarm(AlarmResource
*);
201 KCal::Event
* createKCalEvent(const KAEvent
*, const QString
& baseID
) const;
203 void checkForDisabledAlarms();
204 void checkForDisabledAlarms(bool oldEnabled
, bool newEnabled
);
206 static AlarmCalendar
* mResourcesCalendar
; // the calendar resources
207 static AlarmCalendar
* mDisplayCalendar
; // the display calendar
210 KCalCore::FileStorage::Ptr mCalendarStorage
; // null pointer for Akonadi
212 KCal::Calendar
* mCalendar
; // AlarmResources or CalendarLocal, null for Akonadi
214 ResourceMap mResourceMap
;
215 KAEventMap mEventMap
; // lookup of all events by UID
216 EarliestMap mEarliestAlarm
; // alarm with earliest trigger time, by resource
217 QList
<QString
> mPendingAlarms
; // IDs of alarms which are currently being processed after triggering
218 KUrl mUrl
; // URL of current calendar file
219 KUrl mICalUrl
; // URL of iCalendar file
221 typedef QMap
<AlarmResource
*, ProgressDialog
*> ProgressDlgMap
;
222 typedef QMap
<AlarmResource
*, QWidget
*> ResourceWidgetMap
;
223 ProgressDlgMap mProgressDlgs
; // download progress dialogues
224 ResourceWidgetMap mProgressParents
; // parent widgets for download progress dialogues
226 QString mLocalFile
; // calendar file, or local copy if it's a remote file
227 CalType mCalType
; // what type of calendar mCalendar is (resources/ical/vcal)
228 CalEvent::Type mEventType
; // what type of events the calendar file is for
229 bool mOpen
; // true if the calendar file is open
230 int mUpdateCount
; // nesting level of group of calendar update calls
231 bool mUpdateSave
; // save() was called while mUpdateCount > 0
232 bool mHaveDisabledAlarms
; // there is at least one individually disabled alarm
234 using QObject::event
; // prevent "hidden" warning
237 #endif // ALARMCALENDAR_H