Adapt chatview and nickview to join-and-switch
[quassel.git] / src / uisupport / networkmodelcontroller.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 <QComboBox>
22 #include <QDialogButtonBox>
23 #include <QGridLayout>
24 #include <QLabel>
25 #include <QLineEdit>
26 #include <QInputDialog>
27 #include <QMessageBox>
28 #include <QPushButton>
29
30 #include "networkmodelcontroller.h"
31
32 #include "buffermodel.h"
33 #include "buffersettings.h"
34 #include "iconloader.h"
35 #include "clientidentity.h"
36 #include "network.h"
37 #include "util.h"
38 #include "clientignorelistmanager.h"
39 #include "client.h"
40
41 NetworkModelController::NetworkModelController(QObject *parent)
42 : QObject(parent),
43   _actionCollection(new ActionCollection(this)),
44   _messageFilter(0),
45   _receiver(0)
46 {
47
48   connect(_actionCollection, SIGNAL(actionTriggered(QAction *)), SLOT(actionTriggered(QAction *)));
49
50 }
51
52 NetworkModelController::~NetworkModelController() {
53
54 }
55
56 Action * NetworkModelController::registerAction(ActionType type, const QString &text, bool checkable) {
57   return registerAction(type, QPixmap(), text, checkable);
58 }
59
60 Action * NetworkModelController::registerAction(ActionType type, const QPixmap &icon, const QString &text, bool checkable) {
61   Action *act;
62   if(icon.isNull())
63     act = new Action(text, this);
64   else
65     act = new Action(icon, text, this);
66
67   act->setCheckable(checkable);
68   act->setData(type);
69
70   _actionCollection->addAction(QString::number(type, 16), act);
71   _actionByType[type] = act;
72   return act;
73 }
74
75 /******** Helper Functions ***********************************************************************/
76
77 void NetworkModelController::setIndexList(const QModelIndex &index) {
78   _indexList = QList<QModelIndex>() << index;
79 }
80
81 void NetworkModelController::setIndexList(const QList<QModelIndex> &list) {
82   _indexList = list;
83 }
84
85 void NetworkModelController::setMessageFilter(MessageFilter *filter) {
86   _messageFilter = filter;
87 }
88
89 void NetworkModelController::setContextItem(const QString &contextItem) {
90   _contextItem = contextItem;
91 }
92
93 void NetworkModelController::setSlot(QObject *receiver, const char *method) {
94   _receiver = receiver;
95   _method = method;
96 }
97
98 bool NetworkModelController::checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState) {
99   if(!index.isValid())
100     return false;
101
102   ItemActiveStates isActive = index.data(NetworkModel::ItemActiveRole).toBool()
103   ? ActiveState
104   : InactiveState;
105
106   if(!(isActive & requiredActiveState))
107     return false;
108
109   return true;
110 }
111
112 QString NetworkModelController::nickName(const QModelIndex &index) const {
113   IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
114   if(ircUser)
115     return ircUser->nick();
116
117   BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
118   if(!bufferInfo.isValid())
119     return QString();
120   if(bufferInfo.type() != BufferInfo::QueryBuffer)
121     return QString();
122
123   return bufferInfo.bufferName(); // FIXME this might break with merged queries maybe
124 }
125
126 BufferId NetworkModelController::findQueryBuffer(const QModelIndex &index, const QString &predefinedNick) const {
127   NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
128   if(!networkId.isValid())
129     return BufferId();
130
131   QString nick = predefinedNick.isEmpty() ? nickName(index) : predefinedNick;
132   if(nick.isEmpty())
133     return BufferId();
134
135   return findQueryBuffer(networkId, nick);
136 }
137
138 BufferId NetworkModelController::findQueryBuffer(NetworkId networkId, const QString &nick) const {
139   return Client::networkModel()->bufferId(networkId, nick);
140 }
141
142 void NetworkModelController::removeBuffers(const QModelIndexList &indexList) {
143   QList<BufferInfo> inactive;
144   foreach(QModelIndex index, indexList) {
145     BufferInfo info = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
146     if(info.isValid()) {
147       if(info.type() == BufferInfo::QueryBuffer
148         || (info.type() == BufferInfo::ChannelBuffer && !index.data(NetworkModel::ItemActiveRole).toBool()))
149         inactive << info;
150     }
151   }
152   QString msg;
153   if(inactive.count()) {
154     msg = tr("Do you want to delete the following buffer(s) permanently?", 0, inactive.count());
155     msg += "<ul>";
156     foreach(BufferInfo info, inactive)
157       msg += QString("<li>%1</li>").arg(info.bufferName());
158     msg += "</ul>";
159     msg += tr("<b>Note:</b> This will delete all related data, including all backlog data, from the core's database and cannot be undone.");
160     if(inactive.count() != indexList.count())
161       msg += tr("<br>Active channel buffers cannot be deleted, please part the channel first.");
162     
163     if(QMessageBox::question(0, tr("Remove buffers permanently?"), msg, QMessageBox::Yes|QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
164       foreach(BufferInfo info, inactive)
165         Client::removeBuffer(info.bufferId());
166     }
167   }
168 }
169
170 void NetworkModelController::handleExternalAction(ActionType type, QAction *action) {
171   Q_UNUSED(type);
172   if(receiver() && method()) {
173     if(!QMetaObject::invokeMethod(receiver(), method(), Q_ARG(QAction *, action)))
174       qWarning() << "NetworkModelActionController::handleExternalAction(): Could not invoke slot" << receiver() << method();
175   }
176 }
177
178 /******** Handle Actions *************************************************************************/
179
180 void NetworkModelController::actionTriggered(QAction *action) {
181   ActionType type = (ActionType)action->data().toInt();
182   if(type > 0) {
183     if(type & NetworkMask)
184       handleNetworkAction(type, action);
185     else if(type & BufferMask)
186       handleBufferAction(type, action);
187     else if(type & HideMask)
188       handleHideAction(type, action);
189     else if(type & GeneralMask)
190       handleGeneralAction(type, action);
191     else if(type & NickMask)
192       handleNickAction(type, action);
193     else if(type & ExternalMask)
194       handleExternalAction(type, action);
195     else
196       qWarning() << "NetworkModelController::actionTriggered(): Unhandled action!";
197   }
198 }
199
200 void NetworkModelController::handleNetworkAction(ActionType type, QAction *) {
201   if(type == NetworkConnectAll || type == NetworkDisconnectAll) {
202     foreach(NetworkId id, Client::networkIds()) {
203       const Network *net = Client::network(id);
204       if(type == NetworkConnectAll && net->connectionState() == Network::Disconnected)
205         net->requestConnect();
206       if(type == NetworkDisconnectAll && net->connectionState() != Network::Disconnected)
207         net->requestDisconnect();
208     }
209     return;
210   }
211
212   if(!indexList().count())
213     return;
214
215   const Network *network = Client::network(indexList().at(0).data(NetworkModel::NetworkIdRole).value<NetworkId>());
216   Q_CHECK_PTR(network);
217   if(!network)
218     return;
219
220   switch(type) {
221     case NetworkConnect:
222       network->requestConnect();
223       break;
224     case NetworkDisconnect:
225       network->requestDisconnect();
226       break;
227     default:
228       break;
229   }
230 }
231
232 void NetworkModelController::handleBufferAction(ActionType type, QAction *) {
233   if(type == BufferRemove) {
234     removeBuffers(indexList());
235   } else {
236
237     QList<BufferInfo> bufferList; // create temp list because model indexes might change
238     foreach(QModelIndex index, indexList()) {
239       BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
240       if(bufferInfo.isValid())
241         bufferList << bufferInfo;
242     }
243
244     foreach(BufferInfo bufferInfo, bufferList) {
245       switch(type) {
246         case BufferJoin:
247           Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
248           break;
249         case BufferPart:
250         {
251           QString reason = Client::identity(Client::network(bufferInfo.networkId())->identity())->partReason();
252           Client::userInput(bufferInfo, QString("/PART %1").arg(reason));
253           break;
254         }
255         case BufferSwitchTo:
256           Client::bufferModel()->switchToBuffer(bufferInfo.bufferId());
257           break;
258         default:
259           break;
260       }
261     }
262   }
263 }
264
265 void NetworkModelController::handleHideAction(ActionType type, QAction *action) {
266   Q_UNUSED(action)
267
268   int filter = 0;
269   if(NetworkModelController::action(HideJoin)->isChecked())
270     filter |= Message::Join | Message::NetsplitJoin;
271   if(NetworkModelController::action(HidePart)->isChecked())
272     filter |= Message::Part;
273   if(NetworkModelController::action(HideQuit)->isChecked())
274     filter |= Message::Quit | Message::NetsplitQuit;
275   if(NetworkModelController::action(HideNick)->isChecked())
276     filter |= Message::Nick;
277   if(NetworkModelController::action(HideMode)->isChecked())
278     filter |= Message::Mode;
279   if(NetworkModelController::action(HideDayChange)->isChecked())
280     filter |= Message::DayChange;
281   if(NetworkModelController::action(HideTopic)->isChecked())
282     filter |= Message::Topic;
283
284   switch(type) {
285   case HideJoin:
286   case HidePart:
287   case HideQuit:
288   case HideNick:
289   case HideMode:
290   case HideDayChange:
291   case HideTopic:
292     if(_messageFilter)
293       BufferSettings(_messageFilter->idString()).setMessageFilter(filter);
294     else {
295       foreach(QModelIndex index, _indexList) {
296         BufferId bufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
297         if(!bufferId.isValid())
298           continue;
299         BufferSettings(bufferId).setMessageFilter(filter);
300       }
301     }
302     return;
303   case HideApplyToAll:
304     BufferSettings().setMessageFilter(filter);
305   case HideUseDefaults:
306     if(_messageFilter)
307       BufferSettings(_messageFilter->idString()).removeFilter();
308     else {
309       foreach(QModelIndex index, _indexList) {
310         BufferId bufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
311         if(!bufferId.isValid())
312           continue;
313         BufferSettings(bufferId).removeFilter();
314       }
315     }
316     return;
317   default:
318     return;
319   };
320 }
321
322 void NetworkModelController::handleGeneralAction(ActionType type, QAction *action) {
323   Q_UNUSED(action)
324
325   if(!indexList().count())
326     return;
327   NetworkId networkId = indexList().at(0).data(NetworkModel::NetworkIdRole).value<NetworkId>();
328
329   switch(type) {
330     case JoinChannel: {
331       QString channelName = contextItem();
332       if(channelName.isEmpty()) {
333         JoinDlg dlg(indexList().first());
334         if(dlg.exec() == QDialog::Accepted) {
335           channelName = dlg.channelName();
336           networkId = dlg.networkId();
337         }
338       }
339       if(!channelName.isEmpty()) {
340         Client::instance()->userInput(BufferInfo::fakeStatusBuffer(networkId), QString("/JOIN %1").arg(channelName));
341       }
342       break;
343     }
344     case ShowChannelList:
345       if(networkId.isValid())
346         emit showChannelList(networkId);
347       break;
348     case ShowIgnoreList:
349       if(networkId.isValid())
350         emit showIgnoreList(QString());
351       break;
352     default:
353       break;
354   }
355 }
356
357 void NetworkModelController::handleNickAction(ActionType type, QAction *action) {
358   foreach(QModelIndex index, indexList()) {
359     NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
360     if(!networkId.isValid())
361       continue;
362     QString nick = nickName(index);
363     if(nick.isEmpty())
364       continue;
365     BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
366     if(!bufferInfo.isValid())
367       continue;
368
369     switch(type) {
370       case NickWhois:
371         Client::userInput(bufferInfo, QString("/WHOIS %1 %1").arg(nick));
372         break;
373       case NickCtcpVersion:
374         Client::userInput(bufferInfo, QString("/CTCP %1 VERSION").arg(nick));
375         break;
376       case NickCtcpPing:
377         Client::userInput(bufferInfo, QString("/CTCP %1 PING").arg(nick));
378         break;
379       case NickCtcpTime:
380         Client::userInput(bufferInfo, QString("/CTCP %1 TIME").arg(nick));
381         break;
382       case NickCtcpFinger:
383         Client::userInput(bufferInfo, QString("/CTCP %1 FINGER").arg(nick));
384         break;
385       case NickOp:
386         Client::userInput(bufferInfo, QString("/OP %1").arg(nick));
387         break;
388       case NickDeop:
389         Client::userInput(bufferInfo, QString("/DEOP %1").arg(nick));
390         break;
391       case NickVoice:
392         Client::userInput(bufferInfo, QString("/VOICE %1").arg(nick));
393         break;
394       case NickDevoice:
395         Client::userInput(bufferInfo, QString("/DEVOICE %1").arg(nick));
396         break;
397       case NickKick:
398         Client::userInput(bufferInfo, QString("/KICK %1").arg(nick));
399         break;
400       case NickBan:
401         Client::userInput(bufferInfo, QString("/BAN %1").arg(nick));
402         break;
403       case NickKickBan:
404         Client::userInput(bufferInfo, QString("/BAN %1").arg(nick));
405         Client::userInput(bufferInfo, QString("/KICK %1").arg(nick));
406         break;
407       case NickSwitchTo:
408       case NickQuery:
409         Client::bufferModel()->switchToOrStartQuery(networkId, nick);
410         break;
411       case NickIgnoreUser:
412       {
413         IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
414         if(!ircUser)
415           break;
416         Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::SenderIgnore,
417                                                        action->property("ignoreRule").toString(),
418                                                        false, IgnoreListManager::SoftStrictness,
419                                                        IgnoreListManager::NetworkScope,
420                                                        ircUser->network()->networkName(), true);
421         break;
422       }
423       case NickIgnoreHost:
424       {
425         IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
426         if(!ircUser)
427           break;
428         Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::SenderIgnore,
429                                                        action->property("ignoreRule").toString(),
430                                                        false, IgnoreListManager::SoftStrictness,
431                                                        IgnoreListManager::NetworkScope,
432                                                        ircUser->network()->networkName(), true);
433         break;
434       }
435       case NickIgnoreDomain:
436       {
437         IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
438         if(!ircUser)
439           break;
440         Client::ignoreListManager()->requestAddIgnoreListItem(IgnoreListManager::SenderIgnore,
441                                                        action->property("ignoreRule").toString(),
442                                                        false, IgnoreListManager::SoftStrictness,
443                                                        IgnoreListManager::NetworkScope,
444                                                        ircUser->network()->networkName(), true);
445         break;
446       }
447       case NickIgnoreCustom:
448         // forward that to mainwin since we can access the settingspage only from there
449         emit showIgnoreList(action->property("ignoreRule").toString());
450         break;
451       case NickIgnoreToggleEnabled0:
452       case NickIgnoreToggleEnabled1:
453       case NickIgnoreToggleEnabled2:
454       case NickIgnoreToggleEnabled3:
455       case NickIgnoreToggleEnabled4:
456         Client::ignoreListManager()->requestToggleIgnoreRule(action->property("ignoreRule").toString());
457         break;
458       default:
459         qWarning() << "Unhandled nick action";
460     }
461   }
462 }
463
464 /***************************************************************************************************************
465  * JoinDlg
466  ***************************************************************************************************************/
467
468 NetworkModelController::JoinDlg::JoinDlg(const QModelIndex &index, QWidget *parent) : QDialog(parent) {
469   setWindowIcon(SmallIcon("irc-join-channel"));
470   setWindowTitle(tr("Join Channel"));
471
472   QGridLayout *layout = new QGridLayout(this);
473   layout->addWidget(new QLabel(tr("Network:")), 0, 0);
474   layout->addWidget(networks = new QComboBox, 0, 1);
475   layout->addWidget(new QLabel(tr("Channel:")), 1, 0);
476   layout->addWidget(channel = new QLineEdit, 1, 1);
477   layout->addWidget(buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel), 2, 0, 1, 2);
478   setLayout(layout);
479
480   channel->setFocus();
481   buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
482   networks->setInsertPolicy(QComboBox::InsertAlphabetically);
483
484   connect(buttonBox, SIGNAL(accepted()), SLOT(accept()));
485   connect(buttonBox, SIGNAL(rejected()), SLOT(reject()));
486   connect(channel, SIGNAL(textChanged(QString)), SLOT(on_channel_textChanged(QString)));
487
488   foreach(NetworkId id, Client::networkIds()) {
489     const Network *net = Client::network(id);
490     if(net->isConnected()) {
491       networks->addItem(net->networkName(), QVariant::fromValue<NetworkId>(id));
492     }
493   }
494
495   if(index.isValid()) {
496     NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
497     if(networkId.isValid()) {
498       networks->setCurrentIndex(networks->findText(Client::network(networkId)->networkName()));
499       if(index.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer
500       && !index.data(NetworkModel::ItemActiveRole).toBool())
501           channel->setText(index.data(Qt::DisplayRole).toString());
502     }
503   }
504 }
505
506 NetworkId NetworkModelController::JoinDlg::networkId() const {
507   return networks->itemData(networks->currentIndex()).value<NetworkId>();
508 }
509
510 QString NetworkModelController::JoinDlg::channelName() const {
511   return channel->text();
512 }
513
514 void NetworkModelController::JoinDlg::on_channel_textChanged(const QString &text) {
515   buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!text.isEmpty());
516 }