Added some featues to the SignalProxy. Now SyncSlaves can request that objects residi...
[quassel.git] / src / common / ircchannel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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) version 3.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "ircchannel.h"
22
23 #include "networkinfo.h"
24 //#include "nicktreemodel.h"
25 #include "signalproxy.h"
26 #include "ircuser.h"
27 #include "util.h"
28
29 #include <QMapIterator>
30 #include <QHashIterator>
31 #include <QTextCodec>
32
33 #include <QDebug>
34
35
36 IrcChannel::IrcChannel(const QString &channelname, NetworkInfo *networkinfo) 
37   : QObject(networkinfo),
38     _initialized(false),
39     _name(channelname),
40     _topic(QString()),
41     networkInfo(networkinfo)
42 {
43   setObjectName(QString::number(networkInfo->networkId()) + "/" +  channelname);
44 }
45
46 IrcChannel::~IrcChannel() {
47
48 }
49
50 // ====================
51 //  PUBLIC:
52 // ====================
53 bool IrcChannel::isKnownUser(IrcUser *ircuser) const {
54   if(ircuser == 0) {
55     qWarning() << "Channel" << name() << "received IrcUser Nullpointer!";
56     return false;
57   }
58   
59   if(!_userModes.contains(ircuser)) {
60     qWarning() << "Channel" << name() << "received data for unknown User" << ircuser->nick();
61     return false;
62   }
63
64   return true;
65 }
66
67 bool IrcChannel::isValidChannelUserMode(const QString &mode) const {
68   bool isvalid = true;
69   if(mode.size() > 1) {
70     qWarning() << "Channel" << name() << "received Channel User Mode which is longer then 1 Char:" << mode;
71     isvalid = false;
72   }
73   return isvalid;
74 }
75
76 bool IrcChannel::initialized() const {
77   return _initialized;
78 }
79
80 QString IrcChannel::name() const {
81   return _name;
82 }
83
84 QString IrcChannel::topic() const {
85   return _topic;
86 }
87
88 QList<IrcUser *> IrcChannel::ircUsers() const {
89   return _userModes.keys();
90 }
91
92 QString IrcChannel::userModes(IrcUser *ircuser) const {
93   if(_userModes.contains(ircuser))
94     return _userModes[ircuser];
95   else
96     return QString();
97 }
98
99 QString IrcChannel::userModes(const QString &nick) const {
100   return userModes(networkInfo->ircUser(nick));
101 }
102
103 QTextCodec *IrcChannel::codecForEncoding() const {
104   return _codecForEncoding;
105 }
106
107 void IrcChannel::setCodecForEncoding(const QString &name) {
108   setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
109 }
110
111 void IrcChannel::setCodecForEncoding(QTextCodec *codec) {
112   _codecForEncoding = codec;
113 }
114
115 QTextCodec *IrcChannel::codecForDecoding() const {
116   return _codecForDecoding;
117 }
118
119 void IrcChannel::setCodecForDecoding(const QString &name) {
120   setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
121 }
122
123 void IrcChannel::setCodecForDecoding(QTextCodec *codec) {
124   _codecForDecoding = codec;
125 }
126
127 QString IrcChannel::decodeString(const QByteArray &text) const {
128   if(!codecForDecoding()) return networkInfo->decodeString(text);
129   return ::decodeString(text, _codecForDecoding);
130 }
131
132 QByteArray IrcChannel::encodeString(const QString string) const {
133   if(codecForEncoding()) {
134     return _codecForEncoding->fromUnicode(string);
135   }
136   return networkInfo->encodeString(string);
137 }
138
139 // ====================
140 //  PUBLIC SLOTS:
141 // ====================
142 void IrcChannel::setTopic(const QString &topic) {
143   _topic = topic;
144   emit topicSet(topic);
145 }
146
147 void IrcChannel::join(IrcUser *ircuser) {
148   if(!_userModes.contains(ircuser) && ircuser) {
149     _userModes[ircuser] = QString();
150     ircuser->joinChannel(name());
151     //qDebug() << "JOIN" << name() << ircuser->nick() << ircUsers().count();
152     connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickSet(QString)));
153     connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
154     // if you wonder why there is no counterpart to ircUserJoined:
155     // the joines are propagted by the ircuser. the signal ircUserJoined is only for convenience
156     emit ircUserJoined(ircuser);
157   }
158 }
159
160 void IrcChannel::join(const QString &nick) {
161   join(networkInfo->ircUser(nick));
162 }
163
164 void IrcChannel::part(IrcUser *ircuser) {
165   if(isKnownUser(ircuser)) {
166     _userModes.remove(ircuser);
167     ircuser->partChannel(name());
168     //qDebug() << "PART" << name() << ircuser->nick() << ircUsers().count();
169     // if you wonder why there is no counterpart to ircUserParted:
170     // the joines are propagted by the ircuser. the signal ircUserParted is only for convenience
171     emit ircUserParted(ircuser);
172   }
173 }
174
175 void IrcChannel::part(const QString &nick) {
176   part(networkInfo->ircUser(nick));
177 }
178
179 // SET USER MODE
180 void IrcChannel::setUserModes(IrcUser *ircuser, const QString &modes) {
181   if(isKnownUser(ircuser)) {
182     _userModes[ircuser] = modes;
183     emit userModesSet(ircuser->nick(), modes);
184     emit ircUserModesSet(ircuser, modes);
185   }
186 }
187
188 void IrcChannel::setUserModes(const QString &nick, const QString &modes) {
189   setUserModes(networkInfo->ircUser(nick), modes);
190 }
191
192 // ADD USER MODE
193 void IrcChannel::addUserMode(IrcUser *ircuser, const QString &mode) {
194   if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
195     return;
196
197   if(!_userModes[ircuser].contains(mode)) {
198     _userModes[ircuser] += mode;
199     emit userModeAdded(ircuser->nick(), mode);
200     emit ircUserModeAdded(ircuser, mode);
201   }
202
203 }
204
205 void IrcChannel::addUserMode(const QString &nick, const QString &mode) {
206   addUserMode(networkInfo->ircUser(nick), mode);
207 }
208
209 // REMOVE USER MODE
210 void IrcChannel::removeUserMode(IrcUser *ircuser, const QString &mode) {
211   if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
212     return;
213
214   if(_userModes[ircuser].contains(mode)) {
215     _userModes[ircuser].remove(mode);
216     emit userModeRemoved(ircuser->nick(), mode);
217     emit ircUserModeRemoved(ircuser, mode);
218   }
219
220 }
221
222 void IrcChannel::removeUserMode(const QString &nick, const QString &mode) {
223   removeUserMode(networkInfo->ircUser(nick), mode);
224 }
225
226 // INIT SET USER MODES
227 QVariantMap IrcChannel::initUserModes() const {
228   QVariantMap usermodes;
229   QHash<IrcUser *, QString>::const_iterator iter = _userModes.constBegin();
230   while(iter != _userModes.constEnd()) {
231     usermodes[iter.key()->nick()] = iter.value();
232     iter++;
233   }
234   return usermodes;
235 }
236
237 void IrcChannel::initSetUserModes(const QVariantMap &usermodes) {
238   QMapIterator<QString, QVariant> iter(usermodes);
239   while(iter.hasNext()) {
240     iter.next();
241     setUserModes(iter.key(), iter.value().toString());
242   }
243 }
244
245 void IrcChannel::ircUserDestroyed() {
246   IrcUser *ircUser = static_cast<IrcUser *>(sender());
247   Q_ASSERT(ircUser);
248   _userModes.remove(ircUser);
249   emit ircUserParted(ircUser);
250   //qDebug() << "DEST" << name() << ircUsers().count();
251 }
252
253 void IrcChannel::ircUserNickSet(QString nick) {
254   IrcUser *ircUser = qobject_cast<IrcUser *>(sender());
255   Q_ASSERT(ircUser);
256   emit ircUserNickSet(ircUser, nick);
257 }
258
259 void IrcChannel::setInitialized() {
260   _initialized = true;
261   emit initDone();
262 }
263