File indexing completed on 2024-09-22 04:41:03

0001 /*
0002     SPDX-FileCopyrightText: 2009 Kevin Ottens <ervin@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "akonadicore_export.h"
0010 #include "collection.h"
0011 #include <KSelectionProxyModel>
0012 
0013 #include <memory>
0014 
0015 class KConfigGroup;
0016 class KJob;
0017 
0018 namespace Akonadi
0019 {
0020 class EntityTreeModel;
0021 class FavoriteCollectionsModelPrivate;
0022 
0023 /**
0024  * @short A model that lists a set of favorite collections.
0025  *
0026  * In some applications you want to provide fast access to a list
0027  * of often used collections (e.g. Inboxes from different email accounts
0028  * in a mail application). Therefore you can use the FavoriteCollectionsModel
0029  * which stores the list of favorite collections in a given configuration
0030  * file.
0031  *
0032  * Example:
0033  *
0034  * @code
0035  *
0036  * using namespace Akonadi;
0037  *
0038  * EntityTreeModel *sourceModel = new EntityTreeModel( ... );
0039  *
0040  * const KConfigGroup group = KGlobal::config()->group( "Favorite Collections" );
0041  *
0042  * FavoriteCollectionsModel *model = new FavoriteCollectionsModel( sourceModel, group, this );
0043  *
0044  * EntityListView *view = new EntityListView( this );
0045  * view->setModel( model );
0046  *
0047  * @endcode
0048  *
0049  * @author Kevin Ottens <ervin@kde.org>
0050  * @since 4.4
0051  */
0052 class AKONADICORE_EXPORT FavoriteCollectionsModel : public KSelectionProxyModel
0053 {
0054     Q_OBJECT
0055 
0056 public:
0057     /**
0058      * Creates a new favorite collections model.
0059      *
0060      * @param model The source model where the favorite collections
0061      *              come from.
0062      * @param group The config group that shall be used to save the
0063      *              selection of favorite collections.
0064      * @param parent The parent object.
0065      */
0066     FavoriteCollectionsModel(QAbstractItemModel *model, const KConfigGroup &group, QObject *parent = nullptr);
0067 
0068     /**
0069      * Destroys the favorite collections model.
0070      */
0071     ~FavoriteCollectionsModel() override;
0072 
0073     /**
0074      * Returns the list of favorite collections.
0075      * @deprecated Use collectionIds instead.
0076      */
0077     [[nodiscard]] AKONADICORE_DEPRECATED Collection::List collections() const;
0078 
0079     /**
0080      * Returns the list of ids of favorite collections set on the FavoriteCollectionsModel.
0081      *
0082      * Note that if you want Collections with actual data
0083      * you should use something like this instead:
0084      *
0085      * @code
0086      *   FavoriteCollectionsModel* favs = getFavsModel();
0087      *   Collection::List cols;
0088      *   const int rowCount = favs->rowCount();
0089      *   for (int row = 0; row < rowcount; ++row) {
0090      *     static const int column = 0;
0091      *     const QModelIndex index = favs->index(row, column);
0092      *     const Collection col = index.data(EntityTreeModel::CollectionRole).value<Collection>();
0093      *     cols << col;
0094      *   }
0095      * @endcode
0096      *
0097      * Note that due to the asynchronous nature of the model, this method returns collection ids
0098      * of collections which may not be in the model yet. If you want the ids of the collections
0099      * that are actually in the model, use a loop similar to above with the CollectionIdRole.
0100      */
0101     [[nodiscard]] QList<Collection::Id> collectionIds() const;
0102 
0103     /**
0104      * Return associate label for collection
0105      */
0106     [[nodiscard]] QString favoriteLabel(const Akonadi::Collection &col);
0107     [[nodiscard]] QString defaultFavoriteLabel(const Akonadi::Collection &col);
0108 
0109     [[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0110     [[nodiscard]] bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0111     [[nodiscard]] QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0112     [[nodiscard]] bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
0113     [[nodiscard]] QStringList mimeTypes() const override;
0114     [[nodiscard]] Qt::ItemFlags flags(const QModelIndex &index) const override;
0115 
0116 public Q_SLOTS:
0117     /**
0118      * Sets the @p collections as favorite collections.
0119      */
0120     void setCollections(const Akonadi::Collection::List &collections);
0121 
0122     /**
0123      * Adds a @p collection to the list of favorite collections.
0124      */
0125     void addCollection(const Akonadi::Collection &collection);
0126 
0127     /**
0128      * Removes a @p collection from the list of favorite collections.
0129      */
0130     void removeCollection(const Akonadi::Collection &collection);
0131 
0132     /**
0133      * Sets a custom @p label that will be used when showing the
0134      * favorite @p collection.
0135      */
0136     void setFavoriteLabel(const Akonadi::Collection &collection, const QString &label);
0137 
0138 private Q_SLOTS:
0139     void pasteJobDone(KJob *job);
0140 
0141 private:
0142     /// @cond PRIVATE
0143     using KSelectionProxyModel::setSourceModel;
0144 
0145     std::unique_ptr<FavoriteCollectionsModelPrivate> const d;
0146     /// @endcond
0147 };
0148 
0149 }