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"
25 MessageModel::MessageModel(QObject *parent) : QAbstractItemModel(parent) {
31 MessageModel::~MessageModel() {
36 QVariant MessageModel::data(const QModelIndex &index, int role) const {
37 int row = index.row();
38 if(row < 0 || row >= _messageList.count()) return QVariant();
39 return _messageList[row]->data(index.column(), role);
42 bool MessageModel::setData(const QModelIndex &index, const QVariant &value, int role) {
43 int row = index.row();
44 if(row < 0 || row >= _messageList.count()) return false;
45 if(_messageList[row]->setData(index.column(), value, role)) {
46 emit dataChanged(index, index);
52 void MessageModel::insertMessage(const Message &msg) {
53 MsgId id = msg.msgId();
54 int idx = indexForId(id);
55 MessageItem *item = createMessageItem(msg);
56 beginInsertRows(QModelIndex(), idx, idx);
57 _messageList.insert(idx, item);
61 void MessageModel::insertMessages(const QList<Message> &msglist) {
62 if(msglist.isEmpty()) return;
63 // FIXME make this more efficient by grouping msgs
64 foreach(Message msg, msglist) insertMessage(msg);
68 // returns index of msg with given Id or of the next message after that (i.e., the index where we'd insert this msg)
69 int MessageModel::indexForId(MsgId id) {
70 if(_messageList.isEmpty() || id <= _messageList[0]->data(0, MsgIdRole).value<MsgId>()) return 0;
71 if(id > _messageList.last()->data(0, MsgIdRole).value<MsgId>()) return _messageList.count();
73 int start = 0; int end = _messageList.count()-1;
75 if(end - start == 1) return end;
76 int pivot = (end + start) / 2;
77 if(id <= _messageList[pivot]->data(0, MsgIdRole).value<MsgId>()) end = pivot;
82 /**********************************************************************************/
84 MessageItem::MessageItem(const Message &msg) {
85 _timestamp = msg.timestamp();
87 _bufferId = msg.bufferInfo().bufferId();
93 MessageItem::~MessageItem() {
97 QVariant MessageItem::data(int column, int role) const {
98 if(column < MessageModel::TimestampColumn || column > MessageModel::TextColumn) return QVariant();
100 case MessageModel::MsgIdRole: return QVariant::fromValue<MsgId>(_msgId);
101 case MessageModel::BufferIdRole: return QVariant::fromValue<BufferId>(_bufferId);
102 case MessageModel::TypeRole: return _type;
103 case MessageModel::FlagsRole: return (int)_flags;
104 case MessageModel::TimestampRole: return _timestamp;
105 default: return QVariant();