src: Mark symbols to be exported where needed
[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         DisplayRole = Qt::DisplayRole,
42         EditRole = Qt::EditRole,
43         BackgroundRole = Qt::BackgroundRole,
44         MessageRole = Qt::UserRole,
45         MsgIdRole,
46         BufferIdRole,
47         TypeRole,
48         FlagsRole,
49         TimestampRole,
50         FormatRole,
51         ColumnTypeRole,
52         RedirectedToRole,
53         UserRole
54     };
55
56     enum ColumnType {
57         TimestampColumn, SenderColumn, ContentsColumn, UserColumnType
58     };
59
60     MessageModel(QObject *parent);
61
62     inline QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
63     inline QModelIndex parent(const QModelIndex &) const { return QModelIndex(); }
64     inline int rowCount(const QModelIndex &parent = QModelIndex()) const { return parent.isValid() ? 0 : messageCount(); }
65     inline int columnCount(const QModelIndex & /*parent*/ = QModelIndex()) const { return 3; }
66
67     virtual QVariant data(const QModelIndex &index, int role) const;
68     virtual bool setData(const QModelIndex &index, const QVariant &value, int role);
69
70     //virtual Qt::ItemFlags flags(const QModelIndex &index) const;
71
72     bool insertMessage(const Message &, bool fakeMsg = false);
73     void insertMessages(const QList<Message> &);
74
75     void clear();
76
77 signals:
78     void finishedBacklogFetch(BufferId bufferId);
79
80 public slots:
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);
85
86 protected:
87 //   virtual MessageModelItem *createMessageModelItem(const Message &) = 0;
88
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;
102
103     virtual void customEvent(QEvent *event);
104
105 private slots:
106     void changeOfDay();
107
108 private:
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);
112
113     //  QList<MessageModelItem *> _messageList;
114     QList<Message> _messageBuffer;
115     QTimer _dayChangeTimer;
116     QDateTime _nextDayChange;
117     QHash<BufferId, int> _messagesWaiting;
118
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;
122 };
123
124
125 // inlines
126 QModelIndex MessageModel::index(int row, int column, const QModelIndex &parent) const
127 {
128     if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent))
129         return QModelIndex();
130
131     return createIndex(row, column);
132 }
133
134
135 // **************************************************
136 //  MessageModelItem
137 // **************************************************
138 class CLIENT_EXPORT MessageModelItem
139 {
140 public:
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     MessageModelItem() {}
147     inline virtual ~MessageModelItem() {}
148
149     virtual QVariant data(int column, int role) const;
150     virtual bool setData(int column, const QVariant &value, int role);
151
152     virtual const Message &message() const = 0;
153     virtual const QDateTime &timestamp() const = 0;
154     virtual const MsgId &msgId() const = 0;
155     virtual const BufferId &bufferId() const = 0;
156     virtual void setBufferId(BufferId bufferId) = 0;
157     virtual Message::Type msgType() const = 0;
158     virtual Message::Flags msgFlags() const = 0;
159
160     // For sorting
161     bool operator<(const MessageModelItem &) const;
162     bool operator==(const MessageModelItem &) const;
163     bool operator>(const MessageModelItem &) const;
164     static bool lessThan(const MessageModelItem *m1, const MessageModelItem *m2);
165
166 private:
167     BufferId _redirectedTo;
168 };
169
170
171 QDebug operator<<(QDebug dbg, const MessageModelItem &msgItem);