1 /***************************************************************************
2 * Copyright (C) 2005-08 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 <QHeaderView>
27 #include "nickviewfilter.h"
28 #include "networkmodel.h"
29 #include "buffermodel.h"
33 class ExpandAllEvent : public QEvent {
35 ExpandAllEvent() : QEvent(QEvent::User) {}
38 NickView::NickView(QWidget *parent)
44 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45 setSortingEnabled(true);
46 sortByColumn(0, Qt::AscendingOrder);
48 setContextMenuPolicy(Qt::CustomContextMenu);
50 connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
51 this, SLOT(showContextMenu(const QPoint&)));
52 connect(this, SIGNAL(activated( const QModelIndex& )),
53 this, SLOT(startQuery( const QModelIndex& )));
56 void NickView::init() {
60 for(int i = 1; i < model()->columnCount(); i++)
61 setColumnHidden(i, true);
64 void NickView::setModel(QAbstractItemModel *model) {
65 QTreeView::setModel(model);
69 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
70 QTreeView::rowsInserted(parent, start, end);
71 if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent)) {
72 QCoreApplication::postEvent(this, new ExpandAllEvent);
76 void NickView::setRootIndex(const QModelIndex &index) {
77 QAbstractItemView::setRootIndex(index);
79 QCoreApplication::postEvent(this, new ExpandAllEvent);
82 QString NickView::nickFromModelIndex(const QModelIndex & index) {
83 QString nick = index.sibling(index.row(), 0).data().toString();
87 BufferInfo NickView::bufferInfoFromModelIndex(const QModelIndex & index) {
88 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
92 void NickView::showContextMenu(const QPoint & pos ) {
93 QModelIndex index = indexAt(pos);
94 if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType) return;
96 QString nick = nickFromModelIndex(index);
98 QMenu nickContextMenu(this);
100 QAction *whoisAction = nickContextMenu.addAction(tr("WHOIS"));
101 QAction *versionAction = nickContextMenu.addAction(tr("VERSION"));
102 QAction *pingAction = nickContextMenu.addAction(tr("PING"));
104 nickContextMenu.addSeparator();
106 QMenu *modeMenu = nickContextMenu.addMenu(tr("Modes"));
107 QAction *opAction = modeMenu->addAction(tr("Op %1").arg(nick));
108 QAction *deOpAction = modeMenu->addAction(tr("Deop %1").arg(nick));
109 QAction *voiceAction = modeMenu->addAction(tr("Voice %1").arg(nick));
110 QAction *deVoiceAction = modeMenu->addAction(tr("Devoice %1").arg(nick));
112 QMenu *kickBanMenu = nickContextMenu.addMenu(tr("Kick/Ban"));
113 QAction *kickAction = kickBanMenu->addAction(tr("Kick %1").arg(nick));
114 QAction *banAction = kickBanMenu->addAction(tr("Ban %1").arg(nick));
115 QAction *kickBanAction = kickBanMenu->addAction(tr("Kickban %1").arg(nick));
116 QAction *ignoreAction = nickContextMenu.addAction(tr("Ignore"));
117 ignoreAction->setEnabled(false);
119 nickContextMenu.addSeparator();
121 QAction *queryAction = nickContextMenu.addAction(tr("Query"));
122 QAction *dccChatAction = nickContextMenu.addAction(tr("DCC-Chat"));
123 dccChatAction->setEnabled(false);
124 QAction *sendFileAction = nickContextMenu.addAction(tr("Send file"));
125 sendFileAction->setEnabled(false);
127 QAction *action = nickContextMenu.exec(QCursor::pos());
128 BufferInfo bufferInfo = bufferInfoFromModelIndex(index);
130 if(action == whoisAction) { executeCommand(bufferInfo, QString("/WHOIS %1 %1").arg(nick)); }
131 else if(action == versionAction) { executeCommand(bufferInfo, QString("/CTCP %1 VERSION").arg(nick)); }
132 else if(action == pingAction) { executeCommand(bufferInfo, QString("/CTCP %1 PING ").arg(nick)); }
134 else if(action == opAction) { executeCommand(bufferInfo, QString("/OP %1").arg(nick)); }
135 else if(action == deOpAction) { executeCommand(bufferInfo, QString("/DEOP %1").arg(nick)); }
136 else if(action == voiceAction) { executeCommand(bufferInfo, QString("/VOICE %1").arg(nick)); }
137 else if(action == deVoiceAction) { executeCommand(bufferInfo, QString("/DEVOICE %1").arg(nick)); }
139 else if(action == kickAction) { executeCommand(bufferInfo, QString("/KICK %1").arg(nick)); }
140 else if(action == banAction) { executeCommand(bufferInfo, QString("/BAN %1").arg(nick)); }
141 else if(action == kickBanAction) { executeCommand(bufferInfo, QString("/KICK %1").arg(nick));
142 executeCommand(bufferInfo, QString("/BAN %1").arg(nick)); }
143 else if(action == queryAction) { startQuery(index); }
147 void NickView::startQuery(const QModelIndex &index) {
148 if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType)
151 QString nick = nickFromModelIndex(index);
152 bool activated = false;
154 if(QSortFilterProxyModel *nickviewFilter = qobject_cast<QSortFilterProxyModel *>(model())) {
155 // rootIndex() is the channel, parent() is the corresponding network
156 QModelIndex networkIndex = rootIndex().parent();
157 QModelIndex source_networkIndex = nickviewFilter->mapToSource(networkIndex);
158 for(int i = 0; i < Client::networkModel()->rowCount(source_networkIndex); i++) {
159 QModelIndex childIndex = source_networkIndex.child( i, 0);
160 if(nick.toLower() == childIndex.data().toString().toLower()) {
161 QModelIndex queryIndex = Client::bufferModel()->mapFromSource(childIndex);
162 Client::bufferModel()->setCurrentIndex(queryIndex);
168 BufferInfo bufferInfo = bufferInfoFromModelIndex(index);
169 executeCommand(bufferInfo, QString("/QUERY %1").arg(nick));
173 void NickView::executeCommand(const BufferInfo & bufferInfo, const QString & command) {
174 Client::instance()->userInput(bufferInfo, command);
177 void NickView::customEvent(QEvent *event) {
178 // THIS IS A REPLACEMENT FOR expandAll()
179 /* WARNING: do not call expandAll()!
180 * it fucks up big time in combination with sorting and changing the rootIndex
181 * the following sequence of commands leads to unexpected behavior when inserting new items
182 * setSortingEnabled(true);
187 if(event->type() != QEvent::User)
190 QModelIndex topLevelIdx;
191 for(int i = 0; i < model()->rowCount(rootIndex()); i++) {
192 topLevelIdx = model()->index(i, 0, rootIndex());
193 if(isExpanded(topLevelIdx))
197 if(i < model()->rowCount(rootIndex()) - 1)
198 QCoreApplication::postEvent(this, new ExpandAllEvent);