X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fclient%2Fmessagemodel.cpp;h=82fd71b1b252acc7c7e3bbd32244544bcf5a0f92;hp=7297f4b21f43f1ce70bdb41ea22ee6f0a52a2ac9;hb=eb8ee27deb8346dbf9e8515b0cceefd58cda971b;hpb=722de35940fadb7be9cde29e103d251742ffb3ec diff --git a/src/client/messagemodel.cpp b/src/client/messagemodel.cpp index 7297f4b2..82fd71b1 100644 --- a/src/client/messagemodel.cpp +++ b/src/client/messagemodel.cpp @@ -20,10 +20,14 @@ #include "messagemodel.h" -#include "message.h" - #include +#include "backlogsettings.h" +#include "clientbacklogmanager.h" +#include "client.h" +#include "message.h" +#include "networkmodel.h" + class ProcessBufferEvent : public QEvent { public: inline ProcessBufferEvent() : QEvent(QEvent::User) {} @@ -66,8 +70,6 @@ bool MessageModel::setData(const QModelIndex &index, const QVariant &value, int return false; } - - bool MessageModel::insertMessage(const Message &msg, bool fakeMsg) { MsgId id = msg.msgId(); int idx = indexForId(id); @@ -88,7 +90,7 @@ void MessageModel::insertMessages(const QList &msglist) { int remainingMsgs = msglist.count() - processedMsgs; if(remainingMsgs > 0) { if(msglist.first().msgId() < msglist.last().msgId()) { - // in Order + // in Order - we have just successfully processed "processedMsg" messages from the end of the list _messageBuffer << msglist.mid(0, remainingMsgs); } else { _messageBuffer << msglist.mid(processedMsgs); @@ -103,7 +105,7 @@ void MessageModel::insertMessageGroup(const QList &msglist) { int start = indexForId(msglist.first().msgId()); int end = start + msglist.count() - 1; MessageModelItem *dayChangeItem = 0; - + bool relocatedMsg = false; if(start > 0) { // check if the preceeding msg is a daychange message and if so if // we have to drop or relocate it at the end of this chunk @@ -122,6 +124,7 @@ void MessageModel::insertMessageGroup(const QList &msglist) { endRemoveRows(); start--; end--; + relocatedMsg = true; } } @@ -168,13 +171,21 @@ void MessageModel::insertMessageGroup(const QList &msglist) { } int MessageModel::insertMessagesGracefully(const QList &msglist) { + /* short description: + * 1) first we check where the message with the highest msgId from msglist would be inserted + * 2) check that position for dupe + * 3) determine the messageId of the preceeding msg + * 4) insert as many msgs from msglist with with msgId larger then the just determined id + * those messages are automatically less then the msg of the position we just determined in 1) + */ bool inOrder = (msglist.first().msgId() < msglist.last().msgId()); // depending on the order we have to traverse from the front to the back or vice versa QList grouplist; MsgId id; MsgId dupeId; - int dupeCount = 0; + int processedMsgs = 1; // we know the list isn't empty, so we at least process one message + int idx; bool fastForward = false; QList::const_iterator iter; if(inOrder) { @@ -184,7 +195,7 @@ int MessageModel::insertMessagesGracefully(const QList &msglist) { iter = msglist.constBegin(); } - int idx = indexForId((*iter).msgId()); + idx = indexForId((*iter).msgId()); if(idx >= 0 && !_messageList.isEmpty()) dupeId = _messageList[idx]->msgId(); @@ -197,8 +208,6 @@ int MessageModel::insertMessagesGracefully(const QList &msglist) { if((*iter).msgId() != dupeId) grouplist << *iter; - else - dupeCount++; if(!inOrder) iter++; @@ -209,7 +218,13 @@ int MessageModel::insertMessagesGracefully(const QList &msglist) { if(!fastForward && (*iter).msgId() < id) break; + processedMsgs++; + if(grouplist.isEmpty()) { // as long as we don't have a starting point, we have to update the dupeId + idx = indexForId((*iter).msgId()); + if(idx >= 0 && !_messageList.isEmpty()) + dupeId = _messageList[idx]->msgId(); + } if((*iter).msgId() != dupeId) { if(!grouplist.isEmpty()) { QDateTime nextTs = grouplist.value(0).timestamp(); @@ -224,19 +239,23 @@ int MessageModel::insertMessagesGracefully(const QList &msglist) { Message dayChangeMsg = Message::ChangeOfDay(nextTs); dayChangeMsg.setMsgId((*iter).msgId()); grouplist.prepend(dayChangeMsg); - dupeCount--; } } + dupeId = (*iter).msgId(); grouplist.prepend(*iter); - } else { - dupeCount++; } } } else { while(iter != msglist.constEnd()) { if(!fastForward && (*iter).msgId() < id) break; + processedMsgs++; + if(grouplist.isEmpty()) { // as long as we don't have a starting point, we have to update the dupeId + idx = indexForId((*iter).msgId()); + if(idx >= 0 && !_messageList.isEmpty()) + dupeId = _messageList[idx]->msgId(); + } if((*iter).msgId() != dupeId) { if(!grouplist.isEmpty()) { QDateTime nextTs = grouplist.value(0).timestamp(); @@ -251,20 +270,18 @@ int MessageModel::insertMessagesGracefully(const QList &msglist) { Message dayChangeMsg = Message::ChangeOfDay(nextTs); dayChangeMsg.setMsgId((*iter).msgId()); grouplist.prepend(dayChangeMsg); - dupeCount--; } } + dupeId = (*iter).msgId(); grouplist.prepend(*iter); - } else { - dupeCount++; } - iter++; } } - insertMessageGroup(grouplist); - return grouplist.count() + dupeCount; + if(!grouplist.isEmpty()) + insertMessageGroup(grouplist); + return processedMsgs; } void MessageModel::customEvent(QEvent *event) { @@ -292,10 +309,9 @@ void MessageModel::clear() { qDeleteAll(_messageList); _messageList.clear(); endRemoveRows(); + _messagesWaiting.clear(); } - - // returns index of msg with given Id or of the next message after that (i.e., the index where we'd insert this msg) int MessageModel::indexForId(MsgId id) { if(_messageList.isEmpty() || id <= _messageList.value(0)->msgId()) @@ -330,8 +346,38 @@ void MessageModel::changeOfDay() { _nextDayChange = _nextDayChange.addSecs(86400); } -/**********************************************************************************/ +void MessageModel::requestBacklog(BufferId bufferId) { + if(_messagesWaiting.contains(bufferId)) + return; + + BacklogSettings backlogSettings; + int requestCount = backlogSettings.dynamicBacklogAmount(); + + for(int i = 0; i < _messageList.count(); i++) { + if(_messageList.at(i)->bufferId() == bufferId) { + _messagesWaiting[bufferId] = requestCount; + Client::backlogManager()->emitMessagesRequested(tr("Requesting %1 messages from backlog for buffer %2:%3") + .arg(requestCount) + .arg(Client::networkModel()->networkName(bufferId)) + .arg(Client::networkModel()->bufferName(bufferId))); + Client::backlogManager()->requestBacklog(bufferId, requestCount, _messageList.at(i)->msgId().toInt()); + return; + } + } +} + +void MessageModel::messagesReceived(BufferId bufferId, int count) { + if(!_messagesWaiting.contains(bufferId)) + return; + + _messagesWaiting[bufferId] -= count; + if(_messagesWaiting[bufferId] <= 0) + _messagesWaiting.remove(bufferId); +} +// ======================================== +// MessageModelItem +// ======================================== MessageModelItem::MessageModelItem(const Message &msg) : _timestamp(msg.timestamp()), _msgId(msg.msgId()), @@ -372,3 +418,12 @@ bool MessageModelItem::operator==(const MessageModelItem &other) const { bool MessageModelItem::operator>(const MessageModelItem &other) const { return _msgId > other._msgId; } + +QDebug operator<<(QDebug dbg, const MessageModelItem &msgItem) { + dbg.nospace() << qPrintable(QString("MessageModelItem(MsgId:")) << msgItem.msgId() + << qPrintable(QString(",")) << msgItem.timeStamp() + << qPrintable(QString(", Type:")) << msgItem.msgType() + << qPrintable(QString(", Flags:")) << msgItem.msgFlags() << qPrintable(QString(")")) + << msgItem.data(1, Qt::DisplayRole).toString() << ":" << msgItem.data(2, Qt::DisplayRole).toString(); + return dbg; +}