1 /*
2   Copyright (C) 2008-2020 The Communi Project
3 
4   You may use this file under the terms of BSD license as follows:
5 
6   Redistribution and use in source and binary forms, with or without
7   modification, are permitted provided that the following conditions are met:
8     * Redistributions of source code must retain the above copyright
9       notice, this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright
11       notice, this list of conditions and the following disclaimer in the
12       documentation and/or other materials provided with the distribution.
13     * Neither the name of the copyright holder nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
21   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef IRCBUFFERMODEL_H
30 #define IRCBUFFERMODEL_H
31 
32 #include <Irc>
33 #include <IrcGlobal>
34 #include <QtCore/qmetatype.h>
35 #include <QtCore/qstringlist.h>
36 #include <QtCore/qabstractitemmodel.h>
37 
38 IRC_BEGIN_NAMESPACE
39 
40 class IrcBuffer;
41 class IrcChannel;
42 class IrcMessage;
43 class IrcNetwork;
44 class IrcConnection;
45 class IrcBufferModelPrivate;
46 
47 class IRC_MODEL_EXPORT IrcBufferModel : public QAbstractListModel
48 {
49     Q_OBJECT
50     Q_PROPERTY(int count READ count NOTIFY countChanged)
51     Q_PROPERTY(bool empty READ isEmpty NOTIFY emptyChanged)
52     Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder)
53     Q_PROPERTY(Irc::SortMethod sortMethod READ sortMethod WRITE setSortMethod)
54     Q_PROPERTY(QStringList channels READ channels NOTIFY channelsChanged)
55     Q_PROPERTY(Irc::DataRole displayRole READ displayRole WRITE setDisplayRole)
56     Q_PROPERTY(bool persistent READ isPersistent WRITE setPersistent NOTIFY persistentChanged)
57     Q_PROPERTY(QList<IrcBuffer*> buffers READ buffers NOTIFY buffersChanged)
58     Q_PROPERTY(IrcConnection* connection READ connection WRITE setConnection NOTIFY connectionChanged)
59     Q_PROPERTY(IrcNetwork* network READ network NOTIFY networkChanged)
60     Q_PROPERTY(IrcBuffer* bufferPrototype READ bufferPrototype WRITE setBufferPrototype NOTIFY bufferPrototypeChanged)
61     Q_PROPERTY(IrcChannel* channelPrototype READ channelPrototype WRITE setChannelPrototype NOTIFY channelPrototypeChanged)
62     Q_PROPERTY(int joinDelay READ joinDelay WRITE setJoinDelay NOTIFY joinDelayChanged)
63     Q_PROPERTY(bool monitorEnabled READ isMonitorEnabled WRITE setMonitorEnabled NOTIFY monitorEnabledChanged)
64 
65 public:
66     explicit IrcBufferModel(QObject* parent = nullptr);
67     ~IrcBufferModel() override;
68 
69     IrcConnection* connection() const;
70     void setConnection(IrcConnection* connection);
71 
72     IrcNetwork* network() const;
73 
74     int count() const;
75     bool isEmpty() const;
76     QStringList channels() const;
77     QList<IrcBuffer*> buffers() const;
78     Q_INVOKABLE IrcBuffer* get(int index) const;
79     Q_INVOKABLE IrcBuffer* find(const QString& title) const;
80     Q_INVOKABLE bool contains(const QString& title) const;
81     Q_INVOKABLE int indexOf(IrcBuffer* buffer) const;
82 
83     Q_INVOKABLE IrcBuffer* add(const QString& title);
84     Q_INVOKABLE void add(IrcBuffer* buffer);
85     Q_INVOKABLE void remove(const QString& title);
86     Q_INVOKABLE void remove(IrcBuffer* buffer);
87 
88     Qt::SortOrder sortOrder() const;
89     void setSortOrder(Qt::SortOrder order);
90 
91     Irc::SortMethod sortMethod() const;
92     void setSortMethod(Irc::SortMethod method);
93 
94     Irc::DataRole displayRole() const;
95     void setDisplayRole(Irc::DataRole role);
96 
97     bool isPersistent() const;
98     void setPersistent(bool persistent);
99 
100     QModelIndex index(IrcBuffer* buffer) const;
101     IrcBuffer* buffer(const QModelIndex& index) const;
102 
103 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
104     QHash<int, QByteArray> roleNames() const override;
105 #endif
106     int rowCount(const QModelIndex& parent = QModelIndex()) const override;
107     QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
108     QModelIndex index(int row, int column = 0, const QModelIndex& parent = QModelIndex()) const override;
109 
110     IrcBuffer* bufferPrototype() const;
111     void setBufferPrototype(IrcBuffer* prototype);
112 
113     IrcChannel* channelPrototype() const;
114     void setChannelPrototype(IrcChannel* prototype);
115 
116     int joinDelay() const;
117     void setJoinDelay(int delay);
118 
119     bool isMonitorEnabled() const;
120     void setMonitorEnabled(bool enabled);
121 
122     Q_INVOKABLE QByteArray saveState(int version = 0) const;
123     Q_INVOKABLE bool restoreState(const QByteArray& state, int version = 0);
124 
125 public Q_SLOTS:
126     void clear();
127     void receiveMessage(IrcMessage* message);
128     void sort(int column = 0, Qt::SortOrder order = Qt::AscendingOrder) override;
129     void sort(Irc::SortMethod method, Qt::SortOrder order = Qt::AscendingOrder);
130 
131 Q_SIGNALS:
132     void countChanged(int count);
133     void emptyChanged(bool empty);
134     void added(IrcBuffer* buffer);
135     void removed(IrcBuffer* buffer);
136     void aboutToBeAdded(IrcBuffer* buffer);
137     void aboutToBeRemoved(IrcBuffer* buffer);
138     void persistentChanged(bool persistent);
139     void buffersChanged(const QList<IrcBuffer*>& buffers);
140     void channelsChanged(const QStringList& channels);
141     void connectionChanged(IrcConnection* connection);
142     void networkChanged(IrcNetwork* network);
143     void messageIgnored(IrcMessage* message);
144     void bufferPrototypeChanged(IrcBuffer* prototype);
145     void channelPrototypeChanged(IrcChannel* prototype);
146     void destroyed(IrcBufferModel* model);
147     void joinDelayChanged(int delay);
148     void monitorEnabledChanged(bool enabled);
149 
150 protected Q_SLOTS:
151     virtual IrcBuffer* createBuffer(const QString& title);
152     virtual IrcChannel* createChannel(const QString& title);
153 
154 protected:
155     virtual bool lessThan(IrcBuffer* one, IrcBuffer* another, Irc::SortMethod method) const;
156 
157 private:
158     friend class IrcBufferLessThan;
159     friend class IrcBufferGreaterThan;
160     QScopedPointer<IrcBufferModelPrivate> d_ptr;
161     Q_DECLARE_PRIVATE(IrcBufferModel)
162     Q_DISABLE_COPY(IrcBufferModel)
163 
164     Q_PRIVATE_SLOT(d_func(), void _irc_connected())
165     Q_PRIVATE_SLOT(d_func(), void _irc_initialized())
166     Q_PRIVATE_SLOT(d_func(), void _irc_disconnected())
167     Q_PRIVATE_SLOT(d_func(), void _irc_bufferDestroyed(IrcBuffer*))
168     Q_PRIVATE_SLOT(d_func(), void _irc_restoreBuffers())
169     Q_PRIVATE_SLOT(d_func(), void _irc_monitorStatus())
170 };
171 
172 IRC_END_NAMESPACE
173 
174 Q_DECLARE_METATYPE(IRC_PREPEND_NAMESPACE(IrcBufferModel*))
175 
176 #endif // IRCBUFFERMODEL_H
177