]> Some of my projects - anidbudpclient.git/commitdiff
Add Update Command.
authorAPTX <marek321@gmail.com>
Sat, 23 Jan 2016 14:30:27 +0000 (15:30 +0100)
committerAPTX <marek321@gmail.com>
Sat, 23 Jan 2016 14:30:27 +0000 (15:30 +0100)
anidbudpclient.pro
anidbudpclient_global.h
updatecommand.cpp [new file with mode: 0644]
updatecommand.h [new file with mode: 0644]

index a4e13dc67161f54da5483f0d4cea74ba1049bf25..9c82ca047af87c5bb63c94c476d250e711911b0d 100644 (file)
@@ -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 \
index 8499c9547fe938059c85881929db3825ef4faec9..30e81c39ea82cc86056c900313b070645ae6b7a4 100644 (file)
@@ -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 (file)
index 0000000..276fa5c
--- /dev/null
@@ -0,0 +1,133 @@
+#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
diff --git a/updatecommand.h b/updatecommand.h
new file mode 100644 (file)
index 0000000..63f0b64
--- /dev/null
@@ -0,0 +1,64 @@
+#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