Akonadi

favoritecollectionsmodel.h
1 /*
2  SPDX-FileCopyrightText: 2009 Kevin Ottens <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include "akonadicore_export.h"
10 #include "collection.h"
11 #include <KSelectionProxyModel>
12 
13 #include <memory>
14 
15 class KConfigGroup;
16 class KJob;
17 
18 namespace Akonadi
19 {
20 class EntityTreeModel;
21 class FavoriteCollectionsModelPrivate;
22 
23 /**
24  * @short A model that lists a set of favorite collections.
25  *
26  * In some applications you want to provide fast access to a list
27  * of often used collections (e.g. Inboxes from different email accounts
28  * in a mail application). Therefore you can use the FavoriteCollectionsModel
29  * which stores the list of favorite collections in a given configuration
30  * file.
31  *
32  * Example:
33  *
34  * @code
35  *
36  * using namespace Akonadi;
37  *
38  * EntityTreeModel *sourceModel = new EntityTreeModel( ... );
39  *
40  * const KConfigGroup group = KGlobal::config()->group( "Favorite Collections" );
41  *
42  * FavoriteCollectionsModel *model = new FavoriteCollectionsModel( sourceModel, group, this );
43  *
44  * EntityListView *view = new EntityListView( this );
45  * view->setModel( model );
46  *
47  * @endcode
48  *
49  * @author Kevin Ottens <[email protected]>
50  * @since 4.4
51  */
52 class AKONADICORE_EXPORT FavoriteCollectionsModel : public KSelectionProxyModel
53 {
54  Q_OBJECT
55 
56 public:
57  /**
58  * Creates a new favorite collections model.
59  *
60  * @param model The source model where the favorite collections
61  * come from.
62  * @param group The config group that shall be used to save the
63  * selection of favorite collections.
64  * @param parent The parent object.
65  */
66  FavoriteCollectionsModel(QAbstractItemModel *model, const KConfigGroup &group, QObject *parent = nullptr);
67 
68  /**
69  * Destroys the favorite collections model.
70  */
71  ~FavoriteCollectionsModel() override;
72 
73  /**
74  * Returns the list of favorite collections.
75  * @deprecated Use collectionIds instead.
76  */
77  Q_REQUIRED_RESULT AKONADICORE_DEPRECATED Collection::List collections() const;
78 
79  /**
80  * Returns the list of ids of favorite collections set on the FavoriteCollectionsModel.
81  *
82  * Note that if you want Collections with actual data
83  * you should use something like this instead:
84  *
85  * @code
86  * FavoriteCollectionsModel* favs = getFavsModel();
87  * Collection::List cols;
88  * const int rowCount = favs->rowCount();
89  * for (int row = 0; row < rowcount; ++row) {
90  * static const int column = 0;
91  * const QModelIndex index = favs->index(row, column);
92  * const Collection col = index.data(EntityTreeModel::CollectionRole).value<Collection>();
93  * cols << col;
94  * }
95  * @endcode
96  *
97  * Note that due to the asynchronous nature of the model, this method returns collection ids
98  * of collections which may not be in the model yet. If you want the ids of the collections
99  * that are actually in the model, use a loop similar to above with the CollectionIdRole.
100  */
101  Q_REQUIRED_RESULT QList<Collection::Id> collectionIds() const;
102 
103  /**
104  * Return associate label for collection
105  */
106  Q_REQUIRED_RESULT QString favoriteLabel(const Akonadi::Collection &col);
107  Q_REQUIRED_RESULT QString defaultFavoriteLabel(const Akonadi::Collection &col);
108 
109  Q_REQUIRED_RESULT QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
110  Q_REQUIRED_RESULT bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
111  Q_REQUIRED_RESULT QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
112  Q_REQUIRED_RESULT bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
113  Q_REQUIRED_RESULT QStringList mimeTypes() const override;
114  Q_REQUIRED_RESULT Qt::ItemFlags flags(const QModelIndex &index) const override;
115 
116 public Q_SLOTS:
117  /**
118  * Sets the @p collections as favorite collections.
119  */
120  void setCollections(const Akonadi::Collection::List &collections);
121 
122  /**
123  * Adds a @p collection to the list of favorite collections.
124  */
125  void addCollection(const Akonadi::Collection &collection);
126 
127  /**
128  * Removes a @p collection from the list of favorite collections.
129  */
130  void removeCollection(const Akonadi::Collection &collection);
131 
132  /**
133  * Sets a custom @p label that will be used when showing the
134  * favorite @p collection.
135  */
136  void setFavoriteLabel(const Akonadi::Collection &collection, const QString &label);
137 
138 private Q_SLOTS:
139  void pasteJobDone(KJob *job);
140 
141 private:
142  /// @cond PRIVATE
144 
145  std::unique_ptr<FavoriteCollectionsModelPrivate> const d;
146  /// @endcond
147 };
148 
149 }
150 
DisplayRole
void setSourceModel(QAbstractItemModel *sourceModel) override
Represents a collection of PIM items.
Definition: collection.h:61
typedef ItemFlags
Orientation
DropAction
A model that lists a set of favorite collections.
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:48 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.