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()),
41 _connectionState(Disconnected),
43 _prefixModes(QString()),
48 setObjectName(QString::number(networkid.toInt()));
51 // I think this is unnecessary since IrcUsers have us as their daddy :)
54 // QHashIterator<QString, IrcUser *> ircuser(_ircUsers);
55 // while (ircuser.hasNext()) {
57 // delete ircuser.value();
59 // qDebug() << "Destroying net" << networkName() << networkId();
63 NetworkId Network::networkId() const {
67 SignalProxy *Network::proxy() const {
71 void Network::setProxy(SignalProxy *proxy) {
73 //proxy->synchronize(this); // we should to this explicitly from the outside!
76 bool Network::isMyNick(const QString &nick) const {
77 return (myNick().toLower() == nick.toLower());
80 bool Network::isMe(IrcUser *ircuser) const {
81 return (ircuser->nick().toLower() == myNick().toLower());
84 bool Network::isChannelName(const QString &channelname) const {
85 if(channelname.isEmpty())
88 if(supports("CHANTYPES"))
89 return support("CHANTYPES").contains(channelname[0]);
91 return QString("#&!+").contains(channelname[0]);
94 bool Network::isConnected() const {
98 //Network::ConnectionState Network::connectionState() const {
99 int Network::connectionState() const {
100 return _connectionState;
103 NetworkInfo Network::networkInfo() const {
105 info.networkName = networkName();
106 info.networkId = networkId();
107 info.identity = identity();
108 info.codecForEncoding = codecForEncoding();
109 info.codecForDecoding = codecForDecoding();
110 info.serverList = serverList();
114 void Network::setNetworkInfo(const NetworkInfo &info) {
115 // we don't set our ID!
116 if(!info.networkName.isEmpty() && info.networkName != networkName()) setNetworkName(info.networkName);
117 if(info.identity > 0 && info.identity != identity()) setIdentity(info.identity);
118 if(!info.codecForEncoding.isEmpty() && info.codecForEncoding != codecForEncoding())
119 setCodecForEncoding(QTextCodec::codecForName(info.codecForEncoding));
120 if(!info.codecForDecoding.isEmpty() && info.codecForDecoding != codecForDecoding())
121 setCodecForDecoding(QTextCodec::codecForName(info.codecForDecoding));
122 if(info.serverList.count()) setServerList(info.serverList); // FIXME compare components
125 QString Network::prefixToMode(const QString &prefix) {
126 if(prefixes().contains(prefix))
127 return QString(prefixModes()[prefixes().indexOf(prefix)]);
132 QString Network::prefixToMode(const QCharRef &prefix) {
133 return prefixToMode(QString(prefix));
136 QString Network::modeToPrefix(const QString &mode) {
137 if(prefixModes().contains(mode))
138 return QString(prefixes()[prefixModes().indexOf(mode)]);
143 QString Network::modeToPrefix(const QCharRef &mode) {
144 return modeToPrefix(QString(mode));
147 QString Network::networkName() const {
151 QString Network::currentServer() const {
152 return _currentServer;
155 QString Network::myNick() const {
159 IdentityId Network::identity() const {
163 QStringList Network::nicks() const {
164 // we don't use _ircUsers.keys() since the keys may be
165 // not up to date after a nick change
167 foreach(IrcUser *ircuser, _ircUsers.values()) {
168 nicks << ircuser->nick();
173 QStringList Network::channels() const {
174 return _ircChannels.keys();
177 QVariantList Network::serverList() const {
181 QString Network::prefixes() {
182 if(_prefixes.isNull())
188 QString Network::prefixModes() {
189 if(_prefixModes.isNull())
195 bool Network::supports(const QString ¶m) const {
196 return _supports.contains(param);
199 QString Network::support(const QString ¶m) const {
200 QString support_ = param.toUpper();
201 if(_supports.contains(support_))
202 return _supports[support_];
207 IrcUser *Network::newIrcUser(const QString &hostmask) {
208 QString nick(nickFromMask(hostmask).toLower());
209 if(!_ircUsers.contains(nick)) {
210 IrcUser *ircuser = new IrcUser(hostmask, this);
211 // mark IrcUser as already initialized to keep the SignalProxy from requesting initData
212 //if(isInitialized())
213 // ircuser->setInitialized();
215 proxy()->synchronize(ircuser);
217 qWarning() << "unable to synchronize new IrcUser" << hostmask << "forgot to call Network::setProxy(SignalProxy *)?";
219 connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
220 connect(ircuser, SIGNAL(initDone()), this, SLOT(ircUserInitDone()));
221 _ircUsers[nick] = ircuser;
222 emit ircUserAdded(hostmask);
223 emit ircUserAdded(ircuser);
225 return _ircUsers[nick];
228 IrcUser *Network::newIrcUser(const QByteArray &hostmask) {
229 return newIrcUser(decodeString(hostmask));
232 void Network::removeIrcUser(IrcUser *ircuser) {
233 QString nick = _ircUsers.key(ircuser);
237 _ircUsers.remove(nick);
238 disconnect(ircuser, 0, this, 0);
239 emit ircUserRemoved(nick);
240 emit ircUserRemoved(ircuser);
241 ircuser->deleteLater();
244 void Network::removeChansAndUsers() {
245 QList<IrcUser *> users = ircUsers();
246 foreach(IrcUser *user, users) {
249 QList<IrcChannel *> channels = ircChannels();
250 foreach(IrcChannel *channel, channels) {
251 removeIrcChannel(channel);
255 void Network::removeIrcUser(const QString &nick) {
257 if((ircuser = ircUser(nick)) != 0)
258 removeIrcUser(ircuser);
261 IrcUser *Network::ircUser(QString nickname) const {
262 nickname = nickname.toLower();
263 if(_ircUsers.contains(nickname))
264 return _ircUsers[nickname];
269 IrcUser *Network::ircUser(const QByteArray &nickname) const {
270 return ircUser(decodeString(nickname));
273 QList<IrcUser *> Network::ircUsers() const {
274 return _ircUsers.values();
277 quint32 Network::ircUserCount() const {
278 return _ircUsers.count();
281 IrcChannel *Network::newIrcChannel(const QString &channelname) {
282 if(!_ircChannels.contains(channelname.toLower())) {
283 IrcChannel *channel = new IrcChannel(channelname, this);
284 // mark IrcUser as already initialized to keep the SignalProxy from requesting initData
285 //if(isInitialized())
286 // channel->setInitialized();
289 proxy()->synchronize(channel);
291 qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
293 connect(channel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
294 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
295 _ircChannels[channelname.toLower()] = channel;
296 emit ircChannelAdded(channelname);
297 emit ircChannelAdded(channel);
299 return _ircChannels[channelname.toLower()];
302 IrcChannel *Network::newIrcChannel(const QByteArray &channelname) {
303 return newIrcChannel(decodeString(channelname));
306 IrcChannel *Network::ircChannel(QString channelname) const {
307 channelname = channelname.toLower();
308 if(_ircChannels.contains(channelname))
309 return _ircChannels[channelname];
314 IrcChannel *Network::ircChannel(const QByteArray &channelname) const {
315 return ircChannel(decodeString(channelname));
319 QList<IrcChannel *> Network::ircChannels() const {
320 return _ircChannels.values();
323 quint32 Network::ircChannelCount() const {
324 return _ircChannels.count();
327 QByteArray Network::codecForEncoding() const {
328 if(_codecForEncoding) return _codecForEncoding->name();
332 void Network::setCodecForEncoding(const QByteArray &name) {
333 setCodecForEncoding(QTextCodec::codecForName(name));
336 void Network::setCodecForEncoding(QTextCodec *codec) {
337 _codecForEncoding = codec;
338 emit codecForEncodingSet(codecForEncoding());
341 QByteArray Network::codecForDecoding() const {
342 if(_codecForDecoding) return _codecForDecoding->name();
343 else return QByteArray();
346 void Network::setCodecForDecoding(const QByteArray &name) {
347 setCodecForDecoding(QTextCodec::codecForName(name));
350 void Network::setCodecForDecoding(QTextCodec *codec) {
351 _codecForDecoding = codec;
352 emit codecForDecodingSet(codecForDecoding());
355 QString Network::decodeString(const QByteArray &text) const {
356 return ::decodeString(text, _codecForDecoding);
359 QByteArray Network::encodeString(const QString string) const {
360 if(_codecForEncoding) {
361 return _codecForEncoding->fromUnicode(string);
363 return string.toAscii();
366 // ====================
368 // ====================
369 void Network::setNetworkName(const QString &networkName) {
370 _networkName = networkName;
371 emit networkNameSet(networkName);
374 void Network::setCurrentServer(const QString ¤tServer) {
375 _currentServer = currentServer;
376 emit currentServerSet(currentServer);
379 void Network::setConnected(bool connected) {
380 _connected = connected;
382 removeChansAndUsers();
383 emit connectedSet(connected);
386 //void Network::setConnectionState(ConnectionState state) {
387 void Network::setConnectionState(int state) {
388 _connectionState = (ConnectionState)state;
389 //qDebug() << "netstate" << networkId() << networkName() << state;
390 emit connectionStateSet(state);
391 emit connectionStateSet(_connectionState);
394 void Network::setMyNick(const QString &nickname) {
396 emit myNickSet(nickname);
399 void Network::setIdentity(IdentityId id) {
401 emit identitySet(id);
404 void Network::setServerList(const QVariantList &serverList) {
405 _serverList = serverList;
406 emit serverListSet(serverList);
409 void Network::addSupport(const QString ¶m, const QString &value) {
410 if(!_supports.contains(param)) {
411 _supports[param] = value;
412 emit supportAdded(param, value);
416 void Network::removeSupport(const QString ¶m) {
417 if(_supports.contains(param)) {
418 _supports.remove(param);
419 emit supportRemoved(param);
423 QVariantMap Network::initSupports() const {
424 QVariantMap supports;
425 QHashIterator<QString, QString> iter(_supports);
426 while(iter.hasNext()) {
428 supports[iter.key()] = iter.value();
433 QVariantList Network::initServerList() const {
437 QStringList Network::initIrcUsers() const {
438 QStringList hostmasks;
439 foreach(IrcUser *ircuser, ircUsers()) {
440 hostmasks << ircuser->hostmask();
445 QStringList Network::initIrcChannels() const {
446 return _ircChannels.keys();
449 void Network::initSetSupports(const QVariantMap &supports) {
450 QMapIterator<QString, QVariant> iter(supports);
451 while(iter.hasNext()) {
453 addSupport(iter.key(), iter.value().toString());
457 void Network::initSetServerList(const QVariantList & serverList) {
458 setServerList(serverList);
461 void Network::initSetIrcUsers(const QStringList &hostmasks) {
462 if(!_ircUsers.empty())
464 foreach(QString hostmask, hostmasks) {
465 newIrcUser(hostmask);
469 void Network::initSetChannels(const QStringList &channels) {
470 if(!_ircChannels.empty())
472 foreach(QString channel, channels)
473 newIrcChannel(channel);
476 IrcUser *Network::updateNickFromMask(const QString &mask) {
477 QString nick(nickFromMask(mask).toLower());
480 if(_ircUsers.contains(nick)) {
481 ircuser = _ircUsers[nick];
482 ircuser->updateHostmask(mask);
484 ircuser = newIrcUser(mask);
489 void Network::ircUserNickChanged(QString newnick) {
490 QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
495 if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
497 if(myNick().toLower() == oldnick)
501 void Network::ircUserInitDone() {
502 IrcUser *ircuser = static_cast<IrcUser *>(sender());
504 emit ircUserInitDone(ircuser);
507 void Network::ircChannelInitDone() {
508 IrcChannel *ircchannel = static_cast<IrcChannel *>(sender());
509 Q_ASSERT(ircchannel);
510 emit ircChannelInitDone(ircchannel);
513 void Network::removeIrcChannel(IrcChannel *channel) {
514 QString chanName = _ircChannels.key(channel);
515 if(chanName.isNull())
518 _ircChannels.remove(chanName);
519 disconnect(channel, 0, this, 0);
520 emit ircChannelRemoved(chanName);
521 emit ircChannelRemoved(channel);
522 channel->deleteLater();
525 void Network::removeIrcChannel(const QString &channel) {
527 if((chan = ircChannel(channel)) != 0)
528 removeIrcChannel(chan);
531 void Network::channelDestroyed() {
532 IrcChannel *channel = static_cast<IrcChannel *>(sender());
534 _ircChannels.remove(_ircChannels.key(channel));
535 emit ircChannelRemoved(channel);
538 void Network::requestConnect() const {
540 if(proxy()->proxyMode() == SignalProxy::Client) emit connectRequested(); // on the client this triggers calling this slot on the core
542 if(connectionState() != Disconnected) {
543 qWarning() << "Requesting connect while not being disconnected!";
546 emit connectRequested(networkId()); // and this is for CoreSession :)
550 void Network::requestDisconnect() const {
552 if(proxy()->proxyMode() == SignalProxy::Client) emit disconnectRequested(); // on the client this triggers calling this slot on the core
554 if(connectionState() == Disconnected) {
555 qWarning() << "Requesting disconnect while not being connected!";
558 emit disconnectRequested(networkId()); // and this is for CoreSession :)
562 void Network::emitConnectionError(const QString &errorMsg) {
563 emit connectionError(errorMsg);
566 // ====================
568 // ====================
569 void Network::determinePrefixes() {
570 // seems like we have to construct them first
571 QString PREFIX = support("PREFIX");
573 if(PREFIX.startsWith("(") && PREFIX.contains(")")) {
574 _prefixes = PREFIX.section(")", 1);
575 _prefixModes = PREFIX.mid(1).section(")", 0, 0);
577 QString defaultPrefixes("~&@%+");
578 QString defaultPrefixModes("qaohv");
580 // we just assume that in PREFIX are only prefix chars stored
581 for(int i = 0; i < defaultPrefixes.size(); i++) {
582 if(PREFIX.contains(defaultPrefixes[i])) {
583 _prefixes += defaultPrefixes[i];
584 _prefixModes += defaultPrefixModes[i];
588 if(!_prefixes.isNull())
591 // well... our assumption was obviously wrong...
592 // check if it's only prefix modes
593 for(int i = 0; i < defaultPrefixes.size(); i++) {
594 if(PREFIX.contains(defaultPrefixModes[i])) {
595 _prefixes += defaultPrefixes[i];
596 _prefixModes += defaultPrefixModes[i];
599 // now we've done all we've could...
603 /************************************************************************
605 ************************************************************************/
607 bool NetworkInfo::operator==(const NetworkInfo &other) const {
608 if(networkId != other.networkId) return false;
609 if(networkName != other.networkName) return false;
610 if(identity != other.identity) return false;
611 if(codecForEncoding != other.codecForEncoding) return false;
612 if(codecForDecoding != other.codecForDecoding) return false;
613 if(serverList != other.serverList) return false;
617 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
618 return !(*this == other);
621 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) {
623 i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
624 i["NetworkName"] = info.networkName;
625 i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
626 i["CodecForEncoding"] = info.codecForEncoding;
627 i["CodecForDecoding"] = info.codecForDecoding;
628 i["ServerList"] = info.serverList;
633 QDataStream &operator>>(QDataStream &in, NetworkInfo &info) {
636 info.networkId = i["NetworkId"].value<NetworkId>();
637 info.networkName = i["NetworkName"].toString();
638 info.identity = i["Identity"].value<IdentityId>();
639 info.codecForEncoding = i["CodecForEncoding"].toByteArray();
640 info.codecForDecoding = i["CodecForDecoding"].toByteArray();
641 info.serverList = i["ServerList"].toList();