2013-09-10 18:01:46 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
|
2015-05-04 23:41:39 +01:00
|
|
|
* Copyright 2015 Aleix Pol i Gonzalez <aleixpol@kde.org>
|
2013-09-10 18:01:46 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2019-03-23 16:29:26 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2013-09-10 18:01:46 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "filetransferjob.h"
|
2015-05-04 23:41:39 +01:00
|
|
|
#include "daemon.h"
|
2014-09-21 23:59:34 +01:00
|
|
|
#include <core_debug.h>
|
2013-09-10 18:01:46 +01:00
|
|
|
|
2013-11-06 21:16:55 +00:00
|
|
|
#include <qalgorithms.h>
|
2014-03-03 21:33:22 +00:00
|
|
|
#include <QFileInfo>
|
2014-09-21 23:59:34 +01:00
|
|
|
#include <QDebug>
|
2013-11-06 21:16:55 +00:00
|
|
|
|
2013-09-20 14:54:30 +01:00
|
|
|
#include <KLocalizedString>
|
|
|
|
|
2019-06-02 15:02:21 +01:00
|
|
|
FileTransferJob::FileTransferJob(const NetworkPacket* np, const QUrl& destination)
|
2015-01-21 06:22:14 +00:00
|
|
|
: KJob()
|
2019-06-02 15:02:21 +01:00
|
|
|
, m_origin(np->payload())
|
2019-05-05 14:45:50 +01:00
|
|
|
, m_reply(nullptr)
|
2017-09-03 20:39:44 +01:00
|
|
|
, m_from(QStringLiteral("KDE Connect"))
|
|
|
|
, m_destination(destination)
|
|
|
|
, m_speedBytes(0)
|
|
|
|
, m_written(0)
|
2019-06-02 15:02:21 +01:00
|
|
|
, m_size(np->payloadSize())
|
|
|
|
, m_np(np)
|
2013-09-10 18:01:46 +01:00
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
Q_ASSERT(m_origin);
|
2018-10-04 18:05:15 +01:00
|
|
|
//Disabled this assert: QBluetoothSocket doesn't report "->isReadable() == true" until it's connected
|
|
|
|
//Q_ASSERT(m_origin->isReadable());
|
2017-09-03 20:39:44 +01:00
|
|
|
if (m_destination.scheme().isEmpty()) {
|
|
|
|
qCWarning(KDECONNECT_CORE) << "Destination QUrl" << m_destination << "lacks a scheme. Setting its scheme to 'file'.";
|
|
|
|
m_destination.setScheme(QStringLiteral("file"));
|
2015-01-23 16:10:37 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 21:34:25 +00:00
|
|
|
setCapabilities(Killable);
|
2019-06-02 15:02:21 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "FileTransferJob Downloading payload to" << destination << "size:" << m_size;
|
2013-09-26 23:01:59 +01:00
|
|
|
}
|
|
|
|
|
2013-09-10 18:01:46 +01:00
|
|
|
void FileTransferJob::start()
|
|
|
|
{
|
2014-03-03 21:33:22 +00:00
|
|
|
QMetaObject::invokeMethod(this, "doStart", Qt::QueuedConnection);
|
2014-09-21 22:54:27 +01:00
|
|
|
//qCDebug(KDECONNECT_CORE) << "FileTransferJob start";
|
2014-03-03 21:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::doStart()
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
if (m_destination.isLocalFile() && QFile::exists(m_destination.toLocalFile())) {
|
2015-03-09 12:22:10 +00:00
|
|
|
setError(2);
|
|
|
|
setErrorText(i18n("Filename already present"));
|
2014-03-03 21:33:22 +00:00
|
|
|
emitResult();
|
2016-06-22 11:42:51 +01:00
|
|
|
return;
|
2014-03-03 21:33:22 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
if (m_origin->bytesAvailable())
|
2016-07-05 11:56:27 +01:00
|
|
|
startTransfer();
|
2017-09-03 20:39:44 +01:00
|
|
|
connect(m_origin.data(), &QIODevice::readyRead, this, &FileTransferJob::startTransfer);
|
2014-03-03 21:33:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::startTransfer()
|
|
|
|
{
|
2017-08-01 23:35:24 +01:00
|
|
|
// Don't put each ready read
|
2017-09-03 21:00:06 +01:00
|
|
|
if (m_reply)
|
2017-08-01 23:35:24 +01:00
|
|
|
return;
|
|
|
|
|
2014-03-03 20:12:06 +00:00
|
|
|
setProcessedAmount(Bytes, 0);
|
2014-03-03 21:33:22 +00:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
QNetworkRequest req(m_destination);
|
2017-09-03 21:00:06 +01:00
|
|
|
if (m_size >= 0) {
|
|
|
|
setTotalAmount(Bytes, m_size);
|
|
|
|
req.setHeader(QNetworkRequest::ContentLengthHeader, m_size);
|
2017-08-02 00:48:37 +01:00
|
|
|
}
|
2017-09-03 20:39:44 +01:00
|
|
|
m_reply = Daemon::instance()->networkAccessManager()->put(req, m_origin.data());
|
2015-09-11 16:03:11 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
connect(m_reply, &QNetworkReply::uploadProgress, this, [this](qint64 bytesSent, qint64 /*bytesTotal*/) {
|
2017-09-03 21:00:06 +01:00
|
|
|
if (!m_timer.isValid())
|
|
|
|
m_timer.start();
|
2015-05-04 23:41:39 +01:00
|
|
|
setProcessedAmount(Bytes, bytesSent);
|
2016-09-10 21:52:03 +01:00
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
const auto elapsed = m_timer.elapsed();
|
2016-09-10 21:52:03 +01:00
|
|
|
if (elapsed > 0) {
|
2017-08-01 22:18:15 +01:00
|
|
|
emitSpeed((1000 * bytesSent) / elapsed);
|
2016-09-10 21:52:03 +01:00
|
|
|
}
|
2018-11-09 12:58:28 +00:00
|
|
|
|
|
|
|
m_written = bytesSent;
|
2015-05-04 23:41:39 +01:00
|
|
|
});
|
2017-09-03 20:39:44 +01:00
|
|
|
connect(m_reply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
|
2016-06-22 11:42:51 +01:00
|
|
|
this, &FileTransferJob::transferFailed);
|
2017-09-03 20:39:44 +01:00
|
|
|
connect(m_reply, &QNetworkReply::finished, this, &FileTransferJob::transferFinished);
|
2013-09-10 18:01:46 +01:00
|
|
|
}
|
|
|
|
|
2016-06-22 11:42:51 +01:00
|
|
|
void FileTransferJob::transferFailed(QNetworkReply::NetworkError error)
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Couldn't transfer the file successfully" << error << m_reply->errorString();
|
2016-06-22 11:42:51 +01:00
|
|
|
setError(error);
|
2017-09-03 20:39:44 +01:00
|
|
|
setErrorText(i18n("Received incomplete file: %1", m_reply->errorString()));
|
2016-06-22 11:42:51 +01:00
|
|
|
emitResult();
|
|
|
|
|
2017-09-03 20:39:44 +01:00
|
|
|
m_reply->close();
|
2016-06-22 11:42:51 +01:00
|
|
|
}
|
|
|
|
|
2015-05-04 23:41:39 +01:00
|
|
|
void FileTransferJob::transferFinished()
|
2013-09-10 18:01:46 +01:00
|
|
|
{
|
2014-10-10 19:47:35 +01:00
|
|
|
//TODO: MD5-check the file
|
2018-11-09 12:58:28 +00:00
|
|
|
if (m_size == m_written) {
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Finished transfer" << m_destination;
|
|
|
|
emitResult();
|
|
|
|
} else {
|
2019-02-12 23:11:03 +00:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Received incomplete file ("<< m_written << "/" << m_size << "bytes ), deleting";
|
2018-11-09 12:58:28 +00:00
|
|
|
|
2019-05-02 17:49:50 +01:00
|
|
|
deleteDestinationFile();
|
|
|
|
|
2018-11-09 12:58:28 +00:00
|
|
|
setError(3);
|
|
|
|
setErrorText(i18n("Received incomplete file from: %1", m_from));
|
|
|
|
emitResult();
|
|
|
|
}
|
2013-09-10 18:01:46 +01:00
|
|
|
}
|
|
|
|
|
2019-05-02 17:49:50 +01:00
|
|
|
void FileTransferJob::deleteDestinationFile()
|
|
|
|
{
|
|
|
|
if (m_destination.isLocalFile() && QFile::exists(m_destination.toLocalFile())) {
|
|
|
|
QFile::remove(m_destination.toLocalFile());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-03 21:34:03 +00:00
|
|
|
bool FileTransferJob::doKill()
|
|
|
|
{
|
2017-09-03 20:39:44 +01:00
|
|
|
if (m_reply) {
|
|
|
|
m_reply->close();
|
2014-03-03 21:34:03 +00:00
|
|
|
}
|
2017-09-03 20:39:44 +01:00
|
|
|
if (m_origin) {
|
|
|
|
m_origin->close();
|
2014-03-03 21:34:03 +00:00
|
|
|
}
|
2019-05-02 17:49:50 +01:00
|
|
|
|
|
|
|
deleteDestinationFile();
|
|
|
|
|
2014-03-03 21:34:03 +00:00
|
|
|
return true;
|
|
|
|
}
|