From f43741f6e58c86054a4fb7c8fba5375fedfc613b Mon Sep 17 00:00:00 2001 From: APTX Date: Sat, 29 May 2010 17:47:32 +0200 Subject: [PATCH] - First compiling code. --- abstractcommand.cpp | 35 ++- abstractcommand.h | 59 +++- authcommand.cpp | 41 +-- authcommand.h | 122 ++++---- client.cpp | 87 +++--- client.h | 55 ++-- file.cpp | 79 ++--- file.h | 4 +- filecommand.cpp | 75 ++--- filecommand.h | 211 +++++++------- hash.h | 4 +- logoutcommand.cpp | 2 +- logoutcommand.h | 14 +- mylistaddcommand.cpp | 196 ++----------- mylistaddcommand.h | 63 ++-- mylistcommand.cpp | 671 +++++++++++++++++++++++-------------------- mylistcommand.h | 263 +++++++++-------- rawcommand.cpp | 2 +- rawcommand.h | 14 +- uptimecommand.cpp | 22 +- uptimecommand.h | 19 +- 21 files changed, 1020 insertions(+), 1018 deletions(-) diff --git a/abstractcommand.cpp b/abstractcommand.cpp index 915e715..6c2eb08 100644 --- a/abstractcommand.cpp +++ b/abstractcommand.cpp @@ -2,14 +2,12 @@ namespace AniDBUdpClient { -AbstractCommand::AbstractCommand(QObject *parent) : QObject(parent) +AbstractCommand::AbstractCommand() { - m_replyCode = UNKNOWN_REPLY; } AbstractCommand::~AbstractCommand() { - } Command AbstractCommand::rawCommand() const @@ -27,21 +25,44 @@ bool AbstractCommand::requiresSession() const return true; } -void AbstractCommand::setRawReply(ReplyCode replyCode, const QString &reply, Client *client) +// === + +AbstractReply::AbstractReply(const QByteArray &id, Client *client, QObject *parent) : QObject(parent) +{ + m_replyCode = UNKNOWN_REPLY; + m_id = id; + m_client = client; +} + +AbstractReply::~AbstractReply() +{ + +} + +const AbstractCommand &AbstractReply::command() const +{ + return m_command; +} + +void AbstractReply::setRawReply(ReplyCode replyCode, const QString &reply) { - Q_UNUSED(client); m_replyCode = replyCode; m_rawReply = reply; } -QString AbstractCommand::rawReply() const +QString AbstractReply::rawReply() const { return m_rawReply; } -ReplyCode AbstractCommand::replyCode() const +ReplyCode AbstractReply::replyCode() const { return m_replyCode; } +QByteArray AbstractReply::id() const +{ + return m_id; +} + } // namespace AniDBUdpClient diff --git a/abstractcommand.h b/abstractcommand.h index 8a2dc2a..e8b386f 100644 --- a/abstractcommand.h +++ b/abstractcommand.h @@ -14,26 +14,62 @@ class Client; typedef QPair Command; -class ANIDBUDPCLIENTSHARED_EXPORT AbstractCommand : public QObject -{ - Q_OBJECT - - Q_PROPERTY(ReplyCode replyCode READ replyCode); +class AbstractReply; +class ANIDBUDPCLIENTSHARED_EXPORT AbstractCommand +{ + friend class Client; public: - - AbstractCommand(QObject *parent = 0); + typedef AbstractReply ReplyType; + AbstractCommand(); virtual ~AbstractCommand(); virtual Command rawCommand() const; virtual bool waitForResult() const; virtual bool requiresSession() const; +}; + +#define REPLY_DEFINITION_HELPER(name) \ + friend class Client; \ + public: \ + typedef name##Command CommandType; \ + private: \ + CommandType m_command; \ + Client *m_client; \ + name##Reply(const CommandType command, const QByteArray &id, Client *client, QObject *parent) : m_command(command), m_client(client), AbstractReply(id, client, parent) {} \ + inline const CommandType &command() const { return m_command; } + +#define REPLY_DEFINITION_HELPER2(name) \ + friend class Client; \ + public: \ + typedef name##Command CommandType; \ + private: \ + CommandType m_command; \ + Client *m_client; \ + name##Reply(const CommandType command, const QByteArray &id, Client *client, QObject *parent) : m_command(command), m_client(client), AbstractReply(id, client, parent) {init();} \ + inline const CommandType &command() const { return m_command; } + +class ANIDBUDPCLIENTSHARED_EXPORT AbstractReply : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QByteArray id READ id); + Q_PROPERTY(ReplyCode replyCode READ replyCode); + +public: + typedef AbstractCommand CommandType; - virtual void setRawReply(ReplyCode replyCode, const QString &reply, Client *client); + AbstractReply(const QByteArray &id, Client *client, QObject *parent = 0); + virtual ~AbstractReply(); + + const CommandType &command() const; + + virtual void setRawReply(ReplyCode replyCode, const QString &reply); virtual QString rawReply() const; virtual ReplyCode replyCode() const; + virtual QByteArray id() const; signals: void replyReady(bool success = false); @@ -41,11 +77,12 @@ signals: protected: QString m_rawReply; ReplyCode m_replyCode; + QByteArray m_id; + Client *m_client; + + AbstractCommand m_command; }; } // namespace AniDBUdpClient -#include -Q_SCRIPT_DECLARE_QMETAOBJECT(AniDBUdpClient::AbstractCommand, QObject*); - #endif // ABSTRACTCOMMAND_H diff --git a/authcommand.cpp b/authcommand.cpp index dab0e88..6153b66 100644 --- a/authcommand.cpp +++ b/authcommand.cpp @@ -4,15 +4,16 @@ namespace AniDBUdpClient { -AuthCommand::AuthCommand(QObject *parent) : AbstractCommand(parent) +AuthCommand::AuthCommand() : AbstractCommand() { m_compression = false; } -AuthCommand::AuthCommand(const QString &user, const QString &pass, QObject *parent) : AbstractCommand(parent) +AuthCommand::AuthCommand(const QString &user, const QString &pass) : AbstractCommand() { m_user = user; m_pass = pass; + m_compression = false; } QString AuthCommand::user() const @@ -45,21 +46,6 @@ void AuthCommand::setCompression(bool compress) m_compression = compress; } -QString AuthCommand::sessionId() const -{ - return m_sessionId; -} - -QString AuthCommand::errorString() const -{ - return m_errorString; -} - -void AuthCommand::clearError() -{ - m_errorString = ""; -} - bool AuthCommand::waitForResult() const { return true; @@ -86,10 +72,27 @@ Command AuthCommand::rawCommand() const return command; } -void AuthCommand::setRawReply(ReplyCode replyCode, const QString &reply, Client *client) +// === + +QString AuthReply::sessionId() const +{ + return m_sessionId; +} + +QString AuthReply::errorString() const +{ + return m_errorString; +} + +void AuthReply::clearError() +{ + m_errorString = ""; +} + +void AuthReply::setRawReply(ReplyCode replyCode, const QString &reply) { qDebug() << replyCode; - AbstractCommand::setRawReply(replyCode, reply, client); + AbstractReply::setRawReply(replyCode, reply); switch(replyCode) { diff --git a/authcommand.h b/authcommand.h index b085318..806e5ed 100644 --- a/authcommand.h +++ b/authcommand.h @@ -1,55 +1,67 @@ -#ifndef AUTHCOMMAND_H -#define AUTHCOMMAND_H - -#include "abstractcommand.h" - -namespace AniDBUdpClient { - -class AuthCommand : public AbstractCommand -{ - Q_OBJECT - - Q_PROPERTY(QString user READ user WRITE setUser); - Q_PROPERTY(QString pass READ pass WRITE setPass); - Q_PROPERTY(bool compression READ compression WRITE setCompression); - - Q_PROPERTY(QString sessionId READ sessionId); - Q_PROPERTY(QString errorString READ errorString RESET clearError); - -public: - AuthCommand(QObject *parent = 0); - AuthCommand(const QString &user, const QString &pass, QObject *parent = 0); - - QString user() const; - void setUser(const QString &user); - - QString pass() const; - void setPass(const QString &pass); - - bool compression() const; - void setCompression(bool compress); - - - QString sessionId() const; - QString errorString() const; - void clearError(); - - - bool waitForResult() const; - bool requiresSession() const; - - Command rawCommand() const; - void setRawReply(ReplyCode replyCode, const QString &reply, Client *client); - -private: - QString m_user; - QString m_pass; - QString m_sessionId; - QString m_errorString; - - bool m_compression; -}; - -} // namespace AniDBUdpClient - -#endif // AUTHCOMMAND_H +#ifndef AUTHCOMMAND_H +#define AUTHCOMMAND_H + +#include "abstractcommand.h" + +namespace AniDBUdpClient { + +class AuthReply; + +class AuthCommand : public AbstractCommand +{ +/* + Q_PROPERTY(QString user READ user WRITE setUser); + Q_PROPERTY(QString pass READ pass WRITE setPass); + Q_PROPERTY(bool compression READ compression WRITE setCompression); +*/ +public: + typedef AuthReply ReplyType; + AuthCommand(); + AuthCommand(const QString &user, const QString &pass); + + QString user() const; + void setUser(const QString &user); + + QString pass() const; + void setPass(const QString &pass); + + bool compression() const; + void setCompression(bool compress); + + + bool waitForResult() const; + bool requiresSession() const; + + Command rawCommand() const; + +private: + QString m_user; + QString m_pass; + + bool m_compression; +}; + +class AuthReply : public AbstractReply +{ + Q_OBJECT + REPLY_DEFINITION_HELPER(Auth) + + Q_PROPERTY(QString sessionId READ sessionId); + Q_PROPERTY(QString errorString READ errorString RESET clearError); + +public: + + QString sessionId() const; + QString errorString() const; + void clearError(); + + void setRawReply(ReplyCode replyCode, const QString &reply); + +private: + QString m_sessionId; + QString m_errorString; +}; + +} // namespace AniDBUdpClient + +#endif // AUTHCOMMAND_H diff --git a/client.cpp b/client.cpp index 38d9172..929a879 100644 --- a/client.cpp +++ b/client.cpp @@ -44,12 +44,6 @@ qDebug() << "Api instance init!"; m_host = "api.anidb.info"; m_hostPort = 9000; - authCommand = new AuthCommand(this); - QObject::connect(authCommand, SIGNAL(replyReady(bool)), this, SLOT(doAuthenticate(bool))); - - logoutCommand = new LogoutCommand(this); - uptimeCommand = new UptimeCommand(this); - setFloodInterval(3); stateMachine = new QStateMachine(this); @@ -138,10 +132,10 @@ Client::~Client() foreach (CommandData *cmd, sentCommands) { - if (!cmd->command->waitForResult()) + if (!cmd->command->command().waitForResult()) { // Send CLIENT_DESTROYED to indicate that no real reply will come. - cmd->command->setRawReply(CLIENT_DESTROYED, "", this); + cmd->command->setRawReply(CLIENT_DESTROYED, ""); } } @@ -150,7 +144,7 @@ Client::~Client() while (commandTimer->isActive()) QCoreApplication::processEvents(); - sendCommand(new LogoutCommand, true); + sendCommand(createReply(LogoutCommand()), true); socket->waitForBytesWritten(5); } @@ -191,32 +185,32 @@ void Client::setLocalPort(quint16 port) QString Client::user() const { - return m_user; + return authCommand.user(); } void Client::setUser(const QString &user) { - m_user = user; + authCommand.setUser(user); } QString Client::pass() const { - return m_pass; + return authCommand.pass(); } void Client::setPass(const QString &pass) { - m_pass = pass; + authCommand.setPass(pass); } bool Client::compression() const { - return m_compression; + return authCommand.compression(); } void Client::setCompression(bool compress) { - m_compression = compress; + authCommand.setCompression(compress); } int Client::floodInterval() const @@ -314,11 +308,7 @@ qDebug() << "Entering Authenticating State"; if (m_sessionId.isEmpty()) { - authCommand->setUser(m_user); - authCommand->setPass(m_pass); - authCommand->setCompression(m_compression); - - enqueueControlCommand(authCommand, true); + enqueueControlCommand(createReply(authCommand), true); return; } emit authenticated(); @@ -330,13 +320,13 @@ qDebug() << "doAuthenticate init"; if (success) { qDebug() << "success!"; - m_sessionId = authCommand->sessionId().toUtf8(); + m_sessionId = authReply->sessionId().toUtf8(); emit authenticated(); return; } m_error = AuthenticationError; - m_errorString = authCommand->errorString(); + m_errorString = authReply->errorString(); emit connectionError(); } @@ -399,7 +389,7 @@ qDebug() << m_idlePolicy; idleTimer->start(UDP_API_INACTIVITY_UPDATE * 1000); break; case ImmediateLogoutIdlePolicy: - enqueueControlCommand(logoutCommand); + enqueueControlCommand(logoutReply); break; default: break; @@ -421,7 +411,7 @@ qDebug() << "Entering IdleTiemout State"; m_sessionId = ""; break; case KeepAliveIdlePolicy: - enqueueControlCommand(uptimeCommand); + enqueueControlCommand(uptimeReply); default: break; } @@ -449,7 +439,7 @@ qDebug() << "Entering Recieve State"; continue; } - if (m_compression && tmp.mid(0, 2) == "00") + if (authCommand.compression() && tmp.mid(0, 2) == "00") { qDebug() << "COMPRESSED DATAGRAM = " << tmp; tmp = qUncompress(tmp); @@ -527,7 +517,7 @@ qDebug() << "COMPRESSED DATAGRAM = " << tmp; } CommandData *commandData = sentCommands.take(commandId); - AbstractCommand *cmd = commandData->command; + AbstractReply *cmd = commandData->command; bool controlCommand = commandData->controlCommand; delete commandData; @@ -582,7 +572,7 @@ qDebug() << "LOGIN FIRST required, authing"; // tag + space + replyCode + space = 5 + 1 + 3 + 1 reply = reply.mid(10); - cmd->setRawReply(replyCode, reply, this); + cmd->setRawReply(replyCode, reply); continueLoop: ; } @@ -602,31 +592,31 @@ void Client::clearCommandQueue() // Delete all unsent commands that are managed by the client. while (!controlCommandQueue.isEmpty()) { - AbstractCommand *cmd = commandQueue.dequeue(); - if (!cmd->waitForResult()) + AbstractReply *reply = commandQueue.dequeue(); + if (!reply->command().waitForResult()) { // These would be deleted anyway - delete cmd; + delete reply; } else { // Send CLIENT_DESTROYED to indicate that no real reply will come. - cmd->setRawReply(CLIENT_DESTROYED, "", this); + reply->setRawReply(CLIENT_DESTROYED, ""); } } while (!commandQueue.isEmpty()) { - AbstractCommand *cmd = commandQueue.dequeue(); - if (!cmd->waitForResult()) + AbstractReply *reply = commandQueue.dequeue(); + if (!reply->command().waitForResult()) { // These would be deleted anyway - delete cmd; + delete reply; } else { // Send CLIENT_DESTROYED to indicate that no real reply will come. - cmd->setRawReply(CLIENT_DESTROYED, "", this); + reply->setRawReply(CLIENT_DESTROYED, ""); } } } @@ -662,7 +652,7 @@ qDebug() << "Disconnecting" << (graceful ? "gracefully" : ""); emit startDisconnecting(); } -void Client::send(AbstractCommand *command) +void Client::send(AbstractReply *command) { connect(); @@ -673,18 +663,19 @@ void Client::send(AbstractCommand *command) void Client::sendRaw(QByteArray command) { qDebug() << QString("Sending RAW command: %1").arg(command.constData()); - enqueueCommand(new RawCommand(command)); + enqueueCommand(createReply(RawCommand(command))); } -void Client::cancel(AbstractCommand *command) +void Client::cancel(AbstractReply *reply) { - commandQueue.removeAll(command); + commandQueue.removeAll(reply); + sentCommands.remove(reply->id()); } void Client::logout() { if (!m_sessionId.isEmpty()) - enqueueControlCommand(logoutCommand); + enqueueControlCommand(logoutReply); } void Client::commandTimeout(const QByteArray &commandId) @@ -707,7 +698,7 @@ qDebug() << commandId << "timed out"; emit sendFailed(); } -void Client::enqueueCommand(AbstractCommand *command, bool first) +void Client::enqueueCommand(AbstractReply *command, bool first) { if (first) { @@ -721,7 +712,7 @@ void Client::enqueueCommand(AbstractCommand *command, bool first) emit startSending(); } -void Client::enqueueControlCommand(AbstractCommand *command, bool first) +void Client::enqueueControlCommand(AbstractReply *command, bool first) { if (first) { @@ -735,9 +726,9 @@ void Client::enqueueControlCommand(AbstractCommand *command, bool first) emit startSending(); } -void Client::sendCommand(AbstractCommand *command, bool controlCommand) +void Client::sendCommand(AbstractReply *command, bool controlCommand) { - if (m_sessionId.isEmpty() && command->requiresSession()) + if (m_sessionId.isEmpty() && command->command().requiresSession()) { if (controlCommand) enqueueControlCommand(command, true); @@ -747,10 +738,10 @@ void Client::sendCommand(AbstractCommand *command, bool controlCommand) return; } - Command cmdPair = command->rawCommand(); + Command cmdPair = command->command().rawCommand(); QByteArray datagram = buildCmd(cmdPair.first, cmdPair.second); - QByteArray commandId = nextCommandId(); + QByteArray commandId = command->id(); datagram += datagram.contains(" ") ? "&" : " "; datagram += "tag=" + commandId; @@ -774,7 +765,7 @@ qDebug() << QString("SENDING datagram:\n\t%1\nto: %2 ([%3]:%4)") void Client::removeDeletedFromQueue() { - AbstractCommand *cmd = (AbstractCommand *) sender(); + AbstractReply *cmd = (AbstractReply *) sender(); cancel(cmd); } @@ -823,7 +814,7 @@ qDebug() << QString("Generated id %1").arg(result.constData()); Client *Client::m_instance = 0; -CommandData::CommandData(AbstractCommand *command, const QByteArray &commandId, bool controlCommand) : QObject() +CommandData::CommandData(AbstractReply *command, const QByteArray &commandId, bool controlCommand) : QObject() { this->command = command; this->controlCommand = controlCommand; diff --git a/client.h b/client.h index 854938f..87aabbd 100644 --- a/client.h +++ b/client.h @@ -22,18 +22,18 @@ class QTimer; namespace AniDBUdpClient { class AbstractCommand; -class AuthCommand; -class LogoutCommand; -class UptimeCommand; +class LogoutReply; +class UptimeReply; class CommandData; class ANIDBUDPCLIENTSHARED_EXPORT Client : public QObject { friend class CommandData; + friend class AbstractReply; Q_OBJECT - Q_ENUMS(AniDBUdpClient::State AniDBUdpClient::Error AniDBUdpClient::IdlePolicy AniDBUdpClientReplyCode); + Q_ENUMS(AniDBUdpClient::State AniDBUdpClient::Error AniDBUdpClient::IdlePolicy AniDBUdpClient::ReplyCode); Q_PROPERTY(QString host READ host WRITE setHost); Q_PROPERTY(quint16 hostPort READ hostPort WRITE setHostPort); @@ -101,9 +101,25 @@ public slots: */ void disconnect(bool graceful = false); - void send(AbstractCommand *command); +public: + template typename T::ReplyType *send(const T &command, QObject *parent = 0) + { + typename T::ReplyType *reply = createReply(command, parent); + send(reply); + return reply; + } + +private: + template typename T::ReplyType *createReply(const T &command, QObject *parent = 0) + { + return new T::ReplyType(command, nextCommandId(), this, parent); + } +public slots: + + void send(AbstractReply *reply); + void sendRaw(QByteArray command); - void cancel(AbstractCommand *command); + void cancel(AbstractReply *command); signals: @@ -153,9 +169,9 @@ private slots: void removeDeletedFromQueue(); private: - void enqueueCommand(AbstractCommand *command, bool first = false); - void enqueueControlCommand(AbstractCommand *command, bool first = false); - void sendCommand(AbstractCommand *command, bool controlCommand = false); + void enqueueCommand(AbstractReply *command, bool first = false); + void enqueueControlCommand(AbstractReply *command, bool first = false); + void sendCommand(AbstractReply *command, bool controlCommand = false); QByteArray buildCmd(const QString &cmd, const QVariantMap &args); @@ -165,8 +181,8 @@ private: QTimer *idleTimer; QTimer *replyTimeoutTimer; - QQueue commandQueue; - QQueue controlCommandQueue; + QQueue commandQueue; + QQueue controlCommandQueue; QMap sentCommands; QUdpSocket *socket; @@ -180,12 +196,6 @@ private: int m_floodInterval; - // Auth params - QString m_user; - QString m_pass; - - bool m_compression; - QByteArray m_sessionId; // Misc params @@ -197,9 +207,10 @@ private: int commandsTimedOut; - AuthCommand *authCommand; - LogoutCommand *logoutCommand; - UptimeCommand *uptimeCommand; + AuthCommand authCommand; + AuthReply *authReply; + LogoutReply *logoutReply; + UptimeReply *uptimeReply; static Client *m_instance; @@ -235,12 +246,12 @@ class CommandData : QObject Q_OBJECT public: - AbstractCommand *command; + AbstractReply *command; bool controlCommand; QByteArray commandId; QTimer timer; - CommandData(AbstractCommand *command, const QByteArray &commandId, bool controlCommand = false); + CommandData(AbstractReply *command, const QByteArray &commandId, bool controlCommand = false); signals: void timeout(QByteArray commandId); private slots: diff --git a/file.cpp b/file.cpp index e27592b..a8532ce 100644 --- a/file.cpp +++ b/file.cpp @@ -21,15 +21,15 @@ File::File(const QFileInfo &file, QObject *parent) : QObject(parent) File::~File() { - if (fileCommand) + if (fileReply) { - delete fileCommand; - fileCommand = 0; + delete fileReply; + fileReply = 0; } - if (addCommand) + if (addReply) { - delete addCommand; - addCommand = 0; + delete addReply; + addReply = 0; } if (hashResult) { @@ -150,17 +150,17 @@ qDebug() << "finishRenaming"; return; } - QString name = fileCommand->value(FileAnimeFlag::RomajiName).toString(); + QString name = fileReply->value(FileAnimeFlag::RomajiName).toString(); if (name.isEmpty()) - name = fileCommand->value(FileAnimeFlag::EnglishName).toString(); + name = fileReply->value(FileAnimeFlag::EnglishName).toString(); QString newFileName = tr("%1 - %2 - %3 - [%4](%5).%6") .arg(name) - .arg(fileCommand->value(FileAnimeFlag::EpNo).toString()) - .arg(fileCommand->value(FileAnimeFlag::EpName).toString()) - .arg(fileCommand->value(FileAnimeFlag::GroupShortName).toString()) - .arg(fileCommand->value(FileFlag::Crc32).toString()) - .arg(fileCommand->value(FileFlag::FileType).toString()); + .arg(fileReply->value(FileAnimeFlag::EpNo).toString()) + .arg(fileReply->value(FileAnimeFlag::EpName).toString()) + .arg(fileReply->value(FileAnimeFlag::GroupShortName).toString()) + .arg(fileReply->value(FileFlag::Crc32).toString()) + .arg(fileReply->value(FileFlag::FileType).toString()); newFileName.replace('"', "'"); newFileName.replace(QRegExp("[\\/]"), "-"); @@ -284,24 +284,23 @@ qDebug() << "startRenaming"; return; } - if (fileCommand) - delete fileCommand; - - fileCommand = new FileCommand(m_ed2k, - size(), - FileFlag::Crc32 - | FileFlag::FileType - | FileFlag::Lid, - FileAnimeFlag::EnglishName - | FileAnimeFlag::RomajiName - | FileAnimeFlag::KanjiName - | FileAnimeFlag::EpNo - | FileAnimeFlag::EpName - | FileAnimeFlag::GroupShortName, - this); - - connect(fileCommand, SIGNAL(replyReady(bool)), this, SLOT(finishRenaming(bool))); - Client::instance()->send(fileCommand); + if (fileReply) + delete fileReply; + + FileCommand fileCommand(m_ed2k, + size(), + FileFlag::Crc32 + | FileFlag::FileType + | FileFlag::Lid, + FileAnimeFlag::EnglishName + | FileAnimeFlag::RomajiName + | FileAnimeFlag::KanjiName + | FileAnimeFlag::EpNo + | FileAnimeFlag::EpName + | FileAnimeFlag::GroupShortName); + fileReply = Client::instance()->send(fileCommand); + connect(fileReply, SIGNAL(replyReady(bool)), this, SLOT(finishRenaming(bool))); + updateStatus(Renaming, InProgress); } @@ -313,15 +312,17 @@ void File::startAdding() return; } - if (addCommand) - delete addCommand; + if (addReply) + delete addReply; + + MyListAddCommand addCommand(m_ed2k, size(), false); + addCommand.setState(StateOnHdd); + + addReply = Client::instance()->send(addCommand); - addCommand = new MyListAddCommand(m_ed2k, size(), false); - addCommand->setState(StateOnHdd); + connect(addReply, SIGNAL(replyReady(bool)), this, SLOT(finishAdding(bool))); - connect(addCommand, SIGNAL(replyReady(bool)), this, SLOT(finishAdding(bool))); - Client::instance()->send(addCommand); updateStatus(Adding, InProgress); } @@ -337,8 +338,8 @@ void File::startMarking() void File::init() { hashResult = 0; - fileCommand = 0; - addCommand = 0; + fileReply = 0; + addReply = 0; m_hashingState = m_renamingState = m_addingState = m_markingState = NotStarted; notWorking = true; diff --git a/file.h b/file.h index db61f0a..9118a0d 100644 --- a/file.h +++ b/file.h @@ -112,8 +112,8 @@ private: ActionState m_markingState; HashResult *hashResult; - FileCommand *fileCommand; - MyListAddCommand *addCommand; + FileReply *fileReply; + MyListAddReply *addReply; }; diff --git a/filecommand.cpp b/filecommand.cpp index bf3c51f..5dcf719 100644 --- a/filecommand.cpp +++ b/filecommand.cpp @@ -6,12 +6,12 @@ namespace AniDBUdpClient { -FileCommand::FileCommand(QObject *parent) : AbstractCommand(parent) +FileCommand::FileCommand() : AbstractCommand() { init(); } -FileCommand::FileCommand(int fid, FileFlags fmask, FileAnimeFlags amask, QObject *parent) : AbstractCommand(parent) +FileCommand::FileCommand(int fid, FileFlags fmask, FileAnimeFlags amask) : AbstractCommand() { init(); m_fid = fid; @@ -19,7 +19,7 @@ FileCommand::FileCommand(int fid, FileFlags fmask, FileAnimeFlags amask, QObject m_amask = amask; } -FileCommand::FileCommand(const QByteArray &ed2k, qint64 size, FileFlags fmask, FileAnimeFlags amask, QObject *parent) : AbstractCommand(parent) +FileCommand::FileCommand(const QByteArray &ed2k, qint64 size, FileFlags fmask, FileAnimeFlags amask) : AbstractCommand() { init(); m_ed2k = ed2k; @@ -28,7 +28,7 @@ FileCommand::FileCommand(const QByteArray &ed2k, qint64 size, FileFlags fmask, F m_amask = amask; } -FileCommand::FileCommand(const QString &aname, const QString &gname, int epno, FileFlags fmask, FileAnimeFlags amask, QObject *parent) : AbstractCommand(parent) +FileCommand::FileCommand(const QString &aname, const QString &gname, int epno, FileFlags fmask, FileAnimeFlags amask) : AbstractCommand() { init(); m_aname = aname; @@ -38,7 +38,7 @@ FileCommand::FileCommand(const QString &aname, const QString &gname, int epno, F m_amask = amask; } -FileCommand::FileCommand(const QString &aname, int gid, int epno, FileFlags fmask, FileAnimeFlags amask, QObject *parent) : AbstractCommand(parent) +FileCommand::FileCommand(const QString &aname, int gid, int epno, FileFlags fmask, FileAnimeFlags amask) : AbstractCommand() { init(); m_aname = aname; @@ -48,7 +48,7 @@ FileCommand::FileCommand(const QString &aname, int gid, int epno, FileFlags fmas m_amask = amask; } -FileCommand::FileCommand(int aid, const QString &gname, int epno, FileFlags fmask, FileAnimeFlags amask, QObject *parent) : AbstractCommand(parent) +FileCommand::FileCommand(int aid, const QString &gname, int epno, FileFlags fmask, FileAnimeFlags amask) : AbstractCommand() { init(); m_aid = aid; @@ -58,7 +58,7 @@ FileCommand::FileCommand(int aid, const QString &gname, int epno, FileFlags fmas m_amask = amask; } -FileCommand::FileCommand(int aid, int gid, int epno, FileFlags fmask, FileAnimeFlags amask, QObject *parent) : AbstractCommand(parent) +FileCommand::FileCommand(int aid, int gid, int epno, FileFlags fmask, FileAnimeFlags amask) : AbstractCommand() { init(); m_aid = aid; @@ -169,16 +169,6 @@ void FileCommand::setAmask(FileAnimeFlags amask) m_amask = amask; } -QVariant FileCommand::value(FileFlags f) const -{ - return fileFlagData.value(f); -} - -QVariant FileCommand::value(FileAnimeFlags f) const -{ - return fileAnimeFlagData.value(f); -} - bool FileCommand::waitForResult() const { return true; @@ -241,10 +231,39 @@ Command FileCommand::rawCommand() const return cmd; } +void FileCommand::init() +{ + m_fid = 0; + m_aid = 0; + m_gid = 0; + + m_size = 0; + m_epno = 0; + + m_fmask = FileFlags(0); + m_amask = AnimeFlags(0); +} + +// === -void FileCommand::setRawReply(ReplyCode replyCode, const QString &reply, Client *client) +int FileReply::fid() const { - AbstractCommand::setRawReply(replyCode, reply, client); + return m_fid; +} + +QVariant FileReply::value(FileFlags f) const +{ + return fileFlagData.value(f); +} + +QVariant FileReply::value(FileAnimeFlags f) const +{ + return fileAnimeFlagData.value(f); +} + +void FileReply::setRawReply(ReplyCode replyCode, const QString &reply) +{ + AbstractReply::setRawReply(replyCode, reply); switch (replyCode) { @@ -263,7 +282,7 @@ void FileCommand::setRawReply(ReplyCode replyCode, const QString &reply, Client } } -void FileCommand::readReplyData(const QString &reply) +void FileReply::readReplyData(const QString &reply) { QString d = reply.mid(reply.indexOf('\n')).trimmed(); qDebug() << d; @@ -271,7 +290,7 @@ qDebug() << d; qDebug() << parts; m_fid = parts[0].toInt(); - if (m_fmask == 0 && m_amask == 0) + if (command().fmask() == 0 && command().amask() == 0) { fileFlagData.insert(FileFlag::Aid, parts[1].toInt()); fileFlagData.insert(FileFlag::Eid, parts[2].toInt()); @@ -286,7 +305,7 @@ qDebug() << parts; int partNo = 1; for (int i = 0, flag = 1 << 31; i < 32; ++i, flag = (flag >> 1) & ~(1 << 31)) { - if (m_fmask & flag) + if (command().fmask() & flag) { if (partNo >= parts.size()) { @@ -300,7 +319,7 @@ qDebug() << "Not enough parts in reply."; for (int i = 0, flag = 1 << 31; i < 32; ++i, flag = (flag >> 1) & ~(1 << 31)) { - if (m_amask & flag) + if (command().amask() & flag) { if (partNo >= parts.size()) { @@ -313,17 +332,9 @@ qDebug() << "Not enough parts in reply."; } } -void FileCommand::init() +void FileReply::init() { m_fid = 0; - m_aid = 0; - m_gid = 0; - - m_size = 0; - m_epno = 0; - - m_fmask = FileFlags(0); - m_amask = AnimeFlags(0); } } // namespace AniDBUdpClient diff --git a/filecommand.h b/filecommand.h index 7fdef4b..dabc761 100644 --- a/filecommand.h +++ b/filecommand.h @@ -1,99 +1,112 @@ -#ifndef FILECOMMAND_H -#define FILECOMMAND_H - -#include "anidbudpclient_global.h" -#include "abstractcommand.h" - -#include - -namespace AniDBUdpClient { - -class ANIDBUDPCLIENTSHARED_EXPORT FileCommand : public AbstractCommand -{ - Q_OBJECT - - Q_PROPERTY(int fid READ fid WRITE setFid); - - Q_PROPERTY(QByteArray ed2k READ ed2k WRITE setEd2k); - Q_PROPERTY(qint64 size READ size WRITE setSize); - - Q_PROPERTY(QString aname READ aname WRITE setAname); - Q_PROPERTY(int aid READ aid WRITE setAid); - Q_PROPERTY(QString gname READ gname WRITE setGname); - Q_PROPERTY(int gid READ gid WRITE setGid); - Q_PROPERTY(int epno READ epno WRITE setEpno); - - Q_PROPERTY(FileFlags fmask READ fmask WRITE setFmask); - Q_PROPERTY(FileAnimeFlags amask READ amask WRITE setAmask); - -public: - FileCommand(QObject *parent = 0); - FileCommand(int fid, FileFlags fmask = FileFlags(0), FileAnimeFlags = FileAnimeFlags(0), QObject *parent = 0); - FileCommand(const QByteArray &ed2k, qint64 size, FileFlags fmask = FileFlags(0), FileAnimeFlags = FileAnimeFlags(0), QObject *parent = 0); - FileCommand(const QString &aname, const QString &gname, int epno, FileFlags fmask = FileFlags(0), FileAnimeFlags = FileAnimeFlags(0), QObject *parent = 0); - FileCommand(const QString &aname, int gid, int epno, FileFlags fmask = FileFlags(0), FileAnimeFlags = FileAnimeFlags(0), QObject *parent = 0); - FileCommand(int aid, const QString &gname, int epno, FileFlags fmask = FileFlags(0), FileAnimeFlags = FileAnimeFlags(0), QObject *parent = 0); - FileCommand(int aid, int gid, int epno, FileFlags fmask = FileFlags(0), FileAnimeFlags = FileAnimeFlags(0), QObject *parent = 0); - - int fid() const; - void setFid(int fid); - - QByteArray ed2k() const; - void setEd2k(const QByteArray &ed2k); - qint64 size() const; - void setSize(qint64 size); - - QString aname() const; - void setAname(const QString &aname); - int aid() const; - void setAid(int aid); - QString gname() const; - void setGname(const QString &gname); - int gid() const; - void setGid(int gid); - int epno() const; - void setEpno(int epno); - - FileFlags fmask() const; - void setFmask(FileFlags fmask); - FileAnimeFlags amask() const; - void setAmask(FileAnimeFlags amask); - - - QVariant value(FileFlags f) const; - QVariant value(FileAnimeFlags f) const; - - - bool waitForResult() const; - Command rawCommand() const; - void setRawReply(ReplyCode replyCode, const QString &reply, Client *client); - - -private: - void readReplyData(const QString &reply); - void init(); - - int m_fid; - - QByteArray m_ed2k; - qint64 m_size; - - QString m_aname; - int m_aid; - QString m_gname; - int m_gid; - int m_epno; - - FileFlags m_fmask; - FileAnimeFlags m_amask; - - QMap fileFlagData; - QMap fileAnimeFlagData; -}; - -} // namespace AniDBUdpClient - -#include -Q_SCRIPT_DECLARE_QMETAOBJECT(AniDBUdpClient::FileCommand, QObject*); - -#endif // FILECOMMAND_H +#ifndef FILECOMMAND_H +#define FILECOMMAND_H + +#include "anidbudpclient_global.h" +#include "abstractcommand.h" + +#include + +namespace AniDBUdpClient { + +class FileReply; + +class ANIDBUDPCLIENTSHARED_EXPORT FileCommand : public AbstractCommand +{ +/* + Q_PROPERTY(int fid READ fid WRITE setFid); + + Q_PROPERTY(QByteArray ed2k READ ed2k WRITE setEd2k); + Q_PROPERTY(qint64 size READ size WRITE setSize); + + Q_PROPERTY(QString aname READ aname WRITE setAname); + Q_PROPERTY(int aid READ aid WRITE setAid); + Q_PROPERTY(QString gname READ gname WRITE setGname); + Q_PROPERTY(int gid READ gid WRITE setGid); + Q_PROPERTY(int epno READ epno WRITE setEpno); + + Q_PROPERTY(FileFlags fmask READ fmask WRITE setFmask); + Q_PROPERTY(FileAnimeFlags amask READ amask WRITE setAmask); +*/ +public: + typedef FileReply ReplyType; + FileCommand(); + FileCommand(int fid, FileFlags fmask = FileFlags(0), FileAnimeFlags = FileAnimeFlags(0)); + FileCommand(const QByteArray &ed2k, qint64 size, FileFlags fmask = FileFlags(0), FileAnimeFlags = FileAnimeFlags(0)); + FileCommand(const QString &aname, const QString &gname, int epno, FileFlags fmask = FileFlags(0), FileAnimeFlags = FileAnimeFlags(0)); + FileCommand(const QString &aname, int gid, int epno, FileFlags fmask = FileFlags(0), FileAnimeFlags = FileAnimeFlags(0)); + FileCommand(int aid, const QString &gname, int epno, FileFlags fmask = FileFlags(0), FileAnimeFlags = FileAnimeFlags(0)); + FileCommand(int aid, int gid, int epno, FileFlags fmask = FileFlags(0), FileAnimeFlags = FileAnimeFlags(0)); + + int fid() const; + void setFid(int fid); + + QByteArray ed2k() const; + void setEd2k(const QByteArray &ed2k); + qint64 size() const; + void setSize(qint64 size); + + QString aname() const; + void setAname(const QString &aname); + int aid() const; + void setAid(int aid); + QString gname() const; + void setGname(const QString &gname); + int gid() const; + void setGid(int gid); + int epno() const; + void setEpno(int epno); + + FileFlags fmask() const; + void setFmask(FileFlags fmask); + FileAnimeFlags amask() const; + void setAmask(FileAnimeFlags amask); + + bool waitForResult() const; + Command rawCommand() const; + +private: + void init(); + + int m_fid; + + QByteArray m_ed2k; + qint64 m_size; + + QString m_aname; + int m_aid; + QString m_gname; + int m_gid; + int m_epno; + + FileFlags m_fmask; + FileAnimeFlags m_amask; +}; + +class ANIDBUDPCLIENTSHARED_EXPORT FileReply : public AbstractReply +{ + Q_OBJECT + REPLY_DEFINITION_HELPER2(File) + + Q_PROPERTY(int fid READ fid); + +public: + int fid() const; + + QVariant value(FileFlags f) const; + QVariant value(FileAnimeFlags f) const; + + void setRawReply(ReplyCode replyCode, const QString &reply); + +private: + void readReplyData(const QString &reply); + void init(); + + int m_fid; + + QMap fileFlagData; + QMap fileAnimeFlagData; +}; + +} // namespace AniDBUdpClient + +#endif // FILECOMMAND_H diff --git a/hash.h b/hash.h index 0e834c2..232a2aa 100644 --- a/hash.h +++ b/hash.h @@ -63,7 +63,7 @@ private: static Hash *m_instance; }; -class HashRequest +class ANIDBUDPCLIENTSHARED_EXPORT HashRequest { public: HashRequest(const QFileInfo &fileInfo = QFileInfo()); @@ -78,7 +78,7 @@ private: QFileInfo m_fileInfo; }; -class HashResult : public QObject +class ANIDBUDPCLIENTSHARED_EXPORT HashResult : public QObject { friend class Hash; diff --git a/logoutcommand.cpp b/logoutcommand.cpp index dfac7a1..63372f7 100644 --- a/logoutcommand.cpp +++ b/logoutcommand.cpp @@ -2,7 +2,7 @@ namespace AniDBUdpClient { -LogoutCommand::LogoutCommand(QObject *parent) : AbstractCommand(parent) +LogoutCommand::LogoutCommand() : AbstractCommand() { } diff --git a/logoutcommand.h b/logoutcommand.h index 9495561..f3afab5 100644 --- a/logoutcommand.h +++ b/logoutcommand.h @@ -5,14 +5,24 @@ namespace AniDBUdpClient { +class LogoutReply; + class ANIDBUDPCLIENTSHARED_EXPORT LogoutCommand : public AbstractCommand { - Q_OBJECT public: - LogoutCommand(QObject *parent = 0); + typedef LogoutReply ReplyType; + + LogoutCommand(); Command rawCommand() const; }; +class ANIDBUDPCLIENTSHARED_EXPORT LogoutReply : public AbstractReply +{ + Q_OBJECT + REPLY_DEFINITION_HELPER(Logout) +public: +}; + } // namespace AniDBUdpClient #endif // LOGOUTCOMMAND_H diff --git a/mylistaddcommand.cpp b/mylistaddcommand.cpp index 583d08a..a22f0a6 100644 --- a/mylistaddcommand.cpp +++ b/mylistaddcommand.cpp @@ -11,14 +11,14 @@ namespace AniDBUdpClient { -MyListAddCommand::MyListAddCommand(int fid, bool edit, QObject *parent) : AbstractCommand(parent) +MyListAddCommand::MyListAddCommand(int fid, bool edit) : AbstractCommand() { init(); m_fid = fid; m_edit = edit; } -MyListAddCommand::MyListAddCommand(const QByteArray &ed2k, qint64 size, bool edit, QObject *parent) : AbstractCommand(parent) +MyListAddCommand::MyListAddCommand(const QByteArray &ed2k, qint64 size, bool edit) : AbstractCommand() { init(); m_ed2k = ed2k; @@ -26,7 +26,7 @@ MyListAddCommand::MyListAddCommand(const QByteArray &ed2k, qint64 size, bool edi m_edit = edit; } -MyListAddCommand::MyListAddCommand(int lid, QObject *parent) : AbstractCommand(parent) +MyListAddCommand::MyListAddCommand(int lid) : AbstractCommand() { init(); m_lid = lid; @@ -218,16 +218,32 @@ Command MyListAddCommand::rawCommand() const return cmd; } -void MyListAddCommand::setRawReply(ReplyCode replyCode, const QString &reply, Client *client) +void MyListAddCommand::init() +{ + m_fid = 0; + m_lid = 0; + + m_state = StateUnknown; + m_viewed = Unset; +} + +// === + +int MyListAddReply::lid() const +{ + return m_lid; +} + +void MyListAddReply::setRawReply(ReplyCode replyCode, const QString &reply) { - AbstractCommand::setRawReply(replyCode, reply, client); + AbstractReply::setRawReply(replyCode, reply); switch (replyCode) { case MYLIST_ENTRY_ADDED: { QString lid = reply.mid(reply.indexOf('\n')).trimmed(); - if (!m_edit) + if (!command().edit()) { m_lid = lid.toInt(); } @@ -248,175 +264,9 @@ void MyListAddCommand::setRawReply(ReplyCode replyCode, const QString &reply, Cl } } - -void MyListAddCommand::init() +void MyListAddReply::init() { - m_fid = 0; m_lid = 0; - - m_state = StateUnknown; - m_viewed = Unset; -} - - -// =================================================================================== - - -MylistAddCommand::MylistAddCommand(QString file, QObject *parent) : AbstractCommand(parent) -{ - m_file = file; - m_size = QFileInfo(file).size(); - mylistId = 0; - - connect(&futureWatcher, SIGNAL(finished()), this, SLOT(completeHash())); -} - -QString MylistAddCommand::file() const -{ - return m_file; -} - -QByteArray MylistAddCommand::ed2kHash() const -{ - return m_ed2k; -} - -qint64 MylistAddCommand::fileSize() const -{ - return m_size; -} - -bool MylistAddCommand::markWatched() const -{ - return m_markWatched; -} - -void MylistAddCommand::setMarkWatched(bool mark) -{ - m_markWatched = mark; -} - -bool MylistAddCommand::waitForResult() const -{ - return true; -} - -Command MylistAddCommand::rawCommand() const -{ - Command command; - switch (mylistId) - { - case 0: - command.first = "MYLIST"; - command.second["size"] = m_size; - command.second["ed2k"] = m_ed2k.constData(); - return command; - break; - case -1: - command.first = "MYLISTADD"; - command.second["size"] = m_size; - command.second["ed2k"] = m_ed2k.constData(); - command.second["state"] = 1; - command.second["viewed"] = m_markWatched; - return command; - break; - default: - command.first = "MYLISTADD"; - command.second["lid"] = mylistId; - command.second["viewed"] = m_markWatched; - command.second["edit"] = 1; - return command; - break; - } -} - -void MylistAddCommand::setRawReply(ReplyCode replyCode, const QString &reply, Client *client) -{ - AbstractCommand::setRawReply(replyCode, reply, client); - - switch (mylistId) - { - case 0: - switch(replyCode) - { - case MYLIST: - { - QString reply = m_rawReply.mid(m_rawReply.indexOf("\n")); - QStringList parts = reply.split('|', QString::KeepEmptyParts); -qDebug() << "PARTS" << parts; - mylistId = parts[0].toInt(); -qDebug() << "Mylist ID: " << mylistId; - if (!mylistId) - { -qDebug() << "FAILED to read Mylist ID"; - emit replyReady(false); - } - client->send(this); - } - break; - default: - mylistId = -1; - client->send(this); - break; - } - break; - default: - switch(replyCode) - { - case MYLIST_ENTRY_ADDED: - case MYLIST_ENTRY_EDITED: - case FILE_ALREADY_IN_MYLIST: - emit replyReady(true); - break; - default: - emit replyReady(false); - break; - } - break; - } -} - -void MylistAddCommand::hash() -{ - t.start(); - future = QtConcurrent::run(this, &MylistAddCommand::doHash, m_file); - futureWatcher.setFuture(future); -} - -void MylistAddCommand::completeHash() -{ - if (!future.isFinished()) - { -qDebug() << "WTF?"; - return; - } - m_ed2k = QByteArray(future); - emit hashComplete(); - qDebug() << "Time:" << t.elapsed(); -} - -QByteArray MylistAddCommand::doHash(QString file) -{ -qDebug() << "hash thread init"; - QThread::currentThread()->setPriority(QThread::LowPriority); - - QFile f(file); - if (!f.open(QIODevice::ReadOnly)) - return QByteArray(); - - QCryptographicHash ed2k(QCryptographicHash::Md4); - char *data = new char[ED2K_PART_SIZE]; - int size; - while (!f.atEnd()) - { - size = f.read(data, ED2K_PART_SIZE); - ed2k.addData(QCryptographicHash::hash(QByteArray(data, size), QCryptographicHash::Md4)); -//qDebug() << "hashing..."; - } - f.close(); - delete[] data; -qDebug() << "hashing... complete!"; - return ed2k.result().toHex(); } } // namespace AniDBUdpClient diff --git a/mylistaddcommand.h b/mylistaddcommand.h index ce737f6..0187487 100644 --- a/mylistaddcommand.h +++ b/mylistaddcommand.h @@ -9,12 +9,13 @@ namespace AniDBUdpClient { +class MyListAddReply; + class ANIDBUDPCLIENTSHARED_EXPORT MyListAddCommand : public AbstractCommand { +/* Q_ENUMS(ViewedState); - Q_OBJECT - Q_PROPERTY(int fid READ fid WRITE setFid); Q_PROPERTY(int lid READ lid WRITE setLid); @@ -29,18 +30,18 @@ class ANIDBUDPCLIENTSHARED_EXPORT MyListAddCommand : public AbstractCommand Q_PROPERTY(QString source READ source WRITE setSource); Q_PROPERTY(QString storage READ storage WRITE setStorage); Q_PROPERTY(QString other READ other WRITE setOther); - +*/ public: - + typedef MyListAddReply ReplyType; enum ViewedState { Unset = -1, NotViewed = 0, Viewed = 1, }; - MyListAddCommand(int fid, bool edit, QObject *parent = 0); - MyListAddCommand(const QByteArray &ed2k, qint64 size, bool edit, QObject *parent = 0); - explicit MyListAddCommand(int lid, QObject *parent = 0); + MyListAddCommand(int fid, bool edit); + MyListAddCommand(const QByteArray &ed2k, qint64 size, bool edit); + explicit MyListAddCommand(int lid); int fid() const; void setFid(int fid); @@ -76,13 +77,10 @@ public: QString other() const; void setOther(QString other); - bool waitForResult() const; Command rawCommand() const; - void setRawReply(ReplyCode replyCode, const QString &reply, Client *client); - private: void init(); @@ -102,51 +100,22 @@ private: QString m_other; }; -class ANIDBUDPCLIENTSHARED_EXPORT MylistAddCommand : public AbstractCommand +class ANIDBUDPCLIENTSHARED_EXPORT MyListAddReply : public AbstractReply { Q_OBJECT + REPLY_DEFINITION_HELPER2(MyListAdd) -public: - MylistAddCommand(QString file, QObject *parent = 0); - - QString file() const; - QByteArray ed2kHash() const; - qint64 fileSize() const; - - bool markWatched() const; - void setMarkWatched(bool mark); - + Q_PROPERTY(int lid READ lid); - bool waitForResult() const; - - Command rawCommand() const; - - void setRawReply(ReplyCode replyCode, const QString &reply, Client *client); - - void hash(); - -signals: - void hashComplete(); +public: + int lid() const; -private slots: - void completeHash(); + void setRawReply(ReplyCode replyCode, const QString &reply); private: - QByteArray doHash(QString file); - QFuture future; - QFutureWatcher futureWatcher; - - bool m_markWatched; - - qint64 m_size; - QByteArray m_ed2k; - QString m_file; - - int mylistId; - - static const qint64 ED2K_PART_SIZE = 9728000; + void init(); - QTime t; + int m_lid; }; } // namespace AniDBUdpClient diff --git a/mylistcommand.cpp b/mylistcommand.cpp index 4e1c566..a37bef1 100644 --- a/mylistcommand.cpp +++ b/mylistcommand.cpp @@ -1,319 +1,352 @@ -#include "mylistcommand.h" - -#include - -namespace AniDBUdpClient { - -MyListCommand::MyListCommand(QObject *parent) : AbstractCommand(parent) -{ - init(); -} - -MyListCommand::MyListCommand(int lid, QObject *parent) : AbstractCommand(parent) -{ - init(); - m_lid = lid; -} - -MyListCommand::MyListCommand(int fid, bool isFid, QObject *parent) : AbstractCommand(parent) -{ - Q_UNUSED(isFid); - init(); - m_fid = fid; -} - -MyListCommand::MyListCommand(const QByteArray &ed2k, qint64 size, QObject *parent) : AbstractCommand(parent) -{ - init(); - m_ed2k = ed2k; - m_size = size; -} - -MyListCommand::MyListCommand(const QString &aname, const QString &gname, int epno, QObject *parent) : AbstractCommand(parent) -{ - init(); - m_aname = aname; - m_gname = gname; - m_epno = epno; -} - -MyListCommand::MyListCommand(const QString &aname, int gid, int epno, QObject *parent) : AbstractCommand(parent) -{ - init(); - m_aname = aname; - m_gid = gid; - m_epno = epno; -} - -MyListCommand::MyListCommand(int aid, const QString &gname, int epno, QObject *parent) : AbstractCommand(parent) -{ - init(); - m_aid = aid; - m_gname = gname; - m_epno = epno; -} - -MyListCommand::MyListCommand(int aid, int gid, int epno, QObject *parent) : AbstractCommand(parent) -{ - init(); - m_aid = aid; - m_gid = gid; - m_epno = epno; -} - -int MyListCommand::lid() const -{ - return m_lid; -} - -void MyListCommand::setLid(int lid) -{ - m_lid = lid; -} - -int MyListCommand::fid() const -{ - return m_fid; -} - -void MyListCommand::setFid(int fid) -{ - m_fid = fid; -} - -QByteArray MyListCommand::ed2k() const -{ - return m_ed2k; -} - -void MyListCommand::setEd2k(const QByteArray &ed2k) -{ - m_ed2k = ed2k; -} - -qint64 MyListCommand::size() const -{ - return m_size; -} - -void MyListCommand::setSize(qint64 size) -{ - m_size = size; -} - -QString MyListCommand::aname() const -{ - return m_aname; -} - -void MyListCommand::setAname(const QString &aname) -{ - m_aname = aname; -} - -int MyListCommand::aid() const -{ - return m_aid; -} - -void MyListCommand::setAid(int aid) -{ - m_aid = aid; -} - -QString MyListCommand::gname() const -{ - return m_gname; -} - -void MyListCommand::setGname(const QString &gname) -{ - m_gname = gname; -} - -int MyListCommand::gid() const -{ - return m_gid; -} - -void MyListCommand::setGid(int gid) -{ - m_gid = gid; -} - - -int MyListCommand::epno() const -{ - return m_epno; -} - -void MyListCommand::setEpno(int epno) -{ - m_epno = epno; -} - -bool MyListCommand::isValid() const -{ - return m_lid || m_fid || m_aid - || !m_aname.isEmpty() - || (!m_ed2k.isEmpty() && m_size); -} - -int MyListCommand::eid() const -{ - return m_eid; -} - -QDateTime MyListCommand::date() const -{ - return m_date; -} - -State MyListCommand::state() const -{ - return m_state; -} - -QDateTime MyListCommand::viewDate() const -{ - return m_viewDate; -} - -QString MyListCommand::storage() const -{ - return m_storage; -} - -QString MyListCommand::source() const -{ - return m_source; -} - -QString MyListCommand::other() const -{ - return m_other; -} - -FileState MyListCommand::fileState() const -{ - return m_fileState; -} - -QStringList MyListCommand::multipleEntries() const -{ - return m_multipleEntries; -} - -bool MyListCommand::waitForResult() const -{ - return true; -} - -Command MyListCommand::rawCommand() const -{ - Command cmd; - - cmd.first = "MYLIST"; - - if (m_lid) - { - cmd.second["lid"] = m_lid; - } - else if (m_fid) - { - cmd.second["fid"] = m_fid; - } - else if (!m_ed2k.isEmpty() && m_size) - { - cmd.second["ed2k"] = m_ed2k; - cmd.second["size"] = m_size; - } - else if (!m_aname.isEmpty()) - { - cmd.second["aname"] = m_aname; - if (!m_gname.isEmpty() && m_epno) - { - cmd.second["gname"] = m_gname; - cmd.second["epno"] = m_epno; - } - else if (m_gid && m_epno) - { - cmd.second["gid"] = m_gid; - cmd.second["epno"] = m_epno; - } - } - else if (m_aid) - { - cmd.second["aid"] = m_aid; - if (!m_gname.isEmpty() && m_epno) - { - cmd.second["gname"] = m_gname; - cmd.second["epno"] = m_epno; - } - else if (m_gid && m_epno) - { - cmd.second["gid"] = m_gid; - cmd.second["epno"] = m_epno; - } - } - else - { - // TODO WTF NOW?!? - } - return cmd; -} - -void MyListCommand::setRawReply(ReplyCode replyCode, const QString &reply, Client *client) -{ - AbstractCommand::setRawReply(replyCode, reply, client); - - switch (replyCode) - { - case MYLIST: - { - QStringList parts = reply.mid(reply.indexOf("\n")).split('|', QString::KeepEmptyParts); - bool ok; - m_lid = parts[0].toInt(&ok, 10); - m_fid = parts[1].toInt(&ok, 10); - m_eid = parts[2].toInt(&ok, 10); - m_aid = parts[3].toInt(&ok, 10); - m_gid = parts[4].toInt(&ok, 10); - m_date = QDateTime::fromTime_t(parts[5].toUInt(&ok, 10)); - m_state = State(parts[6].toInt(&ok, 10)); - m_viewDate = QDateTime::fromTime_t(parts[7].toUInt(&ok, 10)); - m_storage = parts[8]; - m_source = parts[9]; - m_other = parts[10]; - m_fileState = FileState(parts[11].toInt(&ok, 10)); - emit replyReady(true); - } - break; - case MULTIPLE_MYLIST_ENTRIES: - { - m_multipleEntries = reply.mid(reply.indexOf("\n")).split('|', QString::KeepEmptyParts); - emit replyReady(true); - } - break; - case NO_SUCH_ENTRY: - default: - emit replyReady(false); - break; - } -} - -void MyListCommand::init() -{ - m_lid = 0; - m_fid = 0; - m_aid = 0; - m_gid = 0; - m_eid = 0; - - m_size = 0; - m_epno = 0; - - m_state = State(0); - m_fileState = FileState(0); -} - -} // namespace AniDBUdpClient +#include "mylistcommand.h" + +#include + +namespace AniDBUdpClient { + +MyListCommand::MyListCommand() : AbstractCommand() +{ + init(); +} + +MyListCommand::MyListCommand(int lid) : AbstractCommand() +{ + init(); + m_lid = lid; +} + +MyListCommand::MyListCommand(int fid, bool isFid) : AbstractCommand() +{ + Q_UNUSED(isFid); + init(); + m_fid = fid; +} + +MyListCommand::MyListCommand(const QByteArray &ed2k, qint64 size) : AbstractCommand() +{ + init(); + m_ed2k = ed2k; + m_size = size; +} + +MyListCommand::MyListCommand(const QString &aname, const QString &gname, int epno) : AbstractCommand() +{ + init(); + m_aname = aname; + m_gname = gname; + m_epno = epno; +} + +MyListCommand::MyListCommand(const QString &aname, int gid, int epno) : AbstractCommand() +{ + init(); + m_aname = aname; + m_gid = gid; + m_epno = epno; +} + +MyListCommand::MyListCommand(int aid, const QString &gname, int epno) : AbstractCommand() +{ + init(); + m_aid = aid; + m_gname = gname; + m_epno = epno; +} + +MyListCommand::MyListCommand(int aid, int gid, int epno) : AbstractCommand() +{ + init(); + m_aid = aid; + m_gid = gid; + m_epno = epno; +} + +int MyListCommand::lid() const +{ + return m_lid; +} + +void MyListCommand::setLid(int lid) +{ + m_lid = lid; +} + +int MyListCommand::fid() const +{ + return m_fid; +} + +void MyListCommand::setFid(int fid) +{ + m_fid = fid; +} + +QByteArray MyListCommand::ed2k() const +{ + return m_ed2k; +} + +void MyListCommand::setEd2k(const QByteArray &ed2k) +{ + m_ed2k = ed2k; +} + +qint64 MyListCommand::size() const +{ + return m_size; +} + +void MyListCommand::setSize(qint64 size) +{ + m_size = size; +} + +QString MyListCommand::aname() const +{ + return m_aname; +} + +void MyListCommand::setAname(const QString &aname) +{ + m_aname = aname; +} + +int MyListCommand::aid() const +{ + return m_aid; +} + +void MyListCommand::setAid(int aid) +{ + m_aid = aid; +} + +QString MyListCommand::gname() const +{ + return m_gname; +} + +void MyListCommand::setGname(const QString &gname) +{ + m_gname = gname; +} + +int MyListCommand::gid() const +{ + return m_gid; +} + +void MyListCommand::setGid(int gid) +{ + m_gid = gid; +} + + +int MyListCommand::epno() const +{ + return m_epno; +} + +void MyListCommand::setEpno(int epno) +{ + m_epno = epno; +} + +bool MyListCommand::isValid() const +{ + return m_lid || m_fid || m_aid + || !m_aname.isEmpty() + || (!m_ed2k.isEmpty() && m_size); +} + +bool MyListCommand::waitForResult() const +{ + return true; +} + +Command MyListCommand::rawCommand() const +{ + Command cmd; + + cmd.first = "MYLIST"; + + if (m_lid) + { + cmd.second["lid"] = m_lid; + } + else if (m_fid) + { + cmd.second["fid"] = m_fid; + } + else if (!m_ed2k.isEmpty() && m_size) + { + cmd.second["ed2k"] = m_ed2k; + cmd.second["size"] = m_size; + } + else if (!m_aname.isEmpty()) + { + cmd.second["aname"] = m_aname; + if (!m_gname.isEmpty() && m_epno) + { + cmd.second["gname"] = m_gname; + cmd.second["epno"] = m_epno; + } + else if (m_gid && m_epno) + { + cmd.second["gid"] = m_gid; + cmd.second["epno"] = m_epno; + } + } + else if (m_aid) + { + cmd.second["aid"] = m_aid; + if (!m_gname.isEmpty() && m_epno) + { + cmd.second["gname"] = m_gname; + cmd.second["epno"] = m_epno; + } + else if (m_gid && m_epno) + { + cmd.second["gid"] = m_gid; + cmd.second["epno"] = m_epno; + } + } + else + { + // TODO WTF NOW?!? + } + return cmd; +} + +void MyListCommand::init() +{ + m_lid = 0; + m_fid = 0; + m_aid = 0; + m_gid = 0; + + m_size = 0; + m_epno = 0; +} + + +// === + +int MyListReply::lid() const +{ + return m_lid; +} + +int MyListReply::fid() const +{ + return m_fid; +} + +int MyListReply::aid() const +{ + return m_aid; +} + + +int MyListReply::gid() const +{ + return m_gid; +} + + +int MyListReply::eid() const +{ + return m_eid; +} + +QDateTime MyListReply::date() const +{ + return m_date; +} + +State MyListReply::state() const +{ + return m_state; +} + +QDateTime MyListReply::viewDate() const +{ + return m_viewDate; +} + +QString MyListReply::storage() const +{ + return m_storage; +} + +QString MyListReply::source() const +{ + return m_source; +} + +QString MyListReply::other() const +{ + return m_other; +} + +FileState MyListReply::fileState() const +{ + return m_fileState; +} + +QStringList MyListReply::multipleEntries() const +{ + return m_multipleEntries; +} + +void MyListReply::setRawReply(ReplyCode replyCode, const QString &reply) +{ + AbstractReply::setRawReply(replyCode, reply); + + switch (replyCode) + { + case MYLIST: + { + QStringList parts = reply.mid(reply.indexOf("\n")).split('|', QString::KeepEmptyParts); + bool ok; + m_lid = parts[0].toInt(&ok, 10); + m_fid = parts[1].toInt(&ok, 10); + m_eid = parts[2].toInt(&ok, 10); + m_aid = parts[3].toInt(&ok, 10); + m_gid = parts[4].toInt(&ok, 10); + m_date = QDateTime::fromTime_t(parts[5].toUInt(&ok, 10)); + m_state = State(parts[6].toInt(&ok, 10)); + m_viewDate = QDateTime::fromTime_t(parts[7].toUInt(&ok, 10)); + m_storage = parts[8]; + m_source = parts[9]; + m_other = parts[10]; + m_fileState = FileState(parts[11].toInt(&ok, 10)); + emit replyReady(true); + } + break; + case MULTIPLE_MYLIST_ENTRIES: + { + m_multipleEntries = reply.mid(reply.indexOf("\n")).split('|', QString::KeepEmptyParts); + emit replyReady(true); + } + break; + case NO_SUCH_ENTRY: + default: + emit replyReady(false); + break; + } +} + +void MyListReply::init() +{ + m_lid = 0; + m_fid = 0; + m_aid = 0; + m_gid = 0; + m_eid = 0; + + m_state = State(0); + m_fileState = FileState(0); +} + +} // namespace AniDBUdpClient diff --git a/mylistcommand.h b/mylistcommand.h index 7d8487e..336b1bf 100644 --- a/mylistcommand.h +++ b/mylistcommand.h @@ -1,123 +1,140 @@ -#ifndef MYLISTCOMMAND_H -#define MYLISTCOMMAND_H - -#include "anidbudpclient_global.h" -#include "abstractcommand.h" - -#include -#include - -namespace AniDBUdpClient { - -class ANIDBUDPCLIENTSHARED_EXPORT MyListCommand : public AbstractCommand -{ - Q_OBJECT - - Q_PROPERTY(int lid READ lid WRITE setLid); - Q_PROPERTY(int fid READ fid WRITE setFid); - - Q_PROPERTY(QByteArray ed2k READ ed2k WRITE setEd2k); - Q_PROPERTY(qint64 size READ size WRITE setSize); - - Q_PROPERTY(QString aname READ aname WRITE setAname); - Q_PROPERTY(int aid READ aid WRITE setAid); - Q_PROPERTY(QString gname READ gname WRITE setGname); - Q_PROPERTY(int gid READ gid WRITE setGid); - Q_PROPERTY(int epno READ epno WRITE setEpno); - - Q_PROPERTY(bool valid READ isValid); - - Q_PROPERTY(int eid READ eid); - Q_PROPERTY(QDateTime date READ date); - Q_PROPERTY(State state READ state); - Q_PROPERTY(QDateTime viewDate READ viewDate); - Q_PROPERTY(QString storage READ storage); - Q_PROPERTY(QString source READ source); - Q_PROPERTY(QString other READ other); - Q_PROPERTY(FileState fileState READ fileState); - - Q_PROPERTY(QStringList multipleEntries READ multipleEntries); - -public: - MyListCommand(QObject *parent = 0); - MyListCommand(int lid, QObject *parent = 0); - MyListCommand(int fid, bool isFid, QObject *parent = 0); - MyListCommand(const QByteArray &ed2k, qint64 size, QObject *parent = 0); - MyListCommand(const QString &aname, const QString &gname, int epno, QObject *parent = 0); - MyListCommand(const QString &aname, int gid, int epno, QObject *parent = 0); - MyListCommand(int aid, const QString &gname, int epno, QObject *parent = 0); - MyListCommand(int aid, int gid, int epno, QObject *parent = 0); - - int lid() const; - void setLid(int lid); - int fid() const; - void setFid(int fid); - - QByteArray ed2k() const; - void setEd2k(const QByteArray &ed2k); - qint64 size() const; - void setSize(qint64 size); - - QString aname() const; - void setAname(const QString &aname); - int aid() const; - void setAid(int aid); - QString gname() const; - void setGname(const QString &gname); - int gid() const; - void setGid(int gid); - int epno() const; - void setEpno(int epno); - - bool isValid() const; - - - int eid() const; - QDateTime date() const; - State state() const; - QDateTime viewDate() const; - QString storage() const; - QString source() const; - QString other() const; - FileState fileState() const; - - QStringList multipleEntries() const; - - bool waitForResult() const; - Command rawCommand() const; - void setRawReply(ReplyCode replyCode, const QString &reply, Client *client); - -private: - void init(); - - int m_lid; - int m_fid; - - QByteArray m_ed2k; - qint64 m_size; - - QString m_aname; - int m_aid; - QString m_gname; - int m_gid; - int m_epno; - - int m_eid; - QDateTime m_date; - State m_state; - QDateTime m_viewDate; - QString m_storage; - QString m_source; - QString m_other; - FileState m_fileState; - - QStringList m_multipleEntries; - -}; - -} // namespace AniDBUdpClient - -#include -Q_SCRIPT_DECLARE_QMETAOBJECT(AniDBUdpClient::MyListCommand, QObject*); - -#endif // MYLISTCOMMAND_H +#ifndef MYLISTCOMMAND_H +#define MYLISTCOMMAND_H + +#include "anidbudpclient_global.h" +#include "abstractcommand.h" + +#include +#include + +namespace AniDBUdpClient { + +class MyListReply; + +class ANIDBUDPCLIENTSHARED_EXPORT MyListCommand : public AbstractCommand +{ +public: + typedef MyListReply ReplyType; + MyListCommand(); + MyListCommand(int lid); + MyListCommand(int fid, bool isFid); + MyListCommand(const QByteArray &ed2k, qint64 size); + MyListCommand(const QString &aname, const QString &gname, int epno); + MyListCommand(const QString &aname, int gid, int epno); + MyListCommand(int aid, const QString &gname, int epno); + MyListCommand(int aid, int gid, int epno); + + int lid() const; + void setLid(int lid); + int fid() const; + void setFid(int fid); + + QByteArray ed2k() const; + void setEd2k(const QByteArray &ed2k); + qint64 size() const; + void setSize(qint64 size); + + QString aname() const; + void setAname(const QString &aname); + int aid() const; + void setAid(int aid); + QString gname() const; + void setGname(const QString &gname); + int gid() const; + void setGid(int gid); + int epno() const; + void setEpno(int epno); + + bool isValid() const; + + bool waitForResult() const; + Command rawCommand() const; + void setRawReply(ReplyCode replyCode, const QString &reply, Client *client); + +private: + void init(); + + int m_lid; + int m_fid; + + QByteArray m_ed2k; + qint64 m_size; + + QString m_aname; + int m_aid; + QString m_gname; + int m_gid; + int m_epno; + +}; + +class ANIDBUDPCLIENTSHARED_EXPORT MyListReply : public AbstractReply +{ + Q_OBJECT + REPLY_DEFINITION_HELPER2(MyList) + + Q_PROPERTY(int lid READ lid); + Q_PROPERTY(int fid READ fid); + + Q_PROPERTY(int aid READ aid); + Q_PROPERTY(int gid READ gid); + + Q_PROPERTY(int eid READ eid); + Q_PROPERTY(QDateTime date READ date); + Q_PROPERTY(State state READ state); + Q_PROPERTY(QDateTime viewDate READ viewDate); + Q_PROPERTY(QString storage READ storage); + Q_PROPERTY(QString source READ source); + Q_PROPERTY(QString other READ other); + Q_PROPERTY(FileState fileState READ fileState); + + Q_PROPERTY(QStringList multipleEntries READ multipleEntries); + +public: + + int lid() const; + int fid() const; + + int aid() const; + int gid() const; + + + int eid() const; + QDateTime date() const; + State state() const; + QDateTime viewDate() const; + QString storage() const; + QString source() const; + QString other() const; + FileState fileState() const; + + QStringList multipleEntries() const; + + void setRawReply(ReplyCode replyCode, const QString &reply); + +private: + void init(); + + int m_lid; + int m_fid; + + int m_aid; + int m_gid; + int m_epno; + + int m_eid; + QDateTime m_date; + State m_state; + QDateTime m_viewDate; + QString m_storage; + QString m_source; + QString m_other; + FileState m_fileState; + + QStringList m_multipleEntries; + +}; + +} // namespace AniDBUdpClient + +#endif // MYLISTCOMMAND_H diff --git a/rawcommand.cpp b/rawcommand.cpp index 982ac87..50f7660 100644 --- a/rawcommand.cpp +++ b/rawcommand.cpp @@ -2,7 +2,7 @@ namespace AniDBUdpClient { -RawCommand::RawCommand(const QString &command, QObject *parent) : AbstractCommand(parent) +RawCommand::RawCommand(const QString &command) : AbstractCommand() { m_command = command; } diff --git a/rawcommand.h b/rawcommand.h index fb18ab4..0795cc5 100644 --- a/rawcommand.h +++ b/rawcommand.h @@ -5,12 +5,13 @@ namespace AniDBUdpClient { +class RawReply; + class ANIDBUDPCLIENTSHARED_EXPORT RawCommand : public AbstractCommand { - Q_OBJECT - public: - RawCommand(const QString &command, QObject *parent = 0); + typedef RawReply ReplyType; + RawCommand(const QString &command); Command rawCommand() const; @@ -18,6 +19,13 @@ private: QString m_command; }; +class ANIDBUDPCLIENTSHARED_EXPORT RawReply : public AbstractReply +{ + Q_OBJECT + REPLY_DEFINITION_HELPER(Raw) +public: +}; + } // namespace AniDBUdpClient #endif // RAWCOMMAND_H diff --git a/uptimecommand.cpp b/uptimecommand.cpp index 503a7a3..b4c05f6 100644 --- a/uptimecommand.cpp +++ b/uptimecommand.cpp @@ -2,14 +2,8 @@ namespace AniDBUdpClient { -UptimeCommand::UptimeCommand(QObject *parent) : AbstractCommand(parent) +UptimeCommand::UptimeCommand() : AbstractCommand() { - m_uptime = 0; -} - -int UptimeCommand::uptime() -{ - return m_uptime; } Command UptimeCommand::rawCommand() const @@ -19,7 +13,14 @@ Command UptimeCommand::rawCommand() const return command; } -void UptimeCommand::setRawReply(ReplyCode replyCode, const QString &reply, Client *client) +// == + +int UptimeReply::uptime() +{ + return m_uptime; +} + +void UptimeReply::setRawReply(ReplyCode replyCode, const QString &reply, Client *client) { Q_UNUSED(client); @@ -42,4 +43,9 @@ void UptimeCommand::setRawReply(ReplyCode replyCode, const QString &reply, Clien } } +void UptimeReply::init() +{ + m_uptime = 0; +} + } // namespace AniDBUdpClient diff --git a/uptimecommand.h b/uptimecommand.h index b71b88f..7b3aaa2 100644 --- a/uptimecommand.h +++ b/uptimecommand.h @@ -5,26 +5,35 @@ namespace AniDBUdpClient { +class UptimeReply; + class ANIDBUDPCLIENTSHARED_EXPORT UptimeCommand : public AbstractCommand +{ +public: + typedef UptimeReply ReplyType; + UptimeCommand(); + + Command rawCommand() const; +}; + +class ANIDBUDPCLIENTSHARED_EXPORT UptimeReply : public AbstractReply { Q_OBJECT + REPLY_DEFINITION_HELPER2(Uptime) Q_PROPERTY(int uptime READ uptime); public: - UptimeCommand(QObject *parent = 0); int uptime(); - Command rawCommand() const; void setRawReply(ReplyCode replyCode, const QString &reply, Client *client); private: + void init(); int m_uptime; }; -} // namespace AniDBUdpClient -#include -Q_SCRIPT_DECLARE_QMETAOBJECT(AniDBUdpClient::UptimeCommand, QObject*); +} // namespace AniDBUdpClient #endif // UPTIMECOMMAND_H -- 2.52.0