fixing dupes
[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 MessageModel::MessageModel(QObject *parent)
26   : QAbstractItemModel(parent)
27 {
28 }
29
30 QVariant MessageModel::data(const QModelIndex &index, int role) const {
31   int row = index.row(); int column = index.column();
32   if(row < 0 || row >= _messageList.count() || column < 0)
33     return QVariant();
34
35   if(role == ColumnTypeRole)
36     return column;
37
38   return _messageList[row]->data(index.column(), role);
39 }
40
41 bool MessageModel::setData(const QModelIndex &index, const QVariant &value, int role) {
42   int row = index.row();
43   if(row < 0 || row >= _messageList.count())
44     return false;
45
46   if(_messageList[row]->setData(index.column(), value, role)) {
47     emit dataChanged(index, index);
48     return true;
49   }
50
51   return false;
52 }
53
54
55
56 bool MessageModel::insertMessage(const Message &msg, bool fakeMsg) {
57   MsgId id = msg.msgId();
58   int idx = indexForId(id);
59   if(!fakeMsg && idx < _messageList.count()) { // check for duplicate
60     if(_messageList[idx]->msgId() == id)
61       return false;
62   }
63
64   MessageModelItem *item = createMessageModelItem(msg);
65   beginInsertRows(QModelIndex(), idx, idx);
66   _messageList.insert(idx, item);
67   endInsertRows();
68   return true;
69 }
70
71
72 void MessageModel::insertMessageGroup(const QList<Message> &msglist) {
73   if(msglist.isEmpty()) return;
74
75   int idx = indexForId(msglist.first().msgId());
76   beginInsertRows(QModelIndex(), idx, idx+msglist.count()-1);
77
78   foreach(Message msg, msglist) {
79     _messageList.insert(idx, createMessageModelItem(msg));
80     idx++;
81   }
82
83   endInsertRows();
84 }
85
86
87 void MessageModel::insertMessages(const QList<Message> &msglist) {
88   if(msglist.isEmpty())
89     return;
90
91   if(_messageList.isEmpty()) {
92     insertMessageGroup(msglist);
93     return;
94   }
95
96   bool inOrder = (msglist.first().msgId() < msglist.last().msgId());
97   
98   // check if the whole bunch fits in at one position
99   if(indexForId(msglist.first().msgId()) == indexForId(msglist.last().msgId())) {
100     if(inOrder) {
101       insertMessageGroup(msglist);
102     } else {
103       QList<Message> messages = msglist;
104       qSort(messages);
105       insertMessageGroup(messages);
106     }
107     return;
108   }
109
110   // depending on the order we have to traverse from the front to the back or vice versa
111   // for the sake of performance we have a little code duplication here
112   // if you need to do some changes here you'll probably need to change them at all
113   // places marked DUPE
114
115
116   // FIXME: keep scrollbars from jumping
117   // the somewhat bulk insert leads to a jumpy scrollbar when the user requests further backlog.
118   // it would probably be the best to stop processing each time we actually insert a messagegroup
119   // and give back controll to the eventloop (similar to what the QtUiMessageProcessor used to do)
120   QList<Message> grouplist;
121   MsgId id;
122   MsgId dupeId;
123   bool fastForward = false;
124   QList<Message>::const_iterator iter;
125   if(inOrder) {
126     iter = msglist.constEnd();
127     iter--; // this op is safe as we've allready passed an empty check
128   } else {
129     iter = msglist.constBegin();
130   }
131
132   // DUPE (1 / 3)
133   int idx = indexForId((*iter).msgId());
134   if(idx > 0)
135     dupeId = _messageList[idx]->msgId();
136   // we always compare to the previous entry...
137   // if there isn't, we can fastforward to the top
138   if(idx - 1 >= 0) // also safe as we've passed another empty check
139     id = _messageList[idx - 1]->msgId();
140   else
141     fastForward = true;
142   if((*iter).msgId() != dupeId)
143     grouplist << *iter;
144
145   if(!inOrder)
146     iter++;
147
148   if(inOrder) {
149     while(iter != msglist.constBegin()) {
150       iter--;
151       // DUPE (2 / 3)
152       if(!fastForward && (*iter).msgId() < id) {
153         insertMessageGroup(grouplist);
154         grouplist.clear();
155         
156         // build new group
157         int idx = indexForId((*iter).msgId());
158         if(idx > 0)
159           dupeId = _messageList[idx]->msgId();
160         if(idx - 1 >= 0)
161           id = _messageList[idx - 1]->msgId();
162         else
163           fastForward = true;
164       }
165       if((*iter).msgId() != dupeId)
166         grouplist.prepend(*iter);
167     }
168   } else {
169     while(iter != msglist.constEnd()) {
170       // DUPE (3 / 3)
171       if(!fastForward && (*iter).msgId() < id) {
172         insertMessageGroup(grouplist);
173         grouplist.clear();
174         
175         // build new group
176         int idx = indexForId((*iter).msgId());
177         if(idx > 0)
178           dupeId = _messageList[idx]->msgId();
179         if(idx - 1 >= 0)
180           id = _messageList[idx - 1]->msgId();
181         else
182           fastForward = true;
183       }
184       if((*iter).msgId() != dupeId)
185         grouplist.prepend(*iter);
186       iter++;
187     }
188   }
189
190   if(!grouplist.isEmpty()) {
191     insertMessageGroup(grouplist);
192   }
193     
194   return;
195 }
196
197
198 void MessageModel::clear() {
199   beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
200   qDeleteAll(_messageList);
201   _messageList.clear();
202   endRemoveRows();
203 }
204
205
206 // returns index of msg with given Id or of the next message after that (i.e., the index where we'd insert this msg)
207 int MessageModel::indexForId(MsgId id) {
208   if(_messageList.isEmpty() || id <= _messageList.value(0)->data(0, MsgIdRole).value<MsgId>()) return 0;
209   if(id > _messageList.last()->data(0, MsgIdRole).value<MsgId>()) return _messageList.count();
210   // binary search
211   int start = 0; int end = _messageList.count()-1;
212   while(1) {
213     if(end - start == 1) return end;
214     int pivot = (end + start) / 2;
215     if(id <= _messageList.value(pivot)->data(0, MsgIdRole).value<MsgId>()) end = pivot;
216     else start = pivot;
217   }
218 }
219
220 /**********************************************************************************/
221
222 MessageModelItem::MessageModelItem(const Message &msg) :
223   _timestamp(msg.timestamp()),
224   _msgId(msg.msgId()),
225   _bufferId(msg.bufferInfo().bufferId()),
226   _type(msg.type()),
227   _flags(msg.flags())
228 {
229 }
230
231 QVariant MessageModelItem::data(int column, int role) const {
232   if(column < MessageModel::TimestampColumn || column > MessageModel::ContentsColumn)
233     return QVariant();
234
235   switch(role) {
236   case MessageModel::MsgIdRole: return QVariant::fromValue<MsgId>(_msgId);
237   case MessageModel::BufferIdRole: return QVariant::fromValue<BufferId>(_bufferId);
238   case MessageModel::TypeRole: return _type;
239   case MessageModel::FlagsRole: return (int)_flags;
240   case MessageModel::TimestampRole: return _timestamp;
241   default: return QVariant();
242   }
243 }
244
245
246 // Stuff for later
247 bool MessageModelItem::lessThan(const MessageModelItem *m1, const MessageModelItem *m2){ 
248   return (*m1) < (*m2);
249 }
250
251 bool MessageModelItem::operator<(const MessageModelItem &other) const {
252   return _msgId < other._msgId;
253 }
254
255 bool MessageModelItem::operator==(const MessageModelItem &other) const {
256   return _msgId == other._msgId;
257 }
258
259 bool MessageModelItem::operator>(const MessageModelItem &other) const {
260   return _msgId > other._msgId;
261 }