From kde-commits Sun May 31 22:42:36 2009 From: Roman Jarosz Date: Sun, 31 May 2009 22:42:36 +0000 To: kde-commits Subject: KDE/kdenetwork/kopete/protocols/wlm Message-Id: <1243809756.049194.15844.nullmailer () svn ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=124386172727517 SVN commit 976115 by rjarosz: If we don't get emoticons in 5 seconds show the pending message otherwise we could wait forever. There's also a bug in libmsn which will be fixed in libmsn 4.0 beta6. BUG: 181026 M +90 -78 wlmchatmanager.cpp M +16 -2 wlmchatmanager.h --- trunk/KDE/kdenetwork/kopete/protocols/wlm/wlmchatmanager.cpp #976114:976115 @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -50,9 +51,11 @@ #include "wlmprotocol.h" #include "wlmaccount.h" +const int EmoticonsTimeoutCheckInterval = 2; //seconds +const int EmoticonsTimeoutThreshold = 5; //seconds WlmChatManager::WlmChatManager (WlmAccount * account1): -m_account (account1) +m_account (account1), m_emoticonsTimeoutTimerId(0) { QObject::connect (&account ()->server ()->cb, SIGNAL (messageReceived @@ -356,37 +359,12 @@ newMessage->setForegroundColor (message.foregroundColor ()); newMessage->setDirection (Kopete::Message::Inbound); - // stolen from msn plugin - QMap::ConstIterator it = chat->emoticonsList.constBegin(); - for(;it!=chat->emoticonsList.constEnd(); ++it) + if (!fillEmoticons(chat, newMessage)) { - QString es=Qt::escape(it.key()); - QString message1=newMessage->escapedBody(); - if(message1.contains(es)) - { - QFile f(it.value()); - if(!f.exists()) - { - // an emoticon is still missing, so wait for it - QLinkedList msgs = pendingMessages[conn]; - newMessage->setHtmlBody(message1); - msgs.append(newMessage); - pendingMessages[conn] = msgs; - return; - } - QImage iconImage = QImageReader(it.value()).read(); - - message1.replace( QRegExp(QString::fromLatin1("%1(?![^><]*>)").arg(QRegExp::escape(es))), - QString::fromLatin1("\"")" ) ); - newMessage->setHtmlBody(message1); - } + pendingMessages[conn].append(PendingMessage(newMessage)); + if (m_emoticonsTimeoutTimerId == 0) + m_emoticonsTimeoutTimerId = startTimer(EmoticonsTimeoutCheckInterval * 1000); + return; } // Add it to the manager chat->appendMessage (*newMessage); @@ -572,7 +550,6 @@ // track display pictures by SHA1D field QString SHA1D = xmlobj.documentElement ().attribute ("SHA1D"); - if (SHA1D.isEmpty ()) return; @@ -608,58 +585,23 @@ chat->emoticonsList[alias] = file; - if(pendingMessages[conn].isEmpty()) + if(pendingMessages.value(conn).isEmpty()) return; - // duplicate to avoid crashes when removing items in the next loop - QLinkedList pendingMessages1 = pendingMessages[conn]; - - QLinkedList::iterator it = pendingMessages1.begin(); - for(;it!=pendingMessages1.end(); ++it) + QMutableLinkedListIterator it(pendingMessages[conn]); + while (it.hasNext()) { - Kopete::Message *message = (*it); - QMap::iterator it2 = chat->emoticonsList.begin(); - bool ok = true; - // for each emoticon in our list - for(;it2!=chat->emoticonsList.end(); ++it2) + PendingMessage pendingMsg = it.next(); + if (fillEmoticons(chat, pendingMsg.message)) { - QString es=Qt::escape(it2.key()); - QString message1=message->escapedBody(); - if(message1.contains(es)) - { - QFile f(it2.value()); - if(!f.exists()) - { - // an emoticon is still missing, so wait for it - QLinkedList pmsgs = pendingMessages[conn]; - message->setHtmlBody(message1); - pmsgs.removeOne((*it)); - pmsgs.append((*it)); - pendingMessages[conn] = pmsgs; - ok = false; - break; - } - QImage iconImage = QImageReader(it2.value()).read(); - - message1.replace( QRegExp(QString::fromLatin1("%1(?![^><]*>)").arg(QRegExp::escape(es))), - QString::fromLatin1("\"")" ) ); - message->setHtmlBody(message1); - } + chat->appendMessage(*pendingMsg.message); + it.remove(); + delete pendingMsg.message; } - if(ok) - { - chatSessions[conn]->appendMessage (*message); - pendingMessages[conn].removeOne(message); - delete message; - } } + + if (pendingMessages.value(conn).isEmpty()) + pendingMessages.remove(conn); } void @@ -672,4 +614,74 @@ Q_UNUSED( file ); } +void WlmChatManager::timerEvent(QTimerEvent *event) +{ + if (m_emoticonsTimeoutTimerId == event->timerId() ) + { + QTime thresholdTime = QTime::currentTime().addSecs(-EmoticonsTimeoutThreshold); + QMutableMapIterator > connIt(pendingMessages); + while (connIt.hasNext()) + { + connIt.next(); + + QMutableLinkedListIterator it(connIt.value()); + while (it.hasNext()) + { + PendingMessage pendingMsg = it.next(); + if (pendingMsg.receiveTime < thresholdTime) + { + kDebug(14210) << "Did not get emoticons in time!"; + WlmChatSession *chat = chatSessions[connIt.key()]; + if (chat) + chat->appendMessage(*pendingMsg.message); + + it.remove(); + delete pendingMsg.message; + } + } + if (connIt.value().isEmpty()) + connIt.remove(); + } + + if (pendingMessages.isEmpty()) + { + killTimer(m_emoticonsTimeoutTimerId); + m_emoticonsTimeoutTimerId = 0; + } + } +} + +bool WlmChatManager::fillEmoticons(WlmChatSession *chat, Kopete::Message* message) +{ + QString escapedMessage = message->escapedBody(); + + // for each emoticon in our list + QMap::iterator it2 = chat->emoticonsList.begin(); + for (;it2!=chat->emoticonsList.end(); ++it2) + { + QString es = Qt::escape(it2.key()); + if (escapedMessage.contains(es)) + { + if (!QFile::exists(it2.value())) + { // an emoticon is still missing, so wait for it + message->setHtmlBody(escapedMessage); + return false; + } + + QImage iconImage = QImageReader(it2.value()).read(); + + escapedMessage.replace( QRegExp(QString::fromLatin1("%1(?![^><]*>)").arg(QRegExp::escape(es))), + QString::fromLatin1("\"")" ) ); + } + } + message->setHtmlBody(escapedMessage); + return true; +} #include "wlmchatmanager.moc" --- trunk/KDE/kdenetwork/kopete/protocols/wlm/wlmchatmanager.h #976114:976115 @@ -40,8 +40,17 @@ QMap < MSN::SwitchboardServerConnection *, WlmChatSession * >chatSessions; // messages waiting for emoticons to be received - QMap < MSN::SwitchboardServerConnection *, QLinkedList > pendingMessages; + class PendingMessage { + public: + PendingMessage( Kopete::Message* msg ) + : receiveTime(QTime::currentTime()), message(msg) + {} + QTime receiveTime; + Kopete::Message* message; + }; + QMap > pendingMessages; + void requestDisplayPicture (QString contactId); void createChat (MSN::SwitchboardServerConnection * conn); @@ -106,8 +115,13 @@ const QString & alias, const QString & msnobject); - private: +protected: + virtual void timerEvent(QTimerEvent *event); + bool fillEmoticons(WlmChatSession *chat, Kopete::Message* message); + +private: WlmAccount * m_account; + int m_emoticonsTimeoutTimerId; }; #endif