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