modernize: Require member function pointers for Settings::notify()
[quassel.git] / src / uisupport / networkmodelcontroller.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 "uisupport-export.h"
24
25 #include <functional>
26 #include <type_traits>
27
28 #include <QDialog>
29
30 #include "action.h"
31 #include "actioncollection.h"
32 #include "messagefilter.h"
33
34 class QComboBox;
35 class QDialogButtonBox;
36 class QLineEdit;
37
38 class UISUPPORT_EXPORT NetworkModelController : public QObject
39 {
40     Q_OBJECT
41
42 public:
43     NetworkModelController(QObject *parent = nullptr);
44
45     // don't change enums without doublechecking masks etc. in code
46     enum ActionType {
47         // Network actions
48         NetworkMask = 0x0f,
49         NetworkConnect = 0x01,
50         NetworkDisconnect = 0x02,
51         NetworkConnectAllWithDropdown = 0x03,
52         NetworkDisconnectAllWithDropdown = 0x04,
53         NetworkConnectAll = 0x05,
54         NetworkDisconnectAll = 0x06,
55
56         // Buffer actions
57         BufferMask = 0xf0,
58         BufferJoin = 0x10,
59         BufferPart = 0x20,
60         BufferSwitchTo = 0x30,
61         BufferRemove = 0x40,
62
63         // Hide actions
64         HideMask = 0x0f00,
65         HideJoin = 0x0100,
66         HidePart = 0x0200,
67         HideQuit = 0x0300,
68         HideNick = 0x0400,
69         HideMode = 0x0500,
70         HideDayChange = 0x0600,
71         HideTopic = 0x0700,
72         HideJoinPartQuit = 0xd00,
73         HideUseDefaults = 0xe00,
74         HideApplyToAll = 0xf00,
75
76         // General actions
77         GeneralMask = 0xf000,
78         JoinChannel = 0x1000,
79         ShowChannelList = 0x2000,
80         ShowIgnoreList = 0x3000,
81         ShowNetworkConfig = 0x4000,
82
83         // Nick actions
84         NickMask = 0xff0000,
85         NickWhois = 0x010000,
86         NickQuery = 0x020000,
87         NickSwitchTo = 0x030000,
88         NickCtcpVersion = 0x040000,
89         NickCtcpPing = 0x050000,
90         NickCtcpTime = 0x060000,
91         NickCtcpClientinfo = 0x070000,
92         NickOp = 0x080000,
93         NickDeop = 0x090000,
94         NickVoice = 0x0a0000,
95         NickDevoice = 0x0b0000,
96         NickHalfop = 0x0c0000,
97         NickDehalfop = 0x0d0000,
98         NickKick = 0x0e0000,
99         NickBan = 0x0f0000,
100         NickKickBan = 0x100000,
101         NickIgnoreUser = 0x200000,
102         NickIgnoreHost = 0x300000,
103         NickIgnoreDomain = 0x400000,
104         NickIgnoreCustom = 0x500000,
105         // The next 5 types have to stay together
106         // Don't change without reading ContextMenuActionProvider::addIgnoreMenu!
107         NickIgnoreToggleEnabled0 = 0x600000,
108         NickIgnoreToggleEnabled1 = 0x700000,
109         NickIgnoreToggleEnabled2 = 0x800000,
110         NickIgnoreToggleEnabled3 = 0x900000,
111         NickIgnoreToggleEnabled4 = 0xa00000,
112
113         // Actions that are handled externally
114         // These emit a signal to the action requester, rather than being handled here
115         ExternalMask = 0xff000000,
116         HideBufferTemporarily = 0x01000000,
117         HideBufferPermanently = 0x02000000
118     };
119
120     inline Action *action(ActionType type) const;
121
122 public:
123     enum ItemActiveState {
124         InactiveState = 0x01,
125         ActiveState = 0x02
126     };
127     Q_DECLARE_FLAGS(ItemActiveStates, ItemActiveState)
128
129 public slots:
130     virtual void connectedToCore() {}
131     virtual void disconnectedFromCore() {}
132
133 protected:
134     inline ActionCollection *actionCollection() const;
135     inline QList<QModelIndex> indexList() const;
136     inline MessageFilter *messageFilter() const;
137     inline QString contextItem() const;  ///< Channel name or nick to provide context menu for
138
139     void setIndexList(const QModelIndex &);
140     void setIndexList(const QList<QModelIndex> &);
141     void setMessageFilter(MessageFilter *);
142     void setContextItem(const QString &);
143
144     using ActionSlot = std::function<void(QAction*)>;
145
146     template<typename Receiver, typename Slot>
147     ActionSlot buildActionSlot(Receiver *receiver, Slot slot)
148     {
149         static_assert(!std::is_same<Slot, const char*>::value, "Old-style slots not supported");
150         return [receiver, slot = std::move(slot)](QAction *action) {
151             (receiver->*slot)(action);
152         };
153     }
154
155     void setSlot(ActionSlot slot);
156
157     Action *registerAction(ActionType type, const QString &text, bool checkable = false);
158     Action *registerAction(NetworkModelController::ActionType type, const QIcon &icon, const QString &text, bool checkable = false);
159     bool checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState = QFlags<ItemActiveState>(ActiveState | InactiveState));
160
161     QString nickName(const QModelIndex &index) const;
162     BufferId findQueryBuffer(const QModelIndex &index, const QString &predefinedNick = QString()) const;
163     BufferId findQueryBuffer(NetworkId, const QString &nickName) const;
164     void removeBuffers(const QModelIndexList &indexList);
165
166 protected slots:
167     virtual void actionTriggered(QAction *);
168
169 signals:
170     /**
171      * Request to show the channel list dialog for the network, optionally searching by channel name
172      *
173      * @see MainWin::showChannelList()
174      *
175      * @param networkId        Network ID for associated network
176      * @param channelFilters   Partial channel name to search for, or empty to show all
177      * @param listImmediately  If true, immediately list channels, otherwise just show dialog
178      */
179     void showChannelList(NetworkId, const QString &, bool);
180     void showNetworkConfig(NetworkId);
181     void showIgnoreList(QString);
182
183 protected:
184     virtual void handleNetworkAction(ActionType, QAction *);
185     virtual void handleBufferAction(ActionType, QAction *);
186     virtual void handleHideAction(ActionType, QAction *);
187     virtual void handleNickAction(ActionType, QAction *action);
188     virtual void handleGeneralAction(ActionType, QAction *);
189     virtual void handleExternalAction(ActionType, QAction *);
190
191     class JoinDlg;
192
193 private:
194     NetworkModel *_model;
195
196     ActionCollection *_actionCollection;
197     QHash<ActionType, Action *> _actionByType;
198
199     QList<QModelIndex> _indexList;
200     MessageFilter *_messageFilter{nullptr};
201     QString _contextItem; ///< Channel name or nick to provide context menu for
202     ActionSlot _actionSlot;
203 };
204
205
206 //! Input dialog for joining a channel
207 class NetworkModelController::JoinDlg : public QDialog
208 {
209     Q_OBJECT
210
211 public:
212     JoinDlg(const QModelIndex &index, QWidget *parent = nullptr);
213
214     QString channelName() const;
215     QString channelPassword() const;
216     NetworkId networkId() const;
217
218 private slots:
219     void on_channel_textChanged(const QString &);
220
221 private:
222     QComboBox *networks;
223     QLineEdit *channel;
224     QLineEdit *password;
225     QDialogButtonBox *buttonBox;
226 };
227
228
229 // inlines
230 ActionCollection *NetworkModelController::actionCollection() const { return _actionCollection; }
231 Action *NetworkModelController::action(ActionType type) const { return _actionByType.value(type, 0); }
232 QList<QModelIndex> NetworkModelController::indexList() const { return _indexList; }
233 MessageFilter *NetworkModelController::messageFilter() const { return _messageFilter; }
234 QString NetworkModelController::contextItem() const { return _contextItem; }