doubleclick on nick now selects the buffer if buffer is available
[quassel.git] / src / uisupport / nickview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 <QHeaderView>
22 #include <QDebug>
23 #include <QMenu>
24
25 #include "nickview.h"
26 #include "nickviewfilter.h"
27 #include "networkmodel.h"
28 #include "buffermodel.h"
29 #include "types.h"
30 #include "client.h"
31
32
33 NickView::NickView(QWidget *parent)
34   : QTreeView(parent),
35     _sizeHint(QTreeView::sizeHint())
36 {
37   setIndentation(10);
38   setAnimated(true);
39   header()->hide();
40   setSortingEnabled(true);
41   sortByColumn(0, Qt::AscendingOrder);
42
43   setContextMenuPolicy(Qt::CustomContextMenu);
44
45   connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
46           this, SLOT(showContextMenu(const QPoint&)));
47   connect(this, SIGNAL(activated( const QModelIndex& )),
48           this, SLOT(startQuery( const QModelIndex& )));
49 }
50
51 NickView::~NickView() {
52 }
53
54 void NickView::init() {
55   if(!model())
56     return;
57
58   for(int i = 1; i < model()->columnCount(); i++)
59     setColumnHidden(i, true);
60
61   expandAll();
62   updateSizeHint();
63 }
64
65 void NickView::setModel(QAbstractItemModel *model) {
66   QTreeView::setModel(model);
67   init();
68 }
69
70 void NickView::rowsInserted(const QModelIndex &parent, int start, int end) {
71   QTreeView::rowsInserted(parent, start, end);
72   if(model()->data(parent, NetworkModel::ItemTypeRole) == NetworkModel::UserCategoryItemType && !isExpanded(parent))
73     expand(parent);
74   updateSizeHint();
75 }
76
77 void NickView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
78   QTreeView::rowsAboutToBeRemoved(parent, start, end);
79   updateSizeHint();
80 }
81
82 void NickView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
83   QTreeView::dataChanged(topLeft, bottomRight);
84   updateSizeHint();
85 }
86
87 QString NickView::nickFromModelIndex(const QModelIndex & index) {
88   QString nick = index.sibling(index.row(), 0).data().toString();
89   return nick;
90 }
91
92 BufferInfo NickView::bufferInfoFromModelIndex(const QModelIndex & index) {
93   BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
94   return bufferInfo;
95 }
96
97 void NickView::showContextMenu(const QPoint & pos ) {
98   QModelIndex index = indexAt(pos);
99   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType) return;
100
101   QString nick = nickFromModelIndex(index);
102
103   QMenu nickContextMenu(this);
104
105   QAction *whoisAction = nickContextMenu.addAction(tr("WHOIS"));
106   QAction *versionAction = nickContextMenu.addAction(tr("VERSION"));
107   QAction *pingAction = nickContextMenu.addAction(tr("PING"));
108
109   nickContextMenu.addSeparator();
110
111   QMenu *modeMenu = nickContextMenu.addMenu(tr("Modes"));
112   QAction *opAction = modeMenu->addAction(tr("Op %1").arg(nick));
113   QAction *deOpAction = modeMenu->addAction(tr("Deop %1").arg(nick));
114   QAction *voiceAction = modeMenu->addAction(tr("Voice %1").arg(nick));
115   QAction *deVoiceAction = modeMenu->addAction(tr("Devoice %1").arg(nick));
116
117   QMenu *kickBanMenu = nickContextMenu.addMenu(tr("Kick/Ban"));
118   //TODO: add kick message from network identity (kick reason)
119   QAction *kickAction = kickBanMenu->addAction(tr("Kick %1").arg(nick));
120   QAction *kickBanAction = kickBanMenu->addAction(tr("Kickban %1").arg(nick));
121   kickBanMenu->setEnabled(false);
122   QAction *ignoreAction = nickContextMenu.addAction(tr("Ignore"));
123   ignoreAction->setEnabled(false);
124
125   nickContextMenu.addSeparator();
126
127   QAction *queryAction = nickContextMenu.addAction(tr("Query"));
128   QAction *dccChatAction = nickContextMenu.addAction(tr("DCC-Chat"));
129   dccChatAction->setEnabled(false);
130   QAction *sendFileAction = nickContextMenu.addAction(tr("Send file"));
131   sendFileAction->setEnabled(false);
132
133   QAction *action = nickContextMenu.exec(QCursor::pos());
134   BufferInfo bufferInfo = bufferInfoFromModelIndex(index);
135
136   if(action == whoisAction)         { executeCommand(bufferInfo, QString("/WHOIS %1 %1").arg(nick)); }
137   else if(action == versionAction)  { executeCommand(bufferInfo, QString("/CTCP %1 VERSION").arg(nick)); }
138   else if(action == pingAction)     { executeCommand(bufferInfo, QString("/CTCP %1 PING ").arg(nick)); }
139
140   else if(action == opAction)       { executeCommand(bufferInfo, QString("/OP %1").arg(nick)); }
141   else if(action == deOpAction)     { executeCommand(bufferInfo, QString("/DEOP %1").arg(nick)); }
142   else if(action == voiceAction)    { executeCommand(bufferInfo, QString("/VOICE %1").arg(nick)); }
143   else if(action == deVoiceAction)  { executeCommand(bufferInfo, QString("/DEVOICE %1").arg(nick)); }
144
145   else if(action == kickAction)     { executeCommand(bufferInfo, QString("/KICK %1").arg(nick)); }
146   else if(action == kickBanAction)  { executeCommand(bufferInfo, QString("/KICKBAN %1").arg(nick)); }
147   else if(action == queryAction)    { executeCommand(bufferInfo, QString("/QUERY %1").arg(nick)); }
148
149 }
150
151 void NickView::startQuery(const QModelIndex & index) {
152   QString nick = nickFromModelIndex(index);
153   bool activated = false;
154
155   if(QSortFilterProxyModel *nickviewFilter = qobject_cast<QSortFilterProxyModel *>(model())) {
156     // rootIndex() is the channel, parent() is the corresponding network
157     QModelIndex networkIndex = rootIndex().parent(); 
158     QModelIndex source_networkIndex = nickviewFilter->mapToSource(networkIndex);
159     for(int i = 0; i < Client::networkModel()->rowCount(source_networkIndex); i++) {
160       QModelIndex childIndex = source_networkIndex.child( i, 0);
161       if(nick.toLower() == childIndex.data().toString().toLower()) {
162         QModelIndex queryIndex = Client::bufferModel()->mapFromSource(childIndex);
163         Client::bufferModel()->setCurrentIndex(queryIndex);
164         activated = true;
165       }
166     }
167   }
168   if(!activated) {
169     BufferInfo bufferInfo = bufferInfoFromModelIndex(index);
170     executeCommand(bufferInfo, QString("/QUERY %1").arg(nick));
171   }
172 }
173
174 void NickView::executeCommand(const BufferInfo & bufferInfo, const QString & command) {
175   Client::instance()->userInput(bufferInfo, command);
176 }
177
178 void NickView::updateSizeHint() {
179   if(!model())
180     return;
181
182   int columnSize = 0;
183   for(int i = 0; i < model()->columnCount(); i++) {
184     if(!isColumnHidden(i))
185       columnSize += sizeHintForColumn(i);
186   }
187
188   _sizeHint = QSize(columnSize, 50);
189 }
190
191 QSize NickView::sizeHint() const {
192   return _sizeHint;
193 }