#include "votecommand.h"
+#include <QStringList>
+
namespace AniDBUdpClient {
VoteCommand::VoteCommand()
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;
m_epno = 0;
}
+int VoteCommand::valueForAction(VoteAction action)
+{
+ switch (action)
+ {
+ case Revoke:
+ return -1;
+ case Retrieve:
+ default:
+ return 0;
+ }
+}
+
// ===
QString VoteReply::entityName() 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()
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);
private:
void init();
+ int valueForAction(VoteAction action);
VoteType m_voteType;
int m_value;
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;
-
};