added colorsettingspage
[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 "types.h"
29 #include "client.h"
30
31
32 NickView::NickView(QWidget *parent)
33   : QTreeView(parent)
34 {
35   setIndentation(10);
36   setAnimated(true);
37   header()->hide();
38   setSortingEnabled(true);
39   sortByColumn(0, Qt::AscendingOrder);
40
41   setContextMenuPolicy(Qt::CustomContextMenu);
42
43   connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
44           this, SLOT(showContextMenu(const QPoint&)));
45   connect(this, SIGNAL(activated( const QModelIndex& )),
46           this, SLOT(startQuery( const QModelIndex& )));
47 }
48
49 NickView::~NickView() {
50 }
51
52 void NickView::init() {
53   if(!model())
54     return;
55
56   for(int i = 1; i < model()->columnCount(); i++)
57     setColumnHidden(i, true);
58
59   expandAll();
60 }
61
62 void NickView::setModel(QAbstractItemModel *model) {
63   QTreeView::setModel(model);
64   init();
65 }
66
67 void NickView::rowsInserted(const QModelIndex &index, int start, int end) {
68   QTreeView::rowsInserted(index, start, end);
69   expandAll();  // FIXME We need to do this more intelligently. Maybe a pimped TreeView?
70 }
71
72 QString NickView::nickFromModelIndex(const QModelIndex & index) {
73   QString nick = index.sibling(index.row(), 0).data().toString();
74   return nick;
75 }
76
77 BufferInfo NickView::bufferInfoFromModelIndex(const QModelIndex & index) {
78   BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
79   return bufferInfo;
80 }
81
82 void NickView::showContextMenu(const QPoint & pos ) {
83   QModelIndex index = indexAt(pos);
84   if(index.data(NetworkModel::ItemTypeRole) != NetworkModel::IrcUserItemType) return;
85
86   QString nick = nickFromModelIndex(index);
87
88   QMenu nickContextMenu(this);
89
90   QAction *whoisAction = nickContextMenu.addAction(tr("WHOIS"));
91   QAction *versionAction = nickContextMenu.addAction(tr("VERSION"));
92   QAction *pingAction = nickContextMenu.addAction(tr("PING"));
93
94   nickContextMenu.addSeparator();
95
96   QMenu *modeMenu = nickContextMenu.addMenu(tr("Modes"));
97   QAction *opAction = modeMenu->addAction(tr("Op %1").arg(nick));
98   QAction *deOpAction = modeMenu->addAction(tr("Deop %1").arg(nick));
99   QAction *voiceAction = modeMenu->addAction(tr("Voice %1").arg(nick));
100   QAction *deVoiceAction = modeMenu->addAction(tr("Devoice %1").arg(nick));
101
102   QMenu *kickBanMenu = nickContextMenu.addMenu(tr("Kick/Ban"));
103   //TODO: add kick message from network identity (kick reason)
104   QAction *kickAction = kickBanMenu->addAction(tr("Kick %1").arg(nick));
105   QAction *kickBanAction = kickBanMenu->addAction(tr("Kickban %1").arg(nick));
106   kickBanMenu->setEnabled(false);
107   QAction *ignoreAction = nickContextMenu.addAction(tr("Ignore"));
108   ignoreAction->setEnabled(false);
109
110   nickContextMenu.addSeparator();
111
112   QAction *queryAction = nickContextMenu.addAction(tr("Query"));
113   QAction *dccChatAction = nickContextMenu.addAction(tr("DCC-Chat"));
114   dccChatAction->setEnabled(false);
115   QAction *sendFileAction = nickContextMenu.addAction(tr("Send file"));
116   sendFileAction->setEnabled(false);
117
118   QAction *action = nickContextMenu.exec(QCursor::pos());
119   BufferInfo bufferInfo = bufferInfoFromModelIndex(index);
120
121   if(action == whoisAction)         { executeCommand(bufferInfo, QString("/WHOIS %1 %1").arg(nick)); }
122   else if(action == versionAction)  { executeCommand(bufferInfo, QString("/CTCP %1 VERSION").arg(nick)); }
123   else if(action == pingAction)     { executeCommand(bufferInfo, QString("/CTCP %1 PING ").arg(nick)); }
124
125   else if(action == opAction)       { executeCommand(bufferInfo, QString("/OP %1").arg(nick)); }
126   else if(action == deOpAction)     { executeCommand(bufferInfo, QString("/DEOP %1").arg(nick)); }
127   else if(action == voiceAction)    { executeCommand(bufferInfo, QString("/VOICE %1").arg(nick)); }
128   else if(action == deVoiceAction)  { executeCommand(bufferInfo, QString("/DEVOICE %1").arg(nick)); }
129
130   else if(action == kickAction)     { executeCommand(bufferInfo, QString("/KICK %1").arg(nick)); }
131   else if(action == kickBanAction)  { executeCommand(bufferInfo, QString("/KICKBAN %1").arg(nick)); }
132   else if(action == queryAction)    { executeCommand(bufferInfo, QString("/QUERY %1").arg(nick)); }
133
134 }
135
136 void NickView::startQuery(const QModelIndex & index) {
137   QString nick = nickFromModelIndex(index);
138   BufferInfo bufferInfo = bufferInfoFromModelIndex(index);
139   executeCommand(bufferInfo, QString("/QUERY %1").arg(nick));
140 }
141
142 void NickView::executeCommand(const BufferInfo & bufferInfo, const QString & command) {
143   Client::instance()->userInput(bufferInfo, command);
144 }