1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
23 #include "client-export.h"
25 #include <QAbstractItemModel>
32 class MessageModelItem;
35 class CLIENT_EXPORT MessageModel : public QAbstractItemModel
40 enum MessageModelRole {
41 DisplayRole = Qt::DisplayRole,
42 EditRole = Qt::EditRole,
43 BackgroundRole = Qt::BackgroundRole,
44 MessageRole = Qt::UserRole,
57 TimestampColumn, SenderColumn, ContentsColumn, UserColumnType
60 MessageModel(QObject *parent);
62 inline QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
63 inline QModelIndex parent(const QModelIndex &) const override { return {}; }
64 inline int rowCount(const QModelIndex &parent = QModelIndex()) const override { return parent.isValid() ? 0 : messageCount(); }
65 inline int columnCount(const QModelIndex & /*parent*/ = QModelIndex()) const override { return 3; }
67 QVariant data(const QModelIndex &index, int role) const override;
68 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
70 //virtual Qt::ItemFlags flags(const QModelIndex &index) const;
72 bool insertMessage(const Message &, bool fakeMsg = false);
73 void insertMessages(const QList<Message> &);
78 void finishedBacklogFetch(BufferId bufferId);
81 void requestBacklog(BufferId bufferId);
82 void messagesReceived(BufferId bufferId, int count);
83 void buffersPermanentlyMerged(BufferId bufferId1, BufferId bufferId2);
84 void insertErrorMessage(BufferInfo bufferInfo, const QString &errorString);
87 // virtual MessageModelItem *createMessageModelItem(const Message &) = 0;
89 virtual int messageCount() const = 0;
90 virtual bool messagesIsEmpty() const = 0;
91 virtual const MessageModelItem *messageItemAt(int i) const = 0;
92 virtual MessageModelItem *messageItemAt(int i) = 0;
93 virtual const MessageModelItem *firstMessageItem() const = 0;
94 virtual MessageModelItem *firstMessageItem() = 0;
95 virtual const MessageModelItem *lastMessageItem() const = 0;
96 virtual MessageModelItem *lastMessageItem() = 0;
97 virtual void insertMessage__(int pos, const Message &) = 0;
98 virtual void insertMessages__(int pos, const QList<Message> &) = 0;
99 virtual void removeMessageAt(int i) = 0;
100 virtual void removeAllMessages() = 0;
101 virtual Message takeMessageAt(int i) = 0;
103 void customEvent(QEvent *event) override;
109 void insertMessageGroup(const QList<Message> &);
110 int insertMessagesGracefully(const QList<Message> &); // inserts as many contiguous msgs as possible. returns numer of inserted msgs.
111 int indexForId(MsgId);
113 // QList<MessageModelItem *> _messageList;
114 QList<Message> _messageBuffer;
115 QTimer _dayChangeTimer;
116 QDateTime _nextDayChange;
117 QHash<BufferId, int> _messagesWaiting;
119 /// Period of time for one day in milliseconds
120 /// 24 hours * 60 minutes * 60 seconds * 1000 milliseconds
121 const qint64 DAY_IN_MSECS = 24 * 60 * 60 * 1000;
126 QModelIndex MessageModel::index(int row, int column, const QModelIndex &parent) const
128 if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent))
131 return createIndex(row, column);
135 // **************************************************
137 // **************************************************
138 //! Creates a MessageModelItem from a Message object.
139 /** This baseclass implementation takes care of all Message data *except* the stylable strings.
140 * Subclasses need to provide Qt::DisplayRole at least, which should describe the plaintext
141 * strings without formattings (e.g. for searching purposes).
143 class CLIENT_EXPORT MessageModelItem
146 inline virtual ~MessageModelItem() = default;
148 virtual QVariant data(int column, int role) const;
149 virtual bool setData(int column, const QVariant &value, int role);
151 virtual const Message &message() const = 0;
152 virtual const QDateTime ×tamp() const = 0;
153 virtual const MsgId &msgId() const = 0;
154 virtual const BufferId &bufferId() const = 0;
155 virtual void setBufferId(BufferId bufferId) = 0;
156 virtual Message::Type msgType() const = 0;
157 virtual Message::Flags msgFlags() const = 0;
160 bool operator<(const MessageModelItem &) const;
161 bool operator==(const MessageModelItem &) const;
162 bool operator>(const MessageModelItem &) const;
163 static bool lessThan(const MessageModelItem *m1, const MessageModelItem *m2);
166 BufferId _redirectedTo;
170 QDebug operator<<(QDebug dbg, const MessageModelItem &msgItem);