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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
2015-01-21 06:44:31 +00:00
|
|
|
FileTransferJob::FileTransferJob(const QSharedPointer<QIODevice>& origin, qint64 size, const QUrl& destination)
|
2015-01-21 06:22:14 +00:00
|
|
|
: KJob()
|
|
|
|
, mOrigin(origin)
|
2015-05-04 23:41:39 +01:00
|
|
|
, mReply(Q_NULLPTR)
|
2016-11-26 14:38:08 +00:00
|
|
|
, mFrom(QStringLiteral("KDE Connect"))
|
2015-01-21 06:22:14 +00:00
|
|
|
, mDestination(destination)
|
|
|
|
, mSpeedBytes(0)
|
|
|
|
, mWritten(0)
|
2017-08-02 00:48:37 +01:00
|
|
|
, mSize(size)
|
2013-09-10 18:01:46 +01:00
|
|
|
{
|
2015-05-04 23:41:39 +01:00
|
|
|
Q_ASSERT(mOrigin);
|
2015-09-07 13:54:33 +01:00
|
|
|
Q_ASSERT(mOrigin->isReadable());
|
2015-01-23 16:10:37 +00:00
|
|
|
if (mDestination.scheme().isEmpty()) {
|
2015-09-07 17:54:11 +01:00
|
|
|
qCWarning(KDECONNECT_CORE) << "Destination QUrl" << mDestination << "lacks a scheme. Setting its scheme to 'file'.";
|
2016-11-26 14:38:08 +00:00
|
|
|
mDestination.setScheme(QStringLiteral("file"));
|
2015-01-23 16:10:37 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 21:34:25 +00:00
|
|
|
setCapabilities(Killable);
|
2017-08-01 21:45:28 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "FileTransferJob Downloading payload to" << destination << "size:" << 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()
|
|
|
|
{
|
2015-10-17 21:32:57 +01:00
|
|
|
description(this, i18n("Receiving file over KDE Connect"),
|
2016-06-22 11:42:51 +01:00
|
|
|
{ i18nc("File transfer origin", "From"), mFrom }
|
2014-03-03 21:33:22 +00:00
|
|
|
);
|
2015-05-04 23:41:39 +01:00
|
|
|
|
|
|
|
if (mDestination.isLocalFile() && QFile::exists(mDestination.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
|
|
|
}
|
|
|
|
|
2016-07-05 11:56:27 +01:00
|
|
|
if (mOrigin->bytesAvailable())
|
|
|
|
startTransfer();
|
2016-06-22 16:49:45 +01:00
|
|
|
connect(mOrigin.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
|
|
|
|
if (mReply)
|
|
|
|
return;
|
|
|
|
|
2014-03-03 20:12:06 +00:00
|
|
|
setProcessedAmount(Bytes, 0);
|
2015-10-17 21:32:57 +01:00
|
|
|
description(this, i18n("Receiving file over KDE Connect"),
|
2016-06-22 11:42:51 +01:00
|
|
|
{ i18nc("File transfer origin", "From"), mFrom },
|
|
|
|
{ i18nc("File transfer destination", "To"), mDestination.toLocalFile() });
|
2014-03-03 21:33:22 +00:00
|
|
|
|
2015-09-11 16:03:11 +01:00
|
|
|
QNetworkRequest req(mDestination);
|
2017-08-02 00:48:37 +01:00
|
|
|
if (mSize >= 0) {
|
|
|
|
setTotalAmount(Bytes, mSize);
|
|
|
|
req.setHeader(QNetworkRequest::ContentLengthHeader, mSize);
|
|
|
|
}
|
2015-09-11 16:03:11 +01:00
|
|
|
mReply = Daemon::instance()->networkAccessManager()->put(req, mOrigin.data());
|
|
|
|
|
2015-05-04 23:41:39 +01:00
|
|
|
connect(mReply, &QNetworkReply::uploadProgress, this, [this](qint64 bytesSent, qint64 /*bytesTotal*/) {
|
2017-08-01 21:45:28 +01:00
|
|
|
if (!mTimer.isValid())
|
|
|
|
mTimer.start();
|
2015-05-04 23:41:39 +01:00
|
|
|
setProcessedAmount(Bytes, bytesSent);
|
2016-09-10 21:52:03 +01:00
|
|
|
|
|
|
|
const auto elapsed = mTimer.elapsed();
|
|
|
|
if (elapsed > 0) {
|
2017-08-01 22:18:15 +01:00
|
|
|
emitSpeed((1000 * bytesSent) / elapsed);
|
2016-09-10 21:52:03 +01:00
|
|
|
}
|
2015-05-04 23:41:39 +01:00
|
|
|
});
|
2016-06-22 11:42:51 +01:00
|
|
|
connect(mReply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
|
|
|
|
this, &FileTransferJob::transferFailed);
|
2015-05-04 23:41:39 +01:00
|
|
|
connect(mReply, &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)
|
|
|
|
{
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Couldn't transfer the file successfully" << error << mReply->errorString();
|
|
|
|
setError(error);
|
|
|
|
setErrorText(i18n("Received incomplete file: %1", mReply->errorString()));
|
|
|
|
emitResult();
|
|
|
|
|
|
|
|
mReply->close();
|
|
|
|
}
|
|
|
|
|
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
|
2016-06-22 11:42:51 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Finished transfer" << mDestination;
|
2015-05-04 23:41:39 +01:00
|
|
|
|
2013-09-10 18:01:46 +01:00
|
|
|
emitResult();
|
|
|
|
}
|
|
|
|
|
2014-03-03 21:34:03 +00:00
|
|
|
bool FileTransferJob::doKill()
|
|
|
|
{
|
2015-05-04 23:41:39 +01:00
|
|
|
if (mReply) {
|
|
|
|
mReply->close();
|
2014-03-03 21:34:03 +00:00
|
|
|
}
|
|
|
|
if (mOrigin) {
|
|
|
|
mOrigin->close();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|