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