+void MessageModel::insertMessages(const QList<Message>& msglist)
+{
+ if (msglist.isEmpty())
+ return;
+
+ if (_messageBuffer.isEmpty()) {
+ 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);
+ }
+ std::sort(_messageBuffer.begin(), _messageBuffer.end());
+ QCoreApplication::postEvent(this, new ProcessBufferEvent());
+ }
+ }
+ else {
+ _messageBuffer << msglist;
+ std::sort(_messageBuffer.begin(), _messageBuffer.end());
+ }
+}
+
+void MessageModel::insertMessageGroup(const QList<Message>& msglist)
+{
+ Q_ASSERT(!msglist.isEmpty()); // the msglist can be assumed to be non empty
+ // int last = msglist.count() - 1;
+ // Q_ASSERT(0 == last || msglist.at(0).msgId() != msglist.at(last).msgId() || msglist.at(last).type() == Message::DayChange);
+ int start = indexForId(msglist.first().msgId());
+ int end = start + msglist.count() - 1;
+ Message dayChangeMsg;
+
+ 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 (messageItemAt(prevIdx)->msgType() == Message::DayChange && messageItemAt(prevIdx)->timestamp() > msglist.at(0).timestamp()) {
+ beginRemoveRows(QModelIndex(), prevIdx, prevIdx);
+ Message oldDayChangeMsg = takeMessageAt(prevIdx);
+ if (msglist.last().timestamp() < oldDayChangeMsg.timestamp()) {
+ // we have to reinsert it with a changed msgId
+ dayChangeMsg = oldDayChangeMsg;
+ dayChangeMsg.setMsgId(msglist.last().msgId());
+ }
+ endRemoveRows();
+
+ start--;
+ end--;
+ }
+ }
+
+ if (!dayChangeMsg.isValid() && start < messageCount()) {
+ // 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(messageItemAt(start)->msgType() != Message::DayChange);
+ QDateTime nextTs = messageItemAt(start)->timestamp();
+ QDateTime prevTs = msglist.last().timestamp();
+ nextTs.setTimeSpec(Qt::UTC);
+ prevTs.setTimeSpec(Qt::UTC);
+ qint64 nextDay = nextTs.toMSecsSinceEpoch() / DAY_IN_MSECS;
+ qint64 prevDay = prevTs.toMSecsSinceEpoch() / DAY_IN_MSECS;
+ if (nextDay != prevDay) {
+ nextTs.setMSecsSinceEpoch(nextDay * DAY_IN_MSECS);
+ nextTs.setTimeSpec(Qt::LocalTime);
+ dayChangeMsg = Message::ChangeOfDay(nextTs);
+ dayChangeMsg.setMsgId(msglist.last().msgId());
+ }
+ }
+
+ if (dayChangeMsg.isValid())
+ end++;
+
+ Q_ASSERT(start == 0 || messageItemAt(start - 1)->msgId() < msglist.first().msgId());
+ Q_ASSERT(start == messageCount() || messageItemAt(start)->msgId() > msglist.last().msgId());
+ beginInsertRows(QModelIndex(), start, end);
+ insertMessages__(start, msglist);
+ if (dayChangeMsg.isValid())
+ insertMessage__(start + msglist.count(), dayChangeMsg);
+ endInsertRows();
+
+ Q_ASSERT(start == end || messageItemAt(start)->msgId() != messageItemAt(end)->msgId()
+ || messageItemAt(end)->msgType() == Message::DayChange);
+ Q_ASSERT(start == 0 || messageItemAt(start - 1)->msgId() < messageItemAt(start)->msgId());
+ Q_ASSERT(end + 1 == messageCount() || messageItemAt(end)->msgId() < messageItemAt(end + 1)->msgId());