File indexing completed on 2024-05-12 05:10:43

0001 /*
0002    SPDX-FileCopyrightText: 2011 Sérgio Martins <sergio.martins@kdab.com>
0003    SPDX-FileCopyrightText: 2012 Sérgio Martins <iamsergio@gmail.com>
0004 
0005    SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include "akonadi-calendar_export.h"
0011 #include "calendarbase.h"
0012 
0013 #include <Akonadi/Collection>
0014 
0015 class QAbstractItemModel;
0016 class KCheckableProxyModel;
0017 
0018 namespace Akonadi
0019 {
0020 class Monitor;
0021 class ETMCalendarPrivate;
0022 class EntityTreeModel;
0023 
0024 /**
0025  * @short A KCalendarCore::Calendar that uses an EntityTreeModel to populate itself.
0026  *
0027  * All non-idempotent KCalendarCore::Calendar methods interact with Akonadi.
0028  * If you need a need a non-persistent calendar use FetchJobCalendar.
0029  *
0030  * ETMCalendar allows to be populated with only a subset of your calendar items,
0031  * by using a KCheckableProxyModel to specify which collections to be used
0032  * and/or by setting a KCalendarCore::CalFilter.
0033  *
0034  * @see FetchJobCalendar
0035  * @see CalendarBase
0036  *
0037  * @author Sérgio Martins <iamsergio@gmail.com>
0038  * @since 4.11
0039  */
0040 class AKONADI_CALENDAR_EXPORT ETMCalendar : public CalendarBase
0041 {
0042     Q_OBJECT
0043 public:
0044     enum CollectionColumn { CollectionTitle = 0, CollectionColumnCount };
0045 
0046     using Ptr = QSharedPointer<ETMCalendar>;
0047 
0048     /**
0049      * Constructs a new ETMCalendar. Loading begins immediately, asynchronously.
0050      */
0051     explicit ETMCalendar(QObject *parent = nullptr);
0052 
0053     /**
0054      * Constructs a new ETMCalendar that will only load the specified mime types.
0055      * Use this ctor to ignore journals or to-dos for example.
0056      * If no mime types are specified, all mime types will be used.
0057      */
0058     explicit ETMCalendar(const QStringList &mimeTypes, QObject *parent = nullptr);
0059 
0060     /**
0061      * Constructs a new ETMCalendar.
0062      *
0063      * This overload exists for optimization reasons, it allows to share an EntityTreeModel across
0064      * several ETMCalendars to save memory.
0065      *
0066      * Usually when having many ETMCalendars, the only bit that's different is the collection
0067      * selection. The memory hungry EntityTreeModel is the same, so should be shared.
0068      *
0069      * @param calendar an existing ETMCalendar who's EntityTreeModel is to be used.
0070      *
0071      * @since 4.13
0072      */
0073     explicit ETMCalendar(ETMCalendar *calendar, QObject *parent = nullptr);
0074 
0075     explicit ETMCalendar(Monitor *monitor, QObject *parent = nullptr);
0076     /**
0077      * Destroys this ETMCalendar.
0078      */
0079     ~ETMCalendar() override;
0080 
0081     /**
0082      * Returns the collection having @p id.
0083      * Use this instead of creating a new collection, the returned collection will have
0084      * the correct right, name, display name, etc all set.
0085      */
0086     [[nodiscard]] Akonadi::Collection collection(Akonadi::Collection::Id) const;
0087 
0088     /**
0089      * Returns true if the collection owning incidence @p has righ @p right
0090      */
0091     [[nodiscard]] bool hasRight(const Akonadi::Item &item, Akonadi::Collection::Right right) const;
0092 
0093     /**
0094      * This is an overloaded function.
0095      * @param uid the identifier for the incidence to check for rights
0096      * @param right the access right to check for
0097      * @see hasRight()
0098      */
0099     [[nodiscard]] bool hasRight(const QString &uid, Akonadi::Collection::Right right) const;
0100 
0101     /**
0102      * Returns the KCheckableProxyModel used to select from which collections should
0103      * the calendar be populated from.
0104      */
0105     KCheckableProxyModel *checkableProxyModel() const;
0106 
0107     /**
0108      * Convenience method to access the contents of this KCalendarCore::Calendar through
0109      * a QAIM interface.
0110      *
0111      * Like through calendar interface, the model only items of selected collections.
0112      * To select or unselect collections, see checkableProxyModel().
0113      *
0114      * @see checkableProxyModel()
0115      * @see entityTreeModel()
0116      */
0117     QAbstractItemModel *model() const;
0118 
0119     /**
0120      * Returns the underlying EntityTreeModel.
0121      *
0122      * For most uses, you'll want model() or the KCalendarCore::Calendar interface instead.
0123      *
0124      * It contains every item and collection with calendar mime type, doesn't have
0125      * KCalendarCore filtering and doesn't honour any collection selection.
0126      *
0127      * This method is exposed for performance reasons only, so you can reuse it,
0128      * since it's resource savvy.
0129      *
0130      * @see model()
0131      */
0132     Akonadi::EntityTreeModel *entityTreeModel() const;
0133 
0134     /**
0135      * Returns all alarms occurring in a specified time interval.
0136      * @param from start date of interval
0137      * @param to end data of interval
0138      * @param excludeBlockedAlarms if true, alarms belonging to blocked collections aren't returned.
0139      */
0140     KCalendarCore::Alarm::List alarms(const QDateTime &from, const QDateTime &to, bool excludeBlockedAlarms = false) const override;
0141 
0142     /**
0143      * Enable or disable collection filtering.
0144      * If true, the calendar will only contain items of selected collections.
0145      * @param enable enables collection filtering if set as @c true
0146      * @see checkableProxyModel()
0147      * @see collectionFilteringEnabled()
0148      */
0149     void setCollectionFilteringEnabled(bool enable);
0150 
0151     /**
0152      * Returns whether collection filtering is enabled. Default is true.
0153      * @see setCollectionFilteringEnabled()
0154      */
0155     [[nodiscard]] bool collectionFilteringEnabled() const;
0156 
0157 Q_SIGNALS:
0158     /**
0159      * This signal is emitted if a collection has been changed (properties or attributes).
0160      *
0161      * @param collection The changed collection.
0162      * @param attributeNames The names of the collection attributes that have been changed.
0163      */
0164     void collectionChanged(const Akonadi::Collection &collection, const QSet<QByteArray> &attributeNames);
0165 
0166     /**
0167      * This signal is emitted when one or more collections are added to the ETM.
0168      *
0169      * @param collection non empty list of collections
0170      */
0171     void collectionsAdded(const Akonadi::Collection::List &collection);
0172 
0173     /**
0174      * This signal is emitted when one or more collections are deleted from the ETM.
0175      *
0176      * @param collection non empty list of collections
0177      */
0178     void collectionsRemoved(const Akonadi::Collection::List &collection);
0179 
0180     /**
0181      * Emitted whenever an Item is inserted, removed or modified.
0182      */
0183     void calendarChanged();
0184 
0185 private:
0186     Q_DECLARE_PRIVATE(ETMCalendar)
0187 };
0188 }