Skip to content

Commit

Permalink
[filetransferjob] Simplify error handling
Browse files Browse the repository at this point in the history
QNetworkReply::finished is also emitted in the error case, so by also connecting to errorOccured we emitResult twice
  • Loading branch information
nicolasfella committed Apr 17, 2024
1 parent 52e64e3 commit 8c62a10
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
34 changes: 14 additions & 20 deletions core/filetransferjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,29 @@ void FileTransferJob::startTransfer()

m_written = bytesSent;
});
connect(m_reply, &QNetworkReply::errorOccurred, this, &FileTransferJob::transferFailed);
connect(m_reply, &QNetworkReply::finished, this, &FileTransferJob::transferFinished);
}

void FileTransferJob::transferFailed(QNetworkReply::NetworkError error)
{
qCDebug(KDECONNECT_CORE) << "Couldn't transfer the file successfully" << error << m_reply->errorString();
setError(error);
setErrorText(i18n("Received incomplete file: %1", m_reply->errorString()));
emitResult();

m_reply->close();
}

void FileTransferJob::transferFinished()
{
// TODO: MD5-check the file
if (m_size == m_written) {
qCDebug(KDECONNECT_CORE) << "Finished transfer" << m_destination;
emitResult();
if (m_reply->error()) {
qCDebug(KDECONNECT_CORE) << "Couldn't transfer the file successfully" << m_reply->error() << m_reply->errorString();
setError(m_reply->error());
setErrorText(m_reply->errorString());
} else {
qCDebug(KDECONNECT_CORE) << "Received incomplete file (" << m_written << "/" << m_size << "bytes ), deleting";
// TODO: MD5-check the file
if (m_size == m_written) {
qCDebug(KDECONNECT_CORE) << "Finished transfer" << m_destination;
} else {
qCDebug(KDECONNECT_CORE) << "Received incomplete file (" << m_written << "/" << m_size << "bytes ), deleting";

deleteDestinationFile();
deleteDestinationFile();

setError(3);
setErrorText(i18n("Received incomplete file from: %1", m_from));
emitResult();
setError(3);
setErrorText(i18n("Received incomplete file from: %1", m_from));
}
}
emitResult();
}

void FileTransferJob::deleteDestinationFile()
Expand Down
1 change: 0 additions & 1 deletion core/filetransferjob.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ private Q_SLOTS:

private:
void startTransfer();
void transferFailed(QNetworkReply::NetworkError error);
void transferFinished();
void deleteDestinationFile();

Expand Down

0 comments on commit 8c62a10

Please sign in to comment.