X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fclient%2Fmessagemodel.cpp;h=82fd71b1b252acc7c7e3bbd32244544bcf5a0f92;hp=02fc8740e6b7e8b86257df72867c22dfd5a6cc99;hb=60c6384aae4035545cd02f6032dd810e98ddba37;hpb=3258b69cab16d7a5cf80a27d166c0a3628fe60d2 diff --git a/src/client/messagemodel.cpp b/src/client/messagemodel.cpp index 02fc8740..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); @@ -151,14 +153,7 @@ void MessageModel::insertMessageGroup(const QList &msglist) { end++; Q_ASSERT(start == 0 || _messageList[start - 1]->msgId() < msglist.first().msgId()); - - if(start < _messageList.count()) { // && _messageList[start]->msgId() <= msglist.last().msgId()) { - qDebug() << *(_messageList[start]) << "<=" << msglist.last(); - qDebug() << start << end << msglist.count() << dayChangeItem << relocatedMsg; - } Q_ASSERT(start == _messageList.count() || _messageList[start]->msgId() > msglist.last().msgId()); - - beginInsertRows(QModelIndex(), start, end); int pos = start; foreach(Message msg, msglist) { @@ -176,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) { @@ -192,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(); @@ -205,8 +208,6 @@ int MessageModel::insertMessagesGracefully(const QList &msglist) { if((*iter).msgId() != dupeId) grouplist << *iter; - else - dupeCount++; if(!inOrder) iter++; @@ -217,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(); @@ -232,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(); @@ -259,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) { @@ -300,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()) @@ -338,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()),