1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
23 #include "uisupport-export.h"
26 #include <type_traits>
31 #include "actioncollection.h"
32 #include "messagefilter.h"
35 class QDialogButtonBox;
38 class UISUPPORT_EXPORT NetworkModelController : public QObject
43 NetworkModelController(QObject *parent = nullptr);
45 // don't change enums without doublechecking masks etc. in code
49 NetworkConnect = 0x01,
50 NetworkDisconnect = 0x02,
51 NetworkConnectAllWithDropdown = 0x03,
52 NetworkDisconnectAllWithDropdown = 0x04,
53 NetworkConnectAll = 0x05,
54 NetworkDisconnectAll = 0x06,
60 BufferSwitchTo = 0x30,
70 HideDayChange = 0x0600,
72 HideJoinPartQuit = 0xd00,
73 HideUseDefaults = 0xe00,
74 HideApplyToAll = 0xf00,
79 ShowChannelList = 0x2000,
80 ShowIgnoreList = 0x3000,
81 ShowNetworkConfig = 0x4000,
87 NickSwitchTo = 0x030000,
88 NickCtcpVersion = 0x040000,
89 NickCtcpPing = 0x050000,
90 NickCtcpTime = 0x060000,
91 NickCtcpClientinfo = 0x070000,
95 NickDevoice = 0x0b0000,
96 NickHalfop = 0x0c0000,
97 NickDehalfop = 0x0d0000,
100 NickKickBan = 0x100000,
101 NickIgnoreUser = 0x200000,
102 NickIgnoreHost = 0x300000,
103 NickIgnoreDomain = 0x400000,
104 NickIgnoreCustom = 0x500000,
105 // The next 5 types have to stay together
106 // Don't change without reading ContextMenuActionProvider::addIgnoreMenu!
107 NickIgnoreToggleEnabled0 = 0x600000,
108 NickIgnoreToggleEnabled1 = 0x700000,
109 NickIgnoreToggleEnabled2 = 0x800000,
110 NickIgnoreToggleEnabled3 = 0x900000,
111 NickIgnoreToggleEnabled4 = 0xa00000,
113 // Actions that are handled externally
114 // These emit a signal to the action requester, rather than being handled here
115 ExternalMask = 0xff000000,
116 HideBufferTemporarily = 0x01000000,
117 HideBufferPermanently = 0x02000000
120 inline Action *action(ActionType type) const;
123 enum ItemActiveState {
124 InactiveState = 0x01,
127 Q_DECLARE_FLAGS(ItemActiveStates, ItemActiveState)
130 virtual void connectedToCore() {}
131 virtual void disconnectedFromCore() {}
134 inline ActionCollection *actionCollection() const;
135 inline QList<QModelIndex> indexList() const;
136 inline MessageFilter *messageFilter() const;
137 inline QString contextItem() const; ///< Channel name or nick to provide context menu for
139 void setIndexList(const QModelIndex &);
140 void setIndexList(const QList<QModelIndex> &);
141 void setMessageFilter(MessageFilter *);
142 void setContextItem(const QString &);
144 using ActionSlot = std::function<void(QAction*)>;
146 template<typename Receiver, typename Slot>
147 ActionSlot buildActionSlot(Receiver *receiver, Slot slot)
149 static_assert(!std::is_same<Slot, const char*>::value, "Old-style slots not supported");
150 return [receiver, slot = std::move(slot)](QAction *action) {
151 (receiver->*slot)(action);
155 void setSlot(ActionSlot slot);
157 Action *registerAction(ActionType type, const QString &text, bool checkable = false);
158 Action *registerAction(NetworkModelController::ActionType type, const QIcon &icon, const QString &text, bool checkable = false);
159 bool checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState = QFlags<ItemActiveState>(ActiveState | InactiveState));
161 QString nickName(const QModelIndex &index) const;
162 BufferId findQueryBuffer(const QModelIndex &index, const QString &predefinedNick = QString()) const;
163 BufferId findQueryBuffer(NetworkId, const QString &nickName) const;
164 void removeBuffers(const QModelIndexList &indexList);
167 virtual void actionTriggered(QAction *);
171 * Request to show the channel list dialog for the network, optionally searching by channel name
173 * @see MainWin::showChannelList()
175 * @param networkId Network ID for associated network
176 * @param channelFilters Partial channel name to search for, or empty to show all
177 * @param listImmediately If true, immediately list channels, otherwise just show dialog
179 void showChannelList(NetworkId, const QString &, bool);
180 void showNetworkConfig(NetworkId);
181 void showIgnoreList(QString);
184 virtual void handleNetworkAction(ActionType, QAction *);
185 virtual void handleBufferAction(ActionType, QAction *);
186 virtual void handleHideAction(ActionType, QAction *);
187 virtual void handleNickAction(ActionType, QAction *action);
188 virtual void handleGeneralAction(ActionType, QAction *);
189 virtual void handleExternalAction(ActionType, QAction *);
194 NetworkModel *_model;
196 ActionCollection *_actionCollection;
197 QHash<ActionType, Action *> _actionByType;
199 QList<QModelIndex> _indexList;
200 MessageFilter *_messageFilter{nullptr};
201 QString _contextItem; ///< Channel name or nick to provide context menu for
202 ActionSlot _actionSlot;
206 //! Input dialog for joining a channel
207 class NetworkModelController::JoinDlg : public QDialog
212 JoinDlg(const QModelIndex &index, QWidget *parent = nullptr);
214 QString channelName() const;
215 QString channelPassword() const;
216 NetworkId networkId() const;
219 void on_channel_textChanged(const QString &);
225 QDialogButtonBox *buttonBox;
230 ActionCollection *NetworkModelController::actionCollection() const { return _actionCollection; }
231 Action *NetworkModelController::action(ActionType type) const { return _actionByType.value(type, 0); }
232 QList<QModelIndex> NetworkModelController::indexList() const { return _indexList; }
233 MessageFilter *NetworkModelController::messageFilter() const { return _messageFilter; }
234 QString NetworkModelController::contextItem() const { return _contextItem; }