From 63357d3de518f85217cb8383c848e7e98375dcb5 Mon Sep 17 00:00:00 2001 From: APTX Date: Fri, 30 Oct 2009 14:04:15 +0100 Subject: [PATCH] Add HashRequest and HashReply to Hash. They should offer better methods of creating and controlling hashing tasks. --- anidbudpclient_global.h | 3 ++ file.cpp | 22 +++++++----- file.h | 4 ++- hash.cpp | 79 ++++++++++++++++++++++++++++++++++++----- hash.h | 50 +++++++++++++++++++++++--- 5 files changed, 135 insertions(+), 23 deletions(-) diff --git a/anidbudpclient_global.h b/anidbudpclient_global.h index 9ccdeb1..3e15e6b 100644 --- a/anidbudpclient_global.h +++ b/anidbudpclient_global.h @@ -3,6 +3,7 @@ #include #include +#include #define CLIENT_NAME "anidbudpclient" #define CLIENT_VERSION 0x000002 @@ -400,4 +401,6 @@ Q_DECLARE_METATYPE(AniDBUdpClient::ReplyCode); Q_DECLARE_METATYPE(AniDBUdpClient::State); Q_DECLARE_METATYPE(AniDBUdpClient::FileState); +Q_DECLARE_METATYPE(QFileInfo); + #endif // ANIDBUDPCLIENT_GLOBAL_H diff --git a/file.cpp b/file.cpp index 92cd821..af72b78 100644 --- a/file.cpp +++ b/file.cpp @@ -120,13 +120,14 @@ bool File::markWatched(bool watched) } -void File::finishHashing(const QFileInfo &file, const QByteArray &hash) +void File::finishHashing() { qDebug() << "finishHashing"; - if (m_file != file) - return; - m_ed2k = hash; + m_ed2k = hashResult->hash(); qDebug() << m_ed2k; + hashResult->deleteLater(); + hashResult = 0; + updateStatus(Hashing, Success); } @@ -143,7 +144,7 @@ qDebug() << "finishRenaming"; if (name.isEmpty()) name = fileCommand->value(FileAnimeFlag::EnglishName).toString(); - QString fileName = tr("%1 - %2 - %3 [%4](%5).%6") + QString fileName = tr("%1 - %2 - %3 - [%4](%5).%6") .arg(name) .arg(fileCommand->value(FileAnimeFlag::EpNo).toString()) .arg(fileCommand->value(FileAnimeFlag::EpName).toString()) @@ -246,7 +247,13 @@ qDebug() << "startHashing"; work(); return; } - Hash::instance()->hashFile(m_file); + + if (hashResult) + hashResult->deleteLater(); + + hashResult = Hash::instance()->hashFile(m_file); + connect(hashResult, SIGNAL(resultReady()), this, SLOT(finishHashing())); + updateStatus(Hashing, InProgress); } @@ -312,14 +319,13 @@ void File::startMarking() void File::init() { - + hashResult = 0; fileCommand = 0; addCommand = 0; m_hashingState = m_renamingState = m_addingState = m_markingState = NotStarted; notWorking = true; connect(this, SIGNAL(statusUpdate(Action,ActionState)), this, SLOT(workOnFinished(Action,ActionState))); - connect(Hash::instance(), SIGNAL(fileHashed(QFileInfo,QByteArray)), this, SLOT(finishHashing(QFileInfo,QByteArray))); } bool File::canContinue(ActionState state) diff --git a/file.h b/file.h index 0949b21..56d0334 100644 --- a/file.h +++ b/file.h @@ -14,6 +14,7 @@ namespace AniDBUdpClient { class FileCommand; class MyListCommand; +class HashResult; class ANIDBUDPCLIENTSHARED_EXPORT File : public QObject { @@ -78,7 +79,7 @@ signals: void statusUpdate(Action action, ActionState state); private slots: - void finishHashing(const QFileInfo &file, const QByteArray &hash); + void finishHashing(); void finishRenaming(bool success); void finishAdding(bool success); void finishMarking(bool success); @@ -110,6 +111,7 @@ private: ActionState m_addingState; ActionState m_markingState; + HashResult *hashResult; FileCommand *fileCommand; MyListAddCommand *addCommand; diff --git a/hash.cpp b/hash.cpp index dd4693d..3b2ada2 100644 --- a/hash.cpp +++ b/hash.cpp @@ -34,30 +34,36 @@ void Hash::destroy() m_instance = 0; } -void Hash::hashFile(const QFileInfo &file) +HashResult *Hash::hashFile(const HashRequest &file) { qDebug() << "Hash::hashFile"; - fileQueue.enqueue(file); - totalFileSize += file.size(); + + HashResult *result = new HashResult(file); + + fileQueue.enqueue(result); + totalFileSize += file.fileInfo().size(); if (hashing) - return; + return result; totalTime.start(); startHashing(); + return result; } void Hash::endHashing(const QByteArray &hash) { qDebug() << "Hash::endHashing"; - QFileInfo f = fileQueue.dequeue(); + HashResult *r = fileQueue.dequeue(); int fileElapsed = fileTime.elapsed(); - emit fileHashed(f, hash); -qDebug() << "File:" << f.fileName() << "Hash:" << hash << "Time:" << fileElapsed; +qDebug() << "File:" << r->fileInfo().fileName() << "Hash:" << hash << "Time:" << fileElapsed; - hashedFileSize += f.size(); + hashedFileSize += r->fileInfo().size(); + + r->setHash(hash); + emit resultReady(r); if (!fileQueue.isEmpty()) { @@ -85,7 +91,7 @@ void Hash::reportProgress(qint64 read, qint64 total) void Hash::startHashing() { - QString file = fileQueue.first().absoluteFilePath(); + QString file = fileQueue.first()->fileInfo().absoluteFilePath(); fileTime.start(); @@ -121,4 +127,59 @@ void Hash::tearDown() Hash *Hash::m_instance = 0; +// ----------------------------------------------------------------------------------- + +HashRequest::HashRequest(const QFileInfo &fileInfo) +{ +qDebug() << "HashRequest::HashRequest(const QFileInfo &fileInfo)"; + m_fileInfo = fileInfo; +} + +HashRequest::HashRequest(const HashRequest &other) +{ +qDebug() << "HashRequest::HashRequest(const HashRequest &other)"; + m_fileInfo = other.m_fileInfo; +qDebug() << m_fileInfo.absoluteFilePath(); +} + +HashRequest &HashRequest::operator=(const HashRequest &other) +{ +qDebug() << "HashRequest &HashRequest::operator=(const HashRequest &other)"; + m_fileInfo = other.m_fileInfo; + return *this; +} + +QFileInfo HashRequest::fileInfo() const +{ + return m_fileInfo; +} + +void HashRequest::setFileInfo(const QFileInfo &fileInfo) +{ + m_fileInfo = fileInfo; +} + +// ----------------------------------------------------------------------------------- + +HashResult::HashResult(const HashRequest &request) : QObject() +{ + this->request = request; +} + +QFileInfo HashResult::fileInfo() const +{ + return request.fileInfo(); +} + +QByteArray HashResult::hash() const +{ + return m_hash; +} + +void HashResult::setHash(const QByteArray &hash) +{ + m_hash = hash; + emit resultReady(); +} + } // namesapce AniDBUdpClient diff --git a/hash.h b/hash.h index 90e1a77..0e834c2 100644 --- a/hash.h +++ b/hash.h @@ -13,9 +13,11 @@ namespace AniDBUdpClient { +class HashRequest; +class HashResult; + class ANIDBUDPCLIENTSHARED_EXPORT Hash : public QObject { - Q_OBJECT protected: @@ -23,13 +25,13 @@ protected: ~Hash(); public: - void hashFile(const QFileInfo &file); + HashResult *hashFile(const HashRequest &file); static Hash *instance(); static void destroy(); signals: - void fileHashed(const QFileInfo &file, const QByteArray &hash); + void resultReady(HashResult *result); void fileProgress(int percent); void progress(int percent); void finished(); @@ -48,8 +50,7 @@ private: HashPrivate::HashProducer *producer; HashPrivate::HashConsumer *consumer; - QQueue fileQueue; - QMap hashedFiles; + QQueue fileQueue; bool hashing; @@ -62,6 +63,45 @@ private: static Hash *m_instance; }; +class HashRequest +{ +public: + HashRequest(const QFileInfo &fileInfo = QFileInfo()); + HashRequest(const HashRequest &other); + + HashRequest &operator=(const HashRequest &other); + + QFileInfo fileInfo() const; + void setFileInfo(const QFileInfo &fileInfo); + +private: + QFileInfo m_fileInfo; +}; + +class HashResult : public QObject +{ + friend class Hash; + + Q_OBJECT + Q_PROPERTY(QFileInfo fileInfo READ fileInfo); + Q_PROPERTY(QByteArray hash READ hash); + + HashResult(const HashRequest &request); + +public: + QFileInfo fileInfo() const; + QByteArray hash() const; + +signals: + void resultReady(); + +private: + void setHash(const QByteArray &hash); + + HashRequest request; + QByteArray m_hash; +}; + } // namesapce AniDBUdpClient #endif // HASH_H -- 2.52.0