-QString ContextMenuActionProvider::nickName(const QModelIndex &index) const {
- IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
- if(ircUser)
- return ircUser->nick();
-
- BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
- if(!bufferInfo.isValid())
- return QString();
- if(!bufferInfo.type() == BufferInfo::QueryBuffer)
- return QString();
-
- return bufferInfo.bufferName(); // FIXME this might break with merged queries maybe
-}
-
-BufferId ContextMenuActionProvider::findQueryBuffer(const QModelIndex &index, const QString &predefinedNick) const {
- NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
- if(!networkId.isValid())
- return BufferId();
-
- QString nick = predefinedNick.isEmpty() ? nickName(index) : predefinedNick;
- if(nick.isEmpty())
- return BufferId();
-
- return findQueryBuffer(networkId, nick);
-}
-
-BufferId ContextMenuActionProvider::findQueryBuffer(NetworkId networkId, const QString &nick) const {
- return Client::networkModel()->bufferId(networkId, nick);
-}
-
-void ContextMenuActionProvider::handleExternalAction(ActionType type, QAction *action) {
- Q_UNUSED(type);
- if(_receiver && _method) {
- if(!QMetaObject::invokeMethod(_receiver, _method, Q_ARG(QAction *, action)))
- qWarning() << "NetworkModelActionProvider::handleExternalAction(): Could not invoke slot" << _receiver << _method;
- }
-}
-
-/******** Handle Actions *************************************************************************/
-
-void ContextMenuActionProvider::actionTriggered(QAction *action) {
- ActionType type = (ActionType)action->data().toInt();
- if(type > 0) {
- if(type & NetworkMask)
- handleNetworkAction(type, action);
- else if(type & BufferMask)
- handleBufferAction(type, action);
- else if(type & HideMask)
- handleHideAction(type, action);
- else if(type & GeneralMask)
- handleGeneralAction(type, action);
- else if(type & NickMask)
- handleNickAction(type, action);
- else if(type & ExternalMask)
- handleExternalAction(type, action);
- else
- qWarning() << "NetworkModelActionProvider::actionTriggered(): Unhandled action!";
- }
- _indexList.clear();
- _messageFilter = 0;
- _receiver = 0;
-}
-
-void ContextMenuActionProvider::handleNetworkAction(ActionType type, QAction *) {
- if(!_indexList.count())
- return;
- const Network *network = Client::network(_indexList.at(0).data(NetworkModel::NetworkIdRole).value<NetworkId>());
- Q_CHECK_PTR(network);
- if(!network)
- return;
-
- switch(type) {
- case NetworkConnect:
- network->requestConnect();
- break;
- case NetworkDisconnect:
- network->requestDisconnect();
- break;
- default:
- break;
- }
-}
-
-void ContextMenuActionProvider::handleBufferAction(ActionType type, QAction *) {
- if(type == BufferRemove) {
- removeBuffers(_indexList);
- } else {
-
- foreach(QModelIndex index, _indexList) {
- BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
- if(!bufferInfo.isValid())
- continue;
-
- switch(type) {
- case BufferJoin:
- Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
- break;
- case BufferPart:
- {
- QString reason = Client::identity(Client::network(bufferInfo.networkId())->identity())->partReason();
- Client::userInput(bufferInfo, QString("/PART %1").arg(reason));
- break;
- }
- case BufferSwitchTo:
- Client::bufferModel()->switchToBuffer(bufferInfo.bufferId());
- break;
- default:
- break;
- }
- }
+void ContextMenuActionProvider::addIgnoreMenu(QMenu *menu, const QString &hostmask, const QMap<QString, bool> &ignoreMap) {
+ QMenu *ignoreMenu = _nickIgnoreMenuAction->menu();
+ ignoreMenu->clear();
+ QString nick = nickFromMask(hostmask);
+ QString ident = userFromMask(hostmask);
+ QString host = hostFromMask(hostmask);
+ QString domain = host;
+ QRegExp domainRx = QRegExp("(\\.[^.]+\\.\\w+\\D)$");
+ if(domainRx.indexIn(host) != -1)
+ domain = domainRx.cap(1);
+ // we can't rely on who-data
+ // if we don't have the data, we skip actions where we would need it
+ bool haveWhoData = !ident.isEmpty() && !host.isEmpty();
+
+ // add "Add Ignore Rule" description
+ ignoreMenu->addAction(_ignoreDescriptions.at(0));
+
+ if(haveWhoData) {
+ QString text;
+ text = QString("*!%1@%2").arg(ident, host);
+ action(NickIgnoreUser)->setText(text);
+ action(NickIgnoreUser)->setProperty("ignoreRule", text);
+
+ text = QString("*!*@%1").arg(host);
+ action(NickIgnoreHost)->setText(text);
+ action(NickIgnoreHost)->setProperty("ignoreRule", text);
+
+ text = domain.at(0) == '.' ? QString("*!%1@*%2").arg(ident, domain)
+ : QString("*!%1@%2").arg(ident, domain);
+
+ action(NickIgnoreDomain)->setText(text);
+ action(NickIgnoreDomain)->setProperty("ignoreRule", text);
+
+ if(!ignoreMap.contains(action(NickIgnoreUser)->property("ignoreRule").toString()))
+ ignoreMenu->addAction(action(NickIgnoreUser));
+ if(!ignoreMap.contains(action(NickIgnoreHost)->property("ignoreRule").toString()))
+ ignoreMenu->addAction(action(NickIgnoreHost));
+ // we only add that NickIgnoreDomain if it isn't the same as NickIgnoreUser
+ // as happens with @foobar.com hostmasks and ips
+ if(!ignoreMap.contains(action(NickIgnoreDomain)->property("ignoreRule").toString())
+ && action(NickIgnoreUser)->property("ignoreRule").toString() != action(NickIgnoreDomain)->property("ignoreRule").toString())
+ ignoreMenu->addAction(action(NickIgnoreDomain));