1 /***************************************************************************
2 * Copyright (C) 2005-09 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <QInputDialog>
22 #include <QMessageBox>
24 #include "networkmodelcontroller.h"
26 #include "buffermodel.h"
27 #include "buffersettings.h"
28 #include "iconloader.h"
29 #include "clientidentity.h"
33 NetworkModelController::NetworkModelController(QObject *parent)
35 _actionCollection(new ActionCollection(this)),
40 connect(_actionCollection, SIGNAL(actionTriggered(QAction *)), SLOT(actionTriggered(QAction *)));
44 NetworkModelController::~NetworkModelController() {
48 void NetworkModelController::registerAction(ActionType type, const QString &text, bool checkable) {
49 registerAction(type, QPixmap(), text, checkable);
52 void NetworkModelController::registerAction(ActionType type, const QPixmap &icon, const QString &text, bool checkable) {
55 act = new Action(text, this);
57 act = new Action(icon, text, this);
59 act->setCheckable(checkable);
62 _actionCollection->addAction(QString::number(type, 16), act);
63 _actionByType[type] = act;
66 /******** Helper Functions ***********************************************************************/
68 void NetworkModelController::setIndexList(const QModelIndex &index) {
69 _indexList = QList<QModelIndex>() << index;
72 void NetworkModelController::setIndexList(const QList<QModelIndex> &list) {
76 void NetworkModelController::setMessageFilter(MessageFilter *filter) {
77 _messageFilter = filter;
80 void NetworkModelController::setContextItem(const QString &contextItem) {
81 _contextItem = contextItem;
84 void NetworkModelController::setSlot(QObject *receiver, const char *method) {
89 bool NetworkModelController::checkRequirements(const QModelIndex &index, ItemActiveStates requiredActiveState) {
93 ItemActiveStates isActive = index.data(NetworkModel::ItemActiveRole).toBool()
97 if(!(isActive & requiredActiveState))
103 QString NetworkModelController::nickName(const QModelIndex &index) const {
104 IrcUser *ircUser = qobject_cast<IrcUser *>(index.data(NetworkModel::IrcUserRole).value<QObject *>());
106 return ircUser->nick();
108 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
109 if(!bufferInfo.isValid())
111 if(!bufferInfo.type() == BufferInfo::QueryBuffer)
114 return bufferInfo.bufferName(); // FIXME this might break with merged queries maybe
117 BufferId NetworkModelController::findQueryBuffer(const QModelIndex &index, const QString &predefinedNick) const {
118 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
119 if(!networkId.isValid())
122 QString nick = predefinedNick.isEmpty() ? nickName(index) : predefinedNick;
126 return findQueryBuffer(networkId, nick);
129 BufferId NetworkModelController::findQueryBuffer(NetworkId networkId, const QString &nick) const {
130 return Client::networkModel()->bufferId(networkId, nick);
133 void NetworkModelController::removeBuffers(const QModelIndexList &indexList) {
134 QList<BufferInfo> inactive;
135 foreach(QModelIndex index, indexList) {
136 BufferInfo info = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
138 if(info.type() == BufferInfo::QueryBuffer
139 || (info.type() == BufferInfo::ChannelBuffer && !index.data(NetworkModel::ItemActiveRole).toBool()))
144 if(inactive.count()) {
145 msg = tr("Do you want to delete the following buffer(s) permanently?", 0, inactive.count());
147 foreach(BufferInfo info, inactive)
148 msg += QString("<li>%1</li>").arg(info.bufferName());
150 msg += tr("<b>Note:</b> This will delete all related data, including all backlog data, from the core's database and cannot be undone.");
151 if(inactive.count() != indexList.count())
152 msg += tr("<br>Active channel buffers cannot be deleted, please part the channel first.");
154 if(QMessageBox::question(0, tr("Remove buffers permanently?"), msg, QMessageBox::Yes|QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
155 foreach(BufferInfo info, inactive)
156 Client::removeBuffer(info.bufferId());
161 void NetworkModelController::handleExternalAction(ActionType type, QAction *action) {
163 if(receiver() && method()) {
164 if(!QMetaObject::invokeMethod(receiver(), method(), Q_ARG(QAction *, action)))
165 qWarning() << "NetworkModelActionController::handleExternalAction(): Could not invoke slot" << receiver() << method();
169 /******** Handle Actions *************************************************************************/
171 void NetworkModelController::actionTriggered(QAction *action) {
172 ActionType type = (ActionType)action->data().toInt();
174 if(type & NetworkMask)
175 handleNetworkAction(type, action);
176 else if(type & BufferMask)
177 handleBufferAction(type, action);
178 else if(type & HideMask)
179 handleHideAction(type, action);
180 else if(type & GeneralMask)
181 handleGeneralAction(type, action);
182 else if(type & NickMask)
183 handleNickAction(type, action);
184 else if(type & ExternalMask)
185 handleExternalAction(type, action);
187 qWarning() << "NetworkModelController::actionTriggered(): Unhandled action!";
191 void NetworkModelController::handleNetworkAction(ActionType type, QAction *) {
192 if(!indexList().count())
194 const Network *network = Client::network(indexList().at(0).data(NetworkModel::NetworkIdRole).value<NetworkId>());
195 Q_CHECK_PTR(network);
201 network->requestConnect();
203 case NetworkDisconnect:
204 network->requestDisconnect();
211 void NetworkModelController::handleBufferAction(ActionType type, QAction *) {
212 if(type == BufferRemove) {
213 removeBuffers(indexList());
216 foreach(QModelIndex index, indexList()) {
217 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
218 if(!bufferInfo.isValid())
223 Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
227 QString reason = Client::identity(Client::network(bufferInfo.networkId())->identity())->partReason();
228 Client::userInput(bufferInfo, QString("/PART %1").arg(reason));
232 Client::bufferModel()->switchToBuffer(bufferInfo.bufferId());
241 void NetworkModelController::handleHideAction(ActionType type, QAction *action) {
245 if(NetworkModelController::action(HideJoin)->isChecked())
246 filter |= Message::Join;
247 if(NetworkModelController::action(HidePart)->isChecked())
248 filter |= Message::Part;
249 if(NetworkModelController::action(HideQuit)->isChecked())
250 filter |= Message::Quit;
251 if(NetworkModelController::action(HideNick)->isChecked())
252 filter |= Message::Nick;
253 if(NetworkModelController::action(HideMode)->isChecked())
254 filter |= Message::Mode;
255 if(NetworkModelController::action(HideDayChange)->isChecked())
256 filter |= Message::DayChange;
266 BufferSettings(_messageFilter->idString()).setMessageFilter(filter);
268 foreach(QModelIndex index, _indexList) {
269 BufferId bufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
270 if(!bufferId.isValid())
272 BufferSettings(bufferId).setMessageFilter(filter);
277 BufferSettings().setMessageFilter(filter);
278 case HideUseDefaults:
280 BufferSettings(_messageFilter->idString()).removeFilter();
282 foreach(QModelIndex index, _indexList) {
283 BufferId bufferId = index.data(NetworkModel::BufferIdRole).value<BufferId>();
284 if(!bufferId.isValid())
286 BufferSettings(bufferId).removeFilter();
295 void NetworkModelController::handleGeneralAction(ActionType type, QAction *action) {
298 if(!indexList().count())
300 NetworkId networkId = indexList().at(0).data(NetworkModel::NetworkIdRole).value<NetworkId>();
301 if(!networkId.isValid())
306 QString channelName = contextItem();
307 if(channelName.isEmpty()) {
309 channelName = QInputDialog::getText(0, tr("Join Channel"), tr("Input channel name:"), QLineEdit::Normal, QString(), &ok);
313 if(!channelName.isEmpty()) {
314 Client::instance()->userInput(BufferInfo::fakeStatusBuffer(networkId), QString("/JOIN %1").arg(channelName));
318 case ShowChannelList:
319 emit showChannelList(networkId);
322 emit showIgnoreList(networkId);
329 void NetworkModelController::handleNickAction(ActionType type, QAction *) {
330 foreach(QModelIndex index, indexList()) {
331 NetworkId networkId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
332 if(!networkId.isValid())
334 QString nick = nickName(index);
337 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
338 if(!bufferInfo.isValid())
343 Client::userInput(bufferInfo, QString("/WHOIS %1 %1").arg(nick));
345 case NickCtcpVersion:
346 Client::userInput(bufferInfo, QString("/CTCP %1 VERSION").arg(nick));
349 Client::userInput(bufferInfo, QString("/CTCP %1 PING").arg(nick));
352 Client::userInput(bufferInfo, QString("/CTCP %1 TIME").arg(nick));
355 Client::userInput(bufferInfo, QString("/CTCP %1 FINGER").arg(nick));
358 Client::userInput(bufferInfo, QString("/OP %1").arg(nick));
361 Client::userInput(bufferInfo, QString("/DEOP %1").arg(nick));
364 Client::userInput(bufferInfo, QString("/VOICE %1").arg(nick));
367 Client::userInput(bufferInfo, QString("/DEVOICE %1").arg(nick));
370 Client::userInput(bufferInfo, QString("/KICK %1").arg(nick));
373 Client::userInput(bufferInfo, QString("/BAN %1").arg(nick));
376 Client::userInput(bufferInfo, QString("/BAN %1").arg(nick));
377 Client::userInput(bufferInfo, QString("/KICK %1").arg(nick));
380 Client::bufferModel()->switchToBuffer(findQueryBuffer(networkId, nick));
383 Client::userInput(bufferInfo, QString("/QUERY %1").arg(nick));
386 qWarning() << "Unhandled nick action";