1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel IRC Team *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "messagemodel.h"
27 class ProcessBufferEvent : public QEvent {
29 inline ProcessBufferEvent() : QEvent(QEvent::User) {}
32 MessageModel::MessageModel(QObject *parent)
33 : QAbstractItemModel(parent)
37 QVariant MessageModel::data(const QModelIndex &index, int role) const {
38 int row = index.row(); int column = index.column();
39 if(row < 0 || row >= _messageList.count() || column < 0)
42 if(role == ColumnTypeRole)
45 return _messageList[row]->data(index.column(), role);
48 bool MessageModel::setData(const QModelIndex &index, const QVariant &value, int role) {
49 int row = index.row();
50 if(row < 0 || row >= _messageList.count())
53 if(_messageList[row]->setData(index.column(), value, role)) {
54 emit dataChanged(index, index);
63 bool MessageModel::insertMessage(const Message &msg, bool fakeMsg) {
64 MsgId id = msg.msgId();
65 int idx = indexForId(id);
66 if(!fakeMsg && idx < _messageList.count()) { // check for duplicate
67 if(_messageList[idx]->msgId() == id)
71 MessageModelItem *item = createMessageModelItem(msg);
72 beginInsertRows(QModelIndex(), idx, idx);
73 _messageList.insert(idx, item);
78 void MessageModel::insertMessages(const QList<Message> &msglist) {
82 if(_messageList.isEmpty()) {
83 insertMessageGroup(msglist);
85 int processedMsgs = insertMessagesGracefully(msglist);
86 int remainingMsgs = msglist.count() - processedMsgs;
87 if(remainingMsgs > 0) {
88 if(msglist.first().msgId() < msglist.last().msgId()) {
90 _messageBuffer << msglist.mid(0, remainingMsgs);
92 _messageBuffer << msglist.mid(processedMsgs);
94 qSort(_messageBuffer);
95 QCoreApplication::postEvent(this, new ProcessBufferEvent());
100 void MessageModel::insertMessageGroup(const QList<Message> &msglist) {
101 if(msglist.isEmpty()) return;
103 int idx = indexForId(msglist.first().msgId());
104 beginInsertRows(QModelIndex(), idx, idx+msglist.count()-1);
106 foreach(Message msg, msglist) {
107 _messageList.insert(idx, createMessageModelItem(msg));
114 int MessageModel::insertMessagesGracefully(const QList<Message> &msglist) {
115 bool inOrder = (msglist.first().msgId() < msglist.last().msgId());
116 // depending on the order we have to traverse from the front to the back or vice versa
118 QList<Message> grouplist;
122 bool fastForward = false;
123 QList<Message>::const_iterator iter;
125 iter = msglist.constEnd();
126 iter--; // this op is safe as we've allready passed an empty check
128 iter = msglist.constBegin();
131 int idx = indexForId((*iter).msgId());
133 dupeId = _messageList[idx]->msgId();
135 // we always compare to the previous entry...
136 // if there isn't, we can fastforward to the top
138 id = _messageList[idx - 1]->msgId();
142 if((*iter).msgId() != dupeId)
151 while(iter != msglist.constBegin()) {
154 if(!fastForward && (*iter).msgId() < id)
157 if((*iter).msgId() != dupeId)
158 grouplist.prepend(*iter);
163 while(iter != msglist.constEnd()) {
164 if(!fastForward && (*iter).msgId() < id)
167 if((*iter).msgId() != dupeId)
168 grouplist.prepend(*iter);
176 insertMessageGroup(grouplist);
177 return grouplist.count() + dupeCount;
180 void MessageModel::customEvent(QEvent *event) {
181 if(event->type() != QEvent::User)
186 if(_messageBuffer.isEmpty())
189 int processedMsgs = insertMessagesGracefully(_messageBuffer);
190 int remainingMsgs = _messageBuffer.count() - processedMsgs;
192 QList<Message>::iterator removeStart = _messageBuffer.begin() + remainingMsgs;
193 QList<Message>::iterator removeEnd = _messageBuffer.end();
194 _messageBuffer.erase(removeStart, removeEnd);
196 if(!_messageBuffer.isEmpty())
197 QCoreApplication::postEvent(this, new ProcessBufferEvent());
200 void MessageModel::clear() {
201 beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
202 qDeleteAll(_messageList);
203 _messageList.clear();
209 // returns index of msg with given Id or of the next message after that (i.e., the index where we'd insert this msg)
210 int MessageModel::indexForId(MsgId id) {
211 if(_messageList.isEmpty() || id <= _messageList.value(0)->data(0, MsgIdRole).value<MsgId>()) return 0;
212 if(id > _messageList.last()->data(0, MsgIdRole).value<MsgId>()) return _messageList.count();
214 int start = 0; int end = _messageList.count()-1;
216 if(end - start == 1) return end;
217 int pivot = (end + start) / 2;
218 if(id <= _messageList.value(pivot)->data(0, MsgIdRole).value<MsgId>()) end = pivot;
223 /**********************************************************************************/
225 MessageModelItem::MessageModelItem(const Message &msg) :
226 _timestamp(msg.timestamp()),
228 _bufferId(msg.bufferInfo().bufferId()),
234 QVariant MessageModelItem::data(int column, int role) const {
235 if(column < MessageModel::TimestampColumn || column > MessageModel::ContentsColumn)
239 case MessageModel::MsgIdRole: return QVariant::fromValue<MsgId>(_msgId);
240 case MessageModel::BufferIdRole: return QVariant::fromValue<BufferId>(_bufferId);
241 case MessageModel::TypeRole: return _type;
242 case MessageModel::FlagsRole: return (int)_flags;
243 case MessageModel::TimestampRole: return _timestamp;
244 default: return QVariant();
250 bool MessageModelItem::lessThan(const MessageModelItem *m1, const MessageModelItem *m2){
251 return (*m1) < (*m2);
254 bool MessageModelItem::operator<(const MessageModelItem &other) const {
255 return _msgId < other._msgId;
258 bool MessageModelItem::operator==(const MessageModelItem &other) const {
259 return _msgId == other._msgId;
262 bool MessageModelItem::operator>(const MessageModelItem &other) const {
263 return _msgId > other._msgId;