ChatScene now properly react on aboutToRemoveRows(), which should improve stability...
[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 bool MessageModel::insertMessage(const Message &msg, bool fakeMsg) {
55   MsgId id = msg.msgId();
56   int idx = indexForId(id);
57   if(!fakeMsg && idx < _messageList.count()) { // check for duplicate
58     if(_messageList[idx]->msgId() == id)
59       return false;
60   }
61
62   MessageModelItem *item = createMessageModelItem(msg);
63   beginInsertRows(QModelIndex(), idx, idx);
64   _messageList.insert(idx, item);
65   endInsertRows();
66   return true;
67 }
68
69 void MessageModel::insertMessages(const QList<Message> &msglist) {
70   if(msglist.isEmpty()) return;
71   // FIXME make this more efficient by grouping msgs
72   foreach(Message msg, msglist)
73     insertMessage(msg);
74 }
75
76 void MessageModel::clear() {
77   beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
78   qDeleteAll(_messageList);
79   _messageList.clear();
80   endRemoveRows();
81 }
82
83 // returns index of msg with given Id or of the next message after that (i.e., the index where we'd insert this msg)
84 int MessageModel::indexForId(MsgId id) {
85   if(_messageList.isEmpty() || id <= _messageList.value(0)->data(0, MsgIdRole).value<MsgId>()) return 0;
86   if(id > _messageList.last()->data(0, MsgIdRole).value<MsgId>()) return _messageList.count();
87   // binary search
88   int start = 0; int end = _messageList.count()-1;
89   while(1) {
90     if(end - start == 1) return end;
91     int pivot = (end + start) / 2;
92     if(id <= _messageList.value(pivot)->data(0, MsgIdRole).value<MsgId>()) end = pivot;
93     else start = pivot;
94   }
95 }
96
97 /**********************************************************************************/
98
99 MessageModelItem::MessageModelItem(const Message &msg) :
100   _timestamp(msg.timestamp()),
101   _msgId(msg.msgId()),
102   _bufferId(msg.bufferInfo().bufferId()),
103   _type(msg.type()),
104   _flags(msg.flags())
105 {
106 }
107
108 QVariant MessageModelItem::data(int column, int role) const {
109   if(column < MessageModel::TimestampColumn || column > MessageModel::ContentsColumn)
110     return QVariant();
111
112   switch(role) {
113   case MessageModel::MsgIdRole: return QVariant::fromValue<MsgId>(_msgId);
114   case MessageModel::BufferIdRole: return QVariant::fromValue<BufferId>(_bufferId);
115   case MessageModel::TypeRole: return _type;
116   case MessageModel::FlagsRole: return (int)_flags;
117   case MessageModel::TimestampRole: return _timestamp;
118   default: return QVariant();
119   }
120 }
121