The concept of local folders doesn't apply any more.
[kdepim.git] / kalarm / alarmcalendar.h
blob81c5ff9cd228d6844b620dd2dc502b0c854e8575
1 /*
2 * alarmcalendar.h - KAlarm calendar file access
3 * Program: kalarm
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 #ifdef USE_AKONADI
25 #include "akonadimodel.h"
26 #include "eventid.h"
27 #else
28 #include "alarmresources.h"
29 #endif
31 #include <kalarmcal/kaevent.h>
33 #ifdef USE_AKONADI
34 #include <akonadi/collection.h>
35 #include <kcalcore/filestorage.h>
36 #include <kcalcore/event.h>
37 #include <QHash>
38 #endif
39 #include <kurl.h>
40 #include <QObject>
42 #ifndef USE_AKONADI
43 namespace KCal {
44 class Calendar;
45 class CalendarLocal;
47 class AlarmResource;
48 class ProgressDialog;
49 #endif
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
60 Q_OBJECT
61 public:
62 virtual ~AlarmCalendar();
63 bool valid() const { return (mCalType == RESOURCES) || mUrl.isValid(); }
64 CalEvent::Type type() const { return (mCalType == RESOURCES) ? CalEvent::EMPTY : mEventType; }
65 bool open();
66 int load();
67 bool reload();
68 #ifndef USE_AKONADI
69 void loadResource(AlarmResource*, QWidget* parent);
70 void reloadFromCache(const QString& resourceID);
71 #endif
72 bool save();
73 void close();
74 void startUpdate();
75 bool endUpdate();
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;
81 #ifdef USE_AKONADI
82 KCalCore::Event::Ptr kcalEvent(const QString& uniqueID); // if Akonadi, display calendar only
83 KAEvent* event(const EventId& uniqueId);
84 #else
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);
89 #endif
90 KAEvent* templateEvent(const QString& templateName);
91 #ifdef USE_AKONADI
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;
95 #else
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);
99 #endif
100 #ifdef USE_AKONADI
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);
106 #else
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);
113 #endif
114 KAEvent* updateEvent(const KAEvent&);
115 KAEvent* updateEvent(const KAEvent*);
116 #ifdef USE_AKONADI
117 bool deleteEvent(const KAEvent&, bool save = false);
118 bool deleteDisplayEvent(const QString& eventID, bool save = false);
119 #else
120 bool deleteEvent(const QString& eventID, bool save = false);
121 #endif
122 void purgeEvents(const KAEvent::List&);
123 bool isOpen();
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();
133 #ifdef USE_AKONADI
134 static KAEvent* getEvent(const EventId&);
135 static bool importAlarms(QWidget*, Akonadi::Collection* = 0);
136 #else
137 static KAEvent* getEvent(const QString& uniqueId);
138 static bool importAlarms(QWidget*, AlarmResource* = 0);
139 #endif
140 static bool exportAlarms(const KAEvent::List&, QWidget* parent);
142 signals:
143 void earliestAlarmChanged();
144 void haveDisabledAlarmsChanged(bool haveDisabled);
145 #ifdef USE_AKONADI
146 void atLoginEventAdded(const KAEvent&);
147 #endif
148 void calendarSaved(AlarmCalendar*);
150 private slots:
151 void setAskResource(bool ask);
152 #ifdef USE_AKONADI
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&);
158 #else
159 void slotCacheDownloaded(AlarmResource*);
160 void slotResourceLoaded(AlarmResource*, bool success);
161 void slotResourceChange(AlarmResource*, AlarmResources::Change);
162 #endif
164 private:
165 enum CalType { RESOURCES, LOCAL_ICAL, LOCAL_VCAL };
166 #ifdef USE_AKONADI
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
170 #else
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
174 #endif
176 AlarmCalendar();
177 AlarmCalendar(const QString& file, CalEvent::Type);
178 bool saveCal(const QString& newFile = QString());
179 #ifdef USE_AKONADI
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
191 #else
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;
202 #endif
203 void checkForDisabledAlarms();
204 void checkForDisabledAlarms(bool oldEnabled, bool newEnabled);
206 static AlarmCalendar* mResourcesCalendar; // the calendar resources
207 static AlarmCalendar* mDisplayCalendar; // the display calendar
209 #ifdef USE_AKONADI
210 KCalCore::FileStorage::Ptr mCalendarStorage; // null pointer for Akonadi
211 #else
212 KCal::Calendar* mCalendar; // AlarmResources or CalendarLocal, null for Akonadi
213 #endif
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
220 #ifndef USE_AKONADI
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
225 #endif
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
239 // vim: et sw=4: