+ if(msglist.isEmpty())
+ return;
+
+ int processedMsgs = insertMessagesGracefully(msglist);
+ int remainingMsgs = msglist.count() - processedMsgs;
+ if(remainingMsgs > 0) {
+ if(msglist.first().msgId() < msglist.last().msgId()) {
+ // 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);
+ }
+ qSort(_messageBuffer);
+ QCoreApplication::postEvent(this, new ProcessBufferEvent());
+ }
+}
+
+void MessageModel::insertMessageGroup(const QList<Message> &msglist) {
+ Q_ASSERT(!msglist.isEmpty()); // the msglist can be assumed to be non empty
+ 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
+ int prevIdx = start - 1;
+ if(_messageList.at(prevIdx)->msgType() == Message::DayChange
+ && _messageList.at(prevIdx)->timeStamp() > msglist.at(0).timestamp()) {
+ beginRemoveRows(QModelIndex(), prevIdx, prevIdx);
+ MessageModelItem *oldItem = _messageList.takeAt(prevIdx);
+ if(msglist.last().timestamp() < oldItem->timeStamp()) {
+ // we have to reinsert it (with changed msgId -> thus we need to recreate it)
+ Message dayChangeMsg = Message::ChangeOfDay(oldItem->timeStamp());
+ dayChangeMsg.setMsgId(msglist.last().msgId());
+ dayChangeItem = createMessageModelItem(dayChangeMsg);
+ }
+ delete oldItem;
+ endRemoveRows();
+ start--;
+ end--;
+ relocatedMsg = true;
+ }
+ }
+
+ if(!dayChangeItem && start < _messageList.count()) {
+ // check if we need to insert a daychange message at the end of the this group
+
+ // if this assert triggers then indexForId() would have found a spot right before a DayChangeMsg
+ // this should never happen as daychange messages share the msgId with the preceeding message
+ Q_ASSERT(_messageList[start]->msgType() != Message::DayChange);
+ QDateTime nextTs = _messageList[start]->timeStamp();
+ QDateTime prevTs = msglist.last().timestamp();
+ nextTs.setTimeSpec(Qt::UTC);
+ prevTs.setTimeSpec(Qt::UTC);
+ uint nextDay = nextTs.toTime_t() / 86400;
+ uint prevDay = prevTs.toTime_t() / 86400;
+ if(nextDay != prevDay) {
+ nextTs.setTime_t(nextDay * 86400);
+ nextTs.setTimeSpec(Qt::LocalTime);
+ Message dayChangeMsg = Message::ChangeOfDay(nextTs);
+ dayChangeMsg.setMsgId(msglist.last().msgId());
+ dayChangeItem = createMessageModelItem(dayChangeMsg);
+ }
+ }
+
+ if(dayChangeItem)
+ end++;
+
+ Q_ASSERT(start == 0 || _messageList[start - 1]->msgId() < msglist.first().msgId());
+ Q_ASSERT(start == _messageList.count() || _messageList[start]->msgId() > msglist.last().msgId());
+ beginInsertRows(QModelIndex(), start, end);
+ int pos = start;
+ foreach(Message msg, msglist) {
+ _messageList.insert(pos, createMessageModelItem(msg));
+ pos++;
+ }
+ if(dayChangeItem) {
+ _messageList.insert(pos, dayChangeItem);
+ pos++; // needed for the following assert
+ }
+ endInsertRows();
+ Q_ASSERT(start == 0 || _messageList[start - 1]->msgId() < _messageList[start]->msgId());
+ Q_ASSERT(end + 1 == _messageList.count() || _messageList[end]->msgId() < _messageList[end + 1]->msgId());
+ Q_ASSERT(pos - 1 == end);
+}
+
+int MessageModel::insertMessagesGracefully(const QList<Message> &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<Message> grouplist;
+ MsgId id;
+ MsgId dupeId;
+ int processedMsgs = 1; // we know the list isn't empty, so we at least process one message
+ int idx;
+ 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();
+ }
+
+ idx = indexForId((*iter).msgId());
+ if(idx >= 0 && !_messageList.isEmpty())
+ 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;
+
+ if(!inOrder)
+ iter++;