Fix few issues in FileTransferJob

Handle errors from different endpoints
Don't ::startTransfer when we find a colliding file
This commit is contained in:
Aleix Pol 2016-06-22 12:42:51 +02:00
parent 4cb50d69d8
commit f011191eca
2 changed files with 18 additions and 10 deletions

View file

@ -62,13 +62,14 @@ void FileTransferJob::start()
void FileTransferJob::doStart() void FileTransferJob::doStart()
{ {
description(this, i18n("Receiving file over KDE Connect"), description(this, i18n("Receiving file over KDE Connect"),
QPair<QString, QString>(i18nc("File transfer origin", "From"), mFrom) { i18nc("File transfer origin", "From"), mFrom }
); );
if (mDestination.isLocalFile() && QFile::exists(mDestination.toLocalFile())) { if (mDestination.isLocalFile() && QFile::exists(mDestination.toLocalFile())) {
setError(2); setError(2);
setErrorText(i18n("Filename already present")); setErrorText(i18n("Filename already present"));
emitResult(); emitResult();
return;
} }
startTransfer(); startTransfer();
@ -79,8 +80,8 @@ void FileTransferJob::startTransfer()
setProcessedAmount(Bytes, 0); setProcessedAmount(Bytes, 0);
mTime = QTime::currentTime(); mTime = QTime::currentTime();
description(this, i18n("Receiving file over KDE Connect"), description(this, i18n("Receiving file over KDE Connect"),
QPair<QString, QString>(i18nc("File transfer origin", "From"), mFrom), { i18nc("File transfer origin", "From"), mFrom },
QPair<QString, QString>(i18nc("File transfer destination", "To"), mDestination.toLocalFile())); { i18nc("File transfer destination", "To"), mDestination.toLocalFile() });
QNetworkRequest req(mDestination); QNetworkRequest req(mDestination);
req.setHeader(QNetworkRequest::ContentLengthHeader, totalAmount(Bytes)); req.setHeader(QNetworkRequest::ContentLengthHeader, totalAmount(Bytes));
@ -90,19 +91,25 @@ void FileTransferJob::startTransfer()
setProcessedAmount(Bytes, bytesSent); setProcessedAmount(Bytes, bytesSent);
emitSpeed(bytesSent/mTime.elapsed()); emitSpeed(bytesSent/mTime.elapsed());
}); });
connect(mReply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
this, &FileTransferJob::transferFailed);
connect(mReply, &QNetworkReply::finished, this, &FileTransferJob::transferFinished); connect(mReply, &QNetworkReply::finished, this, &FileTransferJob::transferFinished);
} }
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();
}
void FileTransferJob::transferFinished() void FileTransferJob::transferFinished()
{ {
//TODO: MD5-check the file //TODO: MD5-check the file
if (mReply->error()) { qCDebug(KDECONNECT_CORE) << "Finished transfer" << mDestination;
qCDebug(KDECONNECT_CORE) << "Couldn't transfer the file successfully" << mReply->errorString();
setError(mReply->error());
setErrorText(i18n("Received incomplete file: %1", mReply->errorString()));
} else {
qCDebug(KDECONNECT_CORE) << "Finished transfer" << mDestination;
}
emitResult(); emitResult();
} }

View file

@ -65,6 +65,7 @@ protected:
private: private:
void startTransfer(); void startTransfer();
void transferFailed(QNetworkReply::NetworkError error);
void transferFinished(); void transferFinished();
QSharedPointer<QIODevice> mOrigin; QSharedPointer<QIODevice> mOrigin;