Don't show migration warnings if we don't have old settings to migrate
[quassel.git] / src / uisupport / networkmodelactionprovider.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 #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     BufferSwitchTo = 0x30,
48     BufferRemove = 0x40,
49
50     // Hide actions
51     HideMask = 0x0f00,
52     HideJoin = 0x0100,
53     HidePart = 0x0200,
54     HideQuit = 0x0300,
55     HideNick = 0x0400,
56     HideMode = 0x0500,
57     HideDayChange = 0x0600,
58     HideUseDefaults = 0xe00,
59     HideApplyToAll = 0xf00,
60
61     // General actions
62     GeneralMask = 0xf000,
63     JoinChannel = 0x1000,
64     ShowChannelList = 0x2000,
65     ShowIgnoreList = 0x3000,
66
67     // Nick actions
68     NickMask = 0xff0000,
69     NickWhois = 0x010000,
70     NickQuery = 0x020000,
71     NickSwitchTo = 0x030000,
72     NickCtcpVersion = 0x040000,
73     NickCtcpPing = 0x050000,
74     NickCtcpTime = 0x060000,
75     NickCtcpFinger = 0x070000,
76     NickOp = 0x080000,
77     NickDeop = 0x090000,
78     NickVoice = 0x0a0000,
79     NickDevoice = 0x0b0000,
80     NickKick = 0x0c0000,
81     NickBan = 0x0d0000,
82     NickKickBan = 0x0e0000,
83
84     // Actions that are handled externally
85     // These emit a signal to the action requester, rather than being handled here
86     ExternalMask = 0xff000000,
87     HideBufferTemporarily = 0x01000000,
88     HideBufferPermanently = 0x02000000
89   };
90
91   inline Action *action(ActionType type) const;
92
93   //! Provide a list of actions applying to the given item
94   /**
95    * Note that this list ist not persistent, hence it should only be used immediately after
96    * calling this function (e.g. in a context menu). Optionally one can provide a QObject and a slot
97    * (that should take a QAction * as parameter) that is invoked for actions that require external
98    * handling.
99    * @param index The item index in the NetworkModel
100    * @param receiver The optional object that is notified for actions that need to be handled externally.
101    *                 The action type will be stored in the QAction's data().
102    * @param slot     The receiving slot name; should be "mySlot" rather than SLOT(mySlot(QAction *))
103    * @return A list of actions applying to the given item
104    */
105   void addActions(QMenu *, const QModelIndex &index, QObject *receiver = 0, const char *slot = 0, bool allowBufferHide = false);
106   void addActions(QMenu *, const QList<QModelIndex> &indexList, QObject *receiver = 0, const char *slot = 0, bool allowBufferHide = false);
107   void addActions(QMenu *, BufferId id, QObject *receiver = 0, const char *slot = 0);
108   void addActions(QMenu *, MessageFilter *filter, BufferId msgBuffer, QObject *receiver = 0, const char *slot = 0);
109   void addActions(QMenu *, MessageFilter *filter, BufferId msgBuffer, const QString &chanOrNick, QObject *receiver = 0, const char *slot = 0);
110
111 signals:
112   void showChannelList(NetworkId);
113   void showIgnoreList(NetworkId);
114
115 protected:
116   inline ActionCollection *actionCollection() const;
117
118 protected slots:
119   void actionTriggered(QAction *);
120
121 private:
122   enum ItemActiveState {
123     InactiveState = 0x01,
124     ActiveState = 0x02
125   };
126
127 public:
128   Q_DECLARE_FLAGS(ItemActiveStates, ItemActiveState)
129
130 private:
131   void registerAction(ActionType type, const QString &text, bool checkable = false);
132   void registerAction(ActionType type, const QPixmap &icon, const QString &text, bool checkable = false);
133
134   void handleNetworkAction(ActionType, QAction *);
135   void handleBufferAction(ActionType, QAction *);
136   void handleHideAction(ActionType, QAction *);
137   void handleNickAction(ActionType, QAction *);
138   void handleGeneralAction(ActionType, QAction *);
139   void handleExternalAction(ActionType, QAction *);
140
141   void addActions(QMenu *, const QList<QModelIndex> &indexList, MessageFilter *filter, const QString &chanOrNick,
142                   QObject *receiver, const char *slot, bool allowBufferHide);
143
144   bool checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState = QFlags<ItemActiveState>(ActiveState | InactiveState));
145   Action * addAction(ActionType, QMenu *, bool condition = true);
146   Action * addAction(Action * , QMenu *, bool condition = true);
147   Action * addAction(ActionType, QMenu *, const QModelIndex &index, ItemActiveStates requiredActiveState = QFlags<ItemActiveState>(ActiveState | InactiveState));
148   Action * addAction(Action * , QMenu *, const QModelIndex &index, ItemActiveStates requiredActiveState = QFlags<ItemActiveState>(ActiveState | InactiveState));
149
150   void addHideEventsMenu(QMenu *, BufferId bufferId);
151   void addHideEventsMenu(QMenu *, MessageFilter *msgFilter);
152   void addHideEventsMenu(QMenu *, int filter = -1);
153
154   void addNetworkItemActions(QMenu *, const QModelIndex &);
155   void addBufferItemActions(QMenu *, const QModelIndex &, bool isCustomBufferView = false);
156   void addIrcUserActions(QMenu *, const QModelIndex &);
157
158   QString nickName(const QModelIndex &index) const;
159   BufferId findQueryBuffer(const QModelIndex &index, const QString &predefinedNick = QString()) const;
160   BufferId findQueryBuffer(NetworkId, const QString &nickName) const;
161   void removeBuffers(const QModelIndexList &indexList);
162
163   NetworkModel *_model;
164
165   ActionCollection *_actionCollection;
166   QHash<ActionType, Action *> _actionByType;
167
168   Action *_hideEventsMenuAction;
169   Action *_nickCtcpMenuAction;
170   Action *_nickModeMenuAction;
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 // inlines
180 ActionCollection *NetworkModelActionProvider::actionCollection() const { return _actionCollection; }
181 Action *NetworkModelActionProvider::action(ActionType type) const { return _actionByType.value(type, 0); }
182 #endif