From 11a084deb042f2142f234755cf40c31c87999e4a Mon Sep 17 00:00:00 2001 From: HanzZ Date: Tue, 4 Dec 2012 19:21:59 +0100 Subject: [PATCH] Update away state regularly --- backends/libcommuni/session.cpp | 36 ++++++++++++++++++++++++++++----- backends/libcommuni/session.h | 20 ++++++++++++++++-- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/backends/libcommuni/session.cpp b/backends/libcommuni/session.cpp index 4c1c8dfa..6013cfc2 100644 --- a/backends/libcommuni/session.cpp +++ b/backends/libcommuni/session.cpp @@ -36,6 +36,14 @@ MyIrcSession::MyIrcSession(const std::string &user, IRCNetworkPlugin *np, const connect(this, SIGNAL(socketError(QAbstractSocket::SocketError)), SLOT(on_socketError(QAbstractSocket::SocketError))); connect(this, SIGNAL(connected()), SLOT(on_connected())); connect(this, SIGNAL(messageReceived(IrcMessage*)), this, SLOT(onMessageReceived(IrcMessage*))); + + m_awayTimer = new QTimer(this); + connect(m_awayTimer, SIGNAL(timeout()), this, SLOT(awayTimeout())); + m_awayTimer->start(10*1000); +} + +MyIrcSession::~MyIrcSession() { + delete m_awayTimer; } void MyIrcSession::on_connected() { @@ -210,14 +218,23 @@ void MyIrcSession::on_numericMessageReceived(IrcMessage *message) { } np->handleSubject(user, TO_UTF8(m->parameters().value(1)) + suffix, m_topicData, nick); break; - case 352: + case 352: { + channel = m->parameters().value(1); + nick = TO_UTF8(m->parameters().value(5)); + IRCBuddy &buddy = getIRCBuddy(TO_UTF8(channel), nick); + if (m->parameters().value(6).toUpper().startsWith("G")) { - channel = m->parameters().value(1); - nick = TO_UTF8(m->parameters().value(5)); - IRCBuddy &buddy = getIRCBuddy(TO_UTF8(channel), nick); - np->handleParticipantChanged(user, nick, TO_UTF8(channel) + suffix, buddy.isOp(), pbnetwork::STATUS_AWAY); + if (!buddy.isAway()) { + buddy.setAway(true); + np->handleParticipantChanged(user, nick, TO_UTF8(channel) + suffix, buddy.isOp(), pbnetwork::STATUS_AWAY); + } + } + else if (buddy.isAway()) { + buddy.setAway(false); + np->handleParticipantChanged(user, nick, TO_UTF8(channel) + suffix, buddy.isOp(), pbnetwork::STATUS_ONLINE); } break; + } case 353: channel = m->parameters().value(2); members = m->parameters().value(3).split(" "); @@ -260,6 +277,15 @@ void MyIrcSession::on_numericMessageReceived(IrcMessage *message) { //qDebug() << "numeric message received:" << receiver() << origin << code << params; } +void MyIrcSession::awayTimeout() { + for(AutoJoinMap::iterator it = m_autoJoin.begin(); it != m_autoJoin.end(); it++) { + if (it->second->shouldAskWho()) { + LOG4CXX_INFO(logger, "The time has come. Asking /who " << it->second->getChannel() << " again to get current away states."); + sendCommand(IrcCommand::createWho(FROM_UTF8(it->second->getChannel()))); + } + } +} + void MyIrcSession::onMessageReceived(IrcMessage *message) { LOG4CXX_INFO(logger, user << ": " << TO_UTF8(message->toString())); switch (message->type()) { diff --git a/backends/libcommuni/session.h b/backends/libcommuni/session.h index 2e65f7a6..0d819cb0 100644 --- a/backends/libcommuni/session.h +++ b/backends/libcommuni/session.h @@ -15,6 +15,7 @@ #include #include "Swiften/Swiften.h" #include +#include using namespace Transport; @@ -27,14 +28,26 @@ class MyIrcSession : public IrcSession public: class AutoJoinChannel { public: - AutoJoinChannel(const std::string &channel = "", const std::string &password = "") : m_channel(channel), m_password(password) {} + AutoJoinChannel(const std::string &channel = "", const std::string &password = "", int awayCycle = 12) : m_channel(channel), m_password(password), + m_awayCycle(awayCycle), m_currentAwayTick(0) {} virtual ~AutoJoinChannel() {} const std::string &getChannel() { return m_channel; } const std::string &getPassword() { return m_password; } + bool shouldAskWho() { + if (m_currentAwayTick == m_awayCycle) { + m_currentAwayTick = 0; + return true; + } + m_currentAwayTick++; + return false; + } + private: std::string m_channel; std::string m_password; + int m_awayCycle; + int m_currentAwayTick; }; class IRCBuddy { @@ -55,11 +68,12 @@ public: typedef std::map > IRCBuddyMap; MyIrcSession(const std::string &user, IRCNetworkPlugin *np, const std::string &suffix = "", QObject* parent = 0); + virtual ~MyIrcSession(); std::string suffix; int rooms; void addAutoJoinChannel(const std::string &channel, const std::string &password) { - m_autoJoin[channel] = boost::make_shared(channel, password); + m_autoJoin[channel] = boost::make_shared(channel, password, 12 + m_autoJoin.size()); } void removeAutoJoinChannel(const std::string &channel) { @@ -104,6 +118,7 @@ protected Q_SLOTS: void on_socketError(QAbstractSocket::SocketError error); void onMessageReceived(IrcMessage* message); + void awayTimeout(); protected: IRCNetworkPlugin *np; @@ -115,6 +130,7 @@ protected: std::list m_rooms; std::list m_names; IRCBuddyMap m_buddies; + QTimer *m_awayTimer; }; #endif // SESSION_H