]> Some of my projects - anidbudpclient.git/commitdiff
Make it possible to cancel a hash request. Hash cancels automatically if the RashResu...
authorAPTX <marek321@gmail.com>
Thu, 25 Aug 2011 16:01:56 +0000 (18:01 +0200)
committerAPTX <marek321@gmail.com>
Thu, 25 Aug 2011 16:01:56 +0000 (18:01 +0200)
hash.cpp
hash.h

index a4f2272ae30a2e53fca56d8589414c0039d7fdfd..a63e9b96502cc832a1a160faa639954f9ead728d 100644 (file)
--- 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 48fa66e70a2fe5b56d3ab027fe0544f9edc6ceaa..d3efbb882fef7033d35a1d456c8a848b84164e2e 100644 (file)
--- 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();