File indexing completed on 2024-11-10 04:50:05

0001 /*
0002  * SPDX-FileCopyrightText: 1996-1998 Stefan Taferner <taferner@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  *
0006  */
0007 
0008 #include "filteractionwithfolder.h"
0009 
0010 #include "filter/dialog/filteractionmissingfolderdialog.h"
0011 #include "filter/filterimporterpathcache.h"
0012 #include "folder/folderrequester.h"
0013 #include "kernel/mailkernel.h"
0014 #include "util/mailutil.h"
0015 
0016 #include <QPointer>
0017 
0018 using namespace MailCommon;
0019 
0020 FilterActionWithFolder::FilterActionWithFolder(const QString &name, const QString &label, QObject *parent)
0021     : FilterAction(name, label, parent)
0022 {
0023 }
0024 
0025 bool FilterActionWithFolder::isEmpty() const
0026 {
0027     return !mFolder.isValid();
0028 }
0029 
0030 QWidget *FilterActionWithFolder::createParamWidget(QWidget *parent) const
0031 {
0032     auto requester = new FolderRequester(parent);
0033     requester->setShowOutbox(false);
0034     setParamWidgetValue(requester);
0035     requester->setObjectName(QLatin1StringView("folderrequester"));
0036 
0037     connect(requester, &FolderRequester::folderChanged, this, &FilterActionWithFolder::filterActionModified);
0038 
0039     return requester;
0040 }
0041 
0042 void FilterActionWithFolder::applyParamWidgetValue(QWidget *paramWidget)
0043 {
0044     mFolder = static_cast<FolderRequester *>(paramWidget)->collection();
0045 }
0046 
0047 void FilterActionWithFolder::setParamWidgetValue(QWidget *paramWidget) const
0048 {
0049     static_cast<FolderRequester *>(paramWidget)->setCollection(mFolder);
0050 }
0051 
0052 void FilterActionWithFolder::clearParamWidget(QWidget *paramWidget) const
0053 {
0054     static_cast<FolderRequester *>(paramWidget)->setCollection(CommonKernel->draftsCollectionFolder());
0055 }
0056 
0057 bool FilterActionWithFolder::argsFromStringInteractive(const QString &argsStr, const QString &name)
0058 {
0059     bool needUpdate = false;
0060     argsFromString(argsStr);
0061     if (!mFolder.isValid()) {
0062         bool exactPath = false;
0063         const Akonadi::Collection::List lst = FilterActionMissingFolderDialog::potentialCorrectFolders(argsStr, exactPath);
0064         if (lst.count() == 1 && exactPath) {
0065             mFolder = lst.at(0);
0066         } else {
0067             const Akonadi::Collection newCol = MailCommon::FilterImporterPathCache::self()->convertedFilterPath(argsStr);
0068             if (!newCol.isValid()) {
0069                 QPointer<FilterActionMissingFolderDialog> dlg = new FilterActionMissingFolderDialog(lst, name, argsStr);
0070                 if (dlg->exec()) {
0071                     mFolder = dlg->selectedCollection();
0072                     needUpdate = true;
0073                     MailCommon::FilterImporterPathCache::self()->insert(argsStr, mFolder);
0074                 }
0075                 delete dlg;
0076             } else {
0077                 mFolder = newCol;
0078                 needUpdate = true;
0079             }
0080         }
0081     }
0082     return needUpdate;
0083 }
0084 
0085 QString FilterActionWithFolder::argsAsStringReal() const
0086 {
0087     if (KernelIf->collectionModel()) {
0088         return MailCommon::Util::fullCollectionPath(mFolder);
0089     }
0090     return FilterActionWithFolder::argsAsString();
0091 }
0092 
0093 void FilterActionWithFolder::argsFromString(const QString &argsStr)
0094 {
0095     bool ok = false;
0096     const Akonadi::Collection::Id id = argsStr.toLongLong(&ok);
0097     if (ok) {
0098         mFolder = Akonadi::Collection(id);
0099     } else {
0100         mFolder = Akonadi::Collection();
0101     }
0102 }
0103 
0104 QString FilterActionWithFolder::argsAsString() const
0105 {
0106     QString result;
0107     if (mFolder.isValid()) {
0108         result = QString::number(mFolder.id());
0109     }
0110 
0111     return result;
0112 }
0113 
0114 QString FilterActionWithFolder::displayString() const
0115 {
0116     QString result;
0117     if (mFolder.isValid()) {
0118         result = MailCommon::Util::fullCollectionPath(CommonKernel->collectionFromId(mFolder.id()));
0119     }
0120 
0121     return label() + QLatin1StringView(" \"") + result.toHtmlEscaped() + QLatin1StringView("\"");
0122 }
0123 
0124 bool FilterActionWithFolder::folderRemoved(const Akonadi::Collection &oldFolder, const Akonadi::Collection &newFolder)
0125 {
0126     if (oldFolder == mFolder) {
0127         mFolder = newFolder;
0128         return true;
0129     } else {
0130         return false;
0131     }
0132 }
0133 
0134 #include "moc_filteractionwithfolder.cpp"