modernize: Reformat ALL the source... again!
[quassel.git] / src / client / messagemodel.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 #pragma once
22
23 #include "client-export.h"
24
25 #include <QAbstractItemModel>
26 #include <QDateTime>
27 #include <QTimer>
28
29 #include "message.h"
30 #include "types.h"
31
32 class MessageModelItem;
33 struct MsgId;
34
35 class CLIENT_EXPORT MessageModel : public QAbstractItemModel
36 {
37     Q_OBJECT
38
39 public:
40     enum MessageModelRole
41     {
42         DisplayRole = Qt::DisplayRole,
43         EditRole = Qt::EditRole,
44         BackgroundRole = Qt::BackgroundRole,
45         MessageRole = Qt::UserRole,
46         MsgIdRole,
47         BufferIdRole,
48         TypeRole,
49         FlagsRole,
50         TimestampRole,
51         FormatRole,
52         ColumnTypeRole,
53         RedirectedToRole,
54         UserRole
55     };
56
57     enum ColumnType
58     {
59         TimestampColumn,
60         SenderColumn,
61         ContentsColumn,
62         UserColumnType
63     };
64
65     MessageModel(QObject* parent);
66
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; }
71
72     QVariant data(const QModelIndex& index, int role) const override;
73     bool setData(const QModelIndex& index, const QVariant& value, int role) override;
74
75     // virtual Qt::ItemFlags flags(const QModelIndex &index) const;
76
77     bool insertMessage(const Message&, bool fakeMsg = false);
78     void insertMessages(const QList<Message>&);
79
80     void clear();
81
82 signals:
83     void finishedBacklogFetch(BufferId bufferId);
84
85 public slots:
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);
90
91 protected:
92     //   virtual MessageModelItem *createMessageModelItem(const Message &) = 0;
93
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;
107
108     void customEvent(QEvent* event) override;
109
110 private slots:
111     void changeOfDay();
112
113 private:
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);
117
118     //  QList<MessageModelItem *> _messageList;
119     QList<Message> _messageBuffer;
120     QTimer _dayChangeTimer;
121     QDateTime _nextDayChange;
122     QHash<BufferId, int> _messagesWaiting;
123
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;
127 };
128
129 // inlines
130 QModelIndex MessageModel::index(int row, int column, const QModelIndex& parent) const
131 {
132     if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent))
133         return {};
134
135     return createIndex(row, column);
136 }
137
138 // **************************************************
139 //  MessageModelItem
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).
145  */
146 class CLIENT_EXPORT MessageModelItem
147 {
148 public:
149     inline virtual ~MessageModelItem() = default;
150
151     virtual QVariant data(int column, int role) const;
152     virtual bool setData(int column, const QVariant& value, int role);
153
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;
161
162     // For sorting
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);
167
168 private:
169     BufferId _redirectedTo;
170 };
171
172 QDebug operator<<(QDebug dbg, const MessageModelItem& msgItem);