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)),
45 _messageFilter(nullptr),
48 connect(_actionCollection, SIGNAL(actionTriggered(QAction *)), SLOT(actionTriggered(QAction *)));
52 NetworkModelController::~NetworkModelController()
57 Action *NetworkModelController::registerAction(ActionType type, const QString &text, bool checkable)
59 return registerAction(type, QPixmap(), text, checkable);
63 Action *NetworkModelController::registerAction(ActionType type, const QIcon &icon, const QString &text, bool checkable)
67 act = new Action(text, this);
69 act = new Action(icon, text, this);
71 act->setCheckable(checkable);
74 _actionCollection->addAction(QString::number(type, 16), act);
75 _actionByType[type] = act;
80 /******** Helper Functions ***********************************************************************/
82 void NetworkModelController::setIndexList(const QModelIndex &index)
84 _indexList = QList<QModelIndex>() << index;
88 void NetworkModelController::setIndexList(const QList<QModelIndex> &list)
94 void NetworkModelController::setMessageFilter(MessageFilter *filter)
96 _messageFilter = filter;
100 void NetworkModelController::setContextItem(const QString &contextItem)
102 _contextItem = contextItem;
106 void NetworkModelController::setSlot(QObject *receiver, const char *method)
108 _receiver = receiver;
113 bool NetworkModelController::checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState)
115 if (!index.isValid())
118 ItemActiveStates isActive = index.data(NetworkModel::ItemActiveRole).toBool()
122 if (!(isActive & requiredActiveState))
129 QString NetworkModelController::nickName(const QModelIndex &index) const
131 IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
133 return ircUser->nick();
135 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
136 if (!bufferInfo.isValid())
138 if (bufferInfo.type() != BufferInfo::QueryBuffer)
141 return bufferInfo.bufferName(); // FIXME this might break with merged queries maybe
145 BufferId NetworkModelController::findQueryBuffer(const QModelIndex &index, const QString &predefinedNick) const
147 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
148 if (!networkId.isValid())
151 QString nick = predefinedNick.isEmpty() ? nickName(index) : predefinedNick;
155 return findQueryBuffer(networkId, nick);
159 BufferId NetworkModelController::findQueryBuffer(NetworkId networkId, const QString &nick) const
161 return Client::networkModel()->bufferId(networkId, nick);
165 void NetworkModelController::removeBuffers(const QModelIndexList &indexList)
167 QList<BufferInfo> inactive;
168 foreach(QModelIndex index, indexList) {
169 BufferInfo info = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
170 if (info.isValid()) {
171 if (info.type() == BufferInfo::QueryBuffer
172 || (info.type() == BufferInfo::ChannelBuffer && !index.data(NetworkModel::ItemActiveRole).toBool()))
177 if (inactive.count()) {
178 msg = tr("Do you want to delete the following buffer(s) permanently?", "", inactive.count());
181 foreach(BufferInfo info, inactive) {
183 msg += QString("<li>%1</li>").arg(info.bufferName());
190 if (count > 9 && inactive.size() - count != 0)
191 msg += tr("...and <b>%1</b> more<br><br>").arg(inactive.size() - count);
192 msg += tr("<b>Note:</b> This will delete all related data, including all backlog data, from the core's database and cannot be undone.");
193 if (inactive.count() != indexList.count())
194 msg += tr("<br>Active channel buffers cannot be deleted, please part the channel first.");
196 if (QMessageBox::question(nullptr, tr("Remove buffers permanently?"), msg, QMessageBox::Yes|QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
197 foreach(BufferInfo info, inactive)
198 Client::removeBuffer(info.bufferId());
204 void NetworkModelController::handleExternalAction(ActionType type, QAction *action)
207 if (receiver() && method()) {
208 if (!QMetaObject::invokeMethod(receiver(), method(), Q_ARG(QAction *, action)))
209 qWarning() << "NetworkModelActionController::handleExternalAction(): Could not invoke slot" << receiver() << method();
214 /******** Handle Actions *************************************************************************/
216 void NetworkModelController::actionTriggered(QAction *action)
218 ActionType type = (ActionType)action->data().toInt();
220 if (type & NetworkMask)
221 handleNetworkAction(type, action);
222 else if (type & BufferMask)
223 handleBufferAction(type, action);
224 else if (type & HideMask)
225 handleHideAction(type, action);
226 else if (type & GeneralMask)
227 handleGeneralAction(type, action);
228 else if (type & NickMask)
229 handleNickAction(type, action);
230 else if (type & ExternalMask)
231 handleExternalAction(type, action);
233 qWarning() << "NetworkModelController::actionTriggered(): Unhandled action!";
238 void NetworkModelController::handleNetworkAction(ActionType type, QAction *)
240 if (type == NetworkConnectAllWithDropdown || type == NetworkDisconnectAllWithDropdown || type == NetworkConnectAll || type == NetworkDisconnectAll) {
241 if (type == NetworkConnectAllWithDropdown && QMessageBox::question(nullptr, tr("Question"), tr("Really Connect to all IRC Networks?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::No)
243 if (type == NetworkDisconnectAllWithDropdown && QMessageBox::question(nullptr, tr("Question"), tr("Really disconnect from all IRC Networks?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::No)
245 foreach(NetworkId id, Client::networkIds()) {
246 const Network *net = Client::network(id);
247 if ((type == NetworkConnectAllWithDropdown || type == NetworkConnectAll) && net->connectionState() == Network::Disconnected)
248 net->requestConnect();
249 if ((type == NetworkDisconnectAllWithDropdown || type == NetworkDisconnectAll) && net->connectionState() != Network::Disconnected)
250 net->requestDisconnect();
255 if (!indexList().count())
258 const Network *network = Client::network(indexList().at(0).data(NetworkModel::NetworkIdRole).value<NetworkId>());
259 Q_CHECK_PTR(network);
265 network->requestConnect();
267 case NetworkDisconnect:
268 network->requestDisconnect();
276 void NetworkModelController::handleBufferAction(ActionType type, QAction *)
278 if (type == BufferRemove) {
279 removeBuffers(indexList());
282 QList<BufferInfo> bufferList; // create temp list because model indexes might change
283 foreach(QModelIndex index, indexList()) {
284 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
285 if (bufferInfo.isValid())
286 bufferList << bufferInfo;
289 foreach(BufferInfo bufferInfo, bufferList) {
292 Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
296 QString reason = Client::identity(Client::network(bufferInfo.networkId())->identity())->partReason();
297 Client::userInput(bufferInfo, QString("/PART %1").arg(reason));
301 Client::bufferModel()->switchToBuffer(bufferInfo.bufferId());
311 void NetworkModelController::handleHideAction(ActionType type, QAction *action)
315 if (type == HideJoinPartQuit) {
316 bool anyChecked = NetworkModelController::action(HideJoin)->isChecked();
317 anyChecked |= NetworkModelController::action(HidePart)->isChecked();
318 anyChecked |= NetworkModelController::action(HideQuit)->isChecked();
320 // If any are checked, uncheck them all.
321 // If none are checked, check them all.
322 bool newCheckedState = !anyChecked;
323 NetworkModelController::action(HideJoin)->setChecked(newCheckedState);
324 NetworkModelController::action(HidePart)->setChecked(newCheckedState);
325 NetworkModelController::action(HideQuit)->setChecked(newCheckedState);
329 if (NetworkModelController::action(HideJoin)->isChecked())
330 filter |= Message::Join | Message::NetsplitJoin;
331 if (NetworkModelController::action(HidePart)->isChecked())
332 filter |= Message::Part;
333 if (NetworkModelController::action(HideQuit)->isChecked())
334 filter |= Message::Quit | Message::NetsplitQuit;
335 if (NetworkModelController::action(HideNick)->isChecked())
336 filter |= Message::Nick;
337 if (NetworkModelController::action(HideMode)->isChecked())
338 filter |= Message::Mode;
339 if (NetworkModelController::action(HideDayChange)->isChecked())
340 filter |= Message::DayChange;
341 if (NetworkModelController::action(HideTopic)->isChecked())
342 filter |= Message::Topic;
345 case HideJoinPartQuit:
354 BufferSettings(_messageFilter->idString()).setMessageFilter(filter);
356 foreach(QModelIndex index, _indexList) {
357 BufferId bufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
358 if (!bufferId.isValid())
360 BufferSettings(bufferId).setMessageFilter(filter);
365 BufferSettings().setMessageFilter(filter);
367 case HideUseDefaults:
369 BufferSettings(_messageFilter->idString()).removeFilter();
371 foreach(QModelIndex index, _indexList) {
372 BufferId bufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
373 if (!bufferId.isValid())
375 BufferSettings(bufferId).removeFilter();
385 void NetworkModelController::handleGeneralAction(ActionType type, QAction *action)
389 if (!indexList().count())
391 NetworkId networkId = indexList().at(0).data(NetworkModel::NetworkIdRole).value<NetworkId>();
396 QString channelName = contextItem();
397 QString channelPassword;
398 if (channelName.isEmpty()) {
399 JoinDlg dlg(indexList().first());
400 if (dlg.exec() == QDialog::Accepted) {
401 channelName = dlg.channelName();
402 networkId = dlg.networkId();
403 channelPassword = dlg.channelPassword();
406 if (!channelName.isEmpty()) {
407 if (!channelPassword.isEmpty())
408 Client::instance()->userInput(BufferInfo::fakeStatusBuffer(networkId), QString("/JOIN %1 %2").arg(channelName).arg(channelPassword));
410 Client::instance()->userInput(BufferInfo::fakeStatusBuffer(networkId), QString("/JOIN %1").arg(channelName));
414 case ShowChannelList:
415 if (networkId.isValid()) {
416 // Don't immediately list channels, allowing customization of filter first
417 emit showChannelList(networkId, {}, false);
420 case ShowNetworkConfig:
421 if (networkId.isValid())
422 emit showNetworkConfig(networkId);
425 if (networkId.isValid())
426 emit showIgnoreList(QString());
434 void NetworkModelController::handleNickAction(ActionType type, QAction *action)
436 foreach(QModelIndex index, indexList()) {
437 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
438 if (!networkId.isValid())
440 QString nick = nickName(index);
443 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
444 if (!bufferInfo.isValid())
449 Client::userInput(bufferInfo, QString("/WHOIS %1 %1").arg(nick));
451 case NickCtcpVersion:
452 Client::userInput(bufferInfo, QString("/CTCP %1 VERSION").arg(nick));
455 Client::userInput(bufferInfo, QString("/CTCP %1 PING").arg(nick));
458 Client::userInput(bufferInfo, QString("/CTCP %1 TIME").arg(nick));
460 case NickCtcpClientinfo:
461 Client::userInput(bufferInfo, QString("/CTCP %1 CLIENTINFO").arg(nick));
464 Client::userInput(bufferInfo, QString("/OP %1").arg(nick));
467 Client::userInput(bufferInfo, QString("/DEOP %1").arg(nick));
470 Client::userInput(bufferInfo, QString("/HALFOP %1").arg(nick));
473 Client::userInput(bufferInfo, QString("/DEHALFOP %1").arg(nick));
476 Client::userInput(bufferInfo, QString("/VOICE %1").arg(nick));
479 Client::userInput(bufferInfo, QString("/DEVOICE %1").arg(nick));
482 Client::userInput(bufferInfo, QString("/KICK %1").arg(nick));
485 Client::userInput(bufferInfo, QString("/BAN %1").arg(nick));
488 Client::userInput(bufferInfo, QString("/BAN %1").arg(nick));
489 Client::userInput(bufferInfo, QString("/KICK %1").arg(nick));
493 Client::bufferModel()->switchToOrStartQuery(networkId, nick);
497 IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
500 Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::SenderIgnore,
501 action->property("ignoreRule").toString(),
502 false, IgnoreListManager::SoftStrictness,
503 IgnoreListManager::NetworkScope,
504 ircUser->network()->networkName(), true);
509 IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
512 Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::SenderIgnore,
513 action->property("ignoreRule").toString(),
514 false, IgnoreListManager::SoftStrictness,
515 IgnoreListManager::NetworkScope,
516 ircUser->network()->networkName(), true);
519 case NickIgnoreDomain:
521 IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
524 Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::SenderIgnore,
525 action->property("ignoreRule").toString(),
526 false, IgnoreListManager::SoftStrictness,
527 IgnoreListManager::NetworkScope,
528 ircUser->network()->networkName(), true);
531 case NickIgnoreCustom:
532 // forward that to mainwin since we can access the settingspage only from there
533 emit showIgnoreList(action->property("ignoreRule").toString());
535 case NickIgnoreToggleEnabled0:
536 case NickIgnoreToggleEnabled1:
537 case NickIgnoreToggleEnabled2:
538 case NickIgnoreToggleEnabled3:
539 case NickIgnoreToggleEnabled4:
540 Client::ignoreListManager()->requestToggleIgnoreRule(action->property("ignoreRule").toString());
543 qWarning() << "Unhandled nick action";
549 /***************************************************************************************************************
551 ***************************************************************************************************************/
553 NetworkModelController::JoinDlg::JoinDlg(const QModelIndex &index, QWidget *parent) : QDialog(parent)
555 setWindowIcon(icon::get("irc-join-channel"));
556 setWindowTitle(tr("Join Channel"));
558 QGridLayout *layout = new QGridLayout(this);
559 layout->addWidget(new QLabel(tr("Network:")), 0, 0);
560 layout->addWidget(networks = new QComboBox, 0, 1);
561 layout->addWidget(new QLabel(tr("Channel:")), 1, 0);
562 layout->addWidget(channel = new QLineEdit, 1, 1);
563 layout->addWidget(new QLabel(tr("Password:")), 2, 0);
564 layout->addWidget(password = new QLineEdit, 2, 1);
565 layout->addWidget(buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel), 3, 0, 1, 2);
569 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
570 networks->setInsertPolicy(QComboBox::InsertAlphabetically);
571 password->setEchoMode(QLineEdit::Password);
573 connect(buttonBox, SIGNAL(accepted()), SLOT(accept()));
574 connect(buttonBox, SIGNAL(rejected()), SLOT(reject()));
575 connect(channel, SIGNAL(textChanged(QString)), SLOT(on_channel_textChanged(QString)));
577 foreach(NetworkId id, Client::networkIds()) {
578 const Network *net = Client::network(id);
579 if (net->isConnected()) {
580 networks->addItem(net->networkName(), QVariant::fromValue<NetworkId>(id));
584 if (index.isValid()) {
585 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
586 if (networkId.isValid()) {
587 networks->setCurrentIndex(networks->findText(Client::network(networkId)->networkName()));
588 if (index.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer
589 && !index.data(NetworkModel::ItemActiveRole).toBool())
590 channel->setText(index.data(Qt::DisplayRole).toString());
596 NetworkId NetworkModelController::JoinDlg::networkId() const
598 return networks->itemData(networks->currentIndex()).value<NetworkId>();
602 QString NetworkModelController::JoinDlg::channelName() const
604 return channel->text();
608 QString NetworkModelController::JoinDlg::channelPassword() const
610 return password->text();
614 void NetworkModelController::JoinDlg::on_channel_textChanged(const QString &text)
616 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!text.isEmpty());