diff --git a/core/backends/lan/compositeuploadjob.cpp b/core/backends/lan/compositeuploadjob.cpp index 1c66a32a3..8100fe443 100644 --- a/core/backends/lan/compositeuploadjob.cpp +++ b/core/backends/lan/compositeuploadjob.cpp @@ -11,13 +11,14 @@ #include #include #include +#include -CompositeUploadJob::CompositeUploadJob(const QString &deviceId, bool displayNotification) - : KCompositeJob() +CompositeUploadJob::CompositeUploadJob(Device *device, bool displayNotification) + : KCompositeJob(device) , m_server(new Server(this)) , m_socket(nullptr) , m_port(0) - , m_deviceId(deviceId) + , m_device(device) , m_running(false) , m_currentJobNum(1) , m_totalJobs(0) @@ -102,17 +103,11 @@ void CompositeUploadJob::startNextSubJob() np.set(QStringLiteral("numberOfFiles"), m_totalJobs); np.set(QStringLiteral("totalPayloadSize"), m_totalPayloadSize); - Device *device = Daemon::instance()->getDevice(m_deviceId); - if (device == nullptr) { - qCWarning(KDECONNECT_CORE) << "Device disconnected" << this->m_deviceId; - return; - } - - if (device->sendPacket(np)) { + if (m_device->sendPacket(np)) { m_server->resumeAccepting(); } else { setError(SendingNetworkPacketFailed); - setErrorText(i18n("Failed to send packet to %1", device->name())); + setErrorText(i18n("Failed to send packet to %1", m_device->name())); emitResult(); } @@ -159,7 +154,7 @@ void CompositeUploadJob::newConnection() m_currentJob->start(); }); - LanLinkProvider::configureSslSocket(m_socket, m_deviceId, true); + LanLinkProvider::configureSslSocket(m_socket, m_device->id(), true); m_socket->startServerEncryption(); } @@ -207,12 +202,7 @@ void CompositeUploadJob::sendUpdatePacket() np.set(QStringLiteral("numberOfFiles"), m_totalJobs); np.set(QStringLiteral("totalPayloadSize"), m_totalPayloadSize); - Device *device = Daemon::instance()->getDevice(m_deviceId); - if (device == nullptr) { - qCWarning(KDECONNECT_CORE) << "Device disconnected" << this->m_deviceId; - return; - } - device->sendPacket(np); + m_device->sendPacket(np); } bool CompositeUploadJob::doKill() @@ -263,12 +253,7 @@ void CompositeUploadJob::slotResult(KJob *job) void CompositeUploadJob::emitDescription(const QString ¤tFileName) { - Device *device = Daemon::instance()->getDevice(this->m_deviceId); - if (device == nullptr) { - qWarning() << "Device disconnected" << this->m_deviceId; - return; - } - Q_EMIT description(this, i18n("Sending to %1", device->name()), {i18n("File"), currentFileName}, {}); + Q_EMIT description(this, i18n("Sending to %1", m_device->name()), {i18n("File"), currentFileName}, {}); setProcessedAmount(Files, m_currentJobNum); setTotalAmount(Files, m_totalJobs); diff --git a/core/backends/lan/compositeuploadjob.h b/core/backends/lan/compositeuploadjob.h index bdf9473ee..0dad09b0d 100644 --- a/core/backends/lan/compositeuploadjob.h +++ b/core/backends/lan/compositeuploadjob.h @@ -12,12 +12,14 @@ #include "uploadjob.h" #include +class Device; + class KDECONNECTCORE_EXPORT CompositeUploadJob : public KCompositeJob { Q_OBJECT public: - explicit CompositeUploadJob(const QString &deviceId, bool displayNotification); + explicit CompositeUploadJob(Device *device, bool displayNotification); void start() override; QVariantMap transferInfo(); @@ -37,7 +39,7 @@ private: Server *const m_server; QSslSocket *m_socket; quint16 m_port; - QString m_deviceId; + Device *m_device; bool m_running; int m_currentJobNum; int m_totalJobs; diff --git a/core/backends/lan/landevicelink.cpp b/core/backends/lan/landevicelink.cpp index 9da1901b1..55c5c8050 100644 --- a/core/backends/lan/landevicelink.cpp +++ b/core/backends/lan/landevicelink.cpp @@ -10,6 +10,7 @@ #include "backends/linkprovider.h" #include "core_debug.h" +#include "daemon.h" #include "kdeconnectconfig.h" #include "lanlinkprovider.h" #include "plugins/share/shareplugin.h" @@ -56,9 +57,15 @@ QHostAddress LanDeviceLink::hostAddress() const bool LanDeviceLink::sendPacket(NetworkPacket &np) { 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 (!m_compositeUploadJob || !m_compositeUploadJob->isRunning()) { - m_compositeUploadJob = new CompositeUploadJob(deviceId(), true); + m_compositeUploadJob = new CompositeUploadJob(device, true); } m_compositeUploadJob->addSubjob(new UploadJob(np)); @@ -67,7 +74,7 @@ bool LanDeviceLink::sendPacket(NetworkPacket &np) m_compositeUploadJob->start(); } } else { // Infinite stream - CompositeUploadJob *fireAndForgetJob = new CompositeUploadJob(deviceId(), false); + CompositeUploadJob *fireAndForgetJob = new CompositeUploadJob(device, false); fireAndForgetJob->addSubjob(new UploadJob(np)); fireAndForgetJob->start(); } diff --git a/core/compositefiletransferjob.cpp b/core/compositefiletransferjob.cpp index 2d620fbd0..0792fc860 100644 --- a/core/compositefiletransferjob.cpp +++ b/core/compositefiletransferjob.cpp @@ -9,10 +9,11 @@ #include #include #include +#include -CompositeFileTransferJob::CompositeFileTransferJob(const QString &deviceId) - : KCompositeJob() - , m_deviceId(deviceId) +CompositeFileTransferJob::CompositeFileTransferJob(const Device *device, QObject *parent) + : KCompositeJob(parent) + , m_device(device) , m_running(false) , m_currentJobNum(1) , m_totalJobs(0) @@ -44,7 +45,7 @@ void CompositeFileTransferJob::startNextSubJob() m_currentJob->start(); Q_EMIT description(this, 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)}); connect(m_currentJob, &FileTransferJob::processedAmountChanged, this, &CompositeFileTransferJob::slotProcessedAmount); diff --git a/core/compositefiletransferjob.h b/core/compositefiletransferjob.h index 529e31229..da5287fce 100644 --- a/core/compositefiletransferjob.h +++ b/core/compositefiletransferjob.h @@ -11,6 +11,7 @@ #include #include +class Device; class FileTransferJob; class KDECONNECTCORE_EXPORT CompositeFileTransferJob : public KCompositeJob @@ -18,7 +19,7 @@ class KDECONNECTCORE_EXPORT CompositeFileTransferJob : public KCompositeJob Q_OBJECT public: - explicit CompositeFileTransferJob(const QString &deviceId); + explicit CompositeFileTransferJob(const Device *device, QObject *parent); void start() override; bool isRunning() const; @@ -33,7 +34,7 @@ private Q_SLOTS: void startNextSubJob(); private: - QString m_deviceId; + const Device *m_device; bool m_running; int m_currentJobNum; int m_totalJobs; diff --git a/plugins/share/shareplugin.cpp b/plugins/share/shareplugin.cpp index 66811a30a..9f87de642 100644 --- a/plugins/share/shareplugin.cpp +++ b/plugins/share/shareplugin.cpp @@ -119,7 +119,7 @@ void SharePlugin::receivePacket(const NetworkPacket &np) const bool open = np.get(QStringLiteral("open"), false); if (!m_compositeJob) { - m_compositeJob = new CompositeFileTransferJob(device()->id()); + m_compositeJob = new CompositeFileTransferJob(device(), this); m_compositeJob->setProperty("destUrl", destinationDir().toString()); m_compositeJob->setProperty("immediateProgressReporting", true); Daemon::instance()->jobTracker()->registerJob(m_compositeJob); diff --git a/tests/sendfiletest.cpp b/tests/sendfiletest.cpp index 1c5345a4a..8fe95e94c 100644 --- a/tests/sendfiletest.cpp +++ b/tests/sendfiletest.cpp @@ -87,7 +87,6 @@ private Q_SLOTS: DeviceInfo deviceInfo = KdeConnectConfig::instance().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); m_daemon->addDevice(device); @@ -95,7 +94,7 @@ private Q_SLOTS: NetworkPacket np(PACKET_TYPE_SHARE_REQUEST); np.setPayload(f, f->size()); - CompositeUploadJob *job = new CompositeUploadJob(deviceInfo.id, false); + CompositeUploadJob *job = new CompositeUploadJob(device, false); UploadJob *uj = new UploadJob(np); job->addSubjob(uj);