2019-06-02 15:02:21 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2019 Nicolas Fella <nicolas.fella@gmx.de>
|
2019-06-02 15:02:21 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2019-06-02 15:02:21 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "compositefiletransferjob.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "filetransferjob.h"
|
|
|
|
#include <KLocalizedString>
|
|
|
|
#include <core_debug.h>
|
2019-06-02 15:02:21 +01:00
|
|
|
#include <daemon.h>
|
2024-06-03 12:52:44 +01:00
|
|
|
#include <device.h>
|
2019-06-02 15:02:21 +01:00
|
|
|
|
2024-06-03 12:52:44 +01:00
|
|
|
CompositeFileTransferJob::CompositeFileTransferJob(const Device *device, QObject *parent)
|
|
|
|
: KCompositeJob(parent)
|
|
|
|
, m_device(device)
|
2019-06-02 15:02:21 +01:00
|
|
|
, m_running(false)
|
|
|
|
, m_currentJobNum(1)
|
|
|
|
, m_totalJobs(0)
|
2021-01-31 22:05:22 +00:00
|
|
|
, m_currentJobSentPayloadSize(0)
|
|
|
|
, m_totalSentPayloadSize(0)
|
|
|
|
, m_oldTotalSentPayloadSize(0)
|
2019-06-02 15:02:21 +01:00
|
|
|
, m_totalPayloadSize(0)
|
|
|
|
, m_currentJob(nullptr)
|
|
|
|
{
|
|
|
|
setCapabilities(Killable);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CompositeFileTransferJob::isRunning() const
|
|
|
|
{
|
|
|
|
return m_running;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompositeFileTransferJob::start()
|
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(this, "startNextSubJob", Qt::QueuedConnection);
|
|
|
|
m_running = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompositeFileTransferJob::startNextSubJob()
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
m_currentJob = qobject_cast<FileTransferJob *>(subjobs().at(0));
|
2021-01-31 22:05:22 +00:00
|
|
|
m_currentJobSentPayloadSize = 0;
|
2021-01-31 21:22:36 +00:00
|
|
|
|
2024-03-30 17:42:40 +00:00
|
|
|
m_currentJob->start();
|
2022-09-10 22:23:52 +01:00
|
|
|
Q_EMIT description(this,
|
|
|
|
i18ncp("@title job", "Receiving file", "Receiving files", m_totalJobs),
|
2024-06-03 12:52:44 +01:00
|
|
|
{i18nc("The source of a file operation", "Source"), this->m_device->name()},
|
2022-09-10 22:23:52 +01:00
|
|
|
{i18nc("The destination of a file operation", "Destination"), m_currentJob->destination().toDisplayString(QUrl::PreferLocalFile)});
|
2021-01-31 21:22:36 +00:00
|
|
|
|
2023-04-19 20:21:05 +01:00
|
|
|
connect(m_currentJob, &FileTransferJob::processedAmountChanged, this, &CompositeFileTransferJob::slotProcessedAmount);
|
2019-06-02 15:02:21 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool CompositeFileTransferJob::addSubjob(KJob *job)
|
2019-06-02 15:02:21 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
if (FileTransferJob *uploadJob = qobject_cast<FileTransferJob *>(job)) {
|
|
|
|
const NetworkPacket *np = uploadJob->networkPacket();
|
2019-06-02 15:02:21 +01:00
|
|
|
|
|
|
|
if (np->has(QStringLiteral("totalPayloadSize"))) {
|
|
|
|
m_totalPayloadSize = np->get<quint64>(QStringLiteral("totalPayloadSize"));
|
|
|
|
setTotalAmount(Bytes, m_totalPayloadSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (np->has(QStringLiteral("numberOfFiles"))) {
|
|
|
|
m_totalJobs = np->get<int>(QStringLiteral("numberOfFiles"));
|
2019-12-19 14:58:05 +00:00
|
|
|
setTotalAmount(Files, m_totalJobs);
|
2019-06-02 15:02:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasSubjobs()) {
|
|
|
|
QMetaObject::invokeMethod(this, "startNextSubJob", Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
return KCompositeJob::addSubjob(job);
|
|
|
|
} else {
|
|
|
|
qCDebug(KDECONNECT_CORE) << "CompositeFileTransferJob::addSubjob() - you can only add FileTransferJob's, ignoring";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CompositeFileTransferJob::doKill()
|
|
|
|
{
|
|
|
|
m_running = false;
|
|
|
|
if (m_currentJob) {
|
|
|
|
return m_currentJob->kill();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-28 08:11:43 +01:00
|
|
|
void CompositeFileTransferJob::slotProcessedAmount(KJob * /*job*/, KJob::Unit unit, qulonglong amount)
|
2019-06-02 15:02:21 +01:00
|
|
|
{
|
2021-01-31 22:05:22 +00:00
|
|
|
m_currentJobSentPayloadSize = amount;
|
|
|
|
const auto totalSent = m_totalSentPayloadSize + m_currentJobSentPayloadSize;
|
2019-06-02 15:02:21 +01:00
|
|
|
|
2021-01-31 22:05:22 +00:00
|
|
|
if (!m_reportTimer.isValid()) {
|
|
|
|
m_reportTimer.start();
|
|
|
|
}
|
|
|
|
if (m_reportTimer.hasExpired(250)) {
|
|
|
|
setProcessedAmount(unit, totalSent);
|
|
|
|
m_reportTimer.restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_speedTimer.isValid()) {
|
|
|
|
m_speedTimer.start();
|
|
|
|
}
|
|
|
|
if (m_speedTimer.hasExpired(1000)) {
|
|
|
|
emitSpeed(1000 * (totalSent - m_oldTotalSentPayloadSize) / m_speedTimer.elapsed());
|
|
|
|
m_oldTotalSentPayloadSize = totalSent;
|
|
|
|
m_speedTimer.restart();
|
2019-06-02 15:02:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompositeFileTransferJob::slotResult(KJob *job)
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
// Copies job error and errorText and emits result if job is in error otherwise removes job from subjob list
|
2019-06-02 15:02:21 +01:00
|
|
|
KCompositeJob::slotResult(job);
|
|
|
|
|
|
|
|
if (error() || !m_running) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-31 22:05:22 +00:00
|
|
|
m_totalSentPayloadSize += m_currentJobSentPayloadSize;
|
2019-06-02 15:02:21 +01:00
|
|
|
|
2021-01-31 22:05:22 +00:00
|
|
|
setProcessedAmount(Bytes, m_totalSentPayloadSize);
|
2019-06-02 15:02:21 +01:00
|
|
|
setProcessedAmount(Files, m_currentJobNum);
|
|
|
|
|
|
|
|
if (m_currentJobNum < m_totalJobs) {
|
|
|
|
m_currentJobNum++;
|
|
|
|
if (!subjobs().empty()) {
|
|
|
|
startNextSubJob();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
emitResult();
|
|
|
|
}
|
|
|
|
}
|
2023-07-26 09:15:11 +01:00
|
|
|
|
|
|
|
#include "moc_compositefiletransferjob.cpp"
|