really don't..
[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     HideUseDefaults = 0xe00,
66     HideApplyToAll = 0xf00,
67
68     // General actions
69     GeneralMask = 0xf000,
70     JoinChannel = 0x1000,
71     ShowChannelList = 0x2000,
72     ShowIgnoreList = 0x3000,
73
74     // Nick actions
75     NickMask = 0xff0000,
76     NickWhois = 0x010000,
77     NickQuery = 0x020000,
78     NickSwitchTo = 0x030000,
79     NickCtcpVersion = 0x040000,
80     NickCtcpPing = 0x050000,
81     NickCtcpTime = 0x060000,
82     NickCtcpFinger = 0x070000,
83     NickOp = 0x080000,
84     NickDeop = 0x090000,
85     NickVoice = 0x0a0000,
86     NickDevoice = 0x0b0000,
87     NickKick = 0x0c0000,
88     NickBan = 0x0d0000,
89     NickKickBan = 0x0e0000,
90
91     // Actions that are handled externally
92     // These emit a signal to the action requester, rather than being handled here
93     ExternalMask = 0xff000000,
94     HideBufferTemporarily = 0x01000000,
95     HideBufferPermanently = 0x02000000
96   };
97
98   inline Action *action(ActionType type) const;
99
100 public:
101   enum ItemActiveState {
102     InactiveState = 0x01,
103     ActiveState = 0x02
104   };
105   Q_DECLARE_FLAGS(ItemActiveStates, ItemActiveState)
106
107 protected:
108   inline ActionCollection *actionCollection() const;
109   inline QList<QModelIndex> indexList() const;
110   inline MessageFilter *messageFilter() const;
111   inline QString contextItem() const ;   ///< Channel name or nick to provide context menu for
112   inline QObject *receiver() const ;
113   inline const char *method() const;
114
115   void setIndexList(const QModelIndex &);
116   void setIndexList(const QList<QModelIndex> &);
117   void setMessageFilter(MessageFilter *);
118   void setContextItem(const QString &);
119   void setSlot(QObject *receiver, const char *method);
120
121   Action * registerAction(ActionType type, const QString &text, bool checkable = false);
122   Action * registerAction(ActionType type, const QPixmap &icon, const QString &text, bool checkable = false);
123   bool checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState = QFlags<ItemActiveState>(ActiveState | InactiveState));
124
125   QString nickName(const QModelIndex &index) const;
126   BufferId findQueryBuffer(const QModelIndex &index, const QString &predefinedNick = QString()) const;
127   BufferId findQueryBuffer(NetworkId, const QString &nickName) const;
128   void removeBuffers(const QModelIndexList &indexList);
129
130 protected slots:
131   virtual void actionTriggered(QAction *);
132
133 signals:
134   void showChannelList(NetworkId);
135   void showIgnoreList(NetworkId);
136
137 protected:
138   virtual void handleNetworkAction(ActionType, QAction *);
139   virtual void handleBufferAction(ActionType, QAction *);
140   virtual void handleHideAction(ActionType, QAction *);
141   virtual void handleNickAction(ActionType, QAction *);
142   virtual void handleGeneralAction(ActionType, QAction *);
143   virtual void handleExternalAction(ActionType, QAction *);
144
145   class JoinDlg;
146
147 private:
148   NetworkModel *_model;
149
150   ActionCollection *_actionCollection;
151   QHash<ActionType, Action *> _actionByType;
152
153   QList<QModelIndex> _indexList;
154   MessageFilter *_messageFilter;
155   QString _contextItem;   ///< Channel name or nick to provide context menu for
156   QObject *_receiver;
157   const char *_method;
158 };
159
160 //! Input dialog for joining a channel
161 class NetworkModelController::JoinDlg : public QDialog {
162   Q_OBJECT
163
164 public:
165   JoinDlg(const QModelIndex &index, QWidget *parent = 0);
166
167   QString channelName() const;
168   NetworkId networkId() const;
169
170 private slots:
171   void on_channel_textChanged(const QString &);
172
173 private:
174   QComboBox *networks;
175   QLineEdit *channel;
176   QDialogButtonBox *buttonBox;
177 };
178
179
180 // inlines
181 ActionCollection *NetworkModelController::actionCollection() const { return _actionCollection; }
182 Action *NetworkModelController::action(ActionType type) const { return _actionByType.value(type, 0); }
183 QList<QModelIndex> NetworkModelController::indexList() const { return _indexList; }
184 MessageFilter *NetworkModelController::messageFilter() const { return _messageFilter; }
185 QString NetworkModelController::contextItem() const { return _contextItem; }
186 QObject *NetworkModelController::receiver() const { return _receiver; }
187 const char *NetworkModelController::method() const { return _method; }
188
189 #endif