Akonadi

collectionrequester.cpp
1 /*
2  Copyright 2008 Ingo Klöcker <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "collectionrequester.h"
21 #include "collectiondialog.h"
22 #include "entitydisplayattribute.h"
23 #include "collectionfetchjob.h"
24 #include "collectionfetchscope.h"
25 
26 #include <QLineEdit>
27 #include <KLocalizedString>
28 #include <KStandardShortcut>
29 
30 #include <QEvent>
31 #include <QAction>
32 #include <QPushButton>
33 #include <QHBoxLayout>
34 
35 using namespace Akonadi;
36 
37 class Q_DECL_HIDDEN CollectionRequester::Private
38 {
39 public:
40  Private(CollectionRequester *parent)
41  : q(parent)
42  {
43  }
44 
45  ~Private()
46  {
47  }
48 
49  void fetchCollection(const Collection &collection);
50 
51  void init();
52 
53  // slots
54  void _k_slotOpenDialog();
55  void _k_collectionReceived(KJob *job);
56  void _k_collectionsNamesReceived(KJob *job);
57 
58  CollectionRequester *q = nullptr;
59  Collection collection;
60  QLineEdit *edit = nullptr;
61  QPushButton *button = nullptr;
62  CollectionDialog *collectionDialog = nullptr;
63 };
64 
65 void CollectionRequester::Private::fetchCollection(const Collection &collection)
66 {
68  job->setProperty("OriginalCollectionId", collection.id());
70  connect(job, &CollectionFetchJob::finished, q, [this](KJob *job) {_k_collectionReceived(job);});
71 }
72 
73 void CollectionRequester::Private::_k_collectionReceived(KJob *job)
74 {
75  CollectionFetchJob *fetch = qobject_cast<CollectionFetchJob *>(job);
76  if (!fetch) {
77  return;
78  }
79  if (fetch->collections().size() == 1) {
80  Collection::List chain;
81  Collection currentCollection = fetch->collections().at(0);
82  while (currentCollection.isValid()) {
83  chain << currentCollection;
84  currentCollection = Collection(currentCollection.parentCollection());
85  }
86 
88  namesFetch->setProperty("OriginalCollectionId", job->property("OriginalCollectionId"));
89  namesFetch->fetchScope().setAncestorRetrieval(CollectionFetchScope::Parent);
90  connect(namesFetch, &CollectionFetchJob::finished, q, [this](KJob *job) {_k_collectionsNamesReceived(job);});
91  } else {
92  _k_collectionsNamesReceived(job);
93  }
94 }
95 
96 void CollectionRequester::Private::_k_collectionsNamesReceived(KJob *job)
97 {
98  CollectionFetchJob *fetch = qobject_cast<CollectionFetchJob *>(job);
99  const qint64 originalId = fetch->property("OriginalCollectionId").toLongLong();
100 
102  const Akonadi::Collection::List lstCols = fetch->collections();
103  for (const Collection &collection : lstCols) {
104  names.insert(collection.id(), collection);
105  }
106 
107  QStringList namesList;
108  Collection currentCollection = names.take(originalId);
109  while (currentCollection.isValid()) {
110  namesList.prepend(currentCollection.displayName());
111  currentCollection = names.take(currentCollection.parentCollection().id());
112  }
113  edit->setText(namesList.join(QLatin1Char('/')));
114 }
115 
116 void CollectionRequester::Private::init()
117 {
118  QHBoxLayout *hbox = new QHBoxLayout(q);
119  hbox->setContentsMargins(0, 0, 0, 0);
120 
121  edit = new QLineEdit(q);
122  edit->setReadOnly(true);
123  edit->setPlaceholderText(i18n("No Folder"));
124  edit->setClearButtonEnabled(false);
125  edit->setFocusPolicy(Qt::NoFocus);
126  hbox->addWidget(edit);
127 
128  button = new QPushButton(q);
129  button->setIcon(QIcon::fromTheme(QStringLiteral("document-open")));
130  const int buttonSize = edit->sizeHint().height();
131  button->setFixedSize(buttonSize, buttonSize);
132  button->setToolTip(i18n("Open collection dialog"));
133  hbox->addWidget(button);
134 
135  hbox->setSpacing(-1);
136 
137  edit->installEventFilter(q);
138  q->setFocusProxy(button);
139  q->setFocusPolicy(Qt::StrongFocus);
140 
141  q->connect(button, &QPushButton::clicked, q, [this]() { _k_slotOpenDialog(); });
142 
143  QAction *openAction = new QAction(q);
145  q->connect(openAction, &QAction::triggered, q, [this]() { _k_slotOpenDialog(); });
146 
147  collectionDialog = new CollectionDialog(q);
148  collectionDialog->setWindowIcon(QIcon::fromTheme(QStringLiteral("akonadi")));
149  collectionDialog->setWindowTitle(i18nc("@title:window", "Select a collection"));
150  collectionDialog->setSelectionMode(QAbstractItemView::SingleSelection);
151  collectionDialog->changeCollectionDialogOptions(CollectionDialog::KeepTreeExpanded);
152 }
153 
154 void CollectionRequester::Private::_k_slotOpenDialog()
155 {
156  CollectionDialog *dlg = collectionDialog;
157 
158  if (dlg->exec() != QDialog::Accepted) {
159  return;
160  }
161 
162  const Akonadi::Collection collection = dlg->selectedCollection();
163  q->setCollection(collection);
164  Q_EMIT q->collectionChanged(collection);
165 }
166 
168  : QWidget(parent)
169  , d(new Private(this))
170 {
171  d->init();
172 }
173 
175  : QWidget(parent)
176  , d(new Private(this))
177 {
178  d->init();
179  setCollection(collection);
180 }
181 
183 {
184  delete d;
185 }
186 
188 {
189  return d->collection;
190 }
191 
193 {
194  d->collection = collection;
195  QString name;
196  if (collection.isValid()) {
197  name = collection.displayName();
198  }
199 
200  d->edit->setText(name);
201  Q_EMIT collectionChanged(collection);
202  d->fetchCollection(collection);
203 }
204 
206 {
207  if (d->collectionDialog) {
208  d->collectionDialog->setMimeTypeFilter(mimeTypes);
209  }
210 }
211 
213 {
214  if (d->collectionDialog) {
215  return d->collectionDialog->mimeTypeFilter();
216  } else {
217  return QStringList();
218  }
219 }
220 
222 {
223  if (d->collectionDialog) {
224  d->collectionDialog->setAccessRightsFilter(rights);
225  }
226 }
227 
229 {
230  if (d->collectionDialog) {
231  return d->collectionDialog->accessRightsFilter();
232  } else {
234  }
235 }
236 
238 {
239  if (d->collectionDialog) {
240  d->collectionDialog->changeCollectionDialogOptions(options);
241  }
242 }
243 
245 {
246  if (d->collectionDialog) {
247  d->collectionDialog->setContentMimeTypes(mimetypes);
248  }
249 }
250 
251 void CollectionRequester::changeEvent(QEvent *event)
252 {
253  if (event->type() == QEvent::WindowTitleChange) {
254  if (d->collectionDialog) {
255  d->collectionDialog->setWindowTitle(windowTitle());
256  }
257  } else if (event->type() == QEvent::EnabledChange) {
258  if (d->collectionDialog) {
259  d->collectionDialog->setEnabled(true);
260  }
261  }
262  QWidget::changeEvent(event);
263 }
264 
265 #include "moc_collectionrequester.cpp"
Akonadi::Collection selectedCollection() const
Returns the selected collection if the selection mode is QAbstractItemView::SingleSelection.
qlonglong toLongLong(bool *ok) const const
void triggered(bool checked)
QEvent::Type type() const const
void setContentsMargins(int left, int top, int right, int bottom)
void setAncestorRetrieval(AncestorRetrieval ancestorDepth)
Sets how many levels of ancestor collections should be included in the retrieval. ...
Collection::List collections() const
Returns the list of fetched collection.
bool isValid() const
Returns whether the collection is valid.
Definition: collection.cpp:137
QString displayName() const
Returns the display name (EntityDisplayAttribute::displayName()) if set, and Collection::name() other...
Definition: collection.cpp:230
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.
A collection selection dialog.
void setMimeTypeFilter(const QStringList &mimeTypes)
Sets the mime types any of which the selected collection shall support.
void collectionChanged(const Akonadi::Collection &collection)
This signal is emitted when the selected collection has changed.
CollectionFetchScope & fetchScope()
Returns the collection fetch scope.
Represents a collection of PIM items.
Definition: collection.h:76
Job that fetches collections from the Akonadi storage.
virtual int exec()
QString join(const QString &separator) const const
Only retrieve the immediate parent collection.
A widget to request an Akonadi collection from the user.
Only fetch the base collection.
QUICKADDONS_EXPORT void init()
void setAccessRightsFilter(Collection::Rights rights)
Sets the access rights that the listed collections shall match with.
Can only read items or subcollection of this collection.
Definition: collection.h:93
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setCollection(const Akonadi::Collection &collection)
Sets the collection of the requester.
CollectionRequester(QWidget *parent=nullptr)
Creates a collection requester.
QVariant property(const char *name) const const
void finished(KJob *job)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void clicked(bool checked)
Retrieve all ancestors, up to Collection::root()
Collection::Rights accessRightsFilter() const
Returns the access rights that the listed collections shall match with.
Collection parentCollection() const
Returns the parent collection of this object.
Definition: collection.cpp:211
virtual void changeEvent(QEvent *event)
void setShortcut(const QKeySequence &shortcut)
QString i18n(const char *text, const TYPE &arg...)
const T & at(int i) const const
Id id() const
Returns the unique identifier of the collection.
Definition: collection.cpp:112
Helper integration between Akonadi and Qt.
QString windowTitle() const const
void changeCollectionDialogOptions(CollectionDialog::CollectionDialogOptions options)
void prepend(const T &value)
QIcon fromTheme(const QString &name)
bool setProperty(const char *name, const QVariant &value)
QMap::iterator insert(const Key &key, const T &value)
~CollectionRequester() override
Destroys the collection requester.
QObject * parent() const const
int size() const const
virtual bool event(QEvent *event) override
T take(const Key &key)
void setContentMimeTypes(const QStringList &mimetypes)
Allow to specify collection content mimetype when we create new one.
void setSpacing(int spacing)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue May 26 2020 22:46:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.