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