Akonadi

collectionrequester.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Ingo Klöcker <kloecker@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "collectionrequester.h"
8#include "collectionfetchjob.h"
9#include "collectionfetchscope.h"
10#include "entitydisplayattribute.h"
11
12#include <KLocalizedString>
13#include <KStandardShortcut>
14#include <QLineEdit>
15
16#include <QAction>
17#include <QEvent>
18#include <QHBoxLayout>
19#include <QPushButton>
20
21using namespace Akonadi;
22
23class Akonadi::CollectionRequesterPrivate
24{
25public:
26 explicit CollectionRequesterPrivate(CollectionRequester *parent)
27 : q(parent)
28 {
29 }
30
31 ~CollectionRequesterPrivate()
32 {
33 }
34
35 void fetchCollection(const Collection &collection);
36
37 void init();
38
39 // slots
40 void _k_slotOpenDialog();
41 void _k_collectionReceived(KJob *job);
42 void _k_collectionsNamesReceived(KJob *job);
43
44 CollectionRequester *const q;
45 Collection collection;
46 QLineEdit *edit = nullptr;
47 QPushButton *button = nullptr;
48 CollectionDialog *collectionDialog = nullptr;
49};
50
51void CollectionRequesterPrivate::fetchCollection(const Collection &collection)
52{
53 auto job = new CollectionFetchJob(collection, Akonadi::CollectionFetchJob::Base, q);
54 job->setProperty("OriginalCollectionId", collection.id());
55 job->fetchScope().setAncestorRetrieval(CollectionFetchScope::All);
57 _k_collectionReceived(job);
58 });
59}
60
61void CollectionRequesterPrivate::_k_collectionReceived(KJob *job)
62{
63 auto fetch = qobject_cast<CollectionFetchJob *>(job);
64 if (!fetch) {
65 return;
66 }
67 if (fetch->collections().size() == 1) {
68 Collection::List chain;
69 Collection currentCollection = fetch->collections().at(0);
70 while (currentCollection.isValid()) {
71 chain << currentCollection;
72 currentCollection = Collection(currentCollection.parentCollection());
73 }
74
75 auto namesFetch = new CollectionFetchJob(chain, CollectionFetchJob::Base, q);
76 namesFetch->setProperty("OriginalCollectionId", job->property("OriginalCollectionId"));
77 namesFetch->fetchScope().setAncestorRetrieval(CollectionFetchScope::Parent);
78 QObject::connect(namesFetch, &CollectionFetchJob::finished, q, [this](KJob *job) {
79 _k_collectionsNamesReceived(job);
80 });
81 } else {
82 _k_collectionsNamesReceived(job);
83 }
84}
85
86void CollectionRequesterPrivate::_k_collectionsNamesReceived(KJob *job)
87{
88 auto fetch = qobject_cast<CollectionFetchJob *>(job);
89 const qint64 originalId = fetch->property("OriginalCollectionId").toLongLong();
90
92 const Akonadi::Collection::List lstCols = fetch->collections();
93 for (const Collection &collection : lstCols) {
94 names.insert(collection.id(), collection);
95 }
96
97 QStringList namesList;
98 Collection currentCollection = names.take(originalId);
99 while (currentCollection.isValid()) {
100 namesList.prepend(currentCollection.displayName());
101 currentCollection = names.take(currentCollection.parentCollection().id());
102 }
103 edit->setText(namesList.join(QLatin1Char('/')));
104}
105
106void CollectionRequesterPrivate::init()
107{
108 auto hbox = new QHBoxLayout(q);
109 hbox->setContentsMargins({});
110
111 edit = new QLineEdit(q);
112 edit->setReadOnly(true);
113 edit->setPlaceholderText(i18n("No Folder"));
114 edit->setClearButtonEnabled(false);
116 hbox->addWidget(edit);
117
118 button = new QPushButton(q);
119 button->setIcon(QIcon::fromTheme(QStringLiteral("document-open")));
120 const int buttonSize = edit->sizeHint().height();
121 button->setFixedSize(buttonSize, buttonSize);
122 button->setToolTip(i18n("Open collection dialog"));
123 hbox->addWidget(button);
124
125 hbox->setSpacing(-1);
126
127 edit->installEventFilter(q);
128 q->setFocusProxy(button);
130
131 q->connect(button, &QPushButton::clicked, q, [this]() {
132 _k_slotOpenDialog();
133 });
134
135 auto openAction = new QAction(q);
136 openAction->setShortcut(KStandardShortcut::Open);
137 q->connect(openAction, &QAction::triggered, q, [this]() {
138 _k_slotOpenDialog();
139 });
140
141 collectionDialog = new CollectionDialog(q);
142 collectionDialog->setWindowIcon(QIcon::fromTheme(QStringLiteral("akonadi")));
143 collectionDialog->setWindowTitle(i18nc("@title:window", "Select a Collection"));
145 collectionDialog->changeCollectionDialogOptions(CollectionDialog::KeepTreeExpanded);
146}
147
148void CollectionRequesterPrivate::_k_slotOpenDialog()
149{
150 CollectionDialog *dlg = collectionDialog;
151
152 if (dlg->exec() != QDialog::Accepted) {
153 return;
154 }
155
156 const Akonadi::Collection collection = dlg->selectedCollection();
157 q->setCollection(collection);
158 Q_EMIT q->collectionChanged(collection);
159}
160
162 : QWidget(parent)
163 , d(new CollectionRequesterPrivate(this))
164{
165 d->init();
166}
167
169 : QWidget(parent)
170 , d(new CollectionRequesterPrivate(this))
171{
172 d->init();
174}
175
177
179{
180 return d->collection;
181}
182
184{
185 d->collection = collection;
186 QString name;
187 if (collection.isValid()) {
188 name = collection.displayName();
189 }
190
191 d->edit->setText(name);
193 d->fetchCollection(collection);
194}
195
197{
198 if (d->collectionDialog) {
199 d->collectionDialog->setMimeTypeFilter(mimeTypes);
200 }
201}
202
204{
205 if (d->collectionDialog) {
206 return d->collectionDialog->mimeTypeFilter();
207 } else {
208 return QStringList();
209 }
210}
211
213{
214 if (d->collectionDialog) {
215 d->collectionDialog->setAccessRightsFilter(rights);
216 }
217}
218
220{
221 if (d->collectionDialog) {
222 return d->collectionDialog->accessRightsFilter();
223 } else {
225 }
226}
227
229{
230 if (d->collectionDialog) {
231 d->collectionDialog->changeCollectionDialogOptions(options);
232 }
233}
234
236{
237 if (d->collectionDialog) {
238 d->collectionDialog->setContentMimeTypes(mimetypes);
239 }
240}
241
242void CollectionRequester::changeEvent(QEvent *event)
243{
244 if (event->type() == QEvent::WindowTitleChange) {
245 if (d->collectionDialog) {
246 d->collectionDialog->setWindowTitle(windowTitle());
247 }
248 } else if (event->type() == QEvent::EnabledChange) {
249 if (d->collectionDialog) {
250 d->collectionDialog->setEnabled(true);
251 }
252 }
254}
255
256#include "moc_collectionrequester.cpp"
A collection selection dialog.
Akonadi::Collection selectedCollection() const
Returns the selected collection if the selection mode is QAbstractItemView::SingleSelection.
void changeCollectionDialogOptions(CollectionDialogOptions options)
Change collection dialog options.
void setSelectionMode(QAbstractItemView::SelectionMode mode)
Sets the selection mode.
Job that fetches collections from the Akonadi storage.
@ Base
Only fetch the base collection.
@ Parent
Only retrieve the immediate parent collection.
@ All
Retrieve all ancestors, up to Collection::root()
A widget to request an Akonadi collection from the user.
void setContentMimeTypes(const QStringList &mimetypes)
Allow to specify collection content mimetype when we create new one.
~CollectionRequester() override
Destroys the collection requester.
void collectionChanged(const Akonadi::Collection &collection)
This signal is emitted when the selected collection has changed.
CollectionRequester(QWidget *parent=nullptr)
Creates a collection requester.
void setCollection(const Akonadi::Collection &collection)
Sets the collection of the requester.
Akonadi::Collection collection() const
Returns the currently chosen collection, or an empty collection if none none was chosen.
QStringList mimeTypeFilter() const
Returns the mime types any of which the selected collection shall support.
Collection::Rights accessRightsFilter() const
Returns the access rights that the listed collections shall match with.
void changeCollectionDialogOptions(CollectionDialog::CollectionDialogOptions options)
void setMimeTypeFilter(const QStringList &mimeTypes)
Sets the mime types any of which the selected collection shall support.
void setAccessRightsFilter(Collection::Rights rights)
Sets the access rights that the listed collections shall match with.
Represents a collection of PIM items.
Definition collection.h:62
@ ReadOnly
Can only read items or subcollection of this collection.
Definition collection.h:90
Collection parentCollection() const
Returns the parent collection of this object.
void finished(KJob *job)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
Helper integration between Akonadi and Qt.
void clicked(bool checked)
void setIcon(const QIcon &icon)
void triggered(bool checked)
virtual int exec()
WindowTitleChange
QIcon fromTheme(const QString &name)
void setClearButtonEnabled(bool enable)
void setPlaceholderText(const QString &)
void setReadOnly(bool)
virtual QSize sizeHint() const const override
void setText(const QString &)
void prepend(parameter_type value)
iterator insert(const Key &key, const T &value)
T take(const Key &key)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void installEventFilter(QObject *filterObj)
QVariant property(const char *name) const const
int height() const const
QString join(QChar separator) const const
virtual void changeEvent(QEvent *event)
virtual bool event(QEvent *event) override
void setFocusPolicy(Qt::FocusPolicy policy)
void setFixedSize(const QSize &s)
void setFocusProxy(QWidget *w)
void setToolTip(const QString &)
void setWindowIcon(const QIcon &icon)
void setWindowTitle(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.