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