+ if(msglist.isEmpty())
+ return;
+
+ if(_messageList.isEmpty()) {
+ insertMessageGroup(msglist);
+ } else {
+ int processedMsgs = insertMessagesGracefully(msglist);
+ int remainingMsgs = msglist.count() - processedMsgs;
+ if(remainingMsgs > 0) {
+ if(msglist.first().msgId() < msglist.last().msgId()) {
+ // in Order
+ _messageBuffer << msglist.mid(0, remainingMsgs);
+ } else {
+ _messageBuffer << msglist.mid(processedMsgs);
+ }
+ qSort(_messageBuffer);
+ QCoreApplication::postEvent(this, new ProcessBufferEvent());
+ }
+ }
+}
+
+void MessageModel::insertMessageGroup(const QList<Message> &msglist) {
+ int idx = indexForId(msglist.first().msgId());
+ beginInsertRows(QModelIndex(), idx, idx+msglist.count()-1);
+
+ foreach(Message msg, msglist) {
+ _messageList.insert(idx, createMessageModelItem(msg));
+ idx++;
+ }
+
+ endInsertRows();
+}
+
+int MessageModel::insertMessagesGracefully(const QList<Message> &msglist) {
+ 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<Message> grouplist;
+ MsgId id;
+ MsgId dupeId;
+ int dupeCount = 0;
+ bool fastForward = false;
+ QList<Message>::const_iterator iter;
+ if(inOrder) {
+ iter = msglist.constEnd();
+ iter--; // this op is safe as we've allready passed an empty check
+ } else {
+ iter = msglist.constBegin();
+ }
+
+ int idx = indexForId((*iter).msgId());
+ if(idx >= 0)
+ dupeId = _messageList[idx]->msgId();
+
+ // we always compare to the previous entry...
+ // if there isn't, we can fastforward to the top
+ if(idx - 1 >= 0)
+ id = _messageList[idx - 1]->msgId();
+ else
+ fastForward = true;
+
+ if((*iter).msgId() != dupeId)
+ grouplist << *iter;
+ else
+ dupeCount++;