qtui: Set proper icon for "About Quassel" menu option
[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 #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     /// Period of time for one day in milliseconds
119     /// 24 hours * 60 minutes * 60 seconds * 1000 milliseconds
120     const qint64 DAY_IN_MSECS = 24 * 60 * 60 * 1000;
121 };
122
123
124 // inlines
125 QModelIndex MessageModel::index(int row, int column, const QModelIndex &parent) const
126 {
127     if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent))
128         return QModelIndex();
129
130     return createIndex(row, column);
131 }
132
133
134 // **************************************************
135 //  MessageModelItem
136 // **************************************************
137 class MessageModelItem
138 {
139 public:
140     //! Creates a MessageModelItem from a Message object.
141     /** This baseclass implementation takes care of all Message data *except* the stylable strings.
142      *  Subclasses need to provide Qt::DisplayRole at least, which should describe the plaintext
143      *  strings without formattings (e.g. for searching purposes).
144      */
145     MessageModelItem() {}
146     inline virtual ~MessageModelItem() {}
147
148     virtual QVariant data(int column, int role) const;
149     virtual bool setData(int column, const QVariant &value, int role);
150
151     virtual const Message &message() const = 0;
152     virtual const QDateTime &timestamp() 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;
158
159     // For sorting
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);
164
165 private:
166     BufferId _redirectedTo;
167 };
168
169
170 QDebug operator<<(QDebug dbg, const MessageModelItem &msgItem);
171
172 #endif