Add function to "(Dis-)Connect from/to all" in connection dropdown
[quassel.git] / src / uisupport / networkmodelcontroller.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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 #ifndef NETWORKMODELCONTROLLER_H_
22 #define NETWORKMODELCONTROLLER_H_
23
24 #include <QDialog>
25
26 #include "action.h"
27 #include "actioncollection.h"
28 #include "messagefilter.h"
29
30 class QComboBox;
31 class QDialogButtonBox;
32 class QLineEdit;
33
34 class NetworkModelController : public QObject
35 {
36     Q_OBJECT
37
38 public:
39     NetworkModelController(QObject *parent = 0);
40     virtual ~NetworkModelController();
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
79         // Nick actions
80         NickMask = 0xff0000,
81         NickWhois = 0x010000,
82         NickQuery = 0x020000,
83         NickSwitchTo = 0x030000,
84         NickCtcpVersion = 0x040000,
85         NickCtcpPing = 0x050000,
86         NickCtcpTime = 0x060000,
87         NickCtcpClientinfo = 0x070000,
88         NickOp = 0x080000,
89         NickDeop = 0x090000,
90         NickVoice = 0x0a0000,
91         NickDevoice = 0x0b0000,
92         NickHalfop = 0x0c0000,
93         NickDehalfop = 0x0d0000,
94         NickKick = 0x0e0000,
95         NickBan = 0x0f0000,
96         NickKickBan = 0x100000,
97         NickIgnoreUser = 0x200000,
98         NickIgnoreHost = 0x300000,
99         NickIgnoreDomain = 0x400000,
100         NickIgnoreCustom = 0x500000,
101         // The next 5 types have to stay together
102         // Don't change without reading ContextMenuActionProvider::addIgnoreMenu!
103         NickIgnoreToggleEnabled0 = 0x600000,
104         NickIgnoreToggleEnabled1 = 0x700000,
105         NickIgnoreToggleEnabled2 = 0x800000,
106         NickIgnoreToggleEnabled3 = 0x900000,
107         NickIgnoreToggleEnabled4 = 0xa00000,
108
109         // Actions that are handled externally
110         // These emit a signal to the action requester, rather than being handled here
111         ExternalMask = 0xff000000,
112         HideBufferTemporarily = 0x01000000,
113         HideBufferPermanently = 0x02000000
114     };
115
116     inline Action *action(ActionType type) const;
117
118 public:
119     enum ItemActiveState {
120         InactiveState = 0x01,
121         ActiveState = 0x02
122     };
123     Q_DECLARE_FLAGS(ItemActiveStates, ItemActiveState)
124
125 public slots:
126     virtual void connectedToCore() {}
127     virtual void disconnectedFromCore() {}
128
129 protected:
130     inline ActionCollection *actionCollection() const;
131     inline QList<QModelIndex> indexList() const;
132     inline MessageFilter *messageFilter() const;
133     inline QString contextItem() const;  ///< Channel name or nick to provide context menu for
134     inline QObject *receiver() const;
135     inline const char *method() const;
136
137     void setIndexList(const QModelIndex &);
138     void setIndexList(const QList<QModelIndex> &);
139     void setMessageFilter(MessageFilter *);
140     void setContextItem(const QString &);
141     void setSlot(QObject *receiver, const char *method);
142
143     Action *registerAction(ActionType type, const QString &text, bool checkable = false);
144     Action *registerAction(NetworkModelController::ActionType type, const QIcon &icon, const QString &text, bool checkable = false);
145     bool checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState = QFlags<ItemActiveState>(ActiveState | InactiveState));
146
147     QString nickName(const QModelIndex &index) const;
148     BufferId findQueryBuffer(const QModelIndex &index, const QString &predefinedNick = QString()) const;
149     BufferId findQueryBuffer(NetworkId, const QString &nickName) const;
150     void removeBuffers(const QModelIndexList &indexList);
151
152 protected slots:
153     virtual void actionTriggered(QAction *);
154
155 signals:
156     void showChannelList(NetworkId);
157     void showIgnoreList(QString);
158
159 protected:
160     virtual void handleNetworkAction(ActionType, QAction *);
161     virtual void handleBufferAction(ActionType, QAction *);
162     virtual void handleHideAction(ActionType, QAction *);
163     virtual void handleNickAction(ActionType, QAction *action);
164     virtual void handleGeneralAction(ActionType, QAction *);
165     virtual void handleExternalAction(ActionType, QAction *);
166
167     class JoinDlg;
168
169 private:
170     NetworkModel *_model;
171
172     ActionCollection *_actionCollection;
173     QHash<ActionType, Action *> _actionByType;
174
175     QList<QModelIndex> _indexList;
176     MessageFilter *_messageFilter;
177     QString _contextItem; ///< Channel name or nick to provide context menu for
178     QObject *_receiver;
179     const char *_method;
180 };
181
182
183 //! Input dialog for joining a channel
184 class NetworkModelController::JoinDlg : public QDialog
185 {
186     Q_OBJECT
187
188 public:
189     JoinDlg(const QModelIndex &index, QWidget *parent = 0);
190
191     QString channelName() const;
192     QString channelPassword() const;
193     NetworkId networkId() const;
194
195 private slots:
196     void on_channel_textChanged(const QString &);
197
198 private:
199     QComboBox *networks;
200     QLineEdit *channel;
201     QLineEdit *password;
202     QDialogButtonBox *buttonBox;
203 };
204
205
206 // inlines
207 ActionCollection *NetworkModelController::actionCollection() const { return _actionCollection; }
208 Action *NetworkModelController::action(ActionType type) const { return _actionByType.value(type, 0); }
209 QList<QModelIndex> NetworkModelController::indexList() const { return _indexList; }
210 MessageFilter *NetworkModelController::messageFilter() const { return _messageFilter; }
211 QString NetworkModelController::contextItem() const { return _contextItem; }
212 QObject *NetworkModelController::receiver() const { return _receiver; }
213 const char *NetworkModelController::method() const { return _method; }
214
215 #endif