From: APTX Date: Sat, 23 Jan 2016 14:30:27 +0000 (+0100) Subject: Add Update Command. X-Git-Url: https://gitweb.aptx.org/?a=commitdiff_plain;h=67fbe39b4f2ece0964c6e47e31011527555e3d59;p=anidbudpclient.git Add Update Command. --- diff --git a/anidbudpclient.pro b/anidbudpclient.pro index a4e13dc..9c82ca0 100644 --- a/anidbudpclient.pro +++ b/anidbudpclient.pro @@ -42,7 +42,8 @@ SOURCES += client.cpp \ myliststate.cpp \ episodecommand.cpp \ animecommand.cpp \ - mylistexportcommand.cpp + mylistexportcommand.cpp \ + updatecommand.cpp HEADERS += client.h \ anidbudpclient_global.h \ @@ -69,7 +70,8 @@ HEADERS += client.h \ myliststate.h \ episodecommand.h \ animecommand.h \ - mylistexportcommand.h + mylistexportcommand.h \ + updatecommand.h CONV_HEADERS += include/AniDBUdpClient/Client \ include/AniDBUdpClient/AbstractCommand \ @@ -81,6 +83,7 @@ CONV_HEADERS += include/AniDBUdpClient/Client \ include/AniDBUdpClient/EpisodeCommand \ include/AniDBUdpClient/FileCommand \ include/AniDBUdpClient/VoteCommand \ + include/AniDBUdpClient/UpdateCommand \ include/AniDBUdpClient/UptimeCommand \ include/AniDBUdpClient/MyListExportCommand \ include/AniDBUdpClient/File \ diff --git a/anidbudpclient_global.h b/anidbudpclient_global.h index 8499c95..30e81c3 100644 --- a/anidbudpclient_global.h +++ b/anidbudpclient_global.h @@ -151,6 +151,7 @@ namespace AniDBUdpClient NO_SUCH_EPISODE = 340, NO_SUCH_UPDATES = 343, NO_SUCH_TITLES = 344, + NO_UPDATES = 343, NO_SUCH_CREATOR = 345, NO_SUCH_GROUP = 350, NO_SUCH_CATEGORY = 351, diff --git a/updatecommand.cpp b/updatecommand.cpp new file mode 100644 index 0000000..276fa5c --- /dev/null +++ b/updatecommand.cpp @@ -0,0 +1,133 @@ +#include "updatecommand.h" + +#include + +namespace AniDBUdpClient { + +UpdateCommand::UpdateCommand() +{ + +} + +int UpdateCommand::entity() const +{ + return m_entity; +} + +int UpdateCommand::age() const +{ + return m_age; +} + +void UpdateCommand::setAge(int age) +{ + m_age = age; +} + +QDateTime UpdateCommand::time() const +{ + return m_time; +} + +void UpdateCommand::setTime(QDateTime time) +{ + m_time = time; +} + +bool UpdateCommand::isValid() const +{ + if (!m_age && !m_time.isValid()) + return false; + + if (m_age && m_time.isValid()) + return false; + + if (m_age < 0) + return false; + + if (m_time.isValid() && m_time.toUTC() >= QDateTime::currentDateTimeUtc()) + return false; + + return true; +} + +Command UpdateCommand::rawCommand() const +{ + Command cmd; + cmd.first = "UPDATED"; + cmd.second["entity"] = m_entity; + if (m_age) + { + cmd.second["entity"] = m_age; + } + else if (m_time.isValid()) + { + cmd.second["time"] = QString::number(m_time.toUTC().toMSecsSinceEpoch() / 1000); + } + return cmd; +} + +int UpdateReply::entity() const +{ + return m_entity; +} + +int UpdateReply::totalCount() const +{ + return m_totalCount; +} + +QDateTime UpdateReply::lastUpdateDate() const +{ + return m_lastUpdateDate; +} + +QList UpdateReply::aid() const +{ + return m_aid; +} + +void UpdateReply::setRawReply(ReplyCode replyCode, const QString &reply) +{ + AbstractReply::setRawReply(replyCode, reply); + switch (replyCode) + { + case UPDATED: + { + QString d = reply.mid(reply.indexOf('\n')).trimmed(); + QStringList parts = d.split('|', QString::KeepEmptyParts); + + if (parts.count() < 4) + { + qWarning() << "Not enough parts in reply."; + return; + } + + bool ok; + m_entity = parts[0].toInt(&ok, 10); + m_totalCount = parts[1].toInt(&ok); + m_lastUpdateDate = QDateTime::fromMSecsSinceEpoch(1000 * QString(parts[2]).toLongLong(&ok)); + QStringList aidList = parts[3].split(','); + m_aid.clear(); + m_aid.reserve(aidList.count()); + for (auto aid : aidList) + { + m_aid << aid.toInt(&ok); + } + replyReady(true); + } + break; + case NO_UPDATES: + m_entity = 1; + m_totalCount = 0; + m_lastUpdateDate = QDateTime{}; + m_aid.clear(); + replyReady(true); + break; + default: + replyReady(false); + break; + } +} + +} // namespace AniDBUdpClient diff --git a/updatecommand.h b/updatecommand.h new file mode 100644 index 0000000..63f0b64 --- /dev/null +++ b/updatecommand.h @@ -0,0 +1,64 @@ +#ifndef UPDATECOMMAND_H +#define UPDATECOMMAND_H + +#include +#include + +#include "abstractcommand.h" + +namespace AniDBUdpClient { + +class ANIDBUDPCLIENTSHARED_EXPORT UpdateReply; + +class ANIDBUDPCLIENTSHARED_EXPORT UpdateCommand : public AbstractCommand +{ +public: + typedef UpdateReply ReplyType; + UpdateCommand(); + explicit UpdateCommand(QDateTime time); + + int entity() const; + + int age() const; + void setAge(int age); + + QDateTime time() const; + void setTime(QDateTime time); + + bool isValid() const override; + Command rawCommand() const override; + +private: + int m_entity = 0; + int m_age = 0; + QDateTime m_time; +}; + +class ANIDBUDPCLIENTSHARED_EXPORT UpdateReply : public AbstractReply +{ + Q_OBJECT + REPLY_DEFINITION_HELPER(Update) + + Q_PROPERTY(int entity READ entity) + Q_PROPERTY(int totalCount READ totalCount) + Q_PROPERTY(QDateTime lastUpdateDate READ lastUpdateDate) + Q_PROPERTY(QList aid READ aid) + +public: + int entity() const; + int totalCount() const; + QDateTime lastUpdateDate() const; + QList aid() const; + + void setRawReply(ReplyCode replyCode, const QString &reply); + +private: + int m_entity; + int m_totalCount; + QDateTime m_lastUpdateDate; + QList m_aid; +}; + +} // namespace AniDBUdpClient + +#endif // UPDATECOMMAND_H