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 // if you wonder why there is no counterpart to ircUserJoined:
112 // the joines are propagted by the ircuser. the signal ircUserJoined is only for convenience
113 emit ircUserJoined(ircuser);
117 void IrcChannel::join(const QString &nick) {
118 join(networkInfo->ircUser(nick));
121 void IrcChannel::part(IrcUser *ircuser) {
122 if(isKnownUser(ircuser)) {
123 _userModes.remove(ircuser);
124 ircuser->partChannel(name());
125 // if you wonder why there is no counterpart to ircUserParted:
126 // the joines are propagted by the ircuser. the signal ircUserParted is only for convenience
127 emit ircUserParted(ircuser);
131 void IrcChannel::part(const QString &nick) {
132 part(networkInfo->ircUser(nick));
136 void IrcChannel::setUserModes(IrcUser *ircuser, const QString &modes) {
137 if(isKnownUser(ircuser)) {
138 _userModes[ircuser] = modes;
139 emit userModesSet(ircuser->nick(), modes);
143 void IrcChannel::setUserModes(const QString &nick, const QString &modes) {
144 setUserModes(networkInfo->ircUser(nick), modes);
148 void IrcChannel::addUserMode(IrcUser *ircuser, const QString &mode) {
149 if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
152 if(!_userModes[ircuser].contains(mode)) {
153 _userModes[ircuser] += mode;
154 emit userModeAdded(ircuser->nick(), mode);
159 void IrcChannel::addUserMode(const QString &nick, const QString &mode) {
160 addUserMode(networkInfo->ircUser(nick), mode);
165 void IrcChannel::removeUserMode(IrcUser *ircuser, const QString &mode) {
166 if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
169 if(_userModes[ircuser].contains(mode)) {
170 _userModes[ircuser].remove(mode);
171 emit userModeRemoved(ircuser->nick(), mode);
176 void IrcChannel::removeUserMode(const QString &nick, const QString &mode) {
177 removeUserMode(networkInfo->ircUser(nick), mode);
180 // INIT SET USER MODES
181 QVariantMap IrcChannel::initUserModes() const {
182 QVariantMap usermodes;
183 QHash<IrcUser *, QString>::const_iterator iter = _userModes.constBegin();
184 while(iter != _userModes.constEnd()) {
185 usermodes[iter.key()->nick()] = iter.value();
191 void IrcChannel::initSetUserModes(const QVariantMap &usermodes) {
192 QMapIterator<QString, QVariant> iter(usermodes);
193 while(iter.hasNext()) {
195 setUserModes(iter.key(), iter.value().toString());
199 void IrcChannel::ircUserDestroyed() {
200 IrcUser *ircUser = static_cast<IrcUser *>(sender());
202 _userModes.remove(ircUser);
205 void IrcChannel::setInitialized() {