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