1 /***************************************************************************
2 * Copyright (C) 2005-07 by the Quassel IRC Team *
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 "nickmodel.h"
23 #include "ircchannel.h"
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);
32 if(channel) setIrcChannel(channel);
36 NickModel::~NickModel() {
41 IrcChannel *NickModel::ircChannel() const {
45 void NickModel::setIrcChannel(IrcChannel *channel) {
47 disconnect(_ircChannel, 0, this, 0);
49 foreach(QList<IrcUser *> l, users) l.clear();
50 _ircChannel = channel;
53 connect(channel, SIGNAL(destroyed()), this, SLOT(setIrcChannel()));
54 connect(channel, SIGNAL(ircUserJoined(IrcUser *)), this, SLOT(addUser(IrcUser *)));
55 connect(channel, SIGNAL(ircUserParted(IrcUser *)), this, SLOT(removeUser(IrcUser *)));
56 connect(channel, SIGNAL(ircUserNickSet(IrcUser *, QString)), this, SLOT(renameUser(IrcUser *)));
57 connect(channel, SIGNAL(ircUserModesSet(IrcUser *, QString)), this, SLOT(changeUserModes(IrcUser *)));
59 foreach(IrcUser *ircuser, channel->ircUsers()) {
60 // TODO: make this efficient by sorting after everything is appended instead!
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";
71 return QAbstractItemModel::headerData(section, orientation, role);
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();
78 //for(int i = 0; i < row; i++) { // we need to skip empty categories
79 if(row > users.count()) {
80 qDebug() << "invalid model index!";
83 return createIndex(row, column, 0);
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!";
91 return createIndex(row, column, cat);
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);
102 qWarning("NickModel: Index of unknown user requested!");
103 return QModelIndex();
105 return createIndex(idx, 0, cat+1);
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();
115 int NickModel::rowCount(const QModelIndex &parent) const {
116 if(!parent.isValid()) {
117 if(!ircChannel()) return 1; // informative text
118 return users.count();
120 int cat = parent.internalId();
121 if(!cat) { // top-level item (category)
122 return users[parent.row()].count();
124 return 0; // second-level items don't have children
127 int NickModel::columnCount(const QModelIndex &) const {
128 //if(!ircChannel()) return 0;
129 return 1; // all our items have exactly one column
132 QVariant NickModel::data(const QModelIndex &index, int role) const {
133 if(!index.isValid()) return QVariant();
135 // we show one item with informative text
137 case Qt::DisplayRole: return tr("Not in channel");
138 default: return QVariant();
141 int cat = index.internalId();
142 if(!cat) { // top-level item (category)
144 case Qt::DisplayRole: {
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();
157 case SortKeyRole: return index.row();
158 default: return QVariant();
161 IrcUser *user = users[cat-1][index.row()];
163 case Qt::DisplayRole:
165 case Qt::ToolTipRole:
166 return user->hostmask();
175 int NickModel::userCategory(IrcUser *user) const {
176 return categoryFromModes(ircChannel()->userModes(user));
179 int NickModel::categoryFromModes(const QString &modes) const {
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;
192 int NickModel::categoryFromIndex(const QModelIndex &index) const {
193 if(!index.isValid()) return -1;
194 return index.internalId();
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);
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);
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());
217 void NickModel::renameUser(IrcUser *user) { //qDebug() << "renaming" << user->nick();
218 QModelIndex index = indexOfUser(user);
219 emit dataChanged(index, index);
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);
228 removeUser(oldindex);
233 /******************************************************************************************
235 ******************************************************************************************/
237 FilteredNickModel::FilteredNickModel(QObject *parent) : QSortFilterProxyModel(parent) {
238 setDynamicSortFilter(true);
239 setSortCaseSensitivity(Qt::CaseInsensitive);
240 setSortRole(NickModel::SortKeyRole);
244 FilteredNickModel::~FilteredNickModel() {
248 void FilteredNickModel::setSourceModel(QAbstractItemModel *model) {
249 QSortFilterProxyModel::setSourceModel(model);
250 connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(sourceRowsInserted(const QModelIndex &, int, int)));
251 connect(model, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SLOT(sourceRowsRemoved(const QModelIndex &, int, int)));
254 // Hide empty categories
255 bool FilteredNickModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
256 if(!source_parent.isValid()) {
257 QModelIndex index = sourceModel()->index(source_row, 0);
258 return sourceModel()->rowCount(index);
263 void FilteredNickModel::sourceRowsInserted(const QModelIndex &index, int start, int end) {
264 if(!index.isValid()) return;
265 if(sourceModel()->rowCount(index) <= end - start + 1) {
266 // category no longer empty
271 void FilteredNickModel::sourceRowsRemoved(const QModelIndex &index, int, int) {
272 if(!index.isValid()) return;
273 if(sourceModel()->rowCount(index) == 0) {
274 // category is now empty!