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)
|
2015-10-17 21:32:57 +01:00
|
|
|
, mFrom("KDE Connect")
|
2015-01-21 06:22:14 +00:00
|
|
|
, mDestination(destination)
|
|
|
|
, mSpeedBytes(0)
|
|
|
|
, mWritten(0)
|
2013-09-10 18:01:46 +01:00
|
|
|
{
|
2015-05-04 23:41:39 +01:00
|
|
|
Q_ASSERT(mOrigin);
|
2015-01-23 16:10:37 +00:00
|
|
|
if (mDestination.scheme().isEmpty()) {
|
|
|
|
qWarning() << "Destination QUrl" << mDestination << "lacks a scheme. Setting its scheme to 'file'.";
|
|
|
|
mDestination.setScheme("file");
|
|
|
|
}
|
|
|
|
|
2015-05-04 23:41:39 +01:00
|
|
|
if (size >= 0) {
|
|
|
|
setTotalAmount(Bytes, size);
|
|
|
|
}
|
2015-10-17 21:32:57 +01:00
|
|
|
|
2014-03-03 21:34:25 +00:00
|
|
|
setCapabilities(Killable);
|
2014-09-21 22:54:27 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "FileTransferJob Downloading payload to" << destination;
|
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"),
|
|
|
|
QPair<QString, QString>(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();
|
|
|
|
}
|
|
|
|
|
|
|
|
startTransfer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferJob::startTransfer()
|
|
|
|
{
|
2014-03-03 20:12:06 +00:00
|
|
|
setProcessedAmount(Bytes, 0);
|
2015-01-21 06:22:14 +00:00
|
|
|
mTime = QTime::currentTime();
|
2015-10-17 21:32:57 +01:00
|
|
|
description(this, i18n("Receiving file over KDE Connect"),
|
|
|
|
QPair<QString, QString>(i18nc("File transfer origin", "From"), mFrom),
|
2015-03-04 21:33:54 +00:00
|
|
|
QPair<QString, QString>(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);
|
|
|
|
req.setHeader(QNetworkRequest::ContentLengthHeader, totalAmount(Bytes));
|
|
|
|
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*/) {
|
|
|
|
setProcessedAmount(Bytes, bytesSent);
|
|
|
|
emitSpeed(bytesSent/mTime.elapsed());
|
|
|
|
});
|
|
|
|
connect(mReply, &QNetworkReply::finished, this, &FileTransferJob::transferFinished);
|
2013-09-10 18:01:46 +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
|
2015-05-04 23:41:39 +01:00
|
|
|
if (mReply->error()) {
|
|
|
|
qCDebug(KDECONNECT_CORE) << "Couldn't transfer the file successfully" << mReply->errorString();
|
|
|
|
setError(mReply->error());
|
|
|
|
setErrorText(i18n("Received incomplete file: %1", mReply->errorString()));
|
2013-09-24 13:13:02 +01:00
|
|
|
} else {
|
2015-05-04 23:41:39 +01:00
|
|
|
qCDebug(KDECONNECT_CORE) << "Finished transfer" << mDestination;
|
2013-09-20 14:54:30 +01:00
|
|
|
}
|
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;
|
|
|
|
}
|