#include <QObject>
#include <QMetaType>
+#include <QFileInfo>
#define CLIENT_NAME "anidbudpclient"
#define CLIENT_VERSION 0x000002
Q_DECLARE_METATYPE(AniDBUdpClient::State);
Q_DECLARE_METATYPE(AniDBUdpClient::FileState);
+Q_DECLARE_METATYPE(QFileInfo);
+
#endif // ANIDBUDPCLIENT_GLOBAL_H
}
-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);
}
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())
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);
}
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)
class FileCommand;
class MyListCommand;
+class HashResult;
class ANIDBUDPCLIENTSHARED_EXPORT File : public QObject
{
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);
ActionState m_addingState;
ActionState m_markingState;
+ HashResult *hashResult;
FileCommand *fileCommand;
MyListAddCommand *addCommand;
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())
{
void Hash::startHashing()
{
- QString file = fileQueue.first().absoluteFilePath();
+ QString file = fileQueue.first()->fileInfo().absoluteFilePath();
fileTime.start();
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
namespace AniDBUdpClient {
+class HashRequest;
+class HashResult;
+
class ANIDBUDPCLIENTSHARED_EXPORT Hash : public QObject
{
-
Q_OBJECT
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();
HashPrivate::HashProducer *producer;
HashPrivate::HashConsumer *consumer;
- QQueue<QFileInfo> fileQueue;
- QMap<QFileInfo, QByteArray> hashedFiles;
+ QQueue<HashResult *> fileQueue;
bool hashing;
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