232efe62ebc2756b226975a26ea6dcd17b6add2b
[quassel.git] / src / client / messagemodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "messagemodel.h"
22
23 #include "message.h"
24
25 #include <QEvent>
26
27 class ProcessBufferEvent : public QEvent {
28 public:
29   inline ProcessBufferEvent() : QEvent(QEvent::User) {}
30 };
31
32 MessageModel::MessageModel(QObject *parent)
33   : QAbstractItemModel(parent)
34 {
35 }
36
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)
40     return QVariant();
41
42   if(role == ColumnTypeRole)
43     return column;
44
45   return _messageList[row]->data(index.column(), role);
46 }
47
48 bool MessageModel::setData(const QModelIndex &index, const QVariant &value, int role) {
49   int row = index.row();
50   if(row < 0 || row >= _messageList.count())
51     return false;
52
53   if(_messageList[row]->setData(index.column(), value, role)) {
54     emit dataChanged(index, index);
55     return true;
56   }
57
58   return false;
59 }
60
61
62
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)
68       return false;
69   }
70
71   MessageModelItem *item = createMessageModelItem(msg);
72   beginInsertRows(QModelIndex(), idx, idx);
73   _messageList.insert(idx, item);
74   endInsertRows();
75   return true;
76 }
77
78 void MessageModel::insertMessages(const QList<Message> &msglist) {
79   if(msglist.isEmpty())
80     return;
81
82   if(_messageList.isEmpty()) {
83     insertMessageGroup(msglist);
84   } else {
85     int processedMsgs = insertMessagesGracefully(msglist);
86     int remainingMsgs = msglist.count() - processedMsgs;
87     if(remainingMsgs > 0) {
88       if(msglist.first().msgId() < msglist.last().msgId()) {
89         // in Order
90         _messageBuffer << msglist.mid(0, remainingMsgs);
91       } else {
92         _messageBuffer << msglist.mid(processedMsgs);
93       }
94       qSort(_messageBuffer);
95       QCoreApplication::postEvent(this, new ProcessBufferEvent());
96     }
97   }
98 }
99
100 void MessageModel::insertMessageGroup(const QList<Message> &msglist) {
101   if(msglist.isEmpty()) return;
102
103   int idx = indexForId(msglist.first().msgId());
104   beginInsertRows(QModelIndex(), idx, idx+msglist.count()-1);
105
106   foreach(Message msg, msglist) {
107     _messageList.insert(idx, createMessageModelItem(msg));
108     idx++;
109   }
110
111   endInsertRows();
112 }
113
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
117
118   QList<Message> grouplist;
119   MsgId id;
120   MsgId dupeId;
121   int dupeCount = 0;
122   bool fastForward = false;
123   QList<Message>::const_iterator iter;
124   if(inOrder) {
125     iter = msglist.constEnd();
126     iter--; // this op is safe as we've allready passed an empty check
127   } else {
128     iter = msglist.constBegin();
129   }
130
131   int idx = indexForId((*iter).msgId());
132   if(idx >= 0)
133     dupeId = _messageList[idx]->msgId();
134
135   // we always compare to the previous entry...
136   // if there isn't, we can fastforward to the top
137   if(idx - 1 >= 0)
138     id = _messageList[idx - 1]->msgId();
139   else
140     fastForward = true;
141
142   if((*iter).msgId() != dupeId)
143     grouplist << *iter;
144   else
145     dupeCount++;
146
147   if(!inOrder)
148     iter++;
149
150   if(inOrder) {
151     while(iter != msglist.constBegin()) {
152       iter--;
153
154       if(!fastForward && (*iter).msgId() < id)
155         break;
156
157       if((*iter).msgId() != dupeId)
158         grouplist.prepend(*iter);
159       else
160         dupeCount++;
161     }
162   } else {
163     while(iter != msglist.constEnd()) {
164       if(!fastForward && (*iter).msgId() < id)
165         break;
166
167       if((*iter).msgId() != dupeId)
168         grouplist.prepend(*iter);
169       else
170         dupeCount++;
171
172       iter++;
173     }
174   }
175
176   insertMessageGroup(grouplist);
177   return grouplist.count() + dupeCount;
178 }
179
180 void MessageModel::customEvent(QEvent *event) {
181   if(event->type() != QEvent::User)
182     return;
183
184   event->accept();
185
186   if(_messageBuffer.isEmpty())
187     return;
188
189   int processedMsgs = insertMessagesGracefully(_messageBuffer);
190   int remainingMsgs = _messageBuffer.count() - processedMsgs;
191
192   QList<Message>::iterator removeStart = _messageBuffer.begin() + remainingMsgs;
193   QList<Message>::iterator removeEnd = _messageBuffer.end();
194   _messageBuffer.erase(removeStart, removeEnd);
195
196   if(!_messageBuffer.isEmpty())
197     QCoreApplication::postEvent(this, new ProcessBufferEvent());
198 }
199
200 void MessageModel::clear() {
201   beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
202   qDeleteAll(_messageList);
203   _messageList.clear();
204   endRemoveRows();
205 }
206
207
208
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();
213   // binary search
214   int start = 0; int end = _messageList.count()-1;
215   while(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;
219     else start = pivot;
220   }
221 }
222
223 /**********************************************************************************/
224
225 MessageModelItem::MessageModelItem(const Message &msg) :
226   _timestamp(msg.timestamp()),
227   _msgId(msg.msgId()),
228   _bufferId(msg.bufferInfo().bufferId()),
229   _type(msg.type()),
230   _flags(msg.flags())
231 {
232 }
233
234 QVariant MessageModelItem::data(int column, int role) const {
235   if(column < MessageModel::TimestampColumn || column > MessageModel::ContentsColumn)
236     return QVariant();
237
238   switch(role) {
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();
245   }
246 }
247
248
249 // Stuff for later
250 bool MessageModelItem::lessThan(const MessageModelItem *m1, const MessageModelItem *m2){
251   return (*m1) < (*m2);
252 }
253
254 bool MessageModelItem::operator<(const MessageModelItem &other) const {
255   return _msgId < other._msgId;
256 }
257
258 bool MessageModelItem::operator==(const MessageModelItem &other) const {
259   return _msgId == other._msgId;
260 }
261
262 bool MessageModelItem::operator>(const MessageModelItem &other) const {
263   return _msgId > other._msgId;
264 }