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 // no emit here since the join is propagated by IrcUser
114 void IrcChannel::join(const QString &nick) {
115 join(networkInfo->ircUser(nick));
118 void IrcChannel::part(IrcUser *ircuser) {
119 if(isKnownUser(ircuser)) {
120 _userModes.remove(ircuser);
121 ircuser->partChannel(name());
122 // no emit here since the part is propagated by IrcUser
126 void IrcChannel::part(const QString &nick) {
127 part(networkInfo->ircUser(nick));
131 void IrcChannel::setUserModes(IrcUser *ircuser, const QString &modes) {
132 if(isKnownUser(ircuser)) {
133 _userModes[ircuser] = modes;
134 emit userModesSet(ircuser->nick(), modes);
138 void IrcChannel::setUserModes(const QString &nick, const QString &modes) {
139 setUserModes(networkInfo->ircUser(nick), modes);
143 void IrcChannel::addUserMode(IrcUser *ircuser, const QString &mode) {
144 if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
147 if(!_userModes[ircuser].contains(mode)) {
148 _userModes[ircuser] += mode;
149 emit userModeAdded(ircuser->nick(), mode);
154 void IrcChannel::addUserMode(const QString &nick, const QString &mode) {
155 addUserMode(networkInfo->ircUser(nick), mode);
160 void IrcChannel::removeUserMode(IrcUser *ircuser, const QString &mode) {
161 if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
164 if(_userModes[ircuser].contains(mode)) {
165 _userModes[ircuser].remove(mode);
166 emit userModeRemoved(ircuser->nick(), mode);
171 void IrcChannel::removeUserMode(const QString &nick, const QString &mode) {
172 removeUserMode(networkInfo->ircUser(nick), mode);
175 // INIT SET USER MODES
176 QVariantMap IrcChannel::initUserModes() const {
177 QVariantMap usermodes;
178 QHashIterator<IrcUser *, QString> iter(_userModes);
179 while(iter.hasNext()) {
181 usermodes[iter.key()->nick()] = iter.value();
186 void IrcChannel::initSetUserModes(const QVariantMap &usermodes) {
187 QMapIterator<QString, QVariant> iter(usermodes);
188 while(iter.hasNext()) {
190 setUserModes(iter.key(), iter.value().toString());
194 void IrcChannel::ircUserDestroyed() {
195 IrcUser *ircUser = qobject_cast<IrcUser *>(sender());
196 // in case this assert triggers we probably need a static_cast
197 // dynamic_casts seem to screw things up when using the destroyed signal
202 void IrcChannel::setInitialized() {