Mailcommon

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

KDE's Doxygen guidelines are available online.