Continue to port to QRegularExpression
[kdepim.git] / kalarm / akonadimodel.h
blobc961baaf5b334478eae4109cf3d24166d0c2a770
1 /*
2 * akonadimodel.h - KAlarm calendar file access using Akonadi
3 * Program: kalarm
4 * Copyright © 2010-2014 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 AKONADIMODEL_H
22 #define AKONADIMODEL_H
24 #include "eventid.h"
26 #include <kalarmcal/kacalendar.h>
27 #include <kalarmcal/kaevent.h>
29 #include <AkonadiCore/entitytreemodel.h>
30 #include <AkonadiCore/servermanager.h>
32 #include <QSize>
33 #include <QColor>
34 #include <QMap>
35 #include <QQueue>
37 class QPixmap;
38 class KJob;
40 using namespace KAlarmCal;
43 class AkonadiModel : public Akonadi::EntityTreeModel
45 Q_OBJECT
46 public:
47 enum Change { Added, Deleted, Invalidated, Enabled, ReadOnly, AlarmTypes, WrongType, Location, Colour };
48 enum { // data columns
49 // Item columns
50 TimeColumn = 0, TimeToColumn, RepeatColumn, ColourColumn, TypeColumn, TextColumn,
51 TemplateNameColumn,
52 ColumnCount
54 enum { // additional data roles
55 // Collection roles
56 EnabledTypesRole = UserRole, // alarm types which are enabled for the collection
57 BaseColourRole, // background colour ignoring collection colour
58 AlarmTypeRole, // OR of event types which collection contains
59 IsStandardRole, // OR of event types which collection is standard for
60 KeepFormatRole, // user has chosen not to update collection's calendar storage format
61 // Item roles
62 EnabledRole, // true for enabled alarm, false for disabled
63 StatusRole, // KAEvent::ACTIVE/ARCHIVED/TEMPLATE
64 AlarmActionsRole, // KAEvent::Actions
65 AlarmSubActionRole, // KAEvent::Action
66 ValueRole, // numeric value
67 SortRole, // the value to use for sorting
68 CommandErrorRole // last command execution error for alarm (per user)
71 /** Struct containing a KAEvent and its parent Collection. */
72 struct Event
74 Event(const KAEvent& e, const Akonadi::Collection& c) : event(e), collection(c) {}
75 EventId eventId() const { return EventId(collection.id(), event.id()); }
76 bool isConsistent() const { return event.collectionId() == collection.id(); }
77 KAEvent event;
78 Akonadi::Collection collection;
80 typedef QList<Event> EventList;
82 static AkonadiModel* instance();
84 ~AkonadiModel();
86 /** Return the display name for a collection. */
87 QString displayName(Akonadi::Collection&) const;
88 /** Return the storage type (file/directory/URL etc.) for a collection. */
89 QString storageType(const Akonadi::Collection&) const;
90 /** Get the foreground color for a collection, based on specified mime types. */
91 static QColor foregroundColor(const Akonadi::Collection&, const QStringList& mimeTypes);
92 /** Set the background color for a collection and its alarms. */
93 void setBackgroundColor(Akonadi::Collection&, const QColor&);
94 /** Get the background color for a collection and its alarms. */
95 QColor backgroundColor(Akonadi::Collection&) const;
96 /** Get the tooltip for a collection. The collection's enabled status is
97 * evaluated for specified alarm types. */
98 QString tooltip(const Akonadi::Collection&, CalEvent::Types) const;
99 /** Return the read-only status tooltip for a collection.
100 * A null string is returned if the collection is fully writable. */
101 static QString readOnlyTooltip(const Akonadi::Collection&);
103 /** To be called when the command error status of an alarm has changed,
104 * to set in the Akonadi database and update the visual command error indications.
106 void updateCommandError(const KAEvent&);
108 QVariant data(const QModelIndex&, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
109 bool setData(const QModelIndex&, const QVariant& value, int role) Q_DECL_OVERRIDE;
111 /** Refresh the specified collection instance with up to date data. */
112 bool refresh(Akonadi::Collection&) const;
113 /** Refresh the specified item instance with up to date data. */
114 bool refresh(Akonadi::Item&) const;
116 QModelIndex collectionIndex(Akonadi::Collection::Id id) const
117 { return collectionIndex(Akonadi::Collection(id)); }
118 QModelIndex collectionIndex(const Akonadi::Collection&) const;
119 Akonadi::Collection collectionById(Akonadi::Collection::Id) const;
120 Akonadi::Collection collectionForItem(Akonadi::Item::Id) const;
121 Akonadi::Collection collection(const KAEvent& e) const { return collectionForItem(e.itemId()); }
123 /** Remove a collection from Akonadi. The calendar file is not removed.
124 * @return true if a removal job has been scheduled.
126 bool removeCollection(const Akonadi::Collection&);
128 /** Reload a collection's data from Akonadi storage (not from the backend). */
129 bool reloadCollection(const Akonadi::Collection&);
131 /** Reload all collections' data from Akonadi storage (not from the backend). */
132 void reload();
134 /** Return whether calendar migration/creation at initialisation has completed. */
135 bool isMigrationCompleted() const;
137 bool isCollectionBeingDeleted(Akonadi::Collection::Id) const;
139 QModelIndex itemIndex(Akonadi::Item::Id id) const
140 { return itemIndex(Akonadi::Item(id)); }
141 QModelIndex itemIndex(const Akonadi::Item&) const;
142 Akonadi::Item itemById(Akonadi::Item::Id) const;
144 /** Return the alarm with the specified unique identifier.
145 * @return the event, or invalid event if no such event exists.
147 KAEvent event(const Akonadi::Item& item) const { return event(item, QModelIndex(), Q_NULLPTR); }
148 KAEvent event(Akonadi::Item::Id) const;
149 KAEvent event(const QModelIndex&) const;
150 using QObject::event; // prevent warning about hidden virtual method
152 /** Return an event's model index, based on its itemId() value. */
153 QModelIndex eventIndex(const KAEvent&);
154 /** Search for an event's item ID. This method ignores any itemId() value
155 * contained in the KAEvent. The collectionId() is used if available.
157 Akonadi::Item::Id findItemId(const KAEvent&);
159 #if 0
160 /** Return all events in a collection, optionally of a specified type. */
161 KAEvent::List events(Akonadi::Collection&, CalEvent::Type = CalEvent::EMPTY) const;
162 #endif
164 bool addEvent(KAEvent&, Akonadi::Collection&);
165 bool addEvents(const KAEvent::List&, Akonadi::Collection&);
166 bool updateEvent(KAEvent& event);
167 bool updateEvent(Akonadi::Item::Id oldId, KAEvent& newEvent);
168 bool deleteEvent(const KAEvent& event);
169 bool deleteEvent(Akonadi::Item::Id itemId);
171 /** Check whether a collection is stored in the current KAlarm calendar format. */
172 static bool isCompatible(const Akonadi::Collection&);
174 /** Return whether a collection is fully writable, i.e. with
175 * create/delete/change rights and compatible with the current KAlarm
176 * calendar format.
178 * @return 1 = fully writable,
179 * 0 = writable except that backend calendar is in an old KAlarm format,
180 * -1 = read-only or incompatible format.
182 static int isWritable(const Akonadi::Collection&);
184 /** Return whether a collection is fully writable, i.e. with
185 * create/delete/change rights and compatible with the current KAlarm
186 * calendar format.
188 * @param format Updated to contain the backend calendar storage format.
189 * If read-only, = KACalendar::Current;
190 * if unknown format, = KACalendar::Incompatible;
191 * otherwise = the backend calendar storage format.
192 * @return 1 = fully writable,
193 * 0 = writable except that backend calendar is in an old KAlarm format,
194 * -1 = read-only (if @p compat == KACalendar::Current), or
195 * incompatible format otherwise.
197 static int isWritable(const Akonadi::Collection&, KACalendar::Compat& format);
199 static CalEvent::Types types(const Akonadi::Collection&);
201 static QSize iconSize() { return mIconSize; }
203 Q_SIGNALS:
204 /** Signal emitted when a collection has been added to the model. */
205 void collectionAdded(const Akonadi::Collection&);
207 /** Signal emitted when a collection's enabled or read-only status has changed.
208 * @param inserted true if the reason for the change is that the collection
209 * has been inserted into the model
211 void collectionStatusChanged(const Akonadi::Collection&, AkonadiModel::Change, const QVariant& newValue, bool inserted);
213 /** Signal emitted when events have been added to the model. */
214 void eventsAdded(const AkonadiModel::EventList&);
216 /** Signal emitted when events are about to be removed from the model. */
217 void eventsToBeRemoved(const AkonadiModel::EventList&);
219 /** Signal emitted when an event in the model has changed. */
220 void eventChanged(const AkonadiModel::Event&);
222 /** Signal emitted when Akonadi has completed a collection modification.
223 * @param id Akonadi ID for the collection
224 * @param status true if successful, false if error
226 void collectionModified(Akonadi::Collection::Id, bool status = true);
228 /** Signal emitted when Akonadi has completed a collection deletion.
229 * @param id Akonadi ID for the collection
230 * @param status true if successful, false if error
232 void collectionDeleted(Akonadi::Collection::Id, bool status = true);
234 /** Signal emitted when Akonadi has completed an item creation, update
235 * or deletion.
236 * @param id Akonadi ID for the item
237 * @param status true if successful, false if error
239 void itemDone(Akonadi::Item::Id, bool status = true);
241 /** Signal emitted when calendar migration/creation has completed. */
242 void migrationCompleted();
244 /** Signal emitted when the Akonadi server has stopped. */
245 void serverStopped();
247 protected:
248 QVariant entityHeaderData(int section, Qt::Orientation, int role, HeaderGroup) const Q_DECL_OVERRIDE;
249 int entityColumnCount(HeaderGroup) const Q_DECL_OVERRIDE;
251 private Q_SLOTS:
252 void checkResources(Akonadi::ServerManager::State);
253 void slotMigrationCompleted();
254 void slotCollectionChanged(const Akonadi::Collection& c, const QSet<QByteArray>& attrNames)
255 { setCollectionChanged(c, attrNames, false); }
256 void slotCollectionRemoved(const Akonadi::Collection&);
257 void slotCollectionBeingCreated(const QString& path, Akonadi::Collection::Id, bool finished);
258 void slotUpdateTimeTo();
259 void slotUpdateArchivedColour(const QColor&);
260 void slotUpdateDisabledColour(const QColor&);
261 void slotUpdateHolidays();
262 void slotUpdateWorkingHours();
263 void slotRowsInserted(const QModelIndex& parent, int start, int end);
264 void slotRowsAboutToBeRemoved(const QModelIndex& parent, int start, int end);
265 void slotMonitoredItemChanged(const Akonadi::Item&, const QSet<QByteArray>&);
266 void slotEmitEventChanged();
267 void modifyCollectionJobDone(KJob*);
268 void itemJobDone(KJob*);
270 private:
271 struct CalData // data per collection
273 CalData() : enabled(false) { }
274 CalData(bool e, const QColor& c) : colour(c), enabled(e) { }
275 QColor colour; // user selected color for the calendar
276 bool enabled; // whether the collection is enabled
278 struct CollJobData // collection data for jobs in progress
280 CollJobData() : id(-1) {}
281 CollJobData(Akonadi::Collection::Id i, const QString& d) : id(i), displayName(d) {}
282 Akonadi::Collection::Id id;
283 QString displayName;
285 struct CollTypeData // data for configuration dialog for collection creation job
287 CollTypeData() : parent(Q_NULLPTR), alarmType(CalEvent::EMPTY) {}
288 CollTypeData(CalEvent::Type t, QWidget* p) : parent(p), alarmType(t) {}
289 QWidget* parent;
290 CalEvent::Type alarmType;
293 AkonadiModel(Akonadi::ChangeRecorder*, QObject* parent);
294 void initCalendarMigrator();
295 KAEvent event(const Akonadi::Item&, const QModelIndex&, Akonadi::Collection*) const;
296 void signalDataChanged(bool (*checkFunc)(const Akonadi::Item&), int startColumn, int endColumn, const QModelIndex& parent);
297 void setCollectionChanged(const Akonadi::Collection&, const QSet<QByteArray>&, bool rowInserted);
298 void queueItemModifyJob(const Akonadi::Item&);
299 void checkQueuedItemModifyJob(const Akonadi::Item&);
300 #if 0
301 void getChildEvents(const QModelIndex& parent, CalEvent::Type, KAEvent::List&) const;
302 #endif
303 QColor backgroundColor_p(const Akonadi::Collection&) const;
304 QString repeatText(const KAEvent&) const;
305 QString repeatOrder(const KAEvent&) const;
306 QPixmap* eventIcon(const KAEvent&) const;
307 QString whatsThisText(int column) const;
308 EventList eventList(const QModelIndex& parent, int start, int end);
310 static AkonadiModel* mInstance;
311 static QPixmap* mTextIcon;
312 static QPixmap* mFileIcon;
313 static QPixmap* mCommandIcon;
314 static QPixmap* mEmailIcon;
315 static QPixmap* mAudioIcon;
316 static QSize mIconSize;
317 static int mTimeHourPos; // position of hour within time string, or -1 if leading zeroes included
319 Akonadi::ChangeRecorder* mMonitor;
320 QMap<Akonadi::Collection::Id, CalEvent::Types> mCollectionAlarmTypes; // last content mime types of each collection
321 QMap<Akonadi::Collection::Id, Akonadi::Collection::Rights> mCollectionRights; // last writable status of each collection
322 QMap<Akonadi::Collection::Id, CalEvent::Types> mCollectionEnabled; // last enabled mime types of each collection
323 QMap<KJob*, CollJobData> mPendingCollectionJobs; // pending collection creation/deletion jobs, with collection ID & name
324 QMap<KJob*, CollTypeData> mPendingColCreateJobs; // default alarm type for pending collection creation jobs
325 QMap<KJob*, Akonadi::Item::Id> mPendingItemJobs; // pending item creation/deletion jobs, with event ID
326 QMap<Akonadi::Item::Id, Akonadi::Item> mItemModifyJobQueue; // pending item modification jobs, invalid item = queue empty but job active
327 QList<QString> mCollectionsBeingCreated; // path names of new collections being created by migrator
328 QList<Akonadi::Collection::Id> mCollectionIdsBeingCreated; // ids of new collections being created by migrator
329 QList<Akonadi::Item::Id> mItemsBeingCreated; // new items not fully initialised yet
330 QList<Akonadi::Collection::Id> mCollectionsDeleting; // collections currently being removed
331 QList<Akonadi::Collection::Id> mCollectionsDeleted; // collections recently removed
332 QQueue<Event> mPendingEventChanges; // changed events with changedEvent() signal pending
333 bool mResourcesChecked; // whether resource existence has been checked yet
334 bool mMigrating; // currently migrating calendars
337 #endif // AKONADIMODEL_H
339 // vim: et sw=4: