]> Some of my projects - aniplayer-old.git/commitdiff
Add HashRequest and HashReply to Hash. They should offer better methods of creating...
authorAPTX <marek321@gmail.com>
Fri, 30 Oct 2009 13:04:15 +0000 (14:04 +0100)
committerAPTX <marek321@gmail.com>
Fri, 30 Oct 2009 13:04:15 +0000 (14:04 +0100)
lib/anidbudpclient/anidbudpclient_global.h
lib/anidbudpclient/file.cpp
lib/anidbudpclient/file.h
lib/anidbudpclient/hash.cpp
lib/anidbudpclient/hash.h

index 9ccdeb1c7ab8d20ace52da62588ba946a39cd641..3e15e6b1d024c4429f4ed41263dd595c4a339896 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <QObject>
 #include <QMetaType>
+#include <QFileInfo>
 
 #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
index 92cd821a4156b7ef229155898c9469c9fe7b1c41..af72b78f8256808fc875febf14e39f3fea53004e 100644 (file)
@@ -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)
index 0949b2177a39c741a670055807dbdce50c3ca9ab..56d03342210946699fdf00dd4a8488225329a6e8 100644 (file)
@@ -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;
 
index dd4693d306799675061b5f6519e60c782eba85de..3b2ada206d58a761da2e9148f7f7a3ff4a2d6773 100644 (file)
@@ -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
index 90e1a77b2934334ef210616ba372fdadd6d5d96b..0e834c2037a38ba2c0cdc8b4e06d0072f815d074 100644 (file)
 
 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<QFileInfo> fileQueue;
-       QMap<QFileInfo, QByteArray> hashedFiles;
+       QQueue<HashResult *> 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