From c1740bbe6ece49e266b6d5f04934c1d1c9f45e60 Mon Sep 17 00:00:00 2001 From: APTX Date: Tue, 22 May 2012 22:17:11 +0200 Subject: [PATCH] Actually implement reply parsing in VoteReply. --- votecommand.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ votecommand.h | 14 +++++++-- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/votecommand.cpp b/votecommand.cpp index 261eaf5..8e9cc50 100644 --- a/votecommand.cpp +++ b/votecommand.cpp @@ -1,5 +1,7 @@ #include "votecommand.h" +#include + 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() diff --git a/votecommand.h b/votecommand.h index 6ed6dbb..cfed933 100644 --- a/votecommand.h +++ b/votecommand.h @@ -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; - }; -- 2.52.0