d7e4248c35869d0b7fb497c92c367c7a8ead9448
[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     if(role ==  Qt::DisplayRole) {
144       QString title;
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;
152         default:
153           qDebug() << "invalid model index"; return QVariant();
154       }
155       return title;
156     } else return QVariant();
157   } else {
158     IrcUser *user = users[cat-1][index.row()];
159     switch(role) {
160       case Qt::DisplayRole:
161         return user->nick();
162       default:
163         return QVariant();
164     }
165   }
166 }
167
168 int NickModel::userCategory(IrcUser *user) const {
169   return categoryFromModes(ircChannel()->userModes(user));
170 }
171
172 int NickModel::categoryFromModes(const QString &modes) const {
173   int cat;
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;
181   else cat = 6;
182   return cat;
183 }
184
185 int NickModel::categoryFromIndex(const QModelIndex &index) const {
186   if(!index.isValid()) return -1;
187   return index.internalId();
188 }
189
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);
194   endInsertRows();
195 }
196
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);
200   removeUser(index);
201 }
202
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());
207   endRemoveRows();
208 }
209
210 void NickModel::renameUser(IrcUser *user) {
211   QModelIndex index = indexOfUser(user);
212   emit dataChanged(index, index);
213 }
214
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);
220   } else {
221     removeUser(oldindex);
222     addUser(user);
223   }
224 }
225
226