File indexing completed on 2024-06-23 05:18:37

0001 /*
0002    SPDX-FileCopyrightText: 2020 Daniel Vrátil <dvratil@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "sendlaterjob.h"
0008 #include "messagecomposer_debug.h"
0009 #include "sendlaterinterface.h"
0010 #include "sendlaterutil_p.h"
0011 
0012 #include <KLocalizedString>
0013 
0014 #include <QDBusPendingCallWatcher>
0015 
0016 #include <memory>
0017 
0018 using namespace MessageComposer;
0019 
0020 SendLaterJob::SendLaterJob(QObject *parent)
0021     : KJob(parent)
0022 {
0023 }
0024 
0025 void SendLaterJob::start()
0026 {
0027     std::unique_ptr<org::freedesktop::Akonadi::SendLaterAgent> iface{
0028         new org::freedesktop::Akonadi::SendLaterAgent{SendLaterUtil::agentServiceName(), SendLaterUtil::dbusPath(), QDBusConnection::sessionBus()}};
0029     if (!iface->isValid()) {
0030         qCWarning(MESSAGECOMPOSER_LOG) << "The SendLater agent is not running!";
0031         setError(SendLaterJob::AgentNotAvailable);
0032         setErrorText(getErrorString(SendLaterJob::AgentNotAvailable, i18n("The Send Later agent is not running.")));
0033         emitResult();
0034         return;
0035     }
0036 
0037     auto reply = doCall(iface.get());
0038 
0039     auto watcher = new QDBusPendingCallWatcher(reply);
0040     connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, iface_ = std::move(iface)](QDBusPendingCallWatcher *call) mutable {
0041         auto iface = std::move(iface_);
0042         call->deleteLater();
0043         QDBusPendingReply<void> reply = *call;
0044         if (reply.isError()) {
0045             qCWarning(MESSAGECOMPOSER_LOG) << "SendLater agent DBus call failed:" << reply.error().message();
0046             setError(SendLaterJob::CallFailed);
0047             setErrorText(getErrorString(SendLaterJob::CallFailed, reply.error().message()));
0048         }
0049 
0050         emitResult();
0051     });
0052 }
0053 
0054 #include "moc_sendlaterjob.cpp"