From: APTX Date: Thu, 25 Aug 2011 16:01:56 +0000 (+0200) Subject: Make it possible to cancel a hash request. Hash cancels automatically if the RashResu... X-Git-Url: https://gitweb.tyo.aptx.org/?a=commitdiff_plain;h=8d377db064d05623f7c9866668d245f06b6b0e08;p=anidbudpclient.git Make it possible to cancel a hash request. Hash cancels automatically if the RashResult is deleted. --- diff --git a/hash.cpp b/hash.cpp index a4f2272..a63e9b9 100644 --- a/hash.cpp +++ b/hash.cpp @@ -37,30 +37,53 @@ void Hash::destroy() HashResult *Hash::hashFile(const HashRequest &file) { HashResult *result = new HashResult(file); + connect(result, SIGNAL(destroyed(QObject*)), this, SLOT(removeDeletedFromQueue(QObject*))); fileQueue.enqueue(result); totalFileSize += file.fileInfo().size(); if (hashing) return result; + hashing = true; totalTime.start(); startHashing(); return result; } +void Hash::cancel(HashResult *result) +{ + if (fileQueue.isEmpty()) + return; + + // Set pointer to 0, the item can not be removed as the hashing threads + // will send some notification and this is the only way Hash tracks results. + if (fileQueue.first() == result && hashing) + { + fileQueue.first() = 0; + return; + } + + if (!fileQueue.removeAll(result)) + return; + totalFileSize -= result->fileInfo().size(); +} + void Hash::endHashing(const QByteArray &hash) { - HashResult *r = fileQueue.dequeue(); + HashResult *result = fileQueue.dequeue(); + + if (!result) + return; #ifdef ANIDBUDPCLIENT_HASH_DEBUG int fileElapsed = fileTime.elapsed(); - qDebug() << "File:" << r->fileInfo().fileName() << "Hash:" << hash << "Time:" << fileElapsed; + qDebug() << "File:" << result->fileInfo().fileName() << "Hash:" << hash << "Time:" << fileElapsed; #endif - hashedFileSize += r->fileInfo().size(); + hashedFileSize += result->fileInfo().size(); - r->setHash(hash); - emit resultReady(r); + result->setHash(hash); + emit resultReady(result); if (!fileQueue.isEmpty()) { @@ -81,6 +104,10 @@ void Hash::endHashing(const QByteArray &hash) void Hash::reportProgress(qint64 read, qint64 total) { + qDebug() << fileQueue; + if (!fileQueue.first()) + return; + int filePercent = (read * 100) / total; emit fileProgress(filePercent); emit fileQueue.first()->progress(filePercent); @@ -89,8 +116,19 @@ void Hash::reportProgress(qint64 read, qint64 total) emit progress(totalPercent); } +void Hash::removeDeletedFromQueue(QObject *object) +{ + HashResult *result = (HashResult *) object; + cancel(result); +} + void Hash::startHashing() { + if (fileQueue.first() == 0) + fileQueue.dequeue(); + if (fileQueue.isEmpty()) + return; + QString file = fileQueue.first()->fileInfo().absoluteFilePath(); fileTime.start(); diff --git a/hash.h b/hash.h index 48fa66e..d3efbb8 100644 --- a/hash.h +++ b/hash.h @@ -26,6 +26,7 @@ protected: public: HashResult *hashFile(const HashRequest &file); + void cancel(HashResult *result); static Hash *instance(); static void destroy(); @@ -41,6 +42,8 @@ private slots: void reportProgress(qint64 read, qint64 total); + void removeDeletedFromQueue(QObject *object); + private: void startHashing(); void setUp();