1 | /*************************************************************************** |
2 | * Copyright (C) 2005-2014 by the Quassel Project * |
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 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
19 | ***************************************************************************/ |
20 | |
21 | #ifndef MESSAGEMODEL_H_ |
22 | #define MESSAGEMODEL_H_ |
23 | |
24 | #include <QAbstractItemModel> |
25 | #include <QDateTime> |
26 | #include <QTimer> |
27 | |
28 | #include "message.h" |
29 | #include "types.h" |
30 | |
31 | class MessageModelItem; |
32 | struct MsgId; |
33 | |
34 | class MessageModel : public QAbstractItemModel |
35 | { |
36 | Q_OBJECT |
37 | |
38 | public: |
39 | enum MessageModelRole { |
40 | DisplayRole = Qt::DisplayRole, |
41 | EditRole = Qt::EditRole, |
42 | BackgroundRole = Qt::BackgroundRole, |
43 | MessageRole = Qt::UserRole, |
44 | MsgIdRole, |
45 | BufferIdRole, |
46 | TypeRole, |
47 | FlagsRole, |
48 | TimestampRole, |
49 | FormatRole, |
50 | ColumnTypeRole, |
51 | RedirectedToRole, |
52 | UserRole |
53 | }; |
54 | |
55 | enum ColumnType { |
56 | TimestampColumn, SenderColumn, ContentsColumn, UserColumnType |
57 | }; |
58 | |
59 | MessageModel(QObject *parent); |
60 | |
61 | inline QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; |
62 | inline QModelIndex parent(const QModelIndex &) const { return QModelIndex(); } |
63 | inline int rowCount(const QModelIndex &parent = QModelIndex()) const { return parent.isValid() ? 0 : messageCount(); } |
64 | inline int columnCount(const QModelIndex & /*parent*/ = QModelIndex()) const { return 3; } |
65 | |
66 | virtual QVariant data(const QModelIndex &index, int role) const; |
67 | virtual bool setData(const QModelIndex &index, const QVariant &value, int role); |
68 | |
69 | //virtual Qt::ItemFlags flags(const QModelIndex &index) const; |
70 | |
71 | bool insertMessage(const Message &, bool fakeMsg = false); |
72 | void insertMessages(const QList<Message> &); |
73 | |
74 | void clear(); |
75 | |
76 | signals: |
77 | void finishedBacklogFetch(BufferId bufferId); |
78 | |
79 | public slots: |
80 | void requestBacklog(BufferId bufferId); |
81 | void messagesReceived(BufferId bufferId, int count); |
82 | void buffersPermanentlyMerged(BufferId bufferId1, BufferId bufferId2); |
83 | void insertErrorMessage(BufferInfo bufferInfo, const QString &errorString); |
84 | |
85 | protected: |
86 | // virtual MessageModelItem *createMessageModelItem(const Message &) = 0; |
87 | |
88 | virtual int messageCount() const = 0; |
89 | virtual bool messagesIsEmpty() const = 0; |
90 | virtual const MessageModelItem *messageItemAt(int i) const = 0; |
91 | virtual MessageModelItem *messageItemAt(int i) = 0; |
92 | virtual const MessageModelItem *firstMessageItem() const = 0; |
93 | virtual MessageModelItem *firstMessageItem() = 0; |
94 | virtual const MessageModelItem *lastMessageItem() const = 0; |
95 | virtual MessageModelItem *lastMessageItem() = 0; |
96 | virtual void insertMessage__(int pos, const Message &) = 0; |
97 | virtual void insertMessages__(int pos, const QList<Message> &) = 0; |
98 | virtual void removeMessageAt(int i) = 0; |
99 | virtual void removeAllMessages() = 0; |
100 | virtual Message takeMessageAt(int i) = 0; |
101 | |
102 | virtual void customEvent(QEvent *event); |
103 | |
104 | private slots: |
105 | void changeOfDay(); |
106 | |
107 | private: |
108 | void insertMessageGroup(const QList<Message> &); |
109 | int insertMessagesGracefully(const QList<Message> &); // inserts as many contiguous msgs as possible. returns numer of inserted msgs. |
110 | int indexForId(MsgId); |
111 | |
112 | // QList<MessageModelItem *> _messageList; |
113 | QList<Message> _messageBuffer; |
114 | QTimer _dayChangeTimer; |
115 | QDateTime _nextDayChange; |
116 | QHash<BufferId, int> _messagesWaiting; |
117 | }; |
118 | |
119 | |
120 | // inlines |
121 | QModelIndex MessageModel::index(int row, int column, const QModelIndex &parent) const |
122 | { |
123 | if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent)) |
124 | return QModelIndex(); |
125 | |
126 | return createIndex(row, column); |
127 | } |
128 | |
129 | |
130 | // ************************************************** |
131 | // MessageModelItem |
132 | // ************************************************** |
133 | class MessageModelItem |
134 | { |
135 | public: |
136 | //! Creates a MessageModelItem from a Message object. |
137 | /** This baseclass implementation takes care of all Message data *except* the stylable strings. |
138 | * Subclasses need to provide Qt::DisplayRole at least, which should describe the plaintext |
139 | * strings without formattings (e.g. for searching purposes). |
140 | */ |
141 | MessageModelItem() {} |
142 | inline virtual ~MessageModelItem() {} |
143 | |
144 | virtual QVariant data(int column, int role) const; |
145 | virtual bool setData(int column, const QVariant &value, int role); |
146 | |
147 | virtual const Message &message() const = 0; |
148 | virtual const QDateTime ×tamp() const = 0; |
149 | virtual const MsgId &msgId() const = 0; |
150 | virtual const BufferId &bufferId() const = 0; |
151 | virtual void setBufferId(BufferId bufferId) = 0; |
152 | virtual Message::Type msgType() const = 0; |
153 | virtual Message::Flags msgFlags() const = 0; |
154 | |
155 | // For sorting |
156 | bool operator<(const MessageModelItem &) const; |
157 | bool operator==(const MessageModelItem &) const; |
158 | bool operator>(const MessageModelItem &) const; |
159 | static bool lessThan(const MessageModelItem *m1, const MessageModelItem *m2); |
160 | |
161 | private: |
162 | BufferId _redirectedTo; |
163 | }; |
164 | |
165 | |
166 | QDebug operator<<(QDebug dbg, const MessageModelItem &msgItem); |
167 | |
168 | #endif |
169 | |