1 | /*************************************************************************** |
2 | * Copyright (C) 2005-2014 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 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
19 | ***************************************************************************/ |
20 | |
21 | #include <QMenu> |
22 | #include <QToolBar> |
23 | |
24 | #include "toolbaractionprovider.h" |
25 | |
26 | #include "iconloader.h" |
27 | |
28 | ToolBarActionProvider::ToolBarActionProvider(QObject *parent) |
29 | : NetworkModelController(parent) |
30 | { |
31 | registerAction(NetworkConnectAll, DesktopIcon("network-connect" ), tr("Connect" ))->setToolTip(tr("Connect to IRC" )); |
32 | registerAction(NetworkDisconnectAll, DesktopIcon("network-disconnect" ), tr("Disconnect" ))->setToolTip(tr("Disconnect from IRC" )); |
33 | |
34 | registerAction(BufferPart, DesktopIcon("irc-close-channel" ), tr("Part" ))->setToolTip(tr("Leave currently selected channel" )); |
35 | registerAction(JoinChannel, DesktopIcon("irc-join-channel" ), tr("Join" ))->setToolTip(tr("Join a channel" )); |
36 | |
37 | registerAction(NickQuery, DesktopIcon("mail-message-new" ), tr("Query" ))->setToolTip(tr("Start a private conversation" )); // fix icon |
38 | registerAction(NickWhois, DesktopIcon("im-user" ), tr("Whois" ))->setToolTip(tr("Request user information" )); // fix icon |
39 | |
40 | registerAction(NickOp, DesktopIcon("irc-operator" ), tr("Op" ))->setToolTip(tr("Give operator privileges to user" )); |
41 | registerAction(NickDeop, DesktopIcon("irc-remove-operator" ), tr("Deop" ))->setToolTip(tr("Take operator privileges from user" )); |
42 | registerAction(NickVoice, DesktopIcon("irc-voice" ), tr("Voice" ))->setToolTip(tr("Give voice to user" )); |
43 | registerAction(NickDevoice, DesktopIcon("irc-unvoice" ), tr("Devoice" ))->setToolTip(tr("Take voice from user" )); |
44 | registerAction(NickKick, DesktopIcon("im-kick-user" ), tr("Kick" ))->setToolTip(tr("Remove user from channel" )); |
45 | registerAction(NickBan, DesktopIcon("im-ban-user" ), tr("Ban" ))->setToolTip(tr("Ban user from channel" )); |
46 | registerAction(NickKickBan, DesktopIcon("im-ban-kick-user" ), tr("Kick/Ban" ))->setToolTip(tr("Remove and ban user from channel" )); |
47 | |
48 | _networksConnectMenu = new QMenu(); |
49 | _networksConnectMenu->setSeparatorsCollapsible(false); |
50 | _networksConnectMenu->addSeparator(); |
51 | _networksConnectMenu->addAction(tr("Connect to all" )); |
52 | action(NetworkConnectAll)->setMenu(_networksConnectMenu); |
53 | action(NetworkConnectAll)->setEnabled(false); |
54 | |
55 | _networksDisconnectMenu = new QMenu(); |
56 | _networksDisconnectMenu->setSeparatorsCollapsible(false); |
57 | _networksDisconnectMenu->addSeparator(); |
58 | _networksDisconnectMenu->addAction(tr("Disconnect from all" )); |
59 | action(NetworkDisconnectAll)->setMenu(_networksDisconnectMenu); |
60 | action(NetworkDisconnectAll)->setEnabled(false); |
61 | |
62 | connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), SLOT(networkCreated(NetworkId))); |
63 | connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), SLOT(networkRemoved(NetworkId))); |
64 | |
65 | updateStates(); |
66 | } |
67 | |
68 | |
69 | ToolBarActionProvider::~ToolBarActionProvider() |
70 | { |
71 | } |
72 | |
73 | |
74 | void ToolBarActionProvider::disconnectedFromCore() |
75 | { |
76 | _currentBuffer = QModelIndex(); |
77 | updateStates(); |
78 | NetworkModelController::disconnectedFromCore(); |
79 | } |
80 | |
81 | |
82 | void ToolBarActionProvider::updateStates() |
83 | { |
84 | action(BufferPart)->setEnabled(_currentBuffer.isValid() |
85 | && _currentBuffer.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer |
86 | && _currentBuffer.data(NetworkModel::ItemActiveRole).toBool()); |
87 | } |
88 | |
89 | |
90 | void ToolBarActionProvider::addActions(QToolBar *bar, ToolBarType type) |
91 | { |
92 | switch (type) { |
93 | case MainToolBar: |
94 | bar->addAction(action(NetworkConnectAll)); |
95 | bar->addAction(action(NetworkDisconnectAll)); |
96 | bar->addAction(action(JoinChannel)); |
97 | bar->addAction(action(BufferPart)); |
98 | break; |
99 | case NickToolBar: |
100 | bar->addAction(action(NickQuery)); |
101 | bar->addAction(action(NickWhois)); |
102 | bar->addSeparator(); |
103 | bar->addAction(action(NickOp)); |
104 | bar->addAction(action(NickDeop)); |
105 | bar->addAction(action(NickVoice)); |
106 | bar->addAction(action(NickDevoice)); |
107 | bar->addAction(action(NickKick)); |
108 | bar->addAction(action(NickBan)); |
109 | bar->addAction(action(NickKickBan)); |
110 | break; |
111 | default: |
112 | return; |
113 | } |
114 | } |
115 | |
116 | |
117 | void ToolBarActionProvider::currentBufferChanged(const QModelIndex &index) |
118 | { |
119 | _currentBuffer = index; |
120 | updateStates(); |
121 | } |
122 | |
123 | |
124 | void ToolBarActionProvider::nickSelectionChanged(const QModelIndexList &indexList) |
125 | { |
126 | _selectedNicks = indexList; |
127 | updateStates(); |
128 | } |
129 | |
130 | |
131 | // override those to set indexes right |
132 | void ToolBarActionProvider::handleNetworkAction(ActionType type, QAction *action) |
133 | { |
134 | setIndexList(_currentBuffer); |
135 | NetworkModelController::handleNetworkAction(type, action); |
136 | } |
137 | |
138 | |
139 | void ToolBarActionProvider::handleBufferAction(ActionType type, QAction *action) |
140 | { |
141 | setIndexList(_currentBuffer); |
142 | NetworkModelController::handleBufferAction(type, action); |
143 | } |
144 | |
145 | |
146 | void ToolBarActionProvider::handleNickAction(ActionType type, QAction *action) |
147 | { |
148 | setIndexList(_selectedNicks); |
149 | NetworkModelController::handleNickAction(type, action); |
150 | } |
151 | |
152 | |
153 | void ToolBarActionProvider::handleGeneralAction(ActionType type, QAction *action) |
154 | { |
155 | setIndexList(_currentBuffer); |
156 | NetworkModelController::handleGeneralAction(type, action); |
157 | } |
158 | |
159 | |
160 | void ToolBarActionProvider::networkCreated(NetworkId id) |
161 | { |
162 | const Network *net = Client::network(id); |
163 | Action *act = new Action(net->networkName(), this); |
164 | _networkActions[id] = act; |
165 | act->setObjectName(QString("NetworkAction-%1" ).arg(id.toInt())); |
166 | act->setData(QVariant::fromValue<NetworkId>(id)); |
167 | connect(net, SIGNAL(updatedRemotely()), SLOT(networkUpdated())); |
168 | connect(act, SIGNAL(triggered()), SLOT(connectOrDisconnectNet())); |
169 | networkUpdated(net); |
170 | } |
171 | |
172 | |
173 | void ToolBarActionProvider::networkRemoved(NetworkId id) |
174 | { |
175 | Action *act = _networkActions.take(id); |
176 | if (act) |
177 | act->deleteLater(); |
178 | } |
179 | |
180 | |
181 | void ToolBarActionProvider::networkUpdated(const Network *net) |
182 | { |
183 | if (!net) |
184 | net = qobject_cast<const Network *>(sender()); |
185 | if (!net) |
186 | return; |
187 | Action *act = _networkActions.value(net->networkId()); |
188 | if (!act) |
189 | return; |
190 | |
191 | _networksConnectMenu->removeAction(act); |
192 | _networksDisconnectMenu->removeAction(act); |
193 | |
194 | QMenu * = net->connectionState() != Network::Disconnected ? _networksDisconnectMenu : _networksConnectMenu; |
195 | act->setText(net->networkName()); |
196 | |
197 | const int lastidx = newMenu->actions().count() - 2; |
198 | QAction *beforeAction = newMenu->actions().at(lastidx); |
199 | for (int i = 0; i < newMenu->actions().count() - 2; i++) { |
200 | QAction *action = newMenu->actions().at(i); |
201 | if (net->networkName().localeAwareCompare(action->text()) < 0) { |
202 | beforeAction = action; |
203 | break; |
204 | } |
205 | } |
206 | newMenu->insertAction(beforeAction, act); |
207 | |
208 | action(NetworkConnectAll)->setEnabled(_networksConnectMenu->actions().count() > 2); |
209 | action(NetworkDisconnectAll)->setEnabled(_networksDisconnectMenu->actions().count() > 2); |
210 | action(JoinChannel)->setEnabled(_networksDisconnectMenu->actions().count() > 2); |
211 | } |
212 | |
213 | |
214 | void ToolBarActionProvider::connectOrDisconnectNet() |
215 | { |
216 | QAction *act = qobject_cast<QAction *>(sender()); |
217 | if (!act) |
218 | return; |
219 | const Network *net = Client::network(act->data().value<NetworkId>()); |
220 | if (!net) |
221 | return; |
222 | |
223 | if (net->connectionState() == Network::Disconnected) net->requestConnect(); |
224 | else net->requestDisconnect(); |
225 | } |
226 | |
227 | |
228 | //void ToolBarActionProvider:: |
229 | |