diff --git a/lib/network/include/qx/network/qx-downloadmanager.h b/lib/network/include/qx/network/qx-downloadmanager.h index 66c7ce1a..afd086b1 100644 --- a/lib/network/include/qx/network/qx-downloadmanager.h +++ b/lib/network/include/qx/network/qx-downloadmanager.h @@ -99,6 +99,8 @@ class QX_NETWORK_EXPORT AsyncDownloadManager: public QObject bool mOverwrite; bool mStopOnError; bool mSkipEnumeration; + bool mDeletePartials; + // TODO: May also want to have option for removing ALL files upon any failure, even complete ones QCryptographicHash::Algorithm mVerificationMethod; // Status @@ -154,6 +156,7 @@ class QX_NETWORK_EXPORT AsyncDownloadManager: public QObject bool isOverwrite() const; bool isStopOnError() const; bool isSkipEnumeration() const; + bool isDeletePartialDownloads() const; QCryptographicHash::Algorithm verificationMethod() const; int taskCount() const; bool hasTasks() const; @@ -166,6 +169,7 @@ class QX_NETWORK_EXPORT AsyncDownloadManager: public QObject void setOverwrite(bool overwrite); void setStopOnError(bool stopOnError); void setSkipEnumeration(bool skipEnumeration); + void setDeletePartialDownloads(bool deletePartialDownloads); void setVerificationMethod(QCryptographicHash::Algorithm method); // Tasks diff --git a/lib/network/src/qx-downloadmanager.cpp b/lib/network/src/qx-downloadmanager.cpp index 04083188..de7649da 100644 --- a/lib/network/src/qx-downloadmanager.cpp +++ b/lib/network/src/qx-downloadmanager.cpp @@ -80,6 +80,7 @@ AsyncDownloadManager::AsyncDownloadManager(QObject* parent) : mOverwrite(false), mStopOnError(false), mSkipEnumeration(false), + mDeletePartials(false), mVerificationMethod(QCryptographicHash::Sha256), mStatus(Status::Initial) { @@ -322,7 +323,7 @@ bool AsyncDownloadManager::isStopOnError() const { return mStopOnError; } /*! * Returns @c true if the manager is configured to query the size of all queued tasks before - * actually initiating any downloads; otherwise returns @c false. + * actually initiating any downloads; otherwise, returns @c false. * * If enumeration is disabled, total download progress reported by the manager will be limited * in scope to only active and finished downloads, as the size of future download tasks cannot @@ -341,6 +342,17 @@ bool AsyncDownloadManager::isStopOnError() const { return mStopOnError; } */ bool AsyncDownloadManager::isSkipEnumeration() const { return mSkipEnumeration; } + +/*! + * Returns @c true if the manager is configured to remove any incomplete downloads after they + * fail or are aborted; otherwise, returns @c false. + * + * The default is @c false. + * + * @sa setDeletePartialDownloads(). + */ +bool AsyncDownloadManager::isDeletePartialDownloads() const { return mDeletePartials; } + /*! * Returns the hash algorithm used to verify downloads for tasks that include a checksum. * @@ -441,6 +453,14 @@ void AsyncDownloadManager::setStopOnError(bool stopOnError) { mStopOnError = sto */ void AsyncDownloadManager::setSkipEnumeration(bool skipEnumeration) { mSkipEnumeration = skipEnumeration; } +/*! + * Configures the manager to automatically remove incomplete files after a download fails or is aborted if + * @a deletePartialDownloads is @c true; otherwise, partial downloads are kept. + * + * @sa isDeletePartialDownloads(). + */ +void AsyncDownloadManager::setDeletePartialDownloads(bool deletePartialDownloads) { mDeletePartials = deletePartialDownloads; } + /*! * Sets the hash algorithm used to verify downloads for tasks that include a checksum. * @@ -539,7 +559,7 @@ void AsyncDownloadManager::readyReadHandler() if(wr.isFailure()) { - // Close and delete file, finished handler will use this info to create correct report + // Close file, finished handler will use this state to create correct report writer->close(); QFile::remove(writer->path()); @@ -701,19 +721,23 @@ void AsyncDownloadManager::downloadFinishedHandler(QNetworkReply* reply) recordFinishedDownload(DownloadOpReport::failedDownload(task, reply->errorString())); } - // Followup if needed - if(fail && mStopOnError && mStatus == Status::Downloading) - stopOnError(); - - // Cleanup writer + // Ensure writer is cleaned up writer->close(); - - // Remove from active writers mActiveWriters.remove(reply); // Mark reply for deletion reply->deleteLater(); + // Followup on fail if needed + if(fail) + { + if(mDeletePartials) + QFile::remove(writer->path()); + + if(mStopOnError && mStatus == Status::Downloading) + stopOnError(); + } + // Proceed on next loop iteration QTimer::singleShot(0, this, &AsyncDownloadManager::pushDownloadsUntilFinished); }