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) any later version. *
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(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 *)));
58 foreach(IrcUser *ircuser, channel->ircUsers()) {
59 // 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)
143 if(role == Qt::DisplayRole) {
145 switch(index.row()) {
146 case 0: title = tr("%n Owner(s)", "", users[index.row()].count()); break;
147 case 1: title = tr("%n Admin(s)", "", users[index.row()].count()); break;
148 case 2: title = tr("%n Operator(s)", "", users[index.row()].count()); break;
149 case 3: title = tr("%n Half-Op(s)", "", users[index.row()].count()); break;
150 case 4: title = tr("%n Voiced", "", users[index.row()].count()); break;
151 case 5: title = tr("%n User(s)", "", users[index.row()].count()); break;
153 qDebug() << "invalid model index"; return QVariant();
156 } else return QVariant();
158 IrcUser *user = users[cat-1][index.row()];
160 case Qt::DisplayRole:
168 int NickModel::userCategory(IrcUser *user) const {
169 return categoryFromModes(ircChannel()->userModes(user));
172 int NickModel::categoryFromModes(const QString &modes) const {
174 // we hardcode this even though we have PREFIX in networkinfo... but that wouldn't help with mapping modes to
175 // category strings anyway.
176 if(modes.contains('q')) cat = 1;
177 else if(modes.contains('a')) cat = 2;
178 else if(modes.contains('o')) cat = 3;
179 else if(modes.contains('h')) cat = 4;
180 else if(modes.contains('v')) cat = 5;
185 int NickModel::categoryFromIndex(const QModelIndex &index) const {
186 if(!index.isValid()) return -1;
187 return index.internalId();
190 void NickModel::addUser(IrcUser *user) {
191 int cat = userCategory(user);
192 beginInsertRows(createIndex(cat-1, 0, 0), 0, 0);
193 users[cat-1].prepend(user);
197 void NickModel::removeUser(IrcUser *user) {
198 // we don't know for sure which category this user was in, so we have to search
199 QModelIndex index = indexOfUser(user);
203 void NickModel::removeUser(const QModelIndex &index) {
204 if(!index.isValid()) return;
205 beginRemoveRows(index.parent(), index.row(), index.row());
206 users[index.internalId()-1].removeAt(index.row());
210 void NickModel::renameUser(IrcUser *user) {
211 QModelIndex index = indexOfUser(user);
212 emit dataChanged(index, index);
215 void NickModel::changeUserModes(IrcUser *user) {
216 QModelIndex oldindex = indexOfUser(user);
217 if(categoryFromIndex(oldindex) == categoryFromModes(ircChannel()->userModes(user))) {
218 // User is still in same category, no change necessary
219 emit dataChanged(oldindex, oldindex);
221 removeUser(oldindex);