1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
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) version 3. *
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 ***************************************************************************/
22 #include "signalproxy.h"
24 #include "ircchannel.h"
31 // ====================
33 // ====================
34 Network::Network(const NetworkId &networkid, QObject *parent) : SyncableObject(parent),
35 _networkId(networkid),
38 _networkName(QString("<not initialized>")),
39 _currentServer(QString()),
42 _prefixModes(QString()),
47 setObjectName(QString::number(networkid.toInt()));
50 // I think this is unnecessary since IrcUsers have us as their daddy :)
53 QHashIterator<QString, IrcUser *> ircuser(_ircUsers);
54 while (ircuser.hasNext()) {
56 delete ircuser.value();
61 NetworkId Network::networkId() const {
65 SignalProxy *Network::proxy() const {
69 void Network::setProxy(SignalProxy *proxy) {
71 //proxy->synchronize(this); // we should to this explicitly from the outside!
74 bool Network::isMyNick(const QString &nick) const {
75 return (myNick().toLower() == nick.toLower());
78 bool Network::isMe(IrcUser *ircuser) const {
79 return (ircuser->nick().toLower() == myNick().toLower());
82 bool Network::isChannelName(const QString &channelname) const {
83 if(channelname.isEmpty())
86 if(supports("CHANTYPES"))
87 return support("CHANTYPES").contains(channelname[0]);
89 return QString("#&!+").contains(channelname[0]);
92 bool Network::isConnected() const {
96 NetworkInfo Network::networkInfo() const {
98 info.networkName = networkName();
99 info.networkId = networkId();
100 info.identity = identity();
101 info.codecForEncoding = codecForEncoding();
102 info.codecForDecoding = codecForDecoding();
103 info.serverList = serverList();
107 void Network::setNetworkInfo(const NetworkInfo &info) {
108 // we don't set our ID!
109 if(!info.networkName.isEmpty()) setNetworkName(info.networkName);
110 if(info.identity > 0) setIdentity(info.identity);
111 if(!info.codecForEncoding.isEmpty()) setCodecForEncoding(QTextCodec::codecForName(info.codecForEncoding));
112 if(!info.codecForDecoding.isEmpty()) setCodecForDecoding(QTextCodec::codecForName(info.codecForDecoding));
113 if(info.serverList.count()) setServerList(info.serverList);
116 QString Network::prefixToMode(const QString &prefix) {
117 if(prefixes().contains(prefix))
118 return QString(prefixModes()[prefixes().indexOf(prefix)]);
123 QString Network::prefixToMode(const QCharRef &prefix) {
124 return prefixToMode(QString(prefix));
127 QString Network::modeToPrefix(const QString &mode) {
128 if(prefixModes().contains(mode))
129 return QString(prefixes()[prefixModes().indexOf(mode)]);
134 QString Network::modeToPrefix(const QCharRef &mode) {
135 return modeToPrefix(QString(mode));
138 QString Network::networkName() const {
142 QString Network::currentServer() const {
143 return _currentServer;
146 QString Network::myNick() const {
150 IdentityId Network::identity() const {
154 QStringList Network::nicks() const {
155 // we don't use _ircUsers.keys() since the keys may be
156 // not up to date after a nick change
158 foreach(IrcUser *ircuser, _ircUsers.values()) {
159 nicks << ircuser->nick();
164 QStringList Network::channels() const {
165 return _ircChannels.keys();
168 QList<QVariantMap> Network::serverList() const {
172 QString Network::prefixes() {
173 if(_prefixes.isNull())
179 QString Network::prefixModes() {
180 if(_prefixModes.isNull())
186 bool Network::supports(const QString ¶m) const {
187 return _supports.contains(param);
190 QString Network::support(const QString ¶m) const {
191 QString support_ = param.toUpper();
192 if(_supports.contains(support_))
193 return _supports[support_];
198 IrcUser *Network::newIrcUser(const QString &hostmask) {
199 QString nick(nickFromMask(hostmask).toLower());
200 if(!_ircUsers.contains(nick)) {
201 IrcUser *ircuser = new IrcUser(hostmask, this);
202 // mark IrcUser as already initialized to keep the SignalProxy from requesting initData
203 //if(isInitialized())
204 // ircuser->setInitialized();
206 proxy()->synchronize(ircuser);
208 qWarning() << "unable to synchronize new IrcUser" << hostmask << "forgot to call Network::setProxy(SignalProxy *)?";
210 connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
211 connect(ircuser, SIGNAL(initDone()), this, SLOT(ircUserInitDone()));
212 connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
213 _ircUsers[nick] = ircuser;
214 emit ircUserAdded(hostmask);
215 emit ircUserAdded(ircuser);
217 return _ircUsers[nick];
220 IrcUser *Network::newIrcUser(const QByteArray &hostmask) {
221 return newIrcUser(decodeString(hostmask));
224 void Network::removeIrcUser(IrcUser *ircuser) {
225 QString nick = _ircUsers.key(ircuser);
229 _ircUsers.remove(nick);
230 emit ircUserRemoved(nick);
231 emit ircUserRemoved(ircuser);
232 ircuser->deleteLater();
235 void Network::removeIrcUser(QString nick) {
237 if((ircuser = ircUser(nick)) != 0)
238 removeIrcUser(ircuser);
241 IrcUser *Network::ircUser(QString nickname) const {
242 nickname = nickname.toLower();
243 if(_ircUsers.contains(nickname))
244 return _ircUsers[nickname];
249 IrcUser *Network::ircUser(const QByteArray &nickname) const {
250 return ircUser(decodeString(nickname));
253 QList<IrcUser *> Network::ircUsers() const {
254 return _ircUsers.values();
257 quint32 Network::ircUserCount() const {
258 return _ircUsers.count();
261 IrcChannel *Network::newIrcChannel(const QString &channelname) {
262 if(!_ircChannels.contains(channelname.toLower())) {
263 IrcChannel *channel = new IrcChannel(channelname, this);
264 // mark IrcUser as already initialized to keep the SignalProxy from requesting initData
265 //if(isInitialized())
266 // channel->setInitialized();
269 proxy()->synchronize(channel);
271 qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
273 connect(channel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
274 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
275 _ircChannels[channelname.toLower()] = channel;
276 emit ircChannelAdded(channelname);
277 emit ircChannelAdded(channel);
279 return _ircChannels[channelname.toLower()];
282 IrcChannel *Network::newIrcChannel(const QByteArray &channelname) {
283 return newIrcChannel(decodeString(channelname));
286 IrcChannel *Network::ircChannel(QString channelname) const {
287 channelname = channelname.toLower();
288 if(_ircChannels.contains(channelname))
289 return _ircChannels[channelname];
294 IrcChannel *Network::ircChannel(const QByteArray &channelname) const {
295 return ircChannel(decodeString(channelname));
299 QList<IrcChannel *> Network::ircChannels() const {
300 return _ircChannels.values();
303 quint32 Network::ircChannelCount() const {
304 return _ircChannels.count();
307 QByteArray Network::codecForEncoding() const {
308 if(_codecForEncoding) return _codecForEncoding->name();
312 void Network::setCodecForEncoding(const QByteArray &name) {
313 setCodecForEncoding(QTextCodec::codecForName(name));
316 void Network::setCodecForEncoding(QTextCodec *codec) {
317 _codecForEncoding = codec;
318 emit codecForEncodingSet(codecForEncoding());
321 QByteArray Network::codecForDecoding() const {
322 if(_codecForDecoding) return _codecForDecoding->name();
323 else return QByteArray();
326 void Network::setCodecForDecoding(const QByteArray &name) {
327 setCodecForDecoding(QTextCodec::codecForName(name));
330 void Network::setCodecForDecoding(QTextCodec *codec) {
331 _codecForDecoding = codec;
332 emit codecForDecodingSet(codecForDecoding());
335 QString Network::decodeString(const QByteArray &text) const {
336 return ::decodeString(text, _codecForDecoding);
339 QByteArray Network::encodeString(const QString string) const {
340 if(_codecForEncoding) {
341 return _codecForEncoding->fromUnicode(string);
343 return string.toAscii();
346 // ====================
348 // ====================
349 void Network::setNetworkName(const QString &networkName) {
350 _networkName = networkName;
351 emit networkNameSet(networkName);
354 void Network::setCurrentServer(const QString ¤tServer) {
355 _currentServer = currentServer;
356 emit currentServerSet(currentServer);
359 void Network::setConnected(bool connected) {
360 _connected = connected;
361 emit connectedSet(connected);
364 void Network::setMyNick(const QString &nickname) {
366 emit myNickSet(nickname);
369 void Network::setIdentity(IdentityId id) {
371 emit identitySet(id);
374 void Network::setServerList(const QList<QVariantMap> &serverList) {
375 _serverList = serverList;
376 emit serverListSet(serverList);
379 void Network::addSupport(const QString ¶m, const QString &value) {
380 if(!_supports.contains(param)) {
381 _supports[param] = value;
382 emit supportAdded(param, value);
386 void Network::removeSupport(const QString ¶m) {
387 if(_supports.contains(param)) {
388 _supports.remove(param);
389 emit supportRemoved(param);
393 QVariantMap Network::initSupports() const {
394 QVariantMap supports;
395 QHashIterator<QString, QString> iter(_supports);
396 while(iter.hasNext()) {
398 supports[iter.key()] = iter.value();
403 QVariantList Network::initServerList() const {
404 QList<QVariant> list;
405 foreach(QVariantMap serverdata, serverList()) list << QVariant(serverdata);
409 QStringList Network::initIrcUsers() const {
410 QStringList hostmasks;
411 foreach(IrcUser *ircuser, ircUsers()) {
412 hostmasks << ircuser->hostmask();
417 QStringList Network::initIrcChannels() const {
418 return _ircChannels.keys();
421 void Network::initSetSupports(const QVariantMap &supports) {
422 QMapIterator<QString, QVariant> iter(supports);
423 while(iter.hasNext()) {
425 addSupport(iter.key(), iter.value().toString());
429 void Network::initSetServerList(const QVariantList & serverList) {
430 QList<QVariantMap> slist;
431 foreach(QVariant v, serverList) slist << v.toMap();
432 setServerList(slist);
435 void Network::initSetIrcUsers(const QStringList &hostmasks) {
436 if(!_ircUsers.empty())
438 foreach(QString hostmask, hostmasks) {
439 newIrcUser(hostmask);
443 void Network::initSetChannels(const QStringList &channels) {
444 if(!_ircChannels.empty())
446 foreach(QString channel, channels)
447 newIrcChannel(channel);
450 IrcUser *Network::updateNickFromMask(const QString &mask) {
451 QString nick(nickFromMask(mask).toLower());
454 if(_ircUsers.contains(nick)) {
455 ircuser = _ircUsers[nick];
456 ircuser->updateHostmask(mask);
458 ircuser = newIrcUser(mask);
463 void Network::ircUserNickChanged(QString newnick) {
464 QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
469 if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
471 if(myNick().toLower() == oldnick)
475 void Network::ircUserInitDone() {
476 IrcUser *ircuser = static_cast<IrcUser *>(sender());
478 emit ircUserInitDone(ircuser);
481 void Network::ircChannelInitDone() {
482 IrcChannel *ircchannel = static_cast<IrcChannel *>(sender());
483 Q_ASSERT(ircchannel);
484 emit ircChannelInitDone(ircchannel);
487 void Network::ircUserDestroyed() {
488 IrcUser *ircuser = static_cast<IrcUser *>(sender());
490 removeIrcUser(ircuser);
493 void Network::channelDestroyed() {
494 IrcChannel *channel = static_cast<IrcChannel *>(sender());
496 emit ircChannelRemoved(sender());
497 _ircChannels.remove(_ircChannels.key(channel));
500 void Network::requestConnect() {
502 if(proxy()->proxyMode() == SignalProxy::Client) emit connectRequested(); // on the client this triggers calling this slot on the core
503 else emit connectRequested(networkId()); // and this is for CoreSession :)
506 void Network::requestDisconnect() {
508 if(proxy()->proxyMode() == SignalProxy::Client) emit disconnectRequested(); // on the client this triggers calling this slot on the core
509 else emit disconnectRequested(networkId()); // and this is for CoreSession :)
512 // ====================
514 // ====================
515 void Network::determinePrefixes() {
516 // seems like we have to construct them first
517 QString PREFIX = support("PREFIX");
519 if(PREFIX.startsWith("(") && PREFIX.contains(")")) {
520 _prefixes = PREFIX.section(")", 1);
521 _prefixModes = PREFIX.mid(1).section(")", 0, 0);
523 QString defaultPrefixes("~&@%+");
524 QString defaultPrefixModes("qaohv");
526 // we just assume that in PREFIX are only prefix chars stored
527 for(int i = 0; i < defaultPrefixes.size(); i++) {
528 if(PREFIX.contains(defaultPrefixes[i])) {
529 _prefixes += defaultPrefixes[i];
530 _prefixModes += defaultPrefixModes[i];
534 if(!_prefixes.isNull())
537 // well... our assumption was obviously wrong...
538 // check if it's only prefix modes
539 for(int i = 0; i < defaultPrefixes.size(); i++) {
540 if(PREFIX.contains(defaultPrefixModes[i])) {
541 _prefixes += defaultPrefixes[i];
542 _prefixModes += defaultPrefixModes[i];
545 // now we've done all we've could...
549 /************************************************************************
551 ************************************************************************/
553 bool NetworkInfo::operator==(const NetworkInfo &other) const {
554 if(networkId != other.networkId) return false;
555 if(networkName != other.networkName) return false;
556 if(identity != other.identity) return false;
557 if(codecForEncoding != other.codecForEncoding) return false;
558 if(codecForDecoding != other.codecForDecoding) return false;
559 if(serverList != other.serverList) return false;
563 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
564 return !(*this == other);