6061e38261c23406a92c4c82f587de05e3acf2e5
[quassel.git] / src / uisupport / networkmodelactionprovider.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include <QInputDialog>
22 #include <QMenu>
23 #include <QMessageBox>
24
25 #include "networkmodelactionprovider.h"
26
27 #include "buffermodel.h"
28 #include "buffersettings.h"
29 #include "iconloader.h"
30 #include "clientidentity.h"
31 #include "network.h"
32 #include "util.h"
33
34 NetworkModelActionProvider::NetworkModelActionProvider(QObject *parent)
35 : AbstractActionProvider(parent),
36   _actionCollection(new ActionCollection(this)),
37   _messageFilter(0),
38   _receiver(0)
39 {
40   registerAction(NetworkConnect, SmallIcon("network-connect"), tr("Connect"));
41   registerAction(NetworkDisconnect, SmallIcon("network-disconnect"), tr("Disconnect"));
42
43   registerAction(BufferJoin, tr("Join"));
44   registerAction(BufferPart, tr("Part"));
45   registerAction(BufferRemove, tr("Delete Buffer(s)..."));
46   registerAction(BufferSwitchTo, tr("Show Buffer"));
47
48   registerAction(HideJoin, tr("Joins"), true);
49   registerAction(HidePart, tr("Parts"), true);
50   registerAction(HideQuit, tr("Quits"), true);
51   registerAction(HideNick, tr("Nick Changes"), true);
52   registerAction(HideMode, tr("Mode Changes"), true);
53   registerAction(HideDayChange, tr("Day Changes"), true);
54   registerAction(HideApplyToAll, tr("Set as Default..."));
55   registerAction(HideUseDefaults, tr("Use Defaults..."));
56
57   registerAction(JoinChannel, tr("Join Channel..."));
58
59   registerAction(NickQuery, tr("Start Query"));
60   registerAction(NickSwitchTo, tr("Show Query"));
61   registerAction(NickWhois, tr("Whois"));
62
63   registerAction(NickCtcpVersion, tr("Version"));
64   registerAction(NickCtcpTime, tr("Time"));
65   registerAction(NickCtcpPing, tr("Ping"));
66   registerAction(NickCtcpFinger, tr("Finger"));
67
68   registerAction(NickOp, tr("Give Operator Status"));
69   registerAction(NickDeop, tr("Take Operator Status"));
70   registerAction(NickVoice, tr("Give Voice"));
71   registerAction(NickDevoice, tr("Take Voice"));
72   registerAction(NickKick, tr("Kick From Channel"));
73   registerAction(NickBan, tr("Ban From Channel"));
74   registerAction(NickKickBan, tr("Kick && Ban"));
75
76   registerAction(HideBufferTemporarily, tr("Hide Buffer(s) Temporarily"));
77   registerAction(HideBufferPermanently, tr("Hide Buffer(s) Permanently"));
78   registerAction(ShowChannelList, SmallIcon("format-list-unordered"), tr("Show Channel List"));
79   registerAction(ShowIgnoreList, tr("Show Ignore List"));
80
81   connect(_actionCollection, SIGNAL(actionTriggered(QAction *)), SLOT(actionTriggered(QAction *)));
82
83   QMenu *hideEventsMenu = new QMenu();
84   hideEventsMenu->addAction(action(HideJoin));
85   hideEventsMenu->addAction(action(HidePart));
86   hideEventsMenu->addAction(action(HideQuit));
87   hideEventsMenu->addAction(action(HideNick));
88   hideEventsMenu->addAction(action(HideMode));
89   hideEventsMenu->addAction(action(HideDayChange));
90   hideEventsMenu->addSeparator();
91   hideEventsMenu->addAction(action(HideApplyToAll));
92   hideEventsMenu->addAction(action(HideUseDefaults));
93   _hideEventsMenuAction = new Action(tr("Hide Events"), 0);
94   _hideEventsMenuAction->setMenu(hideEventsMenu);
95
96   QMenu *nickCtcpMenu = new QMenu();
97   nickCtcpMenu->addAction(action(NickCtcpPing));
98   nickCtcpMenu->addAction(action(NickCtcpVersion));
99   nickCtcpMenu->addAction(action(NickCtcpTime));
100   nickCtcpMenu->addAction(action(NickCtcpFinger));
101   _nickCtcpMenuAction = new Action(tr("CTCP"), 0);
102   _nickCtcpMenuAction->setMenu(nickCtcpMenu);
103
104   QMenu *nickModeMenu = new QMenu();
105   nickModeMenu->addAction(action(NickOp));
106   nickModeMenu->addAction(action(NickDeop));
107   nickModeMenu->addAction(action(NickVoice));
108   nickModeMenu->addAction(action(NickDevoice));
109   nickModeMenu->addSeparator();
110   nickModeMenu->addAction(action(NickKick));
111   nickModeMenu->addAction(action(NickBan));
112   nickModeMenu->addAction(action(NickKickBan));
113   _nickModeMenuAction = new Action(tr("Actions"), 0);
114   _nickModeMenuAction->setMenu(nickModeMenu);
115 }
116
117 NetworkModelActionProvider::~NetworkModelActionProvider() {
118   _hideEventsMenuAction->menu()->deleteLater();
119   _hideEventsMenuAction->deleteLater();
120   _nickCtcpMenuAction->menu()->deleteLater();
121   _nickCtcpMenuAction->deleteLater();
122   _nickModeMenuAction->menu()->deleteLater();
123   _nickModeMenuAction->deleteLater();
124 }
125
126 void NetworkModelActionProvider::registerAction(ActionType type, const QString &text, bool checkable) {
127   registerAction(type, QPixmap(), text, checkable);
128 }
129
130 void NetworkModelActionProvider::registerAction(ActionType type, const QPixmap &icon, const QString &text, bool checkable) {
131   Action *act;
132   if(icon.isNull())
133     act = new Action(text, this);
134   else
135     act = new Action(icon, text, this);
136
137   act->setCheckable(checkable);
138   act->setData(type);
139
140   _actionCollection->addAction(QString::number(type, 16), act);
141   _actionByType[type] = act;
142 }
143
144 void NetworkModelActionProvider::addActions(QMenu *menu, BufferId bufId, QObject *receiver, const char *method) {
145   if(!bufId.isValid())
146     return;
147   _messageFilter = 0;
148   _contextItem = QString();
149   addActions(menu, Client::networkModel()->bufferIndex(bufId), receiver, method);
150 }
151
152 void NetworkModelActionProvider::addActions(QMenu *menu, const QModelIndex &index, QObject *receiver, const char *method, bool isCustomBufferView) {
153   if(!index.isValid())
154     return;
155   _messageFilter = 0;
156   _contextItem = QString();
157   addActions(menu, QList<QModelIndex>() << index, receiver, method, isCustomBufferView);
158 }
159
160 void NetworkModelActionProvider::addActions(QMenu *menu, MessageFilter *filter, BufferId msgBuffer, QObject *receiver, const char *slot) {
161   addActions(menu, filter, msgBuffer, QString(), receiver, slot);
162 }
163
164 void NetworkModelActionProvider::addActions(QMenu *menu, MessageFilter *filter, BufferId msgBuffer, const QString &chanOrNick, QObject *receiver, const char *method) {
165   if(!filter)
166     return;
167   _messageFilter = filter;
168   _contextItem = chanOrNick;
169   addActions(menu, QList<QModelIndex>() << Client::networkModel()->bufferIndex(msgBuffer), receiver, method);
170 }
171
172 // add a list of actions sensible for the current item(s)
173 void NetworkModelActionProvider::addActions(QMenu *menu,
174                                             const QList<QModelIndex> &indexList,
175                                             QObject *receiver,
176                                             const char *method,
177                                             bool isCustomBufferView)
178 {
179   if(!indexList.count())
180     return;
181
182   _indexList = indexList;
183   _receiver = receiver;
184   _method = method;
185
186   if(!_messageFilter) {
187     // this means we are in a BufferView (or NickView) rather than a ChatView
188
189     // first index in list determines the menu type (just in case we have both buffers and networks selected, for example)
190     QModelIndex index = _indexList.at(0);
191     NetworkModel::ItemType itemType = static_cast<NetworkModel::ItemType>(index.data(NetworkModel::ItemTypeRole).toInt());
192
193     switch(itemType) {
194       case NetworkModel::NetworkItemType:
195         addNetworkItemActions(menu, index);
196         break;
197       case NetworkModel::BufferItemType:
198         addBufferItemActions(menu, index, isCustomBufferView);
199         break;
200       case NetworkModel::IrcUserItemType:
201         addIrcUserActions(menu, index);
202         break;
203       default:
204         return;
205
206     }
207   } else {
208     // ChatView actions
209     if(_contextItem.isEmpty()) {
210       // a) query buffer: handle like ircuser
211       // b) general chatview: handle like channel iff it displays a single buffer
212       // NOTE stuff breaks probably with merged buffers, need to rework a lot around here then
213       if(_messageFilter->containedBuffers().count() == 1) {
214         // we can handle this like a single bufferItem
215         QModelIndex index = Client::networkModel()->bufferIndex(_messageFilter->containedBuffers().values().at(0));
216         _indexList = QList<QModelIndex>() << index;
217         addBufferItemActions(menu, index);
218         return;
219       } else {
220         // TODO: actions for merged buffers... _indexList contains the index of the message we clicked on
221
222       }
223     } else {
224       // context item = chan or nick, _indexList = buf where the msg clicked on originated
225       if(isChannelName(_contextItem)) {
226         QModelIndex msgIdx = _indexList.at(0);
227         if(!msgIdx.isValid())
228           return;
229         NetworkId networkId = msgIdx.data(NetworkModel::NetworkIdRole).value<NetworkId>();
230         BufferId bufId = Client::networkModel()->bufferId(networkId, _contextItem);
231         if(bufId.isValid()) {
232           QModelIndex targetIdx = Client::networkModel()->bufferIndex(bufId);
233           _indexList = QList<QModelIndex>() << targetIdx;
234           addAction(BufferJoin, menu, targetIdx, InactiveState);
235           addAction(BufferSwitchTo, menu, targetIdx, ActiveState);
236         } else
237           addAction(JoinChannel, menu);
238       } else {
239         // TODO: actions for a nick
240       }
241     }
242   }
243 }
244
245 void NetworkModelActionProvider::addNetworkItemActions(QMenu *menu, const QModelIndex &index) {
246   NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
247   if(!networkId.isValid())
248     return;
249   const Network *network = Client::network(networkId);
250   Q_CHECK_PTR(network);
251   if(!network)
252     return;
253
254   addAction(NetworkConnect, menu, network->connectionState() == Network::Disconnected);
255   addAction(NetworkDisconnect, menu, network->connectionState() != Network::Disconnected);
256   menu->addSeparator();
257   addAction(ShowChannelList, menu, index, ActiveState);
258   addAction(JoinChannel, menu, index, ActiveState);
259
260 }
261
262 void NetworkModelActionProvider::addBufferItemActions(QMenu *menu, const QModelIndex &index, bool isCustomBufferView) {
263   BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
264
265   switch(bufferInfo.type()) {
266     case BufferInfo::ChannelBuffer:
267       addAction(BufferJoin, menu, index, InactiveState);
268       addAction(BufferPart, menu, index, ActiveState);
269       menu->addSeparator();
270       addHideEventsMenu(menu, bufferInfo.bufferId());
271       menu->addSeparator();
272       addAction(HideBufferTemporarily, menu, isCustomBufferView);
273       addAction(HideBufferPermanently, menu, isCustomBufferView);
274       addAction(BufferRemove, menu, index, InactiveState);
275       break;
276
277     case BufferInfo::QueryBuffer:
278     {
279       //IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
280       //if(ircUser) {
281         addIrcUserActions(menu, index);
282         menu->addSeparator();
283       //}
284       addHideEventsMenu(menu, bufferInfo.bufferId());
285       menu->addSeparator();
286       addAction(HideBufferTemporarily, menu, isCustomBufferView);
287       addAction(HideBufferPermanently, menu, isCustomBufferView);
288       addAction(BufferRemove, menu, index);
289       break;
290     }
291
292     default:
293       addAction(HideBufferTemporarily, menu, isCustomBufferView);
294       addAction(HideBufferPermanently, menu, isCustomBufferView);
295   }
296 }
297
298 void NetworkModelActionProvider::addIrcUserActions(QMenu *menu, const QModelIndex &index) {
299   // this can be called: a) as a nicklist context menu (index has IrcUserItemType)
300   //                     b) as a query buffer context menu (index has BufferItemType and is a QueryBufferItem)
301   //                     c) right-click in a query chatview (same as b), index will be the corresponding QueryBufferItem)
302   //                     d) right-click on some nickname (_contextItem will be non-null, _filter -> chatview, index -> message buffer)
303
304   if(_contextItem.isNull()) {
305     // cases a, b, c
306     bool haveQuery = _indexList.count() == 1 && findQueryBuffer(index).isValid();
307     NetworkModel::ItemType itemType = static_cast<NetworkModel::ItemType>(index.data(NetworkModel::ItemTypeRole).toInt());
308     addAction(_nickModeMenuAction, menu, itemType == NetworkModel::IrcUserItemType);
309     addAction(_nickCtcpMenuAction, menu);
310     menu->addSeparator();
311     addAction(NickQuery, menu, itemType == NetworkModel::IrcUserItemType && !haveQuery && _indexList.count() == 1);
312     addAction(NickSwitchTo, menu, itemType == NetworkModel::IrcUserItemType && haveQuery);
313     menu->addSeparator();
314     addAction(NickWhois, menu, true);
315
316   } else if(!_contextItem.isEmpty() && _messageFilter) {
317     // case d
318     // TODO
319
320   }
321 }
322
323 /******** Helper Functions ***********************************************************************/
324
325 bool NetworkModelActionProvider::checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState) {
326   if(!index.isValid())
327     return false;
328
329   ItemActiveStates isActive = index.data(NetworkModel::ItemActiveRole).toBool()
330   ? ActiveState
331   : InactiveState;
332
333   if(!(isActive & requiredActiveState))
334     return false;
335
336   return true;
337 }
338
339 Action * NetworkModelActionProvider::addAction(ActionType type , QMenu *menu, const QModelIndex &index, ItemActiveStates requiredActiveState) {
340   return addAction(action(type), menu, checkRequirements(index, requiredActiveState));
341 }
342
343 Action * NetworkModelActionProvider::addAction(Action *action , QMenu *menu, const QModelIndex &index, ItemActiveStates requiredActiveState) {
344   return addAction(action, menu, checkRequirements(index, requiredActiveState));
345 }
346
347 Action * NetworkModelActionProvider::addAction(ActionType type , QMenu *menu, bool condition) {
348   return addAction(action(type), menu, condition);
349 }
350
351 Action * NetworkModelActionProvider::addAction(Action *action , QMenu *menu, bool condition) {
352   if(condition) {
353     menu->addAction(action);
354     action->setVisible(true);
355   } else {
356     action->setVisible(false);
357   }
358   return action;
359 }
360
361 void NetworkModelActionProvider::addHideEventsMenu(QMenu *menu, BufferId bufferId) {
362   if(BufferSettings(bufferId).hasFilter())
363     addHideEventsMenu(menu, BufferSettings(bufferId).messageFilter());
364   else
365     addHideEventsMenu(menu);
366 }
367
368 void NetworkModelActionProvider::addHideEventsMenu(QMenu *menu, MessageFilter *msgFilter) {
369   if(BufferSettings(msgFilter->idString()).hasFilter())
370     addHideEventsMenu(menu, BufferSettings(msgFilter->idString()).messageFilter());
371   else
372     addHideEventsMenu(menu);
373 }
374
375 void NetworkModelActionProvider::addHideEventsMenu(QMenu *menu, int filter) {
376   action(HideApplyToAll)->setEnabled(filter != -1);
377   action(HideUseDefaults)->setEnabled(filter != -1);
378   if(filter == -1)
379     filter = BufferSettings().messageFilter();
380
381   action(HideJoin)->setChecked(filter & Message::Join);
382   action(HidePart)->setChecked(filter & Message::Part);
383   action(HideQuit)->setChecked(filter & Message::Quit);
384   action(HideNick)->setChecked(filter & Message::Nick);
385   action(HideMode)->setChecked(filter & Message::Mode);
386   action(HideDayChange)->setChecked(filter & Message::DayChange);
387
388   menu->addAction(_hideEventsMenuAction);
389 }
390
391 QString NetworkModelActionProvider::nickName(const QModelIndex &index) const {
392   IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
393   if(ircUser)
394     return ircUser->nick();
395
396   BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
397   if(!bufferInfo.isValid())
398     return QString();
399   if(!bufferInfo.type() == BufferInfo::QueryBuffer)
400     return QString();
401
402   return bufferInfo.bufferName(); // FIXME this might break with merged queries maybe
403 }
404
405 BufferId NetworkModelActionProvider::findQueryBuffer(const QModelIndex &index, const QString &predefinedNick) const {
406   NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
407   if(!networkId.isValid())
408     return BufferId();
409
410   QString nick = predefinedNick.isEmpty() ? nickName(index) : predefinedNick;
411   if(nick.isEmpty())
412     return BufferId();
413
414   return findQueryBuffer(networkId, nick);
415 }
416
417 BufferId NetworkModelActionProvider::findQueryBuffer(NetworkId networkId, const QString &nick) const {
418   return Client::networkModel()->bufferId(networkId, nick);
419 }
420
421 void NetworkModelActionProvider::handleExternalAction(ActionType type, QAction *action) {
422   Q_UNUSED(type);
423   if(_receiver && _method) {
424     if(!QMetaObject::invokeMethod(_receiver, _method, Q_ARG(QAction *, action)))
425       qWarning() << "NetworkModelActionProvider::handleExternalAction(): Could not invoke slot" << _receiver << _method;
426   }
427 }
428
429 /******** Handle Actions *************************************************************************/
430
431 void NetworkModelActionProvider::actionTriggered(QAction *action) {
432   ActionType type = (ActionType)action->data().toInt();
433   if(type > 0) {
434     if(type & NetworkMask)
435       handleNetworkAction(type, action);
436     else if(type & BufferMask)
437       handleBufferAction(type, action);
438     else if(type & HideMask)
439       handleHideAction(type, action);
440     else if(type & GeneralMask)
441       handleGeneralAction(type, action);
442     else if(type & NickMask)
443       handleNickAction(type, action);
444     else if(type & ExternalMask)
445       handleExternalAction(type, action);
446     else
447       qWarning() << "NetworkModelActionProvider::actionTriggered(): Unhandled action!";
448   }
449   _indexList.clear();
450   _messageFilter = 0;
451   _receiver = 0;
452 }
453
454 void NetworkModelActionProvider::handleNetworkAction(ActionType type, QAction *) {
455   if(!_indexList.count())
456     return;
457   const Network *network = Client::network(_indexList.at(0).data(NetworkModel::NetworkIdRole).value<NetworkId>());
458   Q_CHECK_PTR(network);
459   if(!network)
460     return;
461
462   switch(type) {
463     case NetworkConnect:
464       network->requestConnect();
465       break;
466     case NetworkDisconnect:
467       network->requestDisconnect();
468       break;
469     default:
470       break;
471   }
472 }
473
474 void NetworkModelActionProvider::handleBufferAction(ActionType type, QAction *) {
475   if(type == BufferRemove) {
476     removeBuffers(_indexList);
477   } else {
478
479     foreach(QModelIndex index, _indexList) {
480       BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
481       if(!bufferInfo.isValid())
482         continue;
483
484       switch(type) {
485         case BufferJoin:
486           Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
487           break;
488         case BufferPart:
489         {
490           QString reason = Client::identity(Client::network(bufferInfo.networkId())->identity())->partReason();
491           Client::userInput(bufferInfo, QString("/PART %1").arg(reason));
492           break;
493         }
494         case BufferSwitchTo:
495           Client::bufferModel()->switchToBuffer(bufferInfo.bufferId());
496           break;
497         default:
498           break;
499       }
500     }
501   }
502 }
503
504 void NetworkModelActionProvider::removeBuffers(const QModelIndexList &indexList) {
505   QList<BufferInfo> inactive;
506   foreach(QModelIndex index, indexList) {
507     BufferInfo info = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
508     if(info.isValid()) {
509       if(info.type() == BufferInfo::QueryBuffer
510         || (info.type() == BufferInfo::ChannelBuffer && !index.data(NetworkModel::ItemActiveRole).toBool()))
511           inactive << info;
512     }
513   }
514   QString msg;
515   if(inactive.count()) {
516     msg = tr("Do you want to delete the following buffer(s) permanently?", 0, inactive.count());
517     msg += "<ul>";
518     foreach(BufferInfo info, inactive)
519       msg += QString("<li>%1</li>").arg(info.bufferName());
520     msg += "</ul>";
521     msg += tr("<b>Note:</b> This will delete all related data, including all backlog data, from the core's database and cannot be undone.");
522     if(inactive.count() != indexList.count())
523       msg += tr("<br>Active channel buffers cannot be deleted, please part the channel first.");
524
525     if(QMessageBox::question(0, tr("Remove buffers permanently?"), msg, QMessageBox::Yes|QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
526       foreach(BufferInfo info, inactive)
527         Client::removeBuffer(info.bufferId());
528     }
529   }
530 }
531
532 void NetworkModelActionProvider::handleHideAction(ActionType type, QAction *action) {
533   Q_UNUSED(action)
534
535   int filter = 0;
536   if(NetworkModelActionProvider::action(HideJoin)->isChecked())
537     filter |= Message::Join;
538   if(NetworkModelActionProvider::action(HidePart)->isChecked())
539     filter |= Message::Part;
540   if(NetworkModelActionProvider::action(HideQuit)->isChecked())
541     filter |= Message::Quit;
542   if(NetworkModelActionProvider::action(HideNick)->isChecked())
543     filter |= Message::Nick;
544   if(NetworkModelActionProvider::action(HideMode)->isChecked())
545     filter |= Message::Mode;
546   if(NetworkModelActionProvider::action(HideDayChange)->isChecked())
547     filter |= Message::DayChange;
548
549   switch(type) {
550   case HideJoin:
551   case HidePart:
552   case HideQuit:
553   case HideNick:
554   case HideMode:
555   case HideDayChange:
556     if(_messageFilter)
557       BufferSettings(_messageFilter->idString()).setMessageFilter(filter);
558     else {
559       foreach(QModelIndex index, _indexList) {
560         BufferId bufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
561         if(!bufferId.isValid())
562           continue;
563         BufferSettings(bufferId).setMessageFilter(filter);
564       }
565     }
566     return;
567   case HideApplyToAll:
568     BufferSettings().setMessageFilter(filter);
569   case HideUseDefaults:
570     if(_messageFilter)
571       BufferSettings(_messageFilter->idString()).removeFilter();
572     else {
573       foreach(QModelIndex index, _indexList) {
574         BufferId bufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
575         if(!bufferId.isValid())
576           continue;
577         BufferSettings(bufferId).removeFilter();
578       }
579     }
580     return;
581   default:
582     return;
583   };
584 }
585
586 void NetworkModelActionProvider::handleGeneralAction(ActionType type, QAction *action) {
587   Q_UNUSED(action)
588
589   if(!_indexList.count())
590     return;
591   NetworkId networkId = _indexList.at(0).data(NetworkModel::NetworkIdRole).value<NetworkId>();
592   if(!networkId.isValid())
593     return;
594
595   switch(type) {
596     case JoinChannel: {
597       QString channelName = _contextItem;
598       if(channelName.isEmpty()) {
599         bool ok;
600         channelName = QInputDialog::getText(0, tr("Join Channel"), tr("Input channel name:"), QLineEdit::Normal, QString(), &ok);
601         if(!ok)
602           return;
603       }
604       if(!channelName.isEmpty()) {
605         Client::instance()->userInput(BufferInfo::fakeStatusBuffer(networkId), QString("/JOIN %1").arg(channelName));
606       }
607       break;
608     }
609     case ShowChannelList:
610       emit showChannelList(networkId);
611       break;
612     case ShowIgnoreList:
613       emit showIgnoreList(networkId);
614       break;
615     default:
616       break;
617   }
618 }
619
620 void NetworkModelActionProvider::handleNickAction(ActionType type, QAction *) {
621   foreach(QModelIndex index, _indexList) {
622     NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
623     if(!networkId.isValid())
624       continue;
625     QString nick = nickName(index);
626     if(nick.isEmpty())
627       continue;
628     BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
629     if(!bufferInfo.isValid())
630       continue;
631
632     switch(type) {
633       case NickWhois:
634         Client::userInput(bufferInfo, QString("/WHOIS %1 %1").arg(nick));
635         break;
636       case NickCtcpVersion:
637         Client::userInput(bufferInfo, QString("/CTCP %1 VERSION").arg(nick));
638         break;
639       case NickCtcpPing:
640         Client::userInput(bufferInfo, QString("/CTCP %1 PING").arg(nick));
641         break;
642       case NickCtcpTime:
643         Client::userInput(bufferInfo, QString("/CTCP %1 TIME").arg(nick));
644         break;
645       case NickCtcpFinger:
646         Client::userInput(bufferInfo, QString("/CTCP %1 FINGER").arg(nick));
647         break;
648       case NickOp:
649         Client::userInput(bufferInfo, QString("/OP %1").arg(nick));
650         break;
651       case NickDeop:
652         Client::userInput(bufferInfo, QString("/DEOP %1").arg(nick));
653         break;
654       case NickVoice:
655         Client::userInput(bufferInfo, QString("/VOICE %1").arg(nick));
656         break;
657       case NickDevoice:
658         Client::userInput(bufferInfo, QString("/DEVOICE %1").arg(nick));
659         break;
660       case NickKick:
661         Client::userInput(bufferInfo, QString("/KICK %1").arg(nick));
662         break;
663       case NickBan:
664         Client::userInput(bufferInfo, QString("/BAN %1").arg(nick));
665         break;
666       case NickKickBan:
667         Client::userInput(bufferInfo, QString("/BAN %1").arg(nick));
668         Client::userInput(bufferInfo, QString("/KICK %1").arg(nick));
669         break;
670       case NickSwitchTo:
671         Client::bufferModel()->switchToBuffer(findQueryBuffer(networkId, nick));
672         break;
673       case NickQuery:
674         Client::userInput(bufferInfo, QString("/QUERY %1").arg(nick));
675         break;
676       default:
677         qWarning() << "Unhandled nick action";
678     }
679   }
680 }