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