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