]> Some of my projects - anidbudpclient.git/commitdiff
Actually implement reply parsing in VoteReply.
authorAPTX <marek321@gmail.com>
Tue, 22 May 2012 20:17:11 +0000 (22:17 +0200)
committerAPTX <marek321@gmail.com>
Tue, 22 May 2012 20:17:11 +0000 (22:17 +0200)
votecommand.cpp
votecommand.h

index 261eaf5e4f973a2eb7039fa4ebc4c9cab036b0fd..8e9cc50b5d3fb09802fcd691a26e5d8c124721b8 100644 (file)
@@ -1,5 +1,7 @@
 #include "votecommand.h"
 
+#include <QStringList>
+
 namespace AniDBUdpClient {
 
 VoteCommand::VoteCommand()
@@ -25,6 +27,42 @@ VoteCommand::VoteCommand(VoteType voteType, const QString &name, int value, int
        m_epno = epno;
 }
 
+VoteCommand::VoteCommand(VoteType voteType, int id, VoteAction action, int epno)
+{
+       init();
+       m_voteType = voteType;
+       m_id = id;
+       m_value = valueForAction(action);
+       m_epno = epno;
+}
+
+VoteCommand::VoteCommand(VoteType voteType, const QString &name, VoteAction action, int epno)
+{
+       init();
+       m_voteType = voteType;
+       m_name = name;
+       m_value = valueForAction(action);
+       m_epno = epno;
+}
+
+VoteCommand::VoteCommand(VoteType voteType, int id, double vote, int epno)
+{
+       init();
+       m_voteType = voteType;
+       m_id = id;
+       m_value = int(vote * 1000);
+       m_epno = epno;
+}
+
+VoteCommand::VoteCommand(VoteType voteType, const QString &name, double vote, int epno)
+{
+       init();
+       m_voteType = voteType;
+       m_name = name;
+       m_value = int(vote * 1000);
+       m_epno = epno;
+}
+
 VoteCommand::VoteType VoteCommand::voteType() const
 {
        return m_voteType;
@@ -126,6 +164,18 @@ void VoteCommand::init()
        m_epno = 0;
 }
 
+int VoteCommand::valueForAction(VoteAction action)
+{
+       switch (action)
+       {
+               case Revoke:
+                       return -1;
+               case Retrieve:
+               default:
+                       return 0;
+       }
+}
+
 // ===
 
 QString VoteReply::entityName() const
@@ -151,6 +201,40 @@ int VoteReply::entityId() const
 void VoteReply::setRawReply(ReplyCode replyCode, const QString &reply)
 {
        AbstractReply::setRawReply(replyCode, reply);
+
+       switch (replyCode)
+       {
+               case VOTED:
+               case VOTE_FOUND:
+               case VOTE_UPDATED:
+               // True as well because the vote is 0
+               case NO_SUCH_VOTE:
+                       if (readReplyData(reply))
+                               signalReplyReady(true);
+               break;
+               case PERMVOTE_NOT_ALLOWED:
+               case ALREADY_PERMVOTED:
+               default:
+                       signalReplyReady(false);
+       }
+}
+
+bool VoteReply::readReplyData(const QString &reply)
+{
+       QString d = reply.mid(reply.indexOf('\n')).trimmed();
+       QStringList parts = d.split('|', QString::KeepEmptyParts);
+
+       if (parts.count() < 4)
+       {
+               signalReplyReady(false);
+               return false;
+       }
+
+       m_entityName = parts[0];
+       m_value = parts[1].toInt();
+       m_voteType = VoteCommand::VoteType(parts[2].toInt());
+       m_entityId = parts[3].toInt();
+       return true;
 }
 
 void VoteReply::init()
index 6ed6dbb9fc9f75986ff3ed8ea60cf9001ff5af1d..cfed933866bc41448079adaa2f82f5605dd1d263 100644 (file)
@@ -19,11 +19,20 @@ public:
                GroupVote               = 3,
        };
 
+       enum VoteAction {
+               Retrieve,
+               Revoke
+       };
+
        typedef VoteReply ReplyType;
 
        VoteCommand();
        VoteCommand(VoteType type, int id, int value, int epno = 0);
        VoteCommand(VoteType type, const QString &name, int value, int epno = 0);
+       VoteCommand(VoteType type, int id, VoteAction action = Retrieve, int epno = 0);
+       VoteCommand(VoteType type, const QString &name, VoteAction action = Retrieve, int epno = 0);
+       VoteCommand(VoteType type, int id, double vote, int epno = 0);
+       VoteCommand(VoteType type, const QString &name, double vote, int epno = 0);
 
        VoteType voteType() const;
        void setVoteType(VoteType voteType);
@@ -46,6 +55,7 @@ public:
 
 private:
        void init();
+       int valueForAction(VoteAction action);
 
        VoteType m_voteType;
        int m_value;
@@ -68,19 +78,19 @@ class ANIDBUDPCLIENTSHARED_EXPORT VoteReply : public AbstractReply
 public:
        QString entityName() const;
        int value() const;
-       AniDBUdpClient::VoteCommand::VoteType voteType() const;
+       VoteCommand::VoteType voteType() const;
        int entityId() const;
 
        void setRawReply(ReplyCode replyCode, const QString &reply);
 
 private:
+       bool readReplyData(const QString &reply);
        void init();
 
        QString m_entityName;
        int m_value;
        VoteCommand::VoteType m_voteType;
        int m_entityId;
-
 };