a3e7692f9a6f44d049e57643fc06232fdc1b2003
[quassel.git] / src / client / nickmodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
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) any later version.                                   *
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 "nickmodel.h"
22
23 #include "ircchannel.h"
24 #include "ircuser.h"
25
26 #include <QDebug>
27
28 NickModel::NickModel(IrcChannel *channel, QObject *parent) : QAbstractItemModel(parent) {
29   // we support 6 categories: q, a, o, h, v and standard
30   users = QVector<QList<IrcUser *> >(6);
31
32   if(channel) setIrcChannel(channel);
33   else _ircChannel = 0;
34 }
35
36 NickModel::~NickModel() {
37
38
39 }
40
41 IrcChannel *NickModel::ircChannel() const {
42   return _ircChannel;
43 }
44
45 void NickModel::setIrcChannel(IrcChannel *channel) {
46   if(_ircChannel) {
47     disconnect(_ircChannel, 0, this, 0);
48   }
49   foreach(QList<IrcUser *> l, users) l.clear();
50   _ircChannel = channel;
51   reset();
52   if(_ircChannel) {
53     connect(channel, SIGNAL(ircUserJoined(IrcUser *)), this, SLOT(addUser(IrcUser *)));
54     connect(channel, SIGNAL(ircUserParted(IrcUser *)), this, SLOT(removeUser(IrcUser *)));
55     connect(channel, SIGNAL(ircUserNickSet(IrcUser *, QString)), this, SLOT(renameUser(IrcUser *)));
56     connect(channel, SIGNAL(ircUserModesSet(IrcUser *, QString)), this, SLOT(changeUserModes(IrcUser *)));
57
58     foreach(IrcUser *ircuser, channel->ircUsers()) {
59     // TODO: make this efficient by sorting after everything is appended instead!
60       addUser(ircuser);
61     }
62   }
63
64 }
65
66 QVariant NickModel::headerData(int section, Qt::Orientation orientation, int role) const {
67   if(section == 0 && role == Qt::DisplayRole) {
68     if(ircChannel()) return ircChannel()->name();
69     else return "No channel";
70   }
71   return QAbstractItemModel::headerData(section, orientation, role);
72 }
73
74 QModelIndex NickModel::index(int row, int column, const QModelIndex &parent) const {
75   if(!parent.isValid()) { // Top-level item, i.e. a nick category
76     if(column > 0) return QModelIndex();
77     //int r = 0;
78     //for(int i = 0; i < row; i++) { // we need to skip empty categories
79     if(row > users.count()) {
80       qDebug() << "invalid model index!";
81       return QModelIndex();
82     }
83     return createIndex(row, column, 0);
84   }
85   // Second-level item, i.e. a nick. internalId() contains the parent category (starting at 1).
86   int cat = parent.row() + 1;
87   if(row > users[cat-1].count()) {
88     qDebug() << "invalid model index!";
89     return QModelIndex();
90   }
91   return createIndex(row, column, cat);
92 }
93
94 QModelIndex NickModel::indexOfUser(IrcUser *user) const {
95   int idx = -1; int cat;
96   for(cat = users.count()-1; cat >= 0; cat--) {
97     // we count backwards, since most users will usually be in the last category
98     idx = users[cat].indexOf(user);
99     if(idx >=0) break;
100   }
101   if(idx < 0) {
102     qWarning("NickModel: Index of unknown user requested!");
103     return QModelIndex();
104   }
105   return createIndex(idx, 0, cat+1);
106 }
107
108 QModelIndex NickModel::parent(const QModelIndex &index) const {
109   if(!index.isValid()) return QModelIndex();
110   int cat = index.internalId();
111   if(cat) return createIndex(cat-1, 0, 0);
112   else return QModelIndex();
113 }
114
115 int NickModel::rowCount(const QModelIndex &parent) const {
116   if(!parent.isValid()) {
117     if(!ircChannel()) return 1;  // informative text
118     return users.count();
119   }
120   int cat = parent.internalId();
121   if(!cat) {  // top-level item (category)
122     return users[parent.row()].count();
123   }
124   return 0;  // second-level items don't have children
125 }
126
127 int NickModel::columnCount(const QModelIndex &) const {
128   //if(!ircChannel()) return 0;
129   return 1;  // all our items have exactly one column
130 }
131
132 QVariant NickModel::data(const QModelIndex &index, int role) const {
133   if(!index.isValid()) return QVariant();
134   if(!ircChannel()) {
135     // we show one item with informative text
136     switch(role) {
137       case Qt::DisplayRole: return tr("Not in channel");
138       default: return QVariant();
139     }
140   }
141   int cat = index.internalId();
142   if(!cat) { // top-level item (category)
143     switch(role) {
144       case Qt::DisplayRole: {
145         QString title;
146         switch(index.row()) {
147           case 0: title = tr("%n Owner(s)", "", users[index.row()].count()); break;
148           case 1: title = tr("%n Admin(s)", "", users[index.row()].count()); break;
149           case 2: title = tr("%n Operator(s)", "", users[index.row()].count()); break;
150           case 3: title = tr("%n Half-Op(s)", "", users[index.row()].count()); break;
151           case 4: title = tr("%n Voiced", "", users[index.row()].count()); break;
152           case 5: title = tr("%n User(s)", "", users[index.row()].count()); break;
153           default: qDebug() << "invalid model index"; return QVariant();
154         }
155         return title;
156       }
157       case SortKeyRole: return index.row();
158       default: return QVariant();
159     }
160   } else {
161     IrcUser *user = users[cat-1][index.row()];
162     switch(role) {
163       case Qt::DisplayRole:
164         return user->nick();
165       case Qt::ToolTipRole:
166         return user->hostmask();
167       case SortKeyRole:
168         return user->nick();
169       default:
170         return QVariant();
171     }
172   }
173 }
174
175 int NickModel::userCategory(IrcUser *user) const {
176   return categoryFromModes(ircChannel()->userModes(user));
177 }
178
179 int NickModel::categoryFromModes(const QString &modes) const {
180   int cat;
181   // we hardcode this even though we have PREFIX in networkinfo... but that wouldn't help with mapping modes to
182   // category strings anyway.
183   if(modes.contains('q')) cat = 1;
184   else if(modes.contains('a')) cat = 2;
185   else if(modes.contains('o')) cat = 3;
186   else if(modes.contains('h')) cat = 4;
187   else if(modes.contains('v')) cat = 5;
188   else cat = 6;
189   return cat;
190 }
191
192 int NickModel::categoryFromIndex(const QModelIndex &index) const {
193   if(!index.isValid()) return -1;
194   return index.internalId();
195 }
196
197 void NickModel::addUser(IrcUser *user) {
198   int cat = userCategory(user);
199   beginInsertRows(createIndex(cat-1, 0, 0), 0, 0);
200   users[cat-1].prepend(user);
201   endInsertRows();
202 }
203
204 void NickModel::removeUser(IrcUser *user) {
205   // we don't know for sure which category this user was in, so we have to search
206   QModelIndex index = indexOfUser(user);
207   removeUser(index);
208 }
209
210 void NickModel::removeUser(const QModelIndex &index) {
211   if(!index.isValid()) return;
212   beginRemoveRows(index.parent(), index.row(), index.row());
213   users[index.internalId()-1].removeAt(index.row());
214   endRemoveRows();
215 }
216
217 void NickModel::renameUser(IrcUser *user) {
218   QModelIndex index = indexOfUser(user);
219   emit dataChanged(index, index);
220 }
221
222 void NickModel::changeUserModes(IrcUser *user) {
223   QModelIndex oldindex = indexOfUser(user);
224   if(categoryFromIndex(oldindex) == categoryFromModes(ircChannel()->userModes(user))) {
225     // User is still in same category, no change necessary
226     emit dataChanged(oldindex, oldindex);
227   } else {
228     removeUser(oldindex);
229     addUser(user);
230   }
231 }
232
233 /******************************************************************************************
234  * FilteredNickModel
235  ******************************************************************************************/
236
237 FilteredNickModel::FilteredNickModel(QObject *parent) : QSortFilterProxyModel(parent) {
238   setDynamicSortFilter(true);
239   setSortCaseSensitivity(Qt::CaseInsensitive);
240   setSortRole(NickModel::SortKeyRole);
241
242 }
243
244 // Hide empty categories
245 bool FilteredNickModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
246   if(!source_parent.isValid()) {
247     QModelIndex index = sourceModel()->index(source_row, 0);
248     return sourceModel()->rowCount(index);
249   }
250   return true;
251 }
252