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 ***************************************************************************/
21 #include "networkmodelcontroller.h"
24 #include <QDialogButtonBox>
25 #include <QGridLayout>
29 #include <QInputDialog>
30 #include <QMessageBox>
31 #include <QPushButton>
33 #include "buffermodel.h"
34 #include "buffersettings.h"
36 #include "clientidentity.h"
37 #include "clientignorelistmanager.h"
42 NetworkModelController::NetworkModelController(QObject *parent)
44 _actionCollection(new ActionCollection(this))
46 connect(_actionCollection, &ActionCollection::actionTriggered, this, &NetworkModelController::actionTriggered);
50 Action *NetworkModelController::registerAction(ActionType type, const QString &text, bool checkable)
52 return registerAction(type, QPixmap(), text, checkable);
56 Action *NetworkModelController::registerAction(ActionType type, const QIcon &icon, const QString &text, bool checkable)
60 act = new Action(text, this);
62 act = new Action(icon, text, this);
64 act->setCheckable(checkable);
67 _actionCollection->addAction(QString::number(type, 16), act);
68 _actionByType[type] = act;
73 /******** Helper Functions ***********************************************************************/
75 void NetworkModelController::setIndexList(const QModelIndex &index)
77 _indexList = QList<QModelIndex>() << index;
81 void NetworkModelController::setIndexList(const QList<QModelIndex> &list)
87 void NetworkModelController::setMessageFilter(MessageFilter *filter)
89 _messageFilter = filter;
93 void NetworkModelController::setContextItem(const QString &contextItem)
95 _contextItem = contextItem;
99 void NetworkModelController::setSlot(ActionSlot slot)
101 _actionSlot = std::move(slot);
105 bool NetworkModelController::checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState)
107 if (!index.isValid())
110 ItemActiveStates isActive = index.data(NetworkModel::ItemActiveRole).toBool()
114 if (!(isActive & requiredActiveState))
121 QString NetworkModelController::nickName(const QModelIndex &index) const
123 auto *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
125 return ircUser->nick();
127 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
128 if (!bufferInfo.isValid())
130 if (bufferInfo.type() != BufferInfo::QueryBuffer)
133 return bufferInfo.bufferName(); // FIXME this might break with merged queries maybe
137 BufferId NetworkModelController::findQueryBuffer(const QModelIndex &index, const QString &predefinedNick) const
139 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
140 if (!networkId.isValid())
143 QString nick = predefinedNick.isEmpty() ? nickName(index) : predefinedNick;
147 return findQueryBuffer(networkId, nick);
151 BufferId NetworkModelController::findQueryBuffer(NetworkId networkId, const QString &nick) const
153 return Client::networkModel()->bufferId(networkId, nick);
157 void NetworkModelController::removeBuffers(const QModelIndexList &indexList)
159 QList<BufferInfo> inactive;
160 foreach(QModelIndex index, indexList) {
161 BufferInfo info = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
162 if (info.isValid()) {
163 if (info.type() == BufferInfo::QueryBuffer
164 || (info.type() == BufferInfo::ChannelBuffer && !index.data(NetworkModel::ItemActiveRole).toBool()))
169 if (inactive.count()) {
170 msg = tr("Do you want to delete the following buffer(s) permanently?", "", inactive.count());
173 foreach(BufferInfo info, inactive) {
175 msg += QString("<li>%1</li>").arg(info.bufferName());
182 if (count > 9 && inactive.size() - count != 0)
183 msg += tr("...and <b>%1</b> more<br><br>").arg(inactive.size() - count);
184 msg += tr("<b>Note:</b> This will delete all related data, including all backlog data, from the core's database and cannot be undone.");
185 if (inactive.count() != indexList.count())
186 msg += tr("<br>Active channel buffers cannot be deleted, please part the channel first.");
188 if (QMessageBox::question(nullptr, tr("Remove buffers permanently?"), msg, QMessageBox::Yes|QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
189 foreach(BufferInfo info, inactive)
190 Client::removeBuffer(info.bufferId());
196 void NetworkModelController::handleExternalAction(ActionType type, QAction *action)
205 /******** Handle Actions *************************************************************************/
207 void NetworkModelController::actionTriggered(QAction *action)
209 ActionType type = (ActionType)action->data().toInt();
211 if (type & NetworkMask)
212 handleNetworkAction(type, action);
213 else if (type & BufferMask)
214 handleBufferAction(type, action);
215 else if (type & HideMask)
216 handleHideAction(type, action);
217 else if (type & GeneralMask)
218 handleGeneralAction(type, action);
219 else if (type & NickMask)
220 handleNickAction(type, action);
221 else if (type & ExternalMask)
222 handleExternalAction(type, action);
224 qWarning() << "NetworkModelController::actionTriggered(): Unhandled action!";
229 void NetworkModelController::handleNetworkAction(ActionType type, QAction *)
231 if (type == NetworkConnectAllWithDropdown || type == NetworkDisconnectAllWithDropdown || type == NetworkConnectAll || type == NetworkDisconnectAll) {
232 if (type == NetworkConnectAllWithDropdown && QMessageBox::question(nullptr, tr("Question"), tr("Really Connect to all IRC Networks?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::No)
234 if (type == NetworkDisconnectAllWithDropdown && QMessageBox::question(nullptr, tr("Question"), tr("Really disconnect from all IRC Networks?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::No)
236 foreach(NetworkId id, Client::networkIds()) {
237 const Network *net = Client::network(id);
238 if ((type == NetworkConnectAllWithDropdown || type == NetworkConnectAll) && net->connectionState() == Network::Disconnected)
239 net->requestConnect();
240 if ((type == NetworkDisconnectAllWithDropdown || type == NetworkDisconnectAll) && net->connectionState() != Network::Disconnected)
241 net->requestDisconnect();
246 if (!indexList().count())
249 const Network *network = Client::network(indexList().at(0).data(NetworkModel::NetworkIdRole).value<NetworkId>());
250 Q_CHECK_PTR(network);
256 network->requestConnect();
258 case NetworkDisconnect:
259 network->requestDisconnect();
267 void NetworkModelController::handleBufferAction(ActionType type, QAction *)
269 if (type == BufferRemove) {
270 removeBuffers(indexList());
273 QList<BufferInfo> bufferList; // create temp list because model indexes might change
274 foreach(QModelIndex index, indexList()) {
275 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
276 if (bufferInfo.isValid())
277 bufferList << bufferInfo;
280 foreach(BufferInfo bufferInfo, bufferList) {
283 Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
287 QString reason = Client::identity(Client::network(bufferInfo.networkId())->identity())->partReason();
288 Client::userInput(bufferInfo, QString("/PART %1").arg(reason));
292 Client::bufferModel()->switchToBuffer(bufferInfo.bufferId());
302 void NetworkModelController::handleHideAction(ActionType type, QAction *action)
306 if (type == HideJoinPartQuit) {
307 bool anyChecked = NetworkModelController::action(HideJoin)->isChecked();
308 anyChecked |= NetworkModelController::action(HidePart)->isChecked();
309 anyChecked |= NetworkModelController::action(HideQuit)->isChecked();
311 // If any are checked, uncheck them all.
312 // If none are checked, check them all.
313 bool newCheckedState = !anyChecked;
314 NetworkModelController::action(HideJoin)->setChecked(newCheckedState);
315 NetworkModelController::action(HidePart)->setChecked(newCheckedState);
316 NetworkModelController::action(HideQuit)->setChecked(newCheckedState);
320 if (NetworkModelController::action(HideJoin)->isChecked())
321 filter |= Message::Join | Message::NetsplitJoin;
322 if (NetworkModelController::action(HidePart)->isChecked())
323 filter |= Message::Part;
324 if (NetworkModelController::action(HideQuit)->isChecked())
325 filter |= Message::Quit | Message::NetsplitQuit;
326 if (NetworkModelController::action(HideNick)->isChecked())
327 filter |= Message::Nick;
328 if (NetworkModelController::action(HideMode)->isChecked())
329 filter |= Message::Mode;
330 if (NetworkModelController::action(HideDayChange)->isChecked())
331 filter |= Message::DayChange;
332 if (NetworkModelController::action(HideTopic)->isChecked())
333 filter |= Message::Topic;
336 case HideJoinPartQuit:
345 BufferSettings(_messageFilter->idString()).setMessageFilter(filter);
347 foreach(QModelIndex index, _indexList) {
348 BufferId bufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
349 if (!bufferId.isValid())
351 BufferSettings(bufferId).setMessageFilter(filter);
356 BufferSettings().setMessageFilter(filter);
358 case HideUseDefaults:
360 BufferSettings(_messageFilter->idString()).removeFilter();
362 foreach(QModelIndex index, _indexList) {
363 BufferId bufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
364 if (!bufferId.isValid())
366 BufferSettings(bufferId).removeFilter();
376 void NetworkModelController::handleGeneralAction(ActionType type, QAction *action)
380 if (!indexList().count())
382 NetworkId networkId = indexList().at(0).data(NetworkModel::NetworkIdRole).value<NetworkId>();
387 QString channelName = contextItem();
388 QString channelPassword;
389 if (channelName.isEmpty()) {
390 JoinDlg dlg(indexList().first());
391 if (dlg.exec() == QDialog::Accepted) {
392 channelName = dlg.channelName();
393 networkId = dlg.networkId();
394 channelPassword = dlg.channelPassword();
397 if (!channelName.isEmpty()) {
398 if (!channelPassword.isEmpty())
399 Client::instance()->userInput(BufferInfo::fakeStatusBuffer(networkId), QString("/JOIN %1 %2").arg(channelName).arg(channelPassword));
401 Client::instance()->userInput(BufferInfo::fakeStatusBuffer(networkId), QString("/JOIN %1").arg(channelName));
405 case ShowChannelList:
406 if (networkId.isValid()) {
407 // Don't immediately list channels, allowing customization of filter first
408 emit showChannelList(networkId, {}, false);
411 case ShowNetworkConfig:
412 if (networkId.isValid())
413 emit showNetworkConfig(networkId);
416 if (networkId.isValid())
417 emit showIgnoreList(QString());
425 void NetworkModelController::handleNickAction(ActionType type, QAction *action)
427 foreach(QModelIndex index, indexList()) {
428 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
429 if (!networkId.isValid())
431 QString nick = nickName(index);
434 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
435 if (!bufferInfo.isValid())
440 Client::userInput(bufferInfo, QString("/WHOIS %1 %1").arg(nick));
442 case NickCtcpVersion:
443 Client::userInput(bufferInfo, QString("/CTCP %1 VERSION").arg(nick));
446 Client::userInput(bufferInfo, QString("/CTCP %1 PING").arg(nick));
449 Client::userInput(bufferInfo, QString("/CTCP %1 TIME").arg(nick));
451 case NickCtcpClientinfo:
452 Client::userInput(bufferInfo, QString("/CTCP %1 CLIENTINFO").arg(nick));
455 Client::userInput(bufferInfo, QString("/OP %1").arg(nick));
458 Client::userInput(bufferInfo, QString("/DEOP %1").arg(nick));
461 Client::userInput(bufferInfo, QString("/HALFOP %1").arg(nick));
464 Client::userInput(bufferInfo, QString("/DEHALFOP %1").arg(nick));
467 Client::userInput(bufferInfo, QString("/VOICE %1").arg(nick));
470 Client::userInput(bufferInfo, QString("/DEVOICE %1").arg(nick));
473 Client::userInput(bufferInfo, QString("/KICK %1").arg(nick));
476 Client::userInput(bufferInfo, QString("/BAN %1").arg(nick));
479 Client::userInput(bufferInfo, QString("/BAN %1").arg(nick));
480 Client::userInput(bufferInfo, QString("/KICK %1").arg(nick));
484 Client::bufferModel()->switchToOrStartQuery(networkId, nick);
488 auto *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
491 Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::SenderIgnore,
492 action->property("ignoreRule").toString(),
493 false, IgnoreListManager::SoftStrictness,
494 IgnoreListManager::NetworkScope,
495 ircUser->network()->networkName(), true);
500 auto *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
503 Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::SenderIgnore,
504 action->property("ignoreRule").toString(),
505 false, IgnoreListManager::SoftStrictness,
506 IgnoreListManager::NetworkScope,
507 ircUser->network()->networkName(), true);
510 case NickIgnoreDomain:
512 auto *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
515 Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::SenderIgnore,
516 action->property("ignoreRule").toString(),
517 false, IgnoreListManager::SoftStrictness,
518 IgnoreListManager::NetworkScope,
519 ircUser->network()->networkName(), true);
522 case NickIgnoreCustom:
523 // forward that to mainwin since we can access the settingspage only from there
524 emit showIgnoreList(action->property("ignoreRule").toString());
526 case NickIgnoreToggleEnabled0:
527 case NickIgnoreToggleEnabled1:
528 case NickIgnoreToggleEnabled2:
529 case NickIgnoreToggleEnabled3:
530 case NickIgnoreToggleEnabled4:
531 Client::ignoreListManager()->requestToggleIgnoreRule(action->property("ignoreRule").toString());
534 qWarning() << "Unhandled nick action";
540 /***************************************************************************************************************
542 ***************************************************************************************************************/
544 NetworkModelController::JoinDlg::JoinDlg(const QModelIndex &index, QWidget *parent) : QDialog(parent)
546 setWindowIcon(icon::get("irc-join-channel"));
547 setWindowTitle(tr("Join Channel"));
549 auto *layout = new QGridLayout(this);
550 layout->addWidget(new QLabel(tr("Network:")), 0, 0);
551 layout->addWidget(networks = new QComboBox, 0, 1);
552 layout->addWidget(new QLabel(tr("Channel:")), 1, 0);
553 layout->addWidget(channel = new QLineEdit, 1, 1);
554 layout->addWidget(new QLabel(tr("Password:")), 2, 0);
555 layout->addWidget(password = new QLineEdit, 2, 1);
556 layout->addWidget(buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel), 3, 0, 1, 2);
560 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
561 networks->setInsertPolicy(QComboBox::InsertAlphabetically);
562 password->setEchoMode(QLineEdit::Password);
564 connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
565 connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
566 connect(channel, &QLineEdit::textChanged, this, &JoinDlg::on_channel_textChanged);
568 foreach(NetworkId id, Client::networkIds()) {
569 const Network *net = Client::network(id);
570 if (net->isConnected()) {
571 networks->addItem(net->networkName(), QVariant::fromValue<NetworkId>(id));
575 if (index.isValid()) {
576 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
577 if (networkId.isValid()) {
578 networks->setCurrentIndex(networks->findText(Client::network(networkId)->networkName()));
579 if (index.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer
580 && !index.data(NetworkModel::ItemActiveRole).toBool())
581 channel->setText(index.data(Qt::DisplayRole).toString());
587 NetworkId NetworkModelController::JoinDlg::networkId() const
589 return networks->itemData(networks->currentIndex()).value<NetworkId>();
593 QString NetworkModelController::JoinDlg::channelName() const
595 return channel->text();
599 QString NetworkModelController::JoinDlg::channelPassword() const
601 return password->text();
605 void NetworkModelController::JoinDlg::on_channel_textChanged(const QString &text)
607 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!text.isEmpty());