Add a parent to KCompositeJob

So they are stopped if the device is destroyed
This commit is contained in:
Albert Vaca Cintora 2024-06-03 13:52:44 +02:00
parent 2763b2dbe3
commit 17c97a4768
7 changed files with 32 additions and 37 deletions

View file

@ -11,13 +11,14 @@
#include <KLocalizedString> #include <KLocalizedString>
#include <core_debug.h> #include <core_debug.h>
#include <daemon.h> #include <daemon.h>
#include <device.h>
CompositeUploadJob::CompositeUploadJob(const QString &deviceId, bool displayNotification) CompositeUploadJob::CompositeUploadJob(Device *device, bool displayNotification)
: KCompositeJob() : KCompositeJob(device)
, m_server(new Server(this)) , m_server(new Server(this))
, m_socket(nullptr) , m_socket(nullptr)
, m_port(0) , m_port(0)
, m_deviceId(deviceId) , m_device(device)
, m_running(false) , m_running(false)
, m_currentJobNum(1) , m_currentJobNum(1)
, m_totalJobs(0) , m_totalJobs(0)
@ -102,17 +103,11 @@ void CompositeUploadJob::startNextSubJob()
np.set<int>(QStringLiteral("numberOfFiles"), m_totalJobs); np.set<int>(QStringLiteral("numberOfFiles"), m_totalJobs);
np.set<quint64>(QStringLiteral("totalPayloadSize"), m_totalPayloadSize); np.set<quint64>(QStringLiteral("totalPayloadSize"), m_totalPayloadSize);
Device *device = Daemon::instance()->getDevice(m_deviceId); if (m_device->sendPacket(np)) {
if (device == nullptr) {
qCWarning(KDECONNECT_CORE) << "Device disconnected" << this->m_deviceId;
return;
}
if (device->sendPacket(np)) {
m_server->resumeAccepting(); m_server->resumeAccepting();
} else { } else {
setError(SendingNetworkPacketFailed); setError(SendingNetworkPacketFailed);
setErrorText(i18n("Failed to send packet to %1", device->name())); setErrorText(i18n("Failed to send packet to %1", m_device->name()));
emitResult(); emitResult();
} }
@ -159,7 +154,7 @@ void CompositeUploadJob::newConnection()
m_currentJob->start(); m_currentJob->start();
}); });
LanLinkProvider::configureSslSocket(m_socket, m_deviceId, true); LanLinkProvider::configureSslSocket(m_socket, m_device->id(), true);
m_socket->startServerEncryption(); m_socket->startServerEncryption();
} }
@ -207,12 +202,7 @@ void CompositeUploadJob::sendUpdatePacket()
np.set<int>(QStringLiteral("numberOfFiles"), m_totalJobs); np.set<int>(QStringLiteral("numberOfFiles"), m_totalJobs);
np.set<quint64>(QStringLiteral("totalPayloadSize"), m_totalPayloadSize); np.set<quint64>(QStringLiteral("totalPayloadSize"), m_totalPayloadSize);
Device *device = Daemon::instance()->getDevice(m_deviceId); m_device->sendPacket(np);
if (device == nullptr) {
qCWarning(KDECONNECT_CORE) << "Device disconnected" << this->m_deviceId;
return;
}
device->sendPacket(np);
} }
bool CompositeUploadJob::doKill() bool CompositeUploadJob::doKill()
@ -263,12 +253,7 @@ void CompositeUploadJob::slotResult(KJob *job)
void CompositeUploadJob::emitDescription(const QString &currentFileName) void CompositeUploadJob::emitDescription(const QString &currentFileName)
{ {
Device *device = Daemon::instance()->getDevice(this->m_deviceId); Q_EMIT description(this, i18n("Sending to %1", m_device->name()), {i18n("File"), currentFileName}, {});
if (device == nullptr) {
qWarning() << "Device disconnected" << this->m_deviceId;
return;
}
Q_EMIT description(this, i18n("Sending to %1", device->name()), {i18n("File"), currentFileName}, {});
setProcessedAmount(Files, m_currentJobNum); setProcessedAmount(Files, m_currentJobNum);
setTotalAmount(Files, m_totalJobs); setTotalAmount(Files, m_totalJobs);

View file

@ -12,12 +12,14 @@
#include "uploadjob.h" #include "uploadjob.h"
#include <KCompositeJob> #include <KCompositeJob>
class Device;
class KDECONNECTCORE_EXPORT CompositeUploadJob : public KCompositeJob class KDECONNECTCORE_EXPORT CompositeUploadJob : public KCompositeJob
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit CompositeUploadJob(const QString &deviceId, bool displayNotification); explicit CompositeUploadJob(Device *device, bool displayNotification);
void start() override; void start() override;
QVariantMap transferInfo(); QVariantMap transferInfo();
@ -37,7 +39,7 @@ private:
Server *const m_server; Server *const m_server;
QSslSocket *m_socket; QSslSocket *m_socket;
quint16 m_port; quint16 m_port;
QString m_deviceId; Device *m_device;
bool m_running; bool m_running;
int m_currentJobNum; int m_currentJobNum;
int m_totalJobs; int m_totalJobs;

View file

@ -10,6 +10,7 @@
#include "backends/linkprovider.h" #include "backends/linkprovider.h"
#include "core_debug.h" #include "core_debug.h"
#include "daemon.h"
#include "kdeconnectconfig.h" #include "kdeconnectconfig.h"
#include "lanlinkprovider.h" #include "lanlinkprovider.h"
#include "plugins/share/shareplugin.h" #include "plugins/share/shareplugin.h"
@ -56,9 +57,15 @@ QHostAddress LanDeviceLink::hostAddress() const
bool LanDeviceLink::sendPacket(NetworkPacket &np) bool LanDeviceLink::sendPacket(NetworkPacket &np)
{ {
if (np.payload()) { if (np.payload()) {
Device *device = Daemon::instance()->getDevice(deviceId());
if (device == nullptr) {
qCWarning(KDECONNECT_CORE) << "Device disconnected" << deviceId();
return false;
}
// FIXME: Remove packet-type-specific logic from the link
if (np.type() == PACKET_TYPE_SHARE_REQUEST && np.payloadSize() >= 0) { if (np.type() == PACKET_TYPE_SHARE_REQUEST && np.payloadSize() >= 0) {
if (!m_compositeUploadJob || !m_compositeUploadJob->isRunning()) { if (!m_compositeUploadJob || !m_compositeUploadJob->isRunning()) {
m_compositeUploadJob = new CompositeUploadJob(deviceId(), true); m_compositeUploadJob = new CompositeUploadJob(device, true);
} }
m_compositeUploadJob->addSubjob(new UploadJob(np)); m_compositeUploadJob->addSubjob(new UploadJob(np));
@ -67,7 +74,7 @@ bool LanDeviceLink::sendPacket(NetworkPacket &np)
m_compositeUploadJob->start(); m_compositeUploadJob->start();
} }
} else { // Infinite stream } else { // Infinite stream
CompositeUploadJob *fireAndForgetJob = new CompositeUploadJob(deviceId(), false); CompositeUploadJob *fireAndForgetJob = new CompositeUploadJob(device, false);
fireAndForgetJob->addSubjob(new UploadJob(np)); fireAndForgetJob->addSubjob(new UploadJob(np));
fireAndForgetJob->start(); fireAndForgetJob->start();
} }

View file

@ -9,10 +9,11 @@
#include <KLocalizedString> #include <KLocalizedString>
#include <core_debug.h> #include <core_debug.h>
#include <daemon.h> #include <daemon.h>
#include <device.h>
CompositeFileTransferJob::CompositeFileTransferJob(const QString &deviceId) CompositeFileTransferJob::CompositeFileTransferJob(const Device *device, QObject *parent)
: KCompositeJob() : KCompositeJob(parent)
, m_deviceId(deviceId) , m_device(device)
, m_running(false) , m_running(false)
, m_currentJobNum(1) , m_currentJobNum(1)
, m_totalJobs(0) , m_totalJobs(0)
@ -44,7 +45,7 @@ void CompositeFileTransferJob::startNextSubJob()
m_currentJob->start(); m_currentJob->start();
Q_EMIT description(this, Q_EMIT description(this,
i18ncp("@title job", "Receiving file", "Receiving files", m_totalJobs), i18ncp("@title job", "Receiving file", "Receiving files", m_totalJobs),
{i18nc("The source of a file operation", "Source"), Daemon::instance()->getDevice(this->m_deviceId)->name()}, {i18nc("The source of a file operation", "Source"), this->m_device->name()},
{i18nc("The destination of a file operation", "Destination"), m_currentJob->destination().toDisplayString(QUrl::PreferLocalFile)}); {i18nc("The destination of a file operation", "Destination"), m_currentJob->destination().toDisplayString(QUrl::PreferLocalFile)});
connect(m_currentJob, &FileTransferJob::processedAmountChanged, this, &CompositeFileTransferJob::slotProcessedAmount); connect(m_currentJob, &FileTransferJob::processedAmountChanged, this, &CompositeFileTransferJob::slotProcessedAmount);

View file

@ -11,6 +11,7 @@
#include <KCompositeJob> #include <KCompositeJob>
#include <QElapsedTimer> #include <QElapsedTimer>
class Device;
class FileTransferJob; class FileTransferJob;
class KDECONNECTCORE_EXPORT CompositeFileTransferJob : public KCompositeJob class KDECONNECTCORE_EXPORT CompositeFileTransferJob : public KCompositeJob
@ -18,7 +19,7 @@ class KDECONNECTCORE_EXPORT CompositeFileTransferJob : public KCompositeJob
Q_OBJECT Q_OBJECT
public: public:
explicit CompositeFileTransferJob(const QString &deviceId); explicit CompositeFileTransferJob(const Device *device, QObject *parent);
void start() override; void start() override;
bool isRunning() const; bool isRunning() const;
@ -33,7 +34,7 @@ private Q_SLOTS:
void startNextSubJob(); void startNextSubJob();
private: private:
QString m_deviceId; const Device *m_device;
bool m_running; bool m_running;
int m_currentJobNum; int m_currentJobNum;
int m_totalJobs; int m_totalJobs;

View file

@ -119,7 +119,7 @@ void SharePlugin::receivePacket(const NetworkPacket &np)
const bool open = np.get<bool>(QStringLiteral("open"), false); const bool open = np.get<bool>(QStringLiteral("open"), false);
if (!m_compositeJob) { if (!m_compositeJob) {
m_compositeJob = new CompositeFileTransferJob(device()->id()); m_compositeJob = new CompositeFileTransferJob(device(), this);
m_compositeJob->setProperty("destUrl", destinationDir().toString()); m_compositeJob->setProperty("destUrl", destinationDir().toString());
m_compositeJob->setProperty("immediateProgressReporting", true); m_compositeJob->setProperty("immediateProgressReporting", true);
Daemon::instance()->jobTracker()->registerJob(m_compositeJob); Daemon::instance()->jobTracker()->registerJob(m_compositeJob);

View file

@ -87,7 +87,6 @@ private Q_SLOTS:
DeviceInfo deviceInfo = KdeConnectConfig::instance().deviceInfo(); DeviceInfo deviceInfo = KdeConnectConfig::instance().deviceInfo();
KdeConnectConfig::instance().addTrustedDevice(deviceInfo); KdeConnectConfig::instance().addTrustedDevice(deviceInfo);
// We need the device to be loaded on the daemon, otherwise CompositeUploadJob will get a null device
Device *device = new Device(this, deviceInfo.id); Device *device = new Device(this, deviceInfo.id);
m_daemon->addDevice(device); m_daemon->addDevice(device);
@ -95,7 +94,7 @@ private Q_SLOTS:
NetworkPacket np(PACKET_TYPE_SHARE_REQUEST); NetworkPacket np(PACKET_TYPE_SHARE_REQUEST);
np.setPayload(f, f->size()); np.setPayload(f, f->size());
CompositeUploadJob *job = new CompositeUploadJob(deviceInfo.id, false); CompositeUploadJob *job = new CompositeUploadJob(device, false);
UploadJob *uj = new UploadJob(np); UploadJob *uj = new UploadJob(np);
job->addSubjob(uj); job->addSubjob(uj);