1 /***************************************************************************
2 * Copyright (C) 2005-07 by The Quassel 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 "ircchannel.h"
23 #include "networkinfo.h"
24 #include "signalproxy.h"
27 #include <QMapIterator>
28 #include <QHashIterator>
33 IrcChannel::IrcChannel(const QString &channelname, NetworkInfo *networkinfo)
34 : QObject(networkinfo),
38 networkInfo(networkinfo)
40 setObjectName(QString::number(networkInfo->networkId()) + "/" + channelname);
43 // ====================
45 // ====================
46 bool IrcChannel::isKnownUser(IrcUser *ircuser) const {
50 qWarning() << "Channel" << name() << "received IrcUser Nullpointer!";
54 if(!_userModes.contains(ircuser) && ircuser) {
55 qWarning() << "Channel" << name() << "received data for unknown User" << ircuser->nick();
62 bool IrcChannel::isValidChannelUserMode(const QString &mode) const {
65 qWarning() << "Channel" << name() << "received Channel User Mode which is longer then 1 Char:" << mode;
71 bool IrcChannel::initialized() const {
75 QString IrcChannel::name() const {
79 QString IrcChannel::topic() const {
83 QList<IrcUser *> IrcChannel::ircUsers() const {
84 return _userModes.keys();
87 QString IrcChannel::userMode(IrcUser *ircuser) const {
88 if(_userModes.contains(ircuser))
89 return _userModes[ircuser];
94 QString IrcChannel::userMode(const QString &nick) const {
95 return userMode(networkInfo->ircUser(nick));
98 // ====================
100 // ====================
101 void IrcChannel::setTopic(const QString &topic) {
103 emit topicSet(topic);
106 void IrcChannel::join(IrcUser *ircuser) {
107 if(!_userModes.contains(ircuser) && ircuser) {
108 _userModes[ircuser] = QString();
109 ircuser->joinChannel(name());
110 connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
111 // no emit here since the join is propagated by IrcUser
115 void IrcChannel::join(const QString &nick) {
116 join(networkInfo->ircUser(nick));
119 void IrcChannel::part(IrcUser *ircuser) {
120 if(isKnownUser(ircuser)) {
121 _userModes.remove(ircuser);
122 ircuser->partChannel(name());
123 // no emit here since the part is propagated by IrcUser
127 void IrcChannel::part(const QString &nick) {
128 part(networkInfo->ircUser(nick));
132 void IrcChannel::setUserModes(IrcUser *ircuser, const QString &modes) {
133 if(isKnownUser(ircuser)) {
134 _userModes[ircuser] = modes;
135 emit userModesSet(ircuser->nick(), modes);
139 void IrcChannel::setUserModes(const QString &nick, const QString &modes) {
140 setUserModes(networkInfo->ircUser(nick), modes);
144 void IrcChannel::addUserMode(IrcUser *ircuser, const QString &mode) {
145 if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
148 if(!_userModes[ircuser].contains(mode)) {
149 _userModes[ircuser] += mode;
150 emit userModeAdded(ircuser->nick(), mode);
155 void IrcChannel::addUserMode(const QString &nick, const QString &mode) {
156 addUserMode(networkInfo->ircUser(nick), mode);
161 void IrcChannel::removeUserMode(IrcUser *ircuser, const QString &mode) {
162 if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
165 if(_userModes[ircuser].contains(mode)) {
166 _userModes[ircuser].remove(mode);
167 emit userModeRemoved(ircuser->nick(), mode);
172 void IrcChannel::removeUserMode(const QString &nick, const QString &mode) {
173 removeUserMode(networkInfo->ircUser(nick), mode);
176 // INIT SET USER MODES
177 QVariantMap IrcChannel::initUserModes() const {
178 QVariantMap usermodes;
179 QHash<IrcUser *, QString>::const_iterator iter = _userModes.constBegin();
180 while(iter != _userModes.constEnd()) {
181 usermodes[iter.key()->nick()] = iter.value();
187 void IrcChannel::initSetUserModes(const QVariantMap &usermodes) {
188 QMapIterator<QString, QVariant> iter(usermodes);
189 while(iter.hasNext()) {
191 setUserModes(iter.key(), iter.value().toString());
195 void IrcChannel::ircUserDestroyed() {
196 IrcUser *ircUser = static_cast<IrcUser *>(sender());
198 _userModes.remove(ircUser);
201 void IrcChannel::setInitialized() {