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 "nicktreemodel.h"
25 #include "signalproxy.h"
28 #include <QMapIterator>
29 #include <QHashIterator>
34 IrcChannel::IrcChannel(const QString &channelname, NetworkInfo *networkinfo)
35 : QObject(networkinfo),
39 networkInfo(networkinfo)
41 setObjectName(QString::number(networkInfo->networkId()) + "/" + channelname);
44 IrcChannel::~IrcChannel() {
48 // ====================
50 // ====================
51 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 bool IrcChannel::initialized() const {
80 QString IrcChannel::name() const {
84 QString IrcChannel::topic() const {
88 QList<IrcUser *> IrcChannel::ircUsers() const {
89 return _userModes.keys();
92 QString IrcChannel::userModes(IrcUser *ircuser) const {
93 if(_userModes.contains(ircuser))
94 return _userModes[ircuser];
99 QString IrcChannel::userModes(const QString &nick) const {
100 return userModes(networkInfo->ircUser(nick));
103 // ====================
105 // ====================
106 void IrcChannel::setTopic(const QString &topic) {
108 emit topicSet(topic);
111 void IrcChannel::join(IrcUser *ircuser) {
112 if(!_userModes.contains(ircuser) && ircuser) {
113 _userModes[ircuser] = QString();
114 ircuser->joinChannel(name());
115 qDebug() << "JOIN" << name() << ircuser->nick() << ircUsers().count();
116 connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickSet(QString)));
117 connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
118 // if you wonder why there is no counterpart to ircUserJoined:
119 // the joines are propagted by the ircuser. the signal ircUserJoined is only for convenience
120 emit ircUserJoined(ircuser);
124 void IrcChannel::join(const QString &nick) {
125 join(networkInfo->ircUser(nick));
128 void IrcChannel::part(IrcUser *ircuser) {
129 if(isKnownUser(ircuser)) {
130 _userModes.remove(ircuser);
131 ircuser->partChannel(name());
132 qDebug() << "PART" << name() << ircuser->nick() << ircUsers().count();
133 // if you wonder why there is no counterpart to ircUserParted:
134 // the joines are propagted by the ircuser. the signal ircUserParted is only for convenience
135 emit ircUserParted(ircuser);
139 void IrcChannel::part(const QString &nick) {
140 part(networkInfo->ircUser(nick));
144 void IrcChannel::setUserModes(IrcUser *ircuser, const QString &modes) {
145 if(isKnownUser(ircuser)) {
146 _userModes[ircuser] = modes;
147 emit userModesSet(ircuser->nick(), modes);
148 emit ircUserModesSet(ircuser, modes);
152 void IrcChannel::setUserModes(const QString &nick, const QString &modes) {
153 setUserModes(networkInfo->ircUser(nick), modes);
157 void IrcChannel::addUserMode(IrcUser *ircuser, const QString &mode) {
158 if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
161 if(!_userModes[ircuser].contains(mode)) {
162 _userModes[ircuser] += mode;
163 emit userModeAdded(ircuser->nick(), mode);
164 emit ircUserModeAdded(ircuser, mode);
169 void IrcChannel::addUserMode(const QString &nick, const QString &mode) {
170 addUserMode(networkInfo->ircUser(nick), mode);
174 void IrcChannel::removeUserMode(IrcUser *ircuser, const QString &mode) {
175 if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
178 if(_userModes[ircuser].contains(mode)) {
179 _userModes[ircuser].remove(mode);
180 emit userModeRemoved(ircuser->nick(), mode);
181 emit ircUserModeRemoved(ircuser, mode);
186 void IrcChannel::removeUserMode(const QString &nick, const QString &mode) {
187 removeUserMode(networkInfo->ircUser(nick), mode);
190 // INIT SET USER MODES
191 QVariantMap IrcChannel::initUserModes() const {
192 QVariantMap usermodes;
193 QHash<IrcUser *, QString>::const_iterator iter = _userModes.constBegin();
194 while(iter != _userModes.constEnd()) {
195 usermodes[iter.key()->nick()] = iter.value();
201 void IrcChannel::initSetUserModes(const QVariantMap &usermodes) {
202 QMapIterator<QString, QVariant> iter(usermodes);
203 while(iter.hasNext()) {
205 setUserModes(iter.key(), iter.value().toString());
209 void IrcChannel::ircUserDestroyed() {
210 IrcUser *ircUser = static_cast<IrcUser *>(sender());
212 _userModes.remove(ircUser);
213 emit ircUserParted(ircUser);
214 qDebug() << "DEST" << name() << ircUsers().count();
217 void IrcChannel::ircUserNickSet(QString nick) {
218 IrcUser *ircUser = qobject_cast<IrcUser *>(sender());
220 emit ircUserNickSet(ircUser, nick);
223 void IrcChannel::setInitialized() {