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