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 IrcChannel::~IrcChannel() {
47 // ====================
49 // ====================
50 bool IrcChannel::isKnownUser(IrcUser *ircuser) const {
54 qWarning() << "Channel" << name() << "received IrcUser Nullpointer!";
58 if(!_userModes.contains(ircuser) && ircuser) {
59 qWarning() << "Channel" << name() << "received data for unknown User" << ircuser->nick();
66 bool IrcChannel::isValidChannelUserMode(const QString &mode) const {
69 qWarning() << "Channel" << name() << "received Channel User Mode which is longer then 1 Char:" << mode;
75 bool IrcChannel::initialized() const {
79 QString IrcChannel::name() const {
83 QString IrcChannel::topic() const {
87 QList<IrcUser *> IrcChannel::ircUsers() const {
88 return _userModes.keys();
91 QString IrcChannel::userMode(IrcUser *ircuser) const {
92 if(_userModes.contains(ircuser))
93 return _userModes[ircuser];
98 QString IrcChannel::userMode(const QString &nick) const {
99 return userMode(networkInfo->ircUser(nick));
102 // ====================
104 // ====================
105 void IrcChannel::setTopic(const QString &topic) {
107 emit topicSet(topic);
110 void IrcChannel::join(IrcUser *ircuser) {
111 if(!_userModes.contains(ircuser) && ircuser) {
112 _userModes[ircuser] = QString();
113 ircuser->joinChannel(name());
114 connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
115 // if you wonder why there is no counterpart to ircUserJoined:
116 // the joines are propagted by the ircuser. the signal ircUserJoined is only for convenience
117 emit ircUserJoined(ircuser);
121 void IrcChannel::join(const QString &nick) {
122 join(networkInfo->ircUser(nick));
125 void IrcChannel::part(IrcUser *ircuser) {
126 if(isKnownUser(ircuser)) {
127 _userModes.remove(ircuser);
128 ircuser->partChannel(name());
129 // if you wonder why there is no counterpart to ircUserParted:
130 // the joines are propagted by the ircuser. the signal ircUserParted is only for convenience
131 emit ircUserParted(ircuser);
135 void IrcChannel::part(const QString &nick) {
136 part(networkInfo->ircUser(nick));
140 void IrcChannel::setUserModes(IrcUser *ircuser, const QString &modes) {
141 if(isKnownUser(ircuser)) {
142 _userModes[ircuser] = modes;
143 emit userModesSet(ircuser->nick(), modes);
147 void IrcChannel::setUserModes(const QString &nick, const QString &modes) {
148 setUserModes(networkInfo->ircUser(nick), modes);
152 void IrcChannel::addUserMode(IrcUser *ircuser, const QString &mode) {
153 if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
156 if(!_userModes[ircuser].contains(mode)) {
157 _userModes[ircuser] += mode;
158 emit userModeAdded(ircuser->nick(), mode);
163 void IrcChannel::addUserMode(const QString &nick, const QString &mode) {
164 addUserMode(networkInfo->ircUser(nick), mode);
169 void IrcChannel::removeUserMode(IrcUser *ircuser, const QString &mode) {
170 if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
173 if(_userModes[ircuser].contains(mode)) {
174 _userModes[ircuser].remove(mode);
175 emit userModeRemoved(ircuser->nick(), mode);
180 void IrcChannel::removeUserMode(const QString &nick, const QString &mode) {
181 removeUserMode(networkInfo->ircUser(nick), mode);
184 // INIT SET USER MODES
185 QVariantMap IrcChannel::initUserModes() const {
186 QVariantMap usermodes;
187 QHash<IrcUser *, QString>::const_iterator iter = _userModes.constBegin();
188 while(iter != _userModes.constEnd()) {
189 usermodes[iter.key()->nick()] = iter.value();
195 void IrcChannel::initSetUserModes(const QVariantMap &usermodes) {
196 QMapIterator<QString, QVariant> iter(usermodes);
197 while(iter.hasNext()) {
199 setUserModes(iter.key(), iter.value().toString());
203 void IrcChannel::ircUserDestroyed() {
204 IrcUser *ircUser = static_cast<IrcUser *>(sender());
206 _userModes.remove(ircUser);
209 void IrcChannel::setInitialized() {