Pimcommon

manageserversidesubscriptionjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2014-2022 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "manageserversidesubscriptionjob.h"
8 #include "pimcommonakonadi_debug.h"
9 #include "util/mailutil.h"
10 
11 #include <Akonadi/ServerManager>
12 
13 #include <KLocalizedString>
14 #include <KMessageBox>
15 
16 #include <QDBusConnection>
17 #include <QDBusInterface>
18 #include <QDBusPendingCall>
19 #include <QDBusPendingReply>
20 
21 using namespace PimCommon;
22 
23 class PimCommon::ManageServerSideSubscriptionJobPrivate
24 {
25 public:
26  ManageServerSideSubscriptionJobPrivate() = default;
27 
28  Akonadi::Collection mCurrentCollection;
29  QWidget *mParentWidget = nullptr;
30 };
31 
32 ManageServerSideSubscriptionJob::ManageServerSideSubscriptionJob(QObject *parent)
33  : QObject(parent)
34  , d(new PimCommon::ManageServerSideSubscriptionJobPrivate)
35 {
36 }
37 
38 ManageServerSideSubscriptionJob::~ManageServerSideSubscriptionJob() = default;
39 
40 void ManageServerSideSubscriptionJob::start()
41 {
42  if (!d->mCurrentCollection.isValid()) {
43  qCDebug(PIMCOMMONAKONADI_LOG) << " collection not defined";
44  deleteLater();
45  return;
46  }
47  bool isImapOnline = false;
48  if (PimCommon::MailUtil::isImapFolder(d->mCurrentCollection, isImapOnline)) {
49  const QString service = Akonadi::ServerManager::agentServiceName(Akonadi::ServerManager::Resource, d->mCurrentCollection.resource());
50  QDBusInterface iface(service, QStringLiteral("/"), QStringLiteral("org.kde.Akonadi.ImapResourceBase"), QDBusConnection::sessionBus(), this);
51  if (!iface.isValid()) {
52  qCDebug(PIMCOMMONAKONADI_LOG) << "Cannot create imap dbus interface for service " << service;
53  deleteLater();
54  return;
55  }
56  QDBusPendingCall call = iface.asyncCall(QStringLiteral("configureSubscription"), (qlonglong)d->mParentWidget->winId());
57  auto watcher = new QDBusPendingCallWatcher(call, this);
58  connect(watcher, &QDBusPendingCallWatcher::finished, this, &ManageServerSideSubscriptionJob::slotConfigureSubscriptionFinished);
59  }
60 }
61 
62 void ManageServerSideSubscriptionJob::slotConfigureSubscriptionFinished(QDBusPendingCallWatcher *watcher)
63 {
64  QDBusPendingReply<int> reply = *watcher;
65  if (reply.isValid()) {
66  if (reply == -2) {
68  d->mParentWidget,
69  i18n("IMAP server not configured yet. Please configure the server in the IMAP account before setting up server-side subscription."));
70  } else if (reply == -1) {
71  KMessageBox::error(d->mParentWidget, i18n("Log in failed, please configure the IMAP account before setting up server-side subscription."));
72  }
73  } else {
74  qCDebug(PIMCOMMONAKONADI_LOG) << "ManageServerSideSubscriptionJob return an invalid reply";
75  }
76  watcher->deleteLater();
77  deleteLater();
78 }
79 
80 void ManageServerSideSubscriptionJob::setParentWidget(QWidget *parentWidget)
81 {
82  d->mParentWidget = parentWidget;
83 }
84 
85 void ManageServerSideSubscriptionJob::setCurrentCollection(const Akonadi::Collection &col)
86 {
87  d->mCurrentCollection = col;
88 }
bool isValid() const const
void finished(QDBusPendingCallWatcher *self)
folderdialogacltab.h
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
static QString agentServiceName(ServiceAgentType agentType, const QString &identifier)
void deleteLater()
QString i18n(const char *text, const TYPE &arg...)
QDBusConnection sessionBus()
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Aug 9 2022 03:56:06 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.