- }
-
- return false;
-}
-
-
-
-bool MessageModel::insertMessage(const Message &msg, bool fakeMsg) {
- MsgId id = msg.msgId();
- int idx = indexForId(id);
- if(!fakeMsg && idx < _messageList.count()) { // check for duplicate
- if(_messageList[idx]->msgId() == id)
- return false;
- }
-
- MessageModelItem *item = createMessageModelItem(msg);
- beginInsertRows(QModelIndex(), idx, idx);
- _messageList.insert(idx, item);
- endInsertRows();
- return true;
-}
-
-
-void MessageModel::insertMessageGroup(const QList<Message> &msglist) {
- if(msglist.isEmpty()) return;
-
- int idx = indexForId(msglist.first().msgId());
- beginInsertRows(QModelIndex(), idx, idx+msglist.count()-1);
-
- foreach(Message msg, msglist) {
- _messageList.insert(idx, createMessageModelItem(msg));
- idx++;
- }
-
- endInsertRows();
-}
-
-
-void MessageModel::insertMessages(const QList<Message> &msglist) {
- if(msglist.isEmpty())
- return;
-
- if(_messageList.isEmpty()) {
- insertMessageGroup(msglist);
- return;
- }
-
- 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
- // for the sake of performance we have a little code duplication here
- // if you need to do some changes here you'll probably need to change them at all
- // places marked DUPE
-
-
- // FIXME: keep scrollbars from jumping
- // the somewhat bulk insert leads to a jumpy scrollbar when the user requests further backlog.
- // it would probably be the best to stop processing each time we actually insert a messagegroup
- // and give back controll to the eventloop (similar to what the QtUiMessageProcessor used to do)
- QList<Message> grouplist;
- MsgId id;
- MsgId dupeId;
- 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();
- }
-
- // DUPE (1 / 3)
- 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) // also safe as we've passed another empty check
- id = _messageList[idx - 1]->msgId();
- else
- fastForward = true;
- if((*iter).msgId() != dupeId)
- grouplist << *iter;
-
- if(!inOrder)
- iter++;
-
- if(inOrder) {
- while(iter != msglist.constBegin()) {
- iter--;
- // DUPE (2 / 3)
- if(!fastForward && (*iter).msgId() < id) {
- insertMessageGroup(grouplist);
- grouplist.clear();
-
- // build new group
- int idx = indexForId((*iter).msgId());
- if(idx >= 0)
- dupeId = _messageList[idx]->msgId();
- if(idx - 1 >= 0)
- id = _messageList[idx - 1]->msgId();
- else
- fastForward = true;
- }
- if((*iter).msgId() != dupeId)
- grouplist.prepend(*iter);
- }
- } else {
- while(iter != msglist.constEnd()) {
- // DUPE (3 / 3)
- if(!fastForward && (*iter).msgId() < id) {
- insertMessageGroup(grouplist);
- grouplist.clear();
-
- // build new group
- int idx = indexForId((*iter).msgId());
- if(idx >= 0)
- dupeId = _messageList[idx]->msgId();
- if(idx - 1 >= 0)
- id = _messageList[idx - 1]->msgId();
- else
- fastForward = true;
- }
- if((*iter).msgId() != dupeId)
- grouplist.prepend(*iter);
- iter++;
- }
- }
-
- if(!grouplist.isEmpty()) {
- insertMessageGroup(grouplist);
- }
-
- return;
-}
-
-
-void MessageModel::clear() {
- beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
- qDeleteAll(_messageList);
- _messageList.clear();
- endRemoveRows();
+}
+
+
+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);
+ }
+ qSort(_messageBuffer);
+ QCoreApplication::postEvent(this, new ProcessBufferEvent());
+ }
+ }
+ else {
+ _messageBuffer << msglist;
+ qSort(_messageBuffer);
+ }
+}
+
+
+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);
+ uint nextDay = nextTs.toTime_t() / 86400;
+ uint prevDay = prevTs.toTime_t() / 86400;
+ if (nextDay != prevDay) {
+ nextTs.setTime_t(nextDay * 86400);
+ 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());
+}
+
+
+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 minId;
+ 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 < messageCount())
+ dupeId = messageItemAt(idx)->msgId();
+
+ // we always compare to the previous entry...
+ // if there isn't, we can fastforward to the top
+ if (idx - 1 >= 0)
+ minId = messageItemAt(idx - 1)->msgId();
+ else
+ fastForward = true;
+
+ if ((*iter).msgId() != dupeId) {
+ grouplist << *iter;
+ dupeId = (*iter).msgId();
+ }
+
+ if (!inOrder)
+ iter++;
+
+ if (inOrder) {
+ while (iter != msglist.constBegin()) {
+ iter--;
+
+ if (!fastForward && (*iter).msgId() <= minId)
+ 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 && !messagesIsEmpty())
+ dupeId = messageItemAt(idx)->msgId();
+ }
+ if ((*iter).msgId() != dupeId) {
+ if (!grouplist.isEmpty()) {
+ QDateTime nextTs = grouplist.value(0).timestamp();
+ QDateTime prevTs = (*iter).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((*iter).msgId());
+ grouplist.prepend(dayChangeMsg);
+ }
+ }
+ dupeId = (*iter).msgId();
+ grouplist.prepend(*iter);
+ }
+ }
+ }
+ else {
+ while (iter != msglist.constEnd()) {
+ if (!fastForward && (*iter).msgId() <= minId)
+ 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 && !messagesIsEmpty())
+ dupeId = messageItemAt(idx)->msgId();
+ }
+ if ((*iter).msgId() != dupeId) {
+ if (!grouplist.isEmpty()) {
+ QDateTime nextTs = grouplist.value(0).timestamp();
+ QDateTime prevTs = (*iter).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((*iter).msgId());
+ grouplist.prepend(dayChangeMsg);
+ }
+ }
+ dupeId = (*iter).msgId();
+ grouplist.prepend(*iter);
+ }
+ iter++;
+ }
+ }
+
+ if (!grouplist.isEmpty())
+ insertMessageGroup(grouplist);
+ return processedMsgs;
+}
+
+
+void MessageModel::customEvent(QEvent *event)
+{
+ if (event->type() != QEvent::User)
+ return;
+
+ event->accept();
+
+ if (_messageBuffer.isEmpty())
+ return;
+
+ int processedMsgs = insertMessagesGracefully(_messageBuffer);
+ int remainingMsgs = _messageBuffer.count() - processedMsgs;
+
+ QList<Message>::iterator removeStart = _messageBuffer.begin() + remainingMsgs;
+ QList<Message>::iterator removeEnd = _messageBuffer.end();
+ _messageBuffer.erase(removeStart, removeEnd);
+ if (!_messageBuffer.isEmpty())
+ QCoreApplication::postEvent(this, new ProcessBufferEvent());
+}
+
+
+void MessageModel::clear()
+{
+ _messagesWaiting.clear();
+ if (rowCount() > 0) {
+ beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
+ removeAllMessages();
+ endRemoveRows();
+ }