myliststate.cpp \
episodecommand.cpp \
animecommand.cpp \
- mylistexportcommand.cpp
+ mylistexportcommand.cpp \
+ updatecommand.cpp
HEADERS += client.h \
anidbudpclient_global.h \
myliststate.h \
episodecommand.h \
animecommand.h \
- mylistexportcommand.h
+ mylistexportcommand.h \
+ updatecommand.h
CONV_HEADERS += include/AniDBUdpClient/Client \
include/AniDBUdpClient/AbstractCommand \
include/AniDBUdpClient/EpisodeCommand \
include/AniDBUdpClient/FileCommand \
include/AniDBUdpClient/VoteCommand \
+ include/AniDBUdpClient/UpdateCommand \
include/AniDBUdpClient/UptimeCommand \
include/AniDBUdpClient/MyListExportCommand \
include/AniDBUdpClient/File \
--- /dev/null
+#include "updatecommand.h"
+
+#include <QDebug>
+
+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<int> 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
--- /dev/null
+#ifndef UPDATECOMMAND_H
+#define UPDATECOMMAND_H
+
+#include <QDateTime>
+#include <QList>
+
+#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<int> aid READ aid)
+
+public:
+ int entity() const;
+ int totalCount() const;
+ QDateTime lastUpdateDate() const;
+ QList<int> aid() const;
+
+ void setRawReply(ReplyCode replyCode, const QString &reply);
+
+private:
+ int m_entity;
+ int m_totalCount;
+ QDateTime m_lastUpdateDate;
+ QList<int> m_aid;
+};
+
+} // namespace AniDBUdpClient
+
+#endif // UPDATECOMMAND_H