2 * collectionmodel.h - Akonadi collection models
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 COLLECTIONMODEL_H
22 #define COLLECTIONMODEL_H
24 #include "akonadimodel.h"
26 #include <kalarmcal/kacalendar.h>
28 #include <AkonadiCore/favoritecollectionsmodel.h>
29 #include <kcheckableproxymodel.h>
30 #include <kdescendantsproxymodel.h>
32 #include <QSortFilterProxyModel>
35 using namespace KAlarmCal
;
40 class EntityMimeTypeFilterModel
;
43 /*=============================================================================
44 = Class: CollectionListModel
45 = Proxy model converting the AkonadiModel collection tree into a flat list.
46 = The model may be restricted to specified content mime types.
47 = It can optionally be restricted to writable and/or enabled Collections.
48 =============================================================================*/
49 class CollectionListModel
: public KDescendantsProxyModel
53 explicit CollectionListModel(QObject
* parent
= Q_NULLPTR
);
54 void setEventTypeFilter(CalEvent::Type
);
55 void setFilterWritable(bool writable
);
56 void setFilterEnabled(bool enabled
);
57 void useCollectionColour(bool use
) { mUseCollectionColour
= use
; }
58 Akonadi::Collection
collection(int row
) const;
59 Akonadi::Collection
collection(const QModelIndex
&) const;
60 QModelIndex
collectionIndex(const Akonadi::Collection
&) const;
61 virtual bool isDescendantOf(const QModelIndex
& ancestor
, const QModelIndex
& descendant
) const;
62 QVariant
data(const QModelIndex
&, int role
= Qt::DisplayRole
) const Q_DECL_OVERRIDE
;
65 bool mUseCollectionColour
;
69 /*=============================================================================
70 = Class: CollectionCheckListModel
71 = Proxy model providing a checkable list of all Collections. A Collection's
72 = checked status is equivalent to whether it is selected or not.
73 = An alarm type is specified, whereby Collections which are enabled for that
74 = alarm type are checked; Collections which do not contain that alarm type, or
75 = which are disabled for that alarm type, are unchedked.
76 =============================================================================*/
77 class CollectionCheckListModel
: public KCheckableProxyModel
81 explicit CollectionCheckListModel(CalEvent::Type
, QObject
* parent
= Q_NULLPTR
);
82 ~CollectionCheckListModel();
83 Akonadi::Collection
collection(int row
) const;
84 Akonadi::Collection
collection(const QModelIndex
&) const;
85 QVariant
data(const QModelIndex
&, int role
= Qt::DisplayRole
) const Q_DECL_OVERRIDE
;
86 bool setData(const QModelIndex
&, const QVariant
& value
, int role
) Q_DECL_OVERRIDE
;
89 void collectionTypeChange(CollectionCheckListModel
*);
92 void selectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
);
93 void slotRowsInserted(const QModelIndex
& parent
, int start
, int end
);
94 void collectionStatusChanged(const Akonadi::Collection
&, AkonadiModel::Change
, const QVariant
& value
, bool inserted
);
97 void setSelectionStatus(const Akonadi::Collection
&, const QModelIndex
&);
99 static CollectionListModel
* mModel
;
100 static int mInstanceCount
;
101 CalEvent::Type mAlarmType
; // alarm type contained in this model
102 QItemSelectionModel
* mSelectionModel
;
106 /*=============================================================================
107 = Class: CollectionFilterCheckListModel
108 = Proxy model providing a checkable collection list. The model contains all
109 = alarm types, but returns only one type at any given time. The selected alarm
110 = type may be changed as desired.
111 =============================================================================*/
112 class CollectionFilterCheckListModel
: public QSortFilterProxyModel
116 explicit CollectionFilterCheckListModel(QObject
* parent
= Q_NULLPTR
);
117 void setEventTypeFilter(CalEvent::Type
);
118 Akonadi::Collection
collection(int row
) const;
119 Akonadi::Collection
collection(const QModelIndex
&) const;
120 QVariant
data(const QModelIndex
&, int role
= Qt::DisplayRole
) const Q_DECL_OVERRIDE
;
123 bool filterAcceptsRow(int sourceRow
, const QModelIndex
& sourceParent
) const Q_DECL_OVERRIDE
;
126 void collectionTypeChanged(CollectionCheckListModel
*);
129 CollectionCheckListModel
* mActiveModel
;
130 CollectionCheckListModel
* mArchivedModel
;
131 CollectionCheckListModel
* mTemplateModel
;
132 CalEvent::Type mAlarmType
; // alarm type contained in this model
136 /*=============================================================================
137 = Class: CollectionView
138 = View for a CollectionFilterCheckListModel.
139 =============================================================================*/
140 class CollectionView
: public QListView
144 explicit CollectionView(CollectionFilterCheckListModel
*, QWidget
* parent
= Q_NULLPTR
);
145 CollectionFilterCheckListModel
* collectionModel() const { return static_cast<CollectionFilterCheckListModel
*>(model()); }
146 Akonadi::Collection
collection(int row
) const;
147 Akonadi::Collection
collection(const QModelIndex
&) const;
150 void setModel(QAbstractItemModel
*) Q_DECL_OVERRIDE
;
151 void mouseReleaseEvent(QMouseEvent
*) Q_DECL_OVERRIDE
;
152 bool viewportEvent(QEvent
*) Q_DECL_OVERRIDE
;
156 /*=============================================================================
157 = Class: CollectionControlModel
158 = Proxy model to select which Collections will be enabled. Disabled Collections
159 = are not populated or monitored; their contents are ignored. The set of
160 = enabled Collections is stored in the config file's "Collections" group.
161 = Note that this model is not used directly for displaying - its purpose is to
162 = allow collections to be disabled, which will remove them from the other
164 = This model also controls which collections are standard for their type,
165 = ensuring that there is only one standard collection for any given type.
166 =============================================================================*/
167 class CollectionControlModel
: public Akonadi::FavoriteCollectionsModel
171 static CollectionControlModel
* instance();
173 /** Return whether a collection is enabled (and valid). */
174 static bool isEnabled(const Akonadi::Collection
&, CalEvent::Type
);
176 /** Enable or disable a collection (if it is valid) for specified alarm types.
177 * Note that this only changes the status for the specified alarm types.
178 * @return alarm types which can be enabled
180 static CalEvent::Types
setEnabled(const Akonadi::Collection
&, CalEvent::Types
, bool enabled
);
182 /** Return whether a collection is both enabled and fully writable for a
183 * given alarm type, i.e. with create/delete/change rights and compatible
184 * with the current KAlarm calendar format.
185 * Optionally, the enabled status can be ignored.
187 * @return 1 = fully enabled and writable,
188 * 0 = enabled and writable except that backend calendar is in an
190 * -1 = read-only, disabled or incompatible format.
192 static int isWritableEnabled(const Akonadi::Collection
&, CalEvent::Type
);
194 /** Return whether a collection is both enabled and fully writable for a
195 * given alarm type, i.e. with create/delete/change rights and compatible
196 * with the current KAlarm calendar format.
197 * Optionally, the enabled status can be ignored.
199 * @param format If the reply is false, and the calendar is not read-only
200 * but its backend calendar storage format is not the
201 * current KAlarm format, @p format is set to the calendar
202 * format used by the backend. If the calendar is
203 * non-writable for any other reason, @p format is set
204 * to KACalendar::Current.
205 * @return 1 = fully enabled and writable,
206 * 0 = enabled and writable except that backend calendar is in an
208 * -1 = read-only (if @p format == KACalendar::Current), or
209 * disabled or incompatible format.
211 static int isWritableEnabled(const Akonadi::Collection
&, CalEvent::Type
, KACalendar::Compat
& format
);
213 /** Return the standard collection for a specified mime type.
214 * @param useDefault false to return the defined standard collection, if any;
215 * true to return the standard or only collection for the type.
216 * Reply = invalid collection if there is no standard collection.
218 static Akonadi::Collection
getStandard(CalEvent::Type
, bool useDefault
= false);
220 /** Return whether a collection is the standard collection for a specified
222 static bool isStandard(Akonadi::Collection
&, CalEvent::Type
);
224 /** Return the alarm type(s) for which a collection is the standard collection.
225 * @param useDefault false to return the defined standard types, if any;
226 * true to return the types for which it is the standard or
229 static CalEvent::Types
standardTypes(const Akonadi::Collection
&, bool useDefault
= false);
231 /** Set or clear a collection as the standard collection for a specified
232 * mime type. This does not affect its status for other mime types.
234 static void setStandard(Akonadi::Collection
&, CalEvent::Type
, bool standard
);
236 /** Set which mime types a collection is the standard collection for.
237 * Its standard status is cleared for other mime types.
239 static void setStandard(Akonadi::Collection
&, CalEvent::Types
);
241 /** Set whether the user should be prompted for the destination collection
243 * @param ask true = prompt for which collection to add to;
244 * false = add to standard collection.
246 static void setAskDestinationPolicy(bool ask
) { mAskDestination
= ask
; }
248 /** Find the collection to be used to store an event of a given type.
249 * This will be the standard collection for the type, but if this is not valid,
250 * the user will be prompted to select a collection.
251 * @param noPrompt don't prompt the user even if the standard collection is not valid
252 * @param cancelled If non-null: set to true if the user cancelled
253 * the prompt dialogue; set to false if any other error.
255 static Akonadi::Collection
destination(CalEvent::Type
, QWidget
* promptparent
= Q_NULLPTR
, bool noPrompt
= false, bool* cancelled
= Q_NULLPTR
);
257 /** Return the enabled collections which contain a specified mime type.
258 * If 'writable' is true, only writable collections are included.
260 static Akonadi::Collection::List
enabledCollections(CalEvent::Type
, bool writable
);
262 /** Return the collection ID for a given resource ID.
263 * @return collection ID, or -1 if the resource is not in KAlarm's list.
265 static Akonadi::Collection
collectionForResource(const QString
& resourceId
);
267 /** Return whether one or all enabled collections have been populated,
268 * i.e. whether their items have been fetched.
270 static bool isPopulated(Akonadi::Collection::Id
);
272 /** Wait until one or all enabled collections have been populated,
273 * i.e. whether their items have been fetched.
274 * @param colId collection ID, or -1 for all collections
275 * @param timeout timeout in seconds, or 0 for no timeout
276 * @return true if successful.
278 bool waitUntilPopulated(Akonadi::Collection::Id colId
= -1, int timeout
= 0);
280 QVariant
data(const QModelIndex
&, int role
= Qt::DisplayRole
) const Q_DECL_OVERRIDE
;
282 /** Return a bulleted list of alarm types for inclusion in an i18n message. */
283 static QString
typeListForDisplay(CalEvent::Types
);
287 void statusChanged(const Akonadi::Collection
&, AkonadiModel::Change
, const QVariant
& value
, bool inserted
);
288 void collectionPopulated();
291 explicit CollectionControlModel(QObject
* parent
= Q_NULLPTR
);
292 void findEnabledCollections(const Akonadi::EntityMimeTypeFilterModel
*, const QModelIndex
& parent
, Akonadi::Collection::List
&) const;
293 CalEvent::Types
setEnabledStatus(const Akonadi::Collection
&, CalEvent::Types
, bool inserted
);
294 static CalEvent::Types
checkTypesToEnable(const Akonadi::Collection
&, const Akonadi::Collection::List
&, CalEvent::Types
);
296 static CollectionControlModel
* mInstance
;
297 static bool mAskDestination
;
298 QEventLoop
* mPopulatedCheckLoop
;
301 #endif // COLLECTIONMODEL_H