Introduce NetworkModelActionProvider
[quassel.git] / src / uisupport / networkmodelactionprovider.h
1 /***************************************************************************
2 *   Copyright (C) 2005-08 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 NETWORKMODELACTIONPROVIDER_H
22 #define NETWORKMODELACTIONPROVIDER_H
23
24 #include "action.h"
25 #include "actioncollection.h"
26 #include "messagefilter.h"
27 #include "quasselui.h"
28
29 class NetworkModelActionProvider : public AbstractActionProvider {
30   Q_OBJECT
31
32 public:
33   NetworkModelActionProvider(QObject *parent = 0);
34   ~NetworkModelActionProvider();
35
36   // don't change enums without doublechecking masks etc. in code
37   enum ActionType {
38     // Network actions
39     NetworkMask = 0x0f,
40     NetworkConnect = 0x01,
41     NetworkDisconnect = 0x02,
42
43     // Buffer actions
44     BufferMask = 0xf0,
45     BufferJoin = 0x10,
46     BufferPart = 0x20,
47     BufferRemove = 0x30,
48
49     // Hide actions
50     HideMask = 0x0f00,
51     HideJoin = 0x0100,
52     HidePart = 0x0200,
53     HideQuit = 0x0300,
54     HideNick = 0x0400,
55     HideMode = 0x0500,
56     HideDayChange = 0x0600,
57     HideApplyToAll = 0xf00,
58
59     // General actions
60     GeneralMask = 0xf000,
61     JoinChannel = 0x1000,
62     ShowChannelList = 0x2000,
63     ShowIgnoreList = 0x3000,
64
65     // Nick actions
66     NickMask = 0xff0000,
67     NickCtcpWhois = 0x010000,
68     NickCtcpVersion = 0x020000,
69     NickCtcpPing = 0x030000,
70     NickCtcpTime = 0x040000,
71     NickCtcpFinger = 0x050000,
72     NickOp = 0x080000,
73     NickDeop = 0x090000,
74     NickVoice = 0x0a0000,
75     NickDevoice = 0x0b0000,
76     NickKick = 0x0c0000,
77     NickBan = 0x0d0000,
78     NickKickBan = 0x0e0000,
79     NickQuery = 0x0f0000,
80
81     // Actions that are handled externally
82     // These emit a signal to the action requester, rather than being handled here
83     ExternalMask = 0xff000000,
84     HideBufferTemporarily = 0x01000000,
85     HideBufferPermanently = 0x02000000
86   };
87
88   inline Action *action(ActionType type) const;
89
90   //! Provide a list of actions applying to the given item
91   /**
92    * Note that this list ist not persistent, hence it should only be used immediately after
93    * calling this function (e.g. in a context menu). Optionally one can provide a QObject and a slot
94    * (that should take a QAction * as parameter) that is invoked for actions that require external
95    * handling.
96    * @param index The item index in the NetworkModel
97    * @param receiver The optional object that is notified for actions that need to be handled externally.
98    *                 The action type will be stored in the QAction's data().
99    * @param slot     The receiving slot name; should be "mySlot" rather than SLOT(mySlot(QAction *))
100    * @return A list of actions applying to the given item
101    */
102   void addActions(QMenu *, const QModelIndex &index, QObject *receiver = 0, const char *slot = 0, bool allowBufferHide = false);
103   void addActions(QMenu *, const QList<QModelIndex> &indexList, QObject *receiver = 0, const char *slot = 0, bool allowBufferHide = false);
104   void addActions(QMenu *, BufferId id, QObject *receiver = 0, const char *slot = 0);
105   void addActions(QMenu *, MessageFilter *filter, QObject *receiver = 0, const char *slot = 0);
106
107 signals:
108   void showChannelList(NetworkId);
109   void showIgnoreList(NetworkId);
110
111 protected:
112   inline ActionCollection *actionCollection() const;
113
114 protected slots:
115   void actionTriggered(QAction *);
116
117 private:
118   enum ItemActiveState {
119     InactiveState = 0x01,
120   ActiveState = 0x02
121   };
122
123 public:
124   Q_DECLARE_FLAGS(ItemActiveStates, ItemActiveState)
125
126 private:
127   void registerAction(ActionType type, const QString &text, bool checkable = false);
128   void registerAction(ActionType type, const QPixmap &icon, const QString &text, bool checkable = false);
129
130   void handleNetworkAction(ActionType, QAction *);
131   void handleBufferAction(ActionType, QAction *);
132   void handleHideAction(ActionType, QAction *);
133   void handleNickAction(ActionType, QAction *);
134   void handleGeneralAction(ActionType, QAction *);
135   void handleExternalAction(ActionType, QAction *);
136
137   bool checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState = QFlags<ItemActiveState>(ActiveState | InactiveState));
138   Action * addAction(ActionType, QMenu *, bool condition = true);
139   Action * addAction(Action * , QMenu *, bool condition = true);
140   Action * addAction(ActionType, QMenu *, const QModelIndex &index, ItemActiveStates requiredActiveState = QFlags<ItemActiveState>(ActiveState | InactiveState));
141   Action * addAction(Action * , QMenu *, const QModelIndex &index, ItemActiveStates requiredActiveState = QFlags<ItemActiveState>(ActiveState | InactiveState));
142
143   void addHideEventsMenu(QMenu *, BufferId bufferId);
144   void addHideEventsMenu(QMenu *, MessageFilter *msgFilter);
145   void addHideEventsMenu(QMenu *, int filter);
146
147   void addNetworkItemActions(QMenu *, const QModelIndex &);
148   void addBufferItemActions(QMenu *, const QModelIndex &, bool isCustomBufferView = false);
149   void addIrcUserActions(QMenu *, IrcUser *ircUser, bool isNickView = true);
150
151   NetworkModel *_model;
152
153   ActionCollection *_actionCollection;
154   QHash<ActionType, Action *> _actionByType;
155
156   Action *_hideEventsMenuAction;
157
158   QList<QModelIndex> _indexList;
159   MessageFilter *_messageFilter;
160   QObject *_receiver;
161   const char *_method;
162 };
163
164 // inlines
165 ActionCollection *NetworkModelActionProvider::actionCollection() const { return _actionCollection; }
166 Action *NetworkModelActionProvider::action(ActionType type) const { return _actionByType.value(type, 0); }
167 #endif