+void CoreSession::customEvent(QEvent *event) {
+ if(event->type() != QEvent::User)
+ return;
+
+ processMessages();
+ event->accept();
+}
+
+void CoreSession::processMessages() {
+ if(_messageQueue.count() == 1) {
+ const RawMessage &rawMsg = _messageQueue.first();
+ bool createBuffer = !(rawMsg.flags & Message::Redirected);
+ BufferInfo bufferInfo = Core::bufferInfo(user(), rawMsg.networkId, rawMsg.bufferType, rawMsg.target, createBuffer);
+ if(!bufferInfo.isValid()) {
+ Q_ASSERT(!createBuffer);
+ bufferInfo = Core::bufferInfo(user(), rawMsg.networkId, BufferInfo::StatusBuffer, "");
+ }
+ Message msg(bufferInfo, rawMsg.type, rawMsg.text, rawMsg.sender, rawMsg.flags);
+ Core::storeMessage(msg);
+ emit displayMsg(msg);
+ } else {
+ QHash<NetworkId, QHash<QString, BufferInfo> > bufferInfoCache;
+ MessageList messages;
+ QList<RawMessage> redirectedMessages; // list of Messages which don't enforce a buffer creation
+ BufferInfo bufferInfo;
+ for(int i = 0; i < _messageQueue.count(); i++) {
+ const RawMessage &rawMsg = _messageQueue.at(i);
+ if(bufferInfoCache.contains(rawMsg.networkId) && bufferInfoCache[rawMsg.networkId].contains(rawMsg.target)) {
+ bufferInfo = bufferInfoCache[rawMsg.networkId][rawMsg.target];
+ } else {
+ bool createBuffer = !(rawMsg.flags & Message::Redirected);
+ bufferInfo = Core::bufferInfo(user(), rawMsg.networkId, rawMsg.bufferType, rawMsg.target, createBuffer);
+ if(!bufferInfo.isValid()) {
+ Q_ASSERT(!createBuffer);
+ redirectedMessages << rawMsg;
+ continue;
+ }
+ bufferInfoCache[rawMsg.networkId][rawMsg.target] = bufferInfo;
+ }
+ Message msg(bufferInfo, rawMsg.type, rawMsg.text, rawMsg.sender, rawMsg.flags);
+ messages << msg;
+ }
+
+ // recheck if there exists a buffer to store a redirected message in
+ for(int i = 0; i < redirectedMessages.count(); i++) {
+ const RawMessage &rawMsg = _messageQueue.at(i);
+ if(bufferInfoCache.contains(rawMsg.networkId) && bufferInfoCache[rawMsg.networkId].contains(rawMsg.target)) {
+ bufferInfo = bufferInfoCache[rawMsg.networkId][rawMsg.target];
+ } else {
+ // no luck -> we store them in the StatusBuffer
+ bufferInfo = Core::bufferInfo(user(), rawMsg.networkId, BufferInfo::StatusBuffer, "");
+ // add the StatusBuffer to the Cache in case there are more Messages for the original target
+ bufferInfoCache[rawMsg.networkId][rawMsg.target] = bufferInfo;
+ }
+ Message msg(bufferInfo, rawMsg.type, rawMsg.text, rawMsg.sender, rawMsg.flags);
+ messages << msg;
+ }
+
+ Core::storeMessages(messages);
+ // FIXME: extend protocol to a displayMessages(MessageList)
+ for(int i = 0; i < messages.count(); i++) {
+ emit displayMsg(messages[i]);
+ }
+ }
+ _processMessages = false;
+ _messageQueue.clear();
+}