1 /***************************************************************************
2 * Copyright (C) 2005-2022 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
42 DisplayRole = Qt::DisplayRole,
43 EditRole = Qt::EditRole,
44 BackgroundRole = Qt::BackgroundRole,
45 MessageRole = Qt::UserRole,
65 MessageModel(QObject* parent);
67 inline QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
68 inline QModelIndex parent(const QModelIndex&) const override { return {}; }
69 inline int rowCount(const QModelIndex& parent = QModelIndex()) const override { return parent.isValid() ? 0 : messageCount(); }
70 inline int columnCount(const QModelIndex& /*parent*/ = QModelIndex()) const override { return 3; }
72 QVariant data(const QModelIndex& index, int role) const override;
73 bool setData(const QModelIndex& index, const QVariant& value, int role) override;
75 // virtual Qt::ItemFlags flags(const QModelIndex &index) const;
77 bool insertMessage(const Message&, bool fakeMsg = false);
78 void insertMessages(const QList<Message>&);
83 void finishedBacklogFetch(BufferId bufferId);
86 void requestBacklog(BufferId bufferId);
87 void messagesReceived(BufferId bufferId, int count);
88 void buffersPermanentlyMerged(BufferId bufferId1, BufferId bufferId2);
89 void insertErrorMessage(BufferInfo bufferInfo, const QString& errorString);
92 // virtual MessageModelItem *createMessageModelItem(const Message &) = 0;
94 virtual int messageCount() const = 0;
95 virtual bool messagesIsEmpty() const = 0;
96 virtual const MessageModelItem* messageItemAt(int i) const = 0;
97 virtual MessageModelItem* messageItemAt(int i) = 0;
98 virtual const MessageModelItem* firstMessageItem() const = 0;
99 virtual MessageModelItem* firstMessageItem() = 0;
100 virtual const MessageModelItem* lastMessageItem() const = 0;
101 virtual MessageModelItem* lastMessageItem() = 0;
102 virtual void insertMessage__(int pos, const Message&) = 0;
103 virtual void insertMessages__(int pos, const QList<Message>&) = 0;
104 virtual void removeMessageAt(int i) = 0;
105 virtual void removeAllMessages() = 0;
106 virtual Message takeMessageAt(int i) = 0;
108 void customEvent(QEvent* event) override;
114 void insertMessageGroup(const QList<Message>&);
115 int insertMessagesGracefully(const QList<Message>&); // inserts as many contiguous msgs as possible. returns numer of inserted msgs.
116 int indexForId(MsgId);
118 // QList<MessageModelItem *> _messageList;
119 QList<Message> _messageBuffer;
120 QTimer _dayChangeTimer;
121 QDateTime _nextDayChange;
122 QHash<BufferId, int> _messagesWaiting;
124 /// Period of time for one day in milliseconds
125 /// 24 hours * 60 minutes * 60 seconds * 1000 milliseconds
126 const qint64 DAY_IN_MSECS = 24 * 60 * 60 * 1000;
130 QModelIndex MessageModel::index(int row, int column, const QModelIndex& parent) const
132 if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent))
135 return createIndex(row, column);
138 // **************************************************
140 // **************************************************
141 //! Creates a MessageModelItem from a Message object.
142 /** This baseclass implementation takes care of all Message data *except* the stylable strings.
143 * Subclasses need to provide Qt::DisplayRole at least, which should describe the plaintext
144 * strings without formattings (e.g. for searching purposes).
146 class CLIENT_EXPORT MessageModelItem
149 inline virtual ~MessageModelItem() = default;
151 virtual QVariant data(int column, int role) const;
152 virtual bool setData(int column, const QVariant& value, int role);
154 virtual const Message& message() const = 0;
155 virtual const QDateTime& timestamp() const = 0;
156 virtual const MsgId& msgId() const = 0;
157 virtual const BufferId& bufferId() const = 0;
158 virtual void setBufferId(BufferId bufferId) = 0;
159 virtual Message::Type msgType() const = 0;
160 virtual Message::Flags msgFlags() const = 0;
163 bool operator<(const MessageModelItem&) const;
164 bool operator==(const MessageModelItem&) const;
165 bool operator>(const MessageModelItem&) const;
166 static bool lessThan(const MessageModelItem* m1, const MessageModelItem* m2);
169 BufferId _redirectedTo;
172 QDebug operator<<(QDebug dbg, const MessageModelItem& msgItem);