1 /***************************************************************************
2 * Copyright (C) 2005-2016 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 ***************************************************************************/
22 #include <QDialogButtonBox>
23 #include <QGridLayout>
27 #include <QInputDialog>
28 #include <QMessageBox>
29 #include <QPushButton>
31 #include "networkmodelcontroller.h"
33 #include "buffermodel.h"
34 #include "buffersettings.h"
35 #include "clientidentity.h"
38 #include "clientignorelistmanager.h"
41 NetworkModelController::NetworkModelController(QObject *parent)
43 _actionCollection(new ActionCollection(this)),
47 connect(_actionCollection, SIGNAL(actionTriggered(QAction *)), SLOT(actionTriggered(QAction *)));
51 NetworkModelController::~NetworkModelController()
56 Action *NetworkModelController::registerAction(ActionType type, const QString &text, bool checkable)
58 return registerAction(type, QPixmap(), text, checkable);
62 Action *NetworkModelController::registerAction(ActionType type, const QIcon &icon, const QString &text, bool checkable)
66 act = new Action(text, this);
68 act = new Action(icon, text, this);
70 act->setCheckable(checkable);
73 _actionCollection->addAction(QString::number(type, 16), act);
74 _actionByType[type] = act;
79 /******** Helper Functions ***********************************************************************/
81 void NetworkModelController::setIndexList(const QModelIndex &index)
83 _indexList = QList<QModelIndex>() << index;
87 void NetworkModelController::setIndexList(const QList<QModelIndex> &list)
93 void NetworkModelController::setMessageFilter(MessageFilter *filter)
95 _messageFilter = filter;
99 void NetworkModelController::setContextItem(const QString &contextItem)
101 _contextItem = contextItem;
105 void NetworkModelController::setSlot(QObject *receiver, const char *method)
107 _receiver = receiver;
112 bool NetworkModelController::checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState)
114 if (!index.isValid())
117 ItemActiveStates isActive = index.data(NetworkModel::ItemActiveRole).toBool()
121 if (!(isActive & requiredActiveState))
128 QString NetworkModelController::nickName(const QModelIndex &index) const
130 IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
132 return ircUser->nick();
134 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
135 if (!bufferInfo.isValid())
137 if (bufferInfo.type() != BufferInfo::QueryBuffer)
140 return bufferInfo.bufferName(); // FIXME this might break with merged queries maybe
144 BufferId NetworkModelController::findQueryBuffer(const QModelIndex &index, const QString &predefinedNick) const
146 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
147 if (!networkId.isValid())
150 QString nick = predefinedNick.isEmpty() ? nickName(index) : predefinedNick;
154 return findQueryBuffer(networkId, nick);
158 BufferId NetworkModelController::findQueryBuffer(NetworkId networkId, const QString &nick) const
160 return Client::networkModel()->bufferId(networkId, nick);
164 void NetworkModelController::removeBuffers(const QModelIndexList &indexList)
166 QList<BufferInfo> inactive;
167 foreach(QModelIndex index, indexList) {
168 BufferInfo info = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
169 if (info.isValid()) {
170 if (info.type() == BufferInfo::QueryBuffer
171 || (info.type() == BufferInfo::ChannelBuffer && !index.data(NetworkModel::ItemActiveRole).toBool()))
176 if (inactive.count()) {
177 msg = tr("Do you want to delete the following buffer(s) permanently?", 0, inactive.count());
180 foreach(BufferInfo info, inactive) {
182 msg += QString("<li>%1</li>").arg(info.bufferName());
189 if (count > 9 && inactive.size() - count != 0)
190 msg += tr("...and <b>%1</b> more<br><br>").arg(inactive.size() - count);
191 msg += tr("<b>Note:</b> This will delete all related data, including all backlog data, from the core's database and cannot be undone.");
192 if (inactive.count() != indexList.count())
193 msg += tr("<br>Active channel buffers cannot be deleted, please part the channel first.");
195 if (QMessageBox::question(0, tr("Remove buffers permanently?"), msg, QMessageBox::Yes|QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
196 foreach(BufferInfo info, inactive)
197 Client::removeBuffer(info.bufferId());
203 void NetworkModelController::handleExternalAction(ActionType type, QAction *action)
206 if (receiver() && method()) {
207 if (!QMetaObject::invokeMethod(receiver(), method(), Q_ARG(QAction *, action)))
208 qWarning() << "NetworkModelActionController::handleExternalAction(): Could not invoke slot" << receiver() << method();
213 /******** Handle Actions *************************************************************************/
215 void NetworkModelController::actionTriggered(QAction *action)
217 ActionType type = (ActionType)action->data().toInt();
219 if (type & NetworkMask)
220 handleNetworkAction(type, action);
221 else if (type & BufferMask)
222 handleBufferAction(type, action);
223 else if (type & HideMask)
224 handleHideAction(type, action);
225 else if (type & GeneralMask)
226 handleGeneralAction(type, action);
227 else if (type & NickMask)
228 handleNickAction(type, action);
229 else if (type & ExternalMask)
230 handleExternalAction(type, action);
232 qWarning() << "NetworkModelController::actionTriggered(): Unhandled action!";
237 void NetworkModelController::handleNetworkAction(ActionType type, QAction *)
239 if (type == NetworkConnectAllWithDropdown || type == NetworkDisconnectAllWithDropdown || type == NetworkConnectAll || type == NetworkDisconnectAll) {
240 if (type == NetworkConnectAllWithDropdown && QMessageBox::question(0, tr("Question"), tr("Really Connect to all IRC Networks?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::No)
242 if (type == NetworkDisconnectAllWithDropdown && QMessageBox::question(0, tr("Question"), tr("Really disconnect from all IRC Networks?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::No)
244 foreach(NetworkId id, Client::networkIds()) {
245 const Network *net = Client::network(id);
246 if ((type == NetworkConnectAllWithDropdown || type == NetworkConnectAll) && net->connectionState() == Network::Disconnected)
247 net->requestConnect();
248 if ((type == NetworkDisconnectAllWithDropdown || type == NetworkDisconnectAll) && net->connectionState() != Network::Disconnected)
249 net->requestDisconnect();
254 if (!indexList().count())
257 const Network *network = Client::network(indexList().at(0).data(NetworkModel::NetworkIdRole).value<NetworkId>());
258 Q_CHECK_PTR(network);
264 network->requestConnect();
266 case NetworkDisconnect:
267 network->requestDisconnect();
275 void NetworkModelController::handleBufferAction(ActionType type, QAction *)
277 if (type == BufferRemove) {
278 removeBuffers(indexList());
281 QList<BufferInfo> bufferList; // create temp list because model indexes might change
282 foreach(QModelIndex index, indexList()) {
283 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
284 if (bufferInfo.isValid())
285 bufferList << bufferInfo;
288 foreach(BufferInfo bufferInfo, bufferList) {
291 Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
295 QString reason = Client::identity(Client::network(bufferInfo.networkId())->identity())->partReason();
296 Client::userInput(bufferInfo, QString("/PART %1").arg(reason));
300 Client::bufferModel()->switchToBuffer(bufferInfo.bufferId());
310 void NetworkModelController::handleHideAction(ActionType type, QAction *action)
314 if (type == HideJoinPartQuit) {
315 bool anyChecked = NetworkModelController::action(HideJoin)->isChecked();
316 anyChecked |= NetworkModelController::action(HidePart)->isChecked();
317 anyChecked |= NetworkModelController::action(HideQuit)->isChecked();
319 // If any are checked, uncheck them all.
320 // If none are checked, check them all.
321 bool newCheckedState = !anyChecked;
322 NetworkModelController::action(HideJoin)->setChecked(newCheckedState);
323 NetworkModelController::action(HidePart)->setChecked(newCheckedState);
324 NetworkModelController::action(HideQuit)->setChecked(newCheckedState);
328 if (NetworkModelController::action(HideJoin)->isChecked())
329 filter |= Message::Join | Message::NetsplitJoin;
330 if (NetworkModelController::action(HidePart)->isChecked())
331 filter |= Message::Part;
332 if (NetworkModelController::action(HideQuit)->isChecked())
333 filter |= Message::Quit | Message::NetsplitQuit;
334 if (NetworkModelController::action(HideNick)->isChecked())
335 filter |= Message::Nick;
336 if (NetworkModelController::action(HideMode)->isChecked())
337 filter |= Message::Mode;
338 if (NetworkModelController::action(HideDayChange)->isChecked())
339 filter |= Message::DayChange;
340 if (NetworkModelController::action(HideTopic)->isChecked())
341 filter |= Message::Topic;
344 case HideJoinPartQuit:
353 BufferSettings(_messageFilter->idString()).setMessageFilter(filter);
355 foreach(QModelIndex index, _indexList) {
356 BufferId bufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
357 if (!bufferId.isValid())
359 BufferSettings(bufferId).setMessageFilter(filter);
364 BufferSettings().setMessageFilter(filter);
365 case HideUseDefaults:
367 BufferSettings(_messageFilter->idString()).removeFilter();
369 foreach(QModelIndex index, _indexList) {
370 BufferId bufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
371 if (!bufferId.isValid())
373 BufferSettings(bufferId).removeFilter();
383 void NetworkModelController::handleGeneralAction(ActionType type, QAction *action)
387 if (!indexList().count())
389 NetworkId networkId = indexList().at(0).data(NetworkModel::NetworkIdRole).value<NetworkId>();
394 QString channelName = contextItem();
395 QString channelPassword;
396 if (channelName.isEmpty()) {
397 JoinDlg dlg(indexList().first());
398 if (dlg.exec() == QDialog::Accepted) {
399 channelName = dlg.channelName();
400 networkId = dlg.networkId();
401 channelPassword = dlg.channelPassword();
404 if (!channelName.isEmpty()) {
405 if (!channelPassword.isEmpty())
406 Client::instance()->userInput(BufferInfo::fakeStatusBuffer(networkId), QString("/JOIN %1 %2").arg(channelName).arg(channelPassword));
408 Client::instance()->userInput(BufferInfo::fakeStatusBuffer(networkId), QString("/JOIN %1").arg(channelName));
412 case ShowChannelList:
413 if (networkId.isValid())
414 emit showChannelList(networkId);
417 if (networkId.isValid())
418 emit showIgnoreList(QString());
426 void NetworkModelController::handleNickAction(ActionType type, QAction *action)
428 foreach(QModelIndex index, indexList()) {
429 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
430 if (!networkId.isValid())
432 QString nick = nickName(index);
435 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
436 if (!bufferInfo.isValid())
441 Client::userInput(bufferInfo, QString("/WHOIS %1 %1").arg(nick));
443 case NickCtcpVersion:
444 Client::userInput(bufferInfo, QString("/CTCP %1 VERSION").arg(nick));
447 Client::userInput(bufferInfo, QString("/CTCP %1 PING").arg(nick));
450 Client::userInput(bufferInfo, QString("/CTCP %1 TIME").arg(nick));
452 case NickCtcpClientinfo:
453 Client::userInput(bufferInfo, QString("/CTCP %1 CLIENTINFO").arg(nick));
456 Client::userInput(bufferInfo, QString("/OP %1").arg(nick));
459 Client::userInput(bufferInfo, QString("/DEOP %1").arg(nick));
462 Client::userInput(bufferInfo, QString("/HALFOP %1").arg(nick));
465 Client::userInput(bufferInfo, QString("/DEHALFOP %1").arg(nick));
468 Client::userInput(bufferInfo, QString("/VOICE %1").arg(nick));
471 Client::userInput(bufferInfo, QString("/DEVOICE %1").arg(nick));
474 Client::userInput(bufferInfo, QString("/KICK %1").arg(nick));
477 Client::userInput(bufferInfo, QString("/BAN %1").arg(nick));
480 Client::userInput(bufferInfo, QString("/BAN %1").arg(nick));
481 Client::userInput(bufferInfo, QString("/KICK %1").arg(nick));
485 Client::bufferModel()->switchToOrStartQuery(networkId, nick);
489 IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
492 Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::SenderIgnore,
493 action->property("ignoreRule").toString(),
494 false, IgnoreListManager::SoftStrictness,
495 IgnoreListManager::NetworkScope,
496 ircUser->network()->networkName(), true);
501 IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
504 Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::SenderIgnore,
505 action->property("ignoreRule").toString(),
506 false, IgnoreListManager::SoftStrictness,
507 IgnoreListManager::NetworkScope,
508 ircUser->network()->networkName(), true);
511 case NickIgnoreDomain:
513 IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
516 Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::SenderIgnore,
517 action->property("ignoreRule").toString(),
518 false, IgnoreListManager::SoftStrictness,
519 IgnoreListManager::NetworkScope,
520 ircUser->network()->networkName(), true);
523 case NickIgnoreCustom:
524 // forward that to mainwin since we can access the settingspage only from there
525 emit showIgnoreList(action->property("ignoreRule").toString());
527 case NickIgnoreToggleEnabled0:
528 case NickIgnoreToggleEnabled1:
529 case NickIgnoreToggleEnabled2:
530 case NickIgnoreToggleEnabled3:
531 case NickIgnoreToggleEnabled4:
532 Client::ignoreListManager()->requestToggleIgnoreRule(action->property("ignoreRule").toString());
535 qWarning() << "Unhandled nick action";
541 /***************************************************************************************************************
543 ***************************************************************************************************************/
545 NetworkModelController::JoinDlg::JoinDlg(const QModelIndex &index, QWidget *parent) : QDialog(parent)
547 setWindowIcon(QIcon::fromTheme("irc-join-channel"));
548 setWindowTitle(tr("Join Channel"));
550 QGridLayout *layout = new QGridLayout(this);
551 layout->addWidget(new QLabel(tr("Network:")), 0, 0);
552 layout->addWidget(networks = new QComboBox, 0, 1);
553 layout->addWidget(new QLabel(tr("Channel:")), 1, 0);
554 layout->addWidget(channel = new QLineEdit, 1, 1);
555 layout->addWidget(new QLabel(tr("Password:")), 2, 0);
556 layout->addWidget(password = new QLineEdit, 2, 1);
557 layout->addWidget(buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel), 3, 0, 1, 2);
561 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
562 networks->setInsertPolicy(QComboBox::InsertAlphabetically);
563 password->setEchoMode(QLineEdit::Password);
565 connect(buttonBox, SIGNAL(accepted()), SLOT(accept()));
566 connect(buttonBox, SIGNAL(rejected()), SLOT(reject()));
567 connect(channel, SIGNAL(textChanged(QString)), SLOT(on_channel_textChanged(QString)));
569 foreach(NetworkId id, Client::networkIds()) {
570 const Network *net = Client::network(id);
571 if (net->isConnected()) {
572 networks->addItem(net->networkName(), QVariant::fromValue<NetworkId>(id));
576 if (index.isValid()) {
577 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
578 if (networkId.isValid()) {
579 networks->setCurrentIndex(networks->findText(Client::network(networkId)->networkName()));
580 if (index.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer
581 && !index.data(NetworkModel::ItemActiveRole).toBool())
582 channel->setText(index.data(Qt::DisplayRole).toString());
588 NetworkId NetworkModelController::JoinDlg::networkId() const
590 return networks->itemData(networks->currentIndex()).value<NetworkId>();
594 QString NetworkModelController::JoinDlg::channelName() const
596 return channel->text();
600 QString NetworkModelController::JoinDlg::channelPassword() const
602 return password->text();
606 void NetworkModelController::JoinDlg::on_channel_textChanged(const QString &text)
608 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!text.isEmpty());