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())
{
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);
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();