File indexing completed on 2025-01-05 04:58:22

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 "manageserversidesubscriptionjob.h"
0008 #include "pimcommonakonadi_debug.h"
0009 #include "util/mailutil.h"
0010 
0011 #include <Akonadi/ServerManager>
0012 
0013 #include <KLocalizedString>
0014 #include <KMessageBox>
0015 
0016 #include <QDBusConnection>
0017 #include <QDBusInterface>
0018 #include <QDBusPendingCall>
0019 #include <QDBusPendingReply>
0020 
0021 using namespace PimCommon;
0022 
0023 class PimCommon::ManageServerSideSubscriptionJobPrivate
0024 {
0025 public:
0026     ManageServerSideSubscriptionJobPrivate() = default;
0027 
0028     Akonadi::Collection mCurrentCollection;
0029     QWidget *mParentWidget = nullptr;
0030 };
0031 
0032 ManageServerSideSubscriptionJob::ManageServerSideSubscriptionJob(QObject *parent)
0033     : QObject(parent)
0034     , d(new PimCommon::ManageServerSideSubscriptionJobPrivate)
0035 {
0036 }
0037 
0038 ManageServerSideSubscriptionJob::~ManageServerSideSubscriptionJob() = default;
0039 
0040 void ManageServerSideSubscriptionJob::start()
0041 {
0042     if (!d->mCurrentCollection.isValid()) {
0043         qCDebug(PIMCOMMONAKONADI_LOG) << " collection not defined";
0044         deleteLater();
0045         return;
0046     }
0047     bool isImapOnline = false;
0048     if (PimCommon::MailUtil::isImapFolder(d->mCurrentCollection, isImapOnline)) {
0049         const QString service = Akonadi::ServerManager::agentServiceName(Akonadi::ServerManager::Resource, d->mCurrentCollection.resource());
0050         QDBusInterface iface(service, QStringLiteral("/"), QStringLiteral("org.kde.Akonadi.ImapResourceBase"), QDBusConnection::sessionBus(), this);
0051         if (!iface.isValid()) {
0052             qCDebug(PIMCOMMONAKONADI_LOG) << "Cannot create imap dbus interface for service " << service;
0053             deleteLater();
0054             return;
0055         }
0056         QDBusPendingCall call = iface.asyncCall(QStringLiteral("configureSubscription"), (qlonglong)d->mParentWidget->winId());
0057         auto watcher = new QDBusPendingCallWatcher(call, this);
0058         connect(watcher, &QDBusPendingCallWatcher::finished, this, &ManageServerSideSubscriptionJob::slotConfigureSubscriptionFinished);
0059     }
0060 }
0061 
0062 void ManageServerSideSubscriptionJob::slotConfigureSubscriptionFinished(QDBusPendingCallWatcher *watcher)
0063 {
0064     QDBusPendingReply<int> reply = *watcher;
0065     if (reply.isValid()) {
0066         if (reply == -2) {
0067             KMessageBox::error(
0068                 d->mParentWidget,
0069                 i18n("IMAP server not configured yet. Please configure the server in the IMAP account before setting up server-side subscription."));
0070         } else if (reply == -1) {
0071             KMessageBox::error(d->mParentWidget, i18n("Log in failed, please configure the IMAP account before setting up server-side subscription."));
0072         }
0073     } else {
0074         qCDebug(PIMCOMMONAKONADI_LOG) << "ManageServerSideSubscriptionJob return an invalid reply";
0075     }
0076     watcher->deleteLater();
0077     deleteLater();
0078 }
0079 
0080 void ManageServerSideSubscriptionJob::setParentWidget(QWidget *parentWidget)
0081 {
0082     d->mParentWidget = parentWidget;
0083 }
0084 
0085 void ManageServerSideSubscriptionJob::setCurrentCollection(const Akonadi::Collection &col)
0086 {
0087     d->mCurrentCollection = col;
0088 }
0089 
0090 #include "moc_manageserversidesubscriptionjob.cpp"