french -> French
[kdepim.git] / kalarm / akonadimodel.h
blob3e031ce6c309894c8b7026f1262784b8ecd19034
1 /*
2 * akonadimodel.h - KAlarm calendar file access using Akonadi
3 * Program: kalarm
4 * Copyright © 2010-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 AKONADIMODEL_H
22 #define AKONADIMODEL_H
24 #include "eventid.h"
26 #include <kalarmcal/kacalendar.h>
27 #include <kalarmcal/kaevent.h>
29 #include <akonadi/entitytreemodel.h>
30 #include <akonadi/servermanager.h>
32 #include <QSize>
33 #include <QColor>
34 #include <QMap>
35 #include <QQueue>
37 namespace Akonadi {
38 class AgentInstance;
39 class AgentInstanceCreateJob;
41 class QPixmap;
42 class KJob;
43 class CalendarMigrator;
45 using namespace KAlarmCal;
48 class AkonadiModel : public Akonadi::EntityTreeModel
50 Q_OBJECT
51 public:
52 enum Change { Added, Deleted, Invalidated, Enabled, ReadOnly, AlarmTypes, WrongType, Location, Colour };
53 enum { // data columns
54 // Item columns
55 TimeColumn = 0, TimeToColumn, RepeatColumn, ColourColumn, TypeColumn, TextColumn,
56 TemplateNameColumn,
57 ColumnCount
59 enum { // additional data roles
60 // Collection roles
61 EnabledTypesRole = UserRole, // alarm types which are enabled for the collection
62 BaseColourRole, // background colour ignoring collection colour
63 AlarmTypeRole, // OR of event types which collection contains
64 IsStandardRole, // OR of event types which collection is standard for
65 KeepFormatRole, // user has chosen not to update collection's calendar storage format
66 // Item roles
67 EnabledRole, // true for enabled alarm, false for disabled
68 StatusRole, // KAEvent::ACTIVE/ARCHIVED/TEMPLATE
69 AlarmActionsRole, // KAEvent::Actions
70 AlarmSubActionRole, // KAEvent::Action
71 ValueRole, // numeric value
72 SortRole, // the value to use for sorting
73 CommandErrorRole // last command execution error for alarm (per user)
76 /** Struct containing a KAEvent and its parent Collection. */
77 struct Event
79 Event(const KAEvent& e, const Akonadi::Collection& c) : event(e), collection(c) {}
80 EventId eventId() const { return EventId(collection.id(), event.id()); }
81 bool isConsistent() const { return event.collectionId() == collection.id(); }
82 KAEvent event;
83 Akonadi::Collection collection;
85 typedef QList<Event> EventList;
87 static AkonadiModel* instance();
89 /** Return the display name for a collection. */
90 QString displayName(Akonadi::Collection&) const;
91 /** Return the storage type (file/directory/URL etc.) for a collection. */
92 QString storageType(const Akonadi::Collection&) const;
93 /** Get the foreground color for a collection, based on specified mime types. */
94 static QColor foregroundColor(const Akonadi::Collection&, const QStringList& mimeTypes);
95 /** Set the background color for a collection and its alarms. */
96 void setBackgroundColor(Akonadi::Collection&, const QColor&);
97 /** Get the background color for a collection and its alarms. */
98 QColor backgroundColor(Akonadi::Collection&) const;
99 /** Get the tooltip for a collection. The collection's enabled status is
100 * evaluated for specified alarm types. */
101 QString tooltip(const Akonadi::Collection&, CalEvent::Types) const;
102 /** Return the read-only status tooltip for a collection.
103 * A null string is returned if the collection is fully writable. */
104 static QString readOnlyTooltip(const Akonadi::Collection&);
106 /** To be called when the command error status of an alarm has changed,
107 * to set in the Akonadi database and update the visual command error indications.
109 void updateCommandError(const KAEvent&);
111 virtual QVariant data(const QModelIndex&, int role = Qt::DisplayRole) const;
112 virtual bool setData(const QModelIndex&, const QVariant& value, int role);
114 /** Refresh the specified collection instance with up to date data. */
115 bool refresh(Akonadi::Collection&) const;
116 /** Refresh the specified item instance with up to date data. */
117 bool refresh(Akonadi::Item&) const;
119 QModelIndex collectionIndex(Akonadi::Collection::Id id) const
120 { return collectionIndex(Akonadi::Collection(id)); }
121 QModelIndex collectionIndex(const Akonadi::Collection&) const;
122 Akonadi::Collection collectionById(Akonadi::Collection::Id) const;
123 Akonadi::Collection collectionForItem(Akonadi::Item::Id) const;
124 Akonadi::Collection collection(const KAEvent& e) const { return collectionForItem(e.itemId()); }
126 /** Remove a collection from Akonadi. The calendar file is not removed.
127 * @return true if a removal job has been scheduled.
129 bool removeCollection(const Akonadi::Collection&);
131 /** Reload a collection's data from Akonadi storage (not from the backend). */
132 bool reloadCollection(const Akonadi::Collection&);
134 /** Reload all collections' data from Akonadi storage (not from the backend). */
135 void reload();
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(), 0); }
148 KAEvent event(Akonadi::Item::Id) const;
149 KAEvent event(const QModelIndex&) const;
150 using QObject::event; // prevent warning about hidden virtual method
152 QModelIndex eventIndex(const KAEvent&);
154 #if 0
155 /** Return all events in a collection, optionally of a specified type. */
156 KAEvent::List events(Akonadi::Collection&, CalEvent::Type = CalEvent::EMPTY) const;
157 #endif
159 bool addEvent(KAEvent&, Akonadi::Collection&);
160 bool addEvents(const KAEvent::List&, Akonadi::Collection&);
161 bool updateEvent(KAEvent& event);
162 bool updateEvent(Akonadi::Item::Id oldId, KAEvent& newEvent);
163 bool deleteEvent(const KAEvent& event);
164 bool deleteEvent(Akonadi::Item::Id itemId);
166 /** Check whether a collection is stored in the current KAlarm calendar format. */
167 static bool isCompatible(const Akonadi::Collection&);
169 /** Return whether a collection is fully writable, i.e. with
170 * create/delete/change rights and compatible with the current KAlarm
171 * calendar format.
173 * @return 1 = fully writable,
174 * 0 = writable except that backend calendar is in an old KAlarm format,
175 * -1 = read-only or incompatible format.
177 static int isWritable(const Akonadi::Collection&);
179 /** Return whether a collection is fully writable, i.e. with
180 * create/delete/change rights and compatible with the current KAlarm
181 * calendar format.
183 * @param format Updated to contain the backend calendar storage format.
184 * If read-only, = KACalendar::Current;
185 * if unknown format, = KACalendar::Incompatible;
186 * otherwise = the backend calendar storage format.
187 * @return 1 = fully writable,
188 * 0 = writable except that backend calendar is in an old KAlarm format,
189 * -1 = read-only (if @p compat == KACalendar::Current), or
190 * incompatible format otherwise.
192 static int isWritable(const Akonadi::Collection&, KACalendar::Compat& format);
194 static CalEvent::Types types(const Akonadi::Collection&);
196 static QSize iconSize() { return mIconSize; }
198 signals:
199 /** Signal emitted when a collection has been added to the model. */
200 void collectionAdded(const Akonadi::Collection&);
202 /** Signal emitted when a collection's enabled or read-only status has changed.
203 * @param inserted true if the reason for the change is that the collection
204 * has been inserted into the model
206 void collectionStatusChanged(const Akonadi::Collection&, AkonadiModel::Change, const QVariant& newValue, bool inserted);
208 /** Signal emitted when events have been added to the model. */
209 void eventsAdded(const AkonadiModel::EventList&);
211 /** Signal emitted when events are about to be removed from the model. */
212 void eventsToBeRemoved(const AkonadiModel::EventList&);
214 /** Signal emitted when an event in the model has changed. */
215 void eventChanged(const AkonadiModel::Event&);
217 /** Signal emitted when Akonadi has completed a collection modification.
218 * @param id Akonadi ID for the collection
219 * @param status true if successful, false if error
221 void collectionModified(Akonadi::Collection::Id, bool status = true);
223 /** Signal emitted when Akonadi has completed a collection deletion.
224 * @param id Akonadi ID for the collection
225 * @param status true if successful, false if error
227 void collectionDeleted(Akonadi::Collection::Id, bool status = true);
229 /** Signal emitted when Akonadi has completed an item creation, update
230 * or deletion.
231 * @param id Akonadi ID for the item
232 * @param status true if successful, false if error
234 void itemDone(Akonadi::Item::Id, bool status = true);
236 protected:
237 virtual QVariant entityHeaderData(int section, Qt::Orientation, int role, HeaderGroup) const;
238 virtual int entityColumnCount(HeaderGroup) const;
240 private slots:
241 void checkResources(Akonadi::ServerManager::State);
242 void slotCollectionChanged(const Akonadi::Collection& c, const QSet<QByteArray>& attrNames)
243 { setCollectionChanged(c, attrNames, false); }
244 void slotCollectionRemoved(const Akonadi::Collection&);
245 void slotCollectionBeingCreated(const QString& path, bool finished);
246 void slotUpdateTimeTo();
247 void slotUpdateArchivedColour(const QColor&);
248 void slotUpdateDisabledColour(const QColor&);
249 void slotUpdateHolidays();
250 void slotUpdateWorkingHours();
251 void slotRowsInserted(const QModelIndex& parent, int start, int end);
252 void slotRowsAboutToBeRemoved(const QModelIndex& parent, int start, int end);
253 void slotMonitoredItemChanged(const Akonadi::Item&, const QSet<QByteArray>&);
254 void slotEmitEventChanged();
255 void modifyCollectionJobDone(KJob*);
256 void itemJobDone(KJob*);
258 private:
259 struct CalData // data per collection
261 CalData() : enabled(false) { }
262 CalData(bool e, const QColor& c) : colour(c), enabled(e) { }
263 QColor colour; // user selected color for the calendar
264 bool enabled; // whether the collection is enabled
266 struct CollJobData // collection data for jobs in progress
268 CollJobData() : id(-1) {}
269 CollJobData(Akonadi::Collection::Id i, const QString& d) : id(i), displayName(d) {}
270 Akonadi::Collection::Id id;
271 QString displayName;
273 struct CollTypeData // data for configuration dialog for collection creation job
275 CollTypeData() : parent(0), alarmType(CalEvent::EMPTY) {}
276 CollTypeData(CalEvent::Type t, QWidget* p) : parent(p), alarmType(t) {}
277 QWidget* parent;
278 CalEvent::Type alarmType;
281 AkonadiModel(Akonadi::ChangeRecorder*, QObject* parent);
282 KAEvent event(const Akonadi::Item&, const QModelIndex&, Akonadi::Collection*) const;
283 void signalDataChanged(bool (*checkFunc)(const Akonadi::Item&), int startColumn, int endColumn, const QModelIndex& parent);
284 void setCollectionChanged(const Akonadi::Collection&, const QSet<QByteArray>&, bool rowInserted);
285 void queueItemModifyJob(const Akonadi::Item&);
286 void checkQueuedItemModifyJob(const Akonadi::Item&);
287 #if 0
288 void getChildEvents(const QModelIndex& parent, CalEvent::Type, KAEvent::List&) const;
289 #endif
290 QColor backgroundColor_p(const Akonadi::Collection&) const;
291 QString repeatText(const KAEvent&) const;
292 QString repeatOrder(const KAEvent&) const;
293 QPixmap* eventIcon(const KAEvent&) const;
294 QString whatsThisText(int column) const;
295 EventList eventList(const QModelIndex& parent, int start, int end);
297 static AkonadiModel* mInstance;
298 static QPixmap* mTextIcon;
299 static QPixmap* mFileIcon;
300 static QPixmap* mCommandIcon;
301 static QPixmap* mEmailIcon;
302 static QPixmap* mAudioIcon;
303 static QSize mIconSize;
304 static int mTimeHourPos; // position of hour within time string, or -1 if leading zeroes included
306 Akonadi::ChangeRecorder* mMonitor;
307 QMap<Akonadi::Collection::Id, CalEvent::Types> mCollectionAlarmTypes; // last content mime types of each collection
308 QMap<Akonadi::Collection::Id, Akonadi::Collection::Rights> mCollectionRights; // last writable status of each collection
309 QMap<Akonadi::Collection::Id, CalEvent::Types> mCollectionEnabled; // last enabled mime types of each collection
310 QMap<KJob*, CollJobData> mPendingCollectionJobs; // pending collection creation/deletion jobs, with collection ID & name
311 QMap<KJob*, CollTypeData> mPendingColCreateJobs; // default alarm type for pending collection creation jobs
312 QMap<KJob*, Akonadi::Item::Id> mPendingItemJobs; // pending item creation/deletion jobs, with event ID
313 QMap<Akonadi::Item::Id, Akonadi::Item> mItemModifyJobQueue; // pending item modification jobs, invalid item = queue empty but job active
314 QList<QString> mCollectionsBeingCreated; // path names of new collections being created
315 QList<Akonadi::Item::Id> mItemsBeingCreated; // new items not fully initialised yet
316 QList<Akonadi::Collection::Id> mCollectionsDeleting; // collections currently being removed
317 QList<Akonadi::Collection::Id> mCollectionsDeleted; // collections recently removed
318 QQueue<Event> mPendingEventChanges; // changed events with changedEvent() signal pending
319 bool mResourcesChecked; // whether resource existence has been checked yet
322 #endif // AKONADIMODEL_H
324 // vim: et sw=4: