Warning, file /pim/mailcommon/src/folder/folderrequester.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * SPDX-FileCopyrightText: 2004 Carsten Burghardt <burghardt@kde.org>
0003  * SPDX-FileCopyrightText: 2009-2024 Laurent Montel <montel@kde.org>
0004  *
0005  *  SPDX-License-Identifier: GPL-2.0-only
0006  */
0007 
0008 #include "folderrequester.h"
0009 #include "folderselectiondialog.h"
0010 #include "kernel/mailkernel.h"
0011 #include "util/mailutil.h"
0012 #include <Akonadi/CollectionFetchJob>
0013 #include <KLineEditEventHandler>
0014 
0015 #include <KLocalizedString>
0016 #include <QIcon>
0017 #include <QLineEdit>
0018 #include <QPointer>
0019 
0020 #include <QHBoxLayout>
0021 #include <QKeyEvent>
0022 #include <QToolButton>
0023 
0024 using namespace MailCommon;
0025 
0026 class MailCommon::FolderRequesterPrivate
0027 {
0028 public:
0029     Akonadi::Collection mCollection;
0030     QLineEdit *mEdit = nullptr;
0031     QString mSelectFolderTitleDialog;
0032     bool mMustBeReadWrite = false;
0033     bool mShowOutbox = false;
0034     bool mNotCreateNewFolder = false;
0035 };
0036 
0037 FolderRequester::FolderRequester(QWidget *parent)
0038     : QWidget(parent)
0039     , d(new MailCommon::FolderRequesterPrivate)
0040 {
0041     auto hlay = new QHBoxLayout(this);
0042     hlay->setContentsMargins({});
0043 
0044     d->mEdit = new QLineEdit(this);
0045     d->mEdit->setPlaceholderText(i18n("Select Folder"));
0046     KLineEditEventHandler::catchReturnKey(d->mEdit);
0047     d->mEdit->setReadOnly(true);
0048     hlay->addWidget(d->mEdit);
0049 
0050     auto button = new QToolButton(this);
0051     button->setIcon(QIcon::fromTheme(QStringLiteral("folder")));
0052     hlay->addWidget(button);
0053     connect(button, &QToolButton::clicked, this, &FolderRequester::slotOpenDialog);
0054 
0055     setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed));
0056     setFocusPolicy(Qt::StrongFocus);
0057 }
0058 
0059 //-----------------------------------------------------------------------------
0060 void FolderRequester::slotOpenDialog()
0061 {
0062     FolderSelectionDialog::SelectionFolderOptions options = FolderSelectionDialog::EnableCheck;
0063     options |= FolderSelectionDialog::HideVirtualFolder;
0064     options |= FolderSelectionDialog::NotUseGlobalSettings;
0065     if (d->mNotCreateNewFolder) {
0066         options |= FolderSelectionDialog::NotAllowToCreateNewFolder;
0067     }
0068     if (!d->mShowOutbox) {
0069         options |= FolderSelectionDialog::HideOutboxFolder;
0070     }
0071 
0072     QPointer<FolderSelectionDialog> dlg(new FolderSelectionDialog(this, options));
0073     dlg->setWindowTitle(d->mSelectFolderTitleDialog.isEmpty() ? i18nc("@title:window", "Select Folder") : d->mSelectFolderTitleDialog);
0074     dlg->setModal(false);
0075     dlg->setSelectedCollection(d->mCollection);
0076 
0077     if (dlg->exec() && dlg) {
0078         setCollection(dlg->selectedCollection(), false);
0079     }
0080     delete dlg;
0081 }
0082 
0083 //-----------------------------------------------------------------------------
0084 FolderRequester::~FolderRequester() = default;
0085 
0086 Akonadi::Collection FolderRequester::collection() const
0087 {
0088     return d->mCollection;
0089 }
0090 
0091 void FolderRequester::setCollectionFullPath(const Akonadi::Collection &col)
0092 {
0093     if (KernelIf->collectionModel()) {
0094         d->mEdit->setText(Util::fullCollectionPath(col));
0095     } else {
0096         d->mEdit->clear();
0097     }
0098 }
0099 
0100 //-----------------------------------------------------------------------------
0101 void FolderRequester::setCollection(const Akonadi::Collection &collection, bool fetchCollection)
0102 {
0103     d->mCollection = collection;
0104     if (d->mCollection.isValid()) {
0105         if (fetchCollection) {
0106             auto job = new Akonadi::CollectionFetchJob(d->mCollection, Akonadi::CollectionFetchJob::Base, this);
0107 
0108             connect(job, &Akonadi::CollectionFetchJob::result, this, &FolderRequester::slotCollectionsReceived);
0109         } else {
0110             setCollectionFullPath(d->mCollection);
0111         }
0112     } else if (!d->mMustBeReadWrite) { // the Local Folders root node was selected
0113         d->mEdit->setText(i18n("Local Folders"));
0114     }
0115 
0116     Q_EMIT folderChanged(d->mCollection);
0117 }
0118 
0119 void FolderRequester::slotCollectionsReceived(KJob *job)
0120 {
0121     if (job->error()) {
0122         d->mCollection = Akonadi::Collection();
0123         d->mEdit->setText(i18n("Please select a folder"));
0124         Q_EMIT invalidFolder();
0125         return;
0126     }
0127 
0128     const Akonadi::CollectionFetchJob *fetchJob = qobject_cast<Akonadi::CollectionFetchJob *>(job);
0129     const Akonadi::Collection::List collections = fetchJob->collections();
0130 
0131     if (!collections.isEmpty()) {
0132         const Akonadi::Collection collection = collections.first();
0133         // in case this is still the collection we are interested in, update
0134         if (collection.id() == d->mCollection.id()) {
0135             d->mCollection = collection;
0136             setCollectionFullPath(collection);
0137         }
0138     } else {
0139         // the requested collection doesn't exists anymore
0140         d->mCollection = Akonadi::Collection();
0141         d->mEdit->setText(i18n("Please select a folder"));
0142         Q_EMIT invalidFolder();
0143     }
0144 }
0145 
0146 bool FolderRequester::hasCollection() const
0147 {
0148     return d->mCollection.isValid();
0149 }
0150 
0151 //-----------------------------------------------------------------------------
0152 void FolderRequester::keyPressEvent(QKeyEvent *e)
0153 {
0154     if (e->key() == Qt::Key_Space) {
0155         slotOpenDialog();
0156     } else {
0157         e->ignore();
0158     }
0159 }
0160 
0161 void FolderRequester::setMustBeReadWrite(bool readwrite)
0162 {
0163     d->mMustBeReadWrite = readwrite;
0164 }
0165 
0166 void FolderRequester::setShowOutbox(bool show)
0167 {
0168     d->mShowOutbox = show;
0169 }
0170 
0171 void FolderRequester::setNotAllowToCreateNewFolder(bool notCreateNewFolder)
0172 {
0173     d->mNotCreateNewFolder = notCreateNewFolder;
0174 }
0175 
0176 void FolderRequester::setSelectFolderTitleDialog(const QString &title)
0177 {
0178     d->mSelectFolderTitleDialog = title;
0179 }
0180 
0181 #include "moc_folderrequester.cpp"