Mailcommon

folderrequester.cpp
1 /*
2  * SPDX-FileCopyrightText: 2004 Carsten Burghardt <[email protected]>
3  * SPDX-FileCopyrightText: 2009-2022 Laurent Montel <[email protected]>
4  *
5  * SPDX-License-Identifier: GPL-2.0-only
6  */
7 
8 #include "folderrequester.h"
9 #include "folderselectiondialog.h"
10 #include "kernel/mailkernel.h"
11 #include "util/mailutil.h"
12 #include <Akonadi/CollectionFetchJob>
13 #include <Libkdepim/LineEditCatchReturnKey>
14 
15 #include <KLocalizedString>
16 #include <QIcon>
17 #include <QLineEdit>
18 #include <QPointer>
19 
20 #include <QHBoxLayout>
21 #include <QKeyEvent>
22 #include <QToolButton>
23 
24 using namespace MailCommon;
25 
26 class MailCommon::FolderRequesterPrivate
27 {
28 public:
29  Akonadi::Collection mCollection;
30  QLineEdit *mEdit = nullptr;
31  QString mSelectFolderTitleDialog;
32  bool mMustBeReadWrite = false;
33  bool mShowOutbox = false;
34  bool mNotCreateNewFolder = false;
35 };
36 
37 FolderRequester::FolderRequester(QWidget *parent)
38  : QWidget(parent)
39  , d(new MailCommon::FolderRequesterPrivate)
40 {
41  auto hlay = new QHBoxLayout(this);
42  hlay->setContentsMargins({});
43 
44  d->mEdit = new QLineEdit(this);
45  d->mEdit->setPlaceholderText(i18n("Select Folder"));
46  new KPIM::LineEditCatchReturnKey(d->mEdit, this);
47  d->mEdit->setReadOnly(true);
48  hlay->addWidget(d->mEdit);
49 
50  auto button = new QToolButton(this);
51  button->setIcon(QIcon::fromTheme(QStringLiteral("folder")));
52  hlay->addWidget(button);
53  connect(button, &QToolButton::clicked, this, &FolderRequester::slotOpenDialog);
54 
56  setFocusPolicy(Qt::StrongFocus);
57 }
58 
59 //-----------------------------------------------------------------------------
60 void FolderRequester::slotOpenDialog()
61 {
62  FolderSelectionDialog::SelectionFolderOptions options = FolderSelectionDialog::EnableCheck;
63  options |= FolderSelectionDialog::HideVirtualFolder;
64  options |= FolderSelectionDialog::NotUseGlobalSettings;
65  if (d->mNotCreateNewFolder) {
66  options |= FolderSelectionDialog::NotAllowToCreateNewFolder;
67  }
68  if (!d->mShowOutbox) {
69  options |= FolderSelectionDialog::HideOutboxFolder;
70  }
71 
73  dlg->setWindowTitle(d->mSelectFolderTitleDialog.isEmpty() ? i18nc("@title:window", "Select Folder") : d->mSelectFolderTitleDialog);
74  dlg->setModal(false);
75  dlg->setSelectedCollection(d->mCollection);
76 
77  if (dlg->exec() && dlg) {
78  setCollection(dlg->selectedCollection(), false);
79  }
80  delete dlg;
81 }
82 
83 //-----------------------------------------------------------------------------
84 FolderRequester::~FolderRequester() = default;
85 
86 Akonadi::Collection FolderRequester::collection() const
87 {
88  return d->mCollection;
89 }
90 
91 void FolderRequester::setCollectionFullPath(const Akonadi::Collection &col)
92 {
93  if (KernelIf->collectionModel()) {
94  d->mEdit->setText(Util::fullCollectionPath(col));
95  } else {
96  d->mEdit->clear();
97  }
98 }
99 
100 //-----------------------------------------------------------------------------
101 void FolderRequester::setCollection(const Akonadi::Collection &collection, bool fetchCollection)
102 {
103  d->mCollection = collection;
104  if (d->mCollection.isValid()) {
105  if (fetchCollection) {
106  auto *job = new Akonadi::CollectionFetchJob(d->mCollection, Akonadi::CollectionFetchJob::Base, this);
107 
108  connect(job, &Akonadi::CollectionFetchJob::result, this, &FolderRequester::slotCollectionsReceived);
109  } else {
110  setCollectionFullPath(d->mCollection);
111  }
112  } else if (!d->mMustBeReadWrite) { // the Local Folders root node was selected
113  d->mEdit->setText(i18n("Local Folders"));
114  }
115 
116  Q_EMIT folderChanged(d->mCollection);
117 }
118 
119 void FolderRequester::slotCollectionsReceived(KJob *job)
120 {
121  if (job->error()) {
122  d->mCollection = Akonadi::Collection();
123  d->mEdit->setText(i18n("Please select a folder"));
124  Q_EMIT invalidFolder();
125  return;
126  }
127 
128  const Akonadi::CollectionFetchJob *fetchJob = qobject_cast<Akonadi::CollectionFetchJob *>(job);
129  const Akonadi::Collection::List collections = fetchJob->collections();
130 
131  if (!collections.isEmpty()) {
132  const Akonadi::Collection collection = collections.first();
133  // in case this is still the collection we are interested in, update
134  if (collection.id() == d->mCollection.id()) {
135  d->mCollection = collection;
136  setCollectionFullPath(collection);
137  }
138  } else {
139  // the requested collection doesn't exists anymore
140  d->mCollection = Akonadi::Collection();
141  d->mEdit->setText(i18n("Please select a folder"));
142  Q_EMIT invalidFolder();
143  }
144 }
145 
146 bool FolderRequester::hasCollection() const
147 {
148  return d->mCollection.isValid();
149 }
150 
151 //-----------------------------------------------------------------------------
152 void FolderRequester::keyPressEvent(QKeyEvent *e)
153 {
154  if (e->key() == Qt::Key_Space) {
155  slotOpenDialog();
156  } else {
157  e->ignore();
158  }
159 }
160 
161 void FolderRequester::setMustBeReadWrite(bool readwrite)
162 {
163  d->mMustBeReadWrite = readwrite;
164 }
165 
166 void FolderRequester::setShowOutbox(bool show)
167 {
168  d->mShowOutbox = show;
169 }
170 
171 void FolderRequester::setNotAllowToCreateNewFolder(bool notCreateNewFolder)
172 {
173  d->mNotCreateNewFolder = notCreateNewFolder;
174 }
175 
176 void FolderRequester::setSelectFolderTitleDialog(const QString &title)
177 {
178  d->mSelectFolderTitleDialog = title;
179 }
bool isEmpty() const const
void result(KJob *job)
void clicked(bool checked)
QIcon fromTheme(const QString &name)
T & first()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Collection::List collections() const
QString i18n(const char *text, const TYPE &arg...)
Key_Space
int key() const const
void ignore()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
int error() const
StrongFocus
A dialog that lets the user select a folder.
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Oct 1 2022 04:00:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.