1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
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 "ircchannel.h"
24 //#include "nicktreemodel.h"
25 #include "signalproxy.h"
29 #include <QMapIterator>
30 #include <QHashIterator>
36 IrcChannel::IrcChannel(const QString &channelname, Network *network)
37 : SyncableObject(network),
43 setObjectName(QString::number(network->networkId().toInt()) + "/" + channelname);
46 IrcChannel::~IrcChannel() {
50 // ====================
52 // ====================
53 bool IrcChannel::isKnownUser(IrcUser *ircuser) const {
55 qWarning() << "Channel" << name() << "received IrcUser Nullpointer!";
59 if(!_userModes.contains(ircuser)) {
60 qWarning() << "Channel" << name() << "received data for unknown User" << ircuser->nick();
67 bool IrcChannel::isValidChannelUserMode(const QString &mode) const {
70 qWarning() << "Channel" << name() << "received Channel User Mode which is longer then 1 Char:" << mode;
76 QString IrcChannel::name() const {
80 QString IrcChannel::topic() const {
84 QList<IrcUser *> IrcChannel::ircUsers() const {
85 return _userModes.keys();
88 QString IrcChannel::userModes(IrcUser *ircuser) const {
89 if(_userModes.contains(ircuser))
90 return _userModes[ircuser];
95 QString IrcChannel::userModes(const QString &nick) const {
96 return userModes(network->ircUser(nick));
99 QTextCodec *IrcChannel::codecForEncoding() const {
100 return _codecForEncoding;
103 void IrcChannel::setCodecForEncoding(const QString &name) {
104 setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
107 void IrcChannel::setCodecForEncoding(QTextCodec *codec) {
108 _codecForEncoding = codec;
111 QTextCodec *IrcChannel::codecForDecoding() const {
112 return _codecForDecoding;
115 void IrcChannel::setCodecForDecoding(const QString &name) {
116 setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
119 void IrcChannel::setCodecForDecoding(QTextCodec *codec) {
120 _codecForDecoding = codec;
123 QString IrcChannel::decodeString(const QByteArray &text) const {
124 if(!codecForDecoding()) return network->decodeString(text);
125 return ::decodeString(text, _codecForDecoding);
128 QByteArray IrcChannel::encodeString(const QString string) const {
129 if(codecForEncoding()) {
130 return _codecForEncoding->fromUnicode(string);
132 return network->encodeString(string);
135 // ====================
137 // ====================
138 void IrcChannel::setTopic(const QString &topic) {
140 emit topicSet(topic);
143 void IrcChannel::join(IrcUser *ircuser) {
144 if(!_userModes.contains(ircuser) && ircuser) {
145 _userModes[ircuser] = QString();
146 ircuser->joinChannel(name());
147 //qDebug() << "JOIN" << name() << ircuser->nick() << ircUsers().count();
148 connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickSet(QString)));
149 connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
150 // if you wonder why there is no counterpart to ircUserJoined:
151 // the joines are propagted by the ircuser. the signal ircUserJoined is only for convenience
152 emit ircUserJoined(ircuser);
156 void IrcChannel::join(const QString &nick) {
157 join(network->ircUser(nick));
160 void IrcChannel::part(IrcUser *ircuser) {
161 if(isKnownUser(ircuser)) {
162 _userModes.remove(ircuser);
163 ircuser->partChannel(name());
164 //qDebug() << "PART" << name() << ircuser->nick() << ircUsers().count();
165 // if you wonder why there is no counterpart to ircUserParted:
166 // the joines are propagted by the ircuser. the signal ircUserParted is only for convenience
167 emit ircUserParted(ircuser);
168 if(network->isMe(ircuser))
173 void IrcChannel::part(const QString &nick) {
174 part(network->ircUser(nick));
178 void IrcChannel::setUserModes(IrcUser *ircuser, const QString &modes) {
179 if(isKnownUser(ircuser)) {
180 _userModes[ircuser] = modes;
181 emit userModesSet(ircuser->nick(), modes);
182 emit ircUserModesSet(ircuser, modes);
186 void IrcChannel::setUserModes(const QString &nick, const QString &modes) {
187 setUserModes(network->ircUser(nick), modes);
191 void IrcChannel::addUserMode(IrcUser *ircuser, const QString &mode) {
192 if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
195 if(!_userModes[ircuser].contains(mode)) {
196 _userModes[ircuser] += mode;
197 emit userModeAdded(ircuser->nick(), mode);
198 emit ircUserModeAdded(ircuser, mode);
203 void IrcChannel::addUserMode(const QString &nick, const QString &mode) {
204 addUserMode(network->ircUser(nick), mode);
208 void IrcChannel::removeUserMode(IrcUser *ircuser, const QString &mode) {
209 if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
212 if(_userModes[ircuser].contains(mode)) {
213 _userModes[ircuser].remove(mode);
214 emit userModeRemoved(ircuser->nick(), mode);
215 emit ircUserModeRemoved(ircuser, mode);
220 void IrcChannel::removeUserMode(const QString &nick, const QString &mode) {
221 removeUserMode(network->ircUser(nick), mode);
224 // INIT SET USER MODES
225 QVariantMap IrcChannel::initUserModes() const {
226 QVariantMap usermodes;
227 QHash<IrcUser *, QString>::const_iterator iter = _userModes.constBegin();
228 while(iter != _userModes.constEnd()) {
229 usermodes[iter.key()->nick()] = iter.value();
235 void IrcChannel::initSetUserModes(const QVariantMap &usermodes) {
236 QMapIterator<QString, QVariant> iter(usermodes);
237 while(iter.hasNext()) {
239 setUserModes(iter.key(), iter.value().toString());
243 void IrcChannel::ircUserDestroyed() {
244 IrcUser *ircUser = static_cast<IrcUser *>(sender());
246 _userModes.remove(ircUser);
247 emit ircUserParted(ircUser);
248 //qDebug() << "DEST" << name() << ircUsers().count();
251 void IrcChannel::ircUserNickSet(QString nick) {
252 IrcUser *ircUser = qobject_cast<IrcUser *>(sender());
254 emit ircUserNickSet(ircUser, nick);