File indexing completed on 2024-05-12 05:20:45

0001 /*
0002    SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "manageshowcollectionproperties.h"
0008 #include "kmail_debug.h"
0009 #include "kmmainwidget.h"
0010 #include <Akonadi/AgentInstance>
0011 #include <Akonadi/AgentManager>
0012 #include <Akonadi/CollectionAttributesSynchronizationJob>
0013 #include <Akonadi/CollectionFetchJob>
0014 #include <Akonadi/CollectionFetchScope>
0015 #include <Akonadi/CollectionPropertiesDialog>
0016 #include <KLocalizedString>
0017 #include <KMessageBox>
0018 
0019 Q_DECLARE_METATYPE(KPIM::ProgressItem *)
0020 Q_DECLARE_METATYPE(Akonadi::Job *)
0021 Q_DECLARE_METATYPE(QPointer<KPIM::ProgressItem>)
0022 
0023 ManageShowCollectionProperties::ManageShowCollectionProperties(KMMainWidget *mainWidget, QObject *parent)
0024     : QObject(parent)
0025     , mMainWidget(mainWidget)
0026     , mPages({QStringLiteral("MailCommon::CollectionGeneralPage"),
0027               QStringLiteral("KMail::CollectionViewPage"),
0028               QStringLiteral("Akonadi::CachePolicyPage"),
0029               QStringLiteral("KMail::CollectionTemplatesPage"),
0030               QStringLiteral("MailCommon::CollectionExpiryPage"),
0031               QStringLiteral("PimCommon::CollectionAclPage"),
0032               QStringLiteral("KMail::CollectionMailingListPage"),
0033               QStringLiteral("KMail::CollectionQuotaPage"),
0034               QStringLiteral("KMail::CollectionShortcutPage"),
0035               QStringLiteral("Akonadi::CollectionMaintenancePage")})
0036 {
0037 }
0038 
0039 ManageShowCollectionProperties::~ManageShowCollectionProperties() = default;
0040 
0041 void ManageShowCollectionProperties::slotCollectionProperties()
0042 {
0043     showCollectionProperties(QString());
0044 }
0045 
0046 void ManageShowCollectionProperties::slotShowExpiryProperties()
0047 {
0048     showCollectionProperties(QStringLiteral("MailCommon::CollectionExpiryPage"));
0049 }
0050 
0051 void ManageShowCollectionProperties::slotFolderMailingListProperties()
0052 {
0053     showCollectionProperties(QStringLiteral("KMail::CollectionMailingListPage"));
0054 }
0055 
0056 void ManageShowCollectionProperties::slotShowFolderShortcutDialog()
0057 {
0058     showCollectionProperties(QStringLiteral("KMail::CollectionShortcutPage"));
0059 }
0060 
0061 void ManageShowCollectionProperties::showCollectionProperties(const QString &pageToShow)
0062 {
0063     if (!mMainWidget->currentCollection().isValid()) {
0064         return;
0065     }
0066     const Akonadi::Collection col = mMainWidget->currentCollection();
0067     const Akonadi::Collection::Id id = col.id();
0068     QPointer<Akonadi::CollectionPropertiesDialog> dlg = mHashDialogBox.value(id);
0069     if (dlg) {
0070         if (!pageToShow.isEmpty()) {
0071             dlg->setCurrentPage(pageToShow);
0072         }
0073         dlg->activateWindow();
0074         dlg->raise();
0075         return;
0076     }
0077     if (!KMKernel::self()->isOffline()) {
0078         const Akonadi::AgentInstance agentInstance = Akonadi::AgentManager::self()->instance(col.resource());
0079         bool isOnline = agentInstance.isOnline();
0080         if (!isOnline) {
0081             showCollectionPropertiesContinued(pageToShow, QPointer<KPIM::ProgressItem>());
0082         } else {
0083             QPointer<KPIM::ProgressItem> progressItem(KPIM::ProgressManager::createProgressItem(i18n("Retrieving folder properties")));
0084             progressItem->setUsesBusyIndicator(true);
0085             progressItem->setCryptoStatus(KPIM::ProgressItem::Unknown);
0086 
0087             auto sync = new Akonadi::CollectionAttributesSynchronizationJob(col);
0088             sync->setProperty("collectionId", id);
0089             sync->setProperty("pageToShow", pageToShow); // note for dialog later
0090             sync->setProperty("progressItem", QVariant::fromValue(progressItem));
0091             connect(sync, &KJob::result, this, &ManageShowCollectionProperties::slotCollectionPropertiesContinued);
0092             // clang-format off
0093             connect(progressItem, SIGNAL(progressItemCanceled(KPIM::ProgressItem*)), sync, SLOT(kill()));
0094             // clang-format on
0095             connect(progressItem.data(),
0096                     &KPIM::ProgressItem::progressItemCanceled,
0097                     KPIM::ProgressManager::instance(),
0098                     &KPIM::ProgressManager::slotStandardCancelHandler);
0099             sync->start();
0100         }
0101     } else {
0102         KMessageBox::information(mMainWidget, i18n("Network is unconnected. Folder information cannot be updated."));
0103         showCollectionPropertiesContinued(pageToShow, QPointer<KPIM::ProgressItem>());
0104     }
0105 }
0106 
0107 void ManageShowCollectionProperties::slotCollectionPropertiesContinued(KJob *job)
0108 {
0109     QString pageToShow;
0110     QPointer<KPIM::ProgressItem> progressItem;
0111 
0112     if (job) {
0113         auto sync = qobject_cast<Akonadi::CollectionAttributesSynchronizationJob *>(job);
0114         Q_ASSERT(sync);
0115         if (sync->property("collectionId") != mMainWidget->currentCollection().id()) {
0116             return;
0117         }
0118         pageToShow = sync->property("pageToShow").toString();
0119         progressItem = sync->property("progressItem").value<QPointer<KPIM::ProgressItem>>();
0120         if (progressItem) {
0121             // clang-format off
0122             disconnect(progressItem, SIGNAL(progressItemCanceled(KPIM::ProgressItem*)), sync, SLOT(kill()));
0123             // clang-format on
0124         } else {
0125             // progressItem does not exist anymore, operation has been canceled
0126             return;
0127         }
0128     }
0129 
0130     showCollectionPropertiesContinued(pageToShow, progressItem);
0131 }
0132 
0133 void ManageShowCollectionProperties::showCollectionPropertiesContinued(const QString &pageToShow, QPointer<KPIM::ProgressItem> progressItem)
0134 {
0135     if (!progressItem) {
0136         progressItem = KPIM::ProgressManager::createProgressItem(i18n("Retrieving folder properties"));
0137         progressItem->setUsesBusyIndicator(true);
0138         progressItem->setCryptoStatus(KPIM::ProgressItem::Unknown);
0139         connect(progressItem.data(),
0140                 &KPIM::ProgressItem::progressItemCanceled,
0141                 KPIM::ProgressManager::instance(),
0142                 &KPIM::ProgressManager::slotStandardCancelHandler);
0143     }
0144 
0145     auto fetch = new Akonadi::CollectionFetchJob(mMainWidget->currentCollection(), Akonadi::CollectionFetchJob::Base);
0146     // clang-format off
0147     connect(progressItem, SIGNAL(progressItemCanceled(KPIM::ProgressItem*)), fetch, SLOT(kill()));
0148     // clang-format on
0149     fetch->fetchScope().setIncludeStatistics(true);
0150     fetch->setProperty("pageToShow", pageToShow);
0151     fetch->setProperty("progressItem", QVariant::fromValue(progressItem));
0152     connect(fetch, &KJob::result, this, &ManageShowCollectionProperties::slotCollectionPropertiesFinished);
0153     // clang-format off
0154     connect(progressItem, SIGNAL(progressItemCanceled(KPIM::ProgressItem*)), fetch, SLOT(kill()));
0155     // clang-format on
0156 }
0157 
0158 void ManageShowCollectionProperties::slotCollectionPropertiesFinished(KJob *job)
0159 {
0160     if (!job) {
0161         return;
0162     }
0163 
0164     auto progressItem = job->property("progressItem").value<QPointer<KPIM::ProgressItem>>();
0165     // progressItem does not exist anymore, operation has been canceled
0166     if (!progressItem) {
0167         return;
0168     }
0169 
0170     progressItem->setComplete();
0171     progressItem->setStatus(i18n("Done"));
0172 
0173     auto fetch = qobject_cast<Akonadi::CollectionFetchJob *>(job);
0174     Q_ASSERT(fetch);
0175     if (fetch->collections().isEmpty()) {
0176         qCWarning(KMAIL_LOG) << "no collection";
0177         return;
0178     }
0179 
0180     const Akonadi::Collection collection = fetch->collections().constFirst();
0181 
0182     QPointer<Akonadi::CollectionPropertiesDialog> dlg = new Akonadi::CollectionPropertiesDialog(collection, mPages, mMainWidget);
0183     dlg->setWindowTitle(i18nc("@title:window", "Properties of Folder %1", collection.name()));
0184     connect(dlg.data(), &Akonadi::CollectionPropertiesDialog::settingsSaved, mMainWidget, &KMMainWidget::slotUpdateConfig);
0185 
0186     const QString pageToShow = fetch->property("pageToShow").toString();
0187     if (!pageToShow.isEmpty()) { // show a specific page
0188         dlg->setCurrentPage(pageToShow);
0189     }
0190     dlg->show();
0191     mHashDialogBox.insert(collection.id(), dlg);
0192 }
0193 
0194 #include "moc_manageshowcollectionproperties.cpp"