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 emit aboutToBeDestroyed();
55 // QHashIterator<QString, IrcUser *> ircuser(_ircUsers);
56 // while (ircuser.hasNext()) {
58 // delete ircuser.value();
60 // qDebug() << "Destroying net" << networkName() << networkId();
64 NetworkId Network::networkId() const {
68 SignalProxy *Network::proxy() const {
72 void Network::setProxy(SignalProxy *proxy) {
74 //proxy->synchronize(this); // we should to this explicitly from the outside!
77 bool Network::isMyNick(const QString &nick) const {
78 return (myNick().toLower() == nick.toLower());
81 bool Network::isMe(IrcUser *ircuser) const {
82 return (ircuser->nick().toLower() == myNick().toLower());
85 bool Network::isChannelName(const QString &channelname) const {
86 if(channelname.isEmpty())
89 if(supports("CHANTYPES"))
90 return support("CHANTYPES").contains(channelname[0]);
92 return QString("#&!+").contains(channelname[0]);
95 bool Network::isConnected() const {
99 //Network::ConnectionState Network::connectionState() const {
100 int Network::connectionState() const {
101 return _connectionState;
104 NetworkInfo Network::networkInfo() const {
106 info.networkName = networkName();
107 info.networkId = networkId();
108 info.identity = identity();
109 info.codecForEncoding = codecForEncoding();
110 info.codecForDecoding = codecForDecoding();
111 info.serverList = serverList();
115 void Network::setNetworkInfo(const NetworkInfo &info) {
116 // we don't set our ID!
117 if(!info.networkName.isEmpty() && info.networkName != networkName()) setNetworkName(info.networkName);
118 if(info.identity > 0 && info.identity != identity()) setIdentity(info.identity);
119 if(!info.codecForEncoding.isEmpty() && info.codecForEncoding != codecForEncoding())
120 setCodecForEncoding(QTextCodec::codecForName(info.codecForEncoding));
121 if(!info.codecForDecoding.isEmpty() && info.codecForDecoding != codecForDecoding())
122 setCodecForDecoding(QTextCodec::codecForName(info.codecForDecoding));
123 if(info.serverList.count()) setServerList(info.serverList); // FIXME compare components
126 QString Network::prefixToMode(const QString &prefix) {
127 if(prefixes().contains(prefix))
128 return QString(prefixModes()[prefixes().indexOf(prefix)]);
133 QString Network::prefixToMode(const QCharRef &prefix) {
134 return prefixToMode(QString(prefix));
137 QString Network::modeToPrefix(const QString &mode) {
138 if(prefixModes().contains(mode))
139 return QString(prefixes()[prefixModes().indexOf(mode)]);
144 QString Network::modeToPrefix(const QCharRef &mode) {
145 return modeToPrefix(QString(mode));
148 QString Network::networkName() const {
152 QString Network::currentServer() const {
153 return _currentServer;
156 QString Network::myNick() const {
160 IdentityId Network::identity() const {
164 QStringList Network::nicks() const {
165 // we don't use _ircUsers.keys() since the keys may be
166 // not up to date after a nick change
168 foreach(IrcUser *ircuser, _ircUsers.values()) {
169 nicks << ircuser->nick();
174 QStringList Network::channels() const {
175 return _ircChannels.keys();
178 QVariantList Network::serverList() const {
182 QString Network::prefixes() {
183 if(_prefixes.isNull())
189 QString Network::prefixModes() {
190 if(_prefixModes.isNull())
196 bool Network::supports(const QString ¶m) const {
197 return _supports.contains(param);
200 QString Network::support(const QString ¶m) const {
201 QString support_ = param.toUpper();
202 if(_supports.contains(support_))
203 return _supports[support_];
208 IrcUser *Network::newIrcUser(const QString &hostmask) {
209 QString nick(nickFromMask(hostmask).toLower());
210 if(!_ircUsers.contains(nick)) {
211 IrcUser *ircuser = new IrcUser(hostmask, this);
212 // mark IrcUser as already initialized to keep the SignalProxy from requesting initData
213 //if(isInitialized())
214 // ircuser->setInitialized();
216 proxy()->synchronize(ircuser);
218 qWarning() << "unable to synchronize new IrcUser" << hostmask << "forgot to call Network::setProxy(SignalProxy *)?";
220 connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
221 connect(ircuser, SIGNAL(initDone()), this, SLOT(ircUserInitDone()));
222 _ircUsers[nick] = ircuser;
223 emit ircUserAdded(hostmask);
224 emit ircUserAdded(ircuser);
226 return _ircUsers[nick];
229 IrcUser *Network::newIrcUser(const QByteArray &hostmask) {
230 return newIrcUser(decodeString(hostmask));
233 void Network::removeIrcUser(IrcUser *ircuser) {
234 QString nick = _ircUsers.key(ircuser);
238 _ircUsers.remove(nick);
239 disconnect(ircuser, 0, this, 0);
240 emit ircUserRemoved(nick);
241 emit ircUserRemoved(ircuser);
242 ircuser->deleteLater();
245 void Network::removeChansAndUsers() {
246 QList<IrcUser *> users = ircUsers();
247 foreach(IrcUser *user, users) {
250 QList<IrcChannel *> channels = ircChannels();
251 foreach(IrcChannel *channel, channels) {
252 removeIrcChannel(channel);
256 void Network::removeIrcUser(const QString &nick) {
258 if((ircuser = ircUser(nick)) != 0)
259 removeIrcUser(ircuser);
262 IrcUser *Network::ircUser(QString nickname) const {
263 nickname = nickname.toLower();
264 if(_ircUsers.contains(nickname))
265 return _ircUsers[nickname];
270 IrcUser *Network::ircUser(const QByteArray &nickname) const {
271 return ircUser(decodeString(nickname));
274 QList<IrcUser *> Network::ircUsers() const {
275 return _ircUsers.values();
278 quint32 Network::ircUserCount() const {
279 return _ircUsers.count();
282 IrcChannel *Network::newIrcChannel(const QString &channelname) {
283 if(!_ircChannels.contains(channelname.toLower())) {
284 IrcChannel *channel = new IrcChannel(channelname, this);
285 // mark IrcUser as already initialized to keep the SignalProxy from requesting initData
286 //if(isInitialized())
287 // channel->setInitialized();
290 proxy()->synchronize(channel);
292 qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
294 connect(channel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
295 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
296 _ircChannels[channelname.toLower()] = channel;
297 emit ircChannelAdded(channelname);
298 emit ircChannelAdded(channel);
300 return _ircChannels[channelname.toLower()];
303 IrcChannel *Network::newIrcChannel(const QByteArray &channelname) {
304 return newIrcChannel(decodeString(channelname));
307 IrcChannel *Network::ircChannel(QString channelname) const {
308 channelname = channelname.toLower();
309 if(_ircChannels.contains(channelname))
310 return _ircChannels[channelname];
315 IrcChannel *Network::ircChannel(const QByteArray &channelname) const {
316 return ircChannel(decodeString(channelname));
320 QList<IrcChannel *> Network::ircChannels() const {
321 return _ircChannels.values();
324 quint32 Network::ircChannelCount() const {
325 return _ircChannels.count();
328 QByteArray Network::codecForEncoding() const {
329 if(_codecForEncoding) return _codecForEncoding->name();
333 void Network::setCodecForEncoding(const QByteArray &name) {
334 setCodecForEncoding(QTextCodec::codecForName(name));
337 void Network::setCodecForEncoding(QTextCodec *codec) {
338 _codecForEncoding = codec;
339 emit codecForEncodingSet(codecForEncoding());
342 QByteArray Network::codecForDecoding() const {
343 if(_codecForDecoding) return _codecForDecoding->name();
344 else return QByteArray();
347 void Network::setCodecForDecoding(const QByteArray &name) {
348 setCodecForDecoding(QTextCodec::codecForName(name));
351 void Network::setCodecForDecoding(QTextCodec *codec) {
352 _codecForDecoding = codec;
353 emit codecForDecodingSet(codecForDecoding());
356 QString Network::decodeString(const QByteArray &text) const {
357 return ::decodeString(text, _codecForDecoding);
360 QByteArray Network::encodeString(const QString string) const {
361 if(_codecForEncoding) {
362 return _codecForEncoding->fromUnicode(string);
364 return string.toAscii();
367 // ====================
369 // ====================
370 void Network::setNetworkName(const QString &networkName) {
371 _networkName = networkName;
372 emit networkNameSet(networkName);
375 void Network::setCurrentServer(const QString ¤tServer) {
376 _currentServer = currentServer;
377 emit currentServerSet(currentServer);
380 void Network::setConnected(bool connected) {
381 _connected = connected;
383 removeChansAndUsers();
384 emit connectedSet(connected);
387 //void Network::setConnectionState(ConnectionState state) {
388 void Network::setConnectionState(int state) {
389 _connectionState = (ConnectionState)state;
390 //qDebug() << "netstate" << networkId() << networkName() << state;
391 emit connectionStateSet(state);
392 emit connectionStateSet(_connectionState);
395 void Network::setMyNick(const QString &nickname) {
397 emit myNickSet(nickname);
400 void Network::setIdentity(IdentityId id) {
402 emit identitySet(id);
405 void Network::setServerList(const QVariantList &serverList) {
406 _serverList = serverList;
407 emit serverListSet(serverList);
410 void Network::addSupport(const QString ¶m, const QString &value) {
411 if(!_supports.contains(param)) {
412 _supports[param] = value;
413 emit supportAdded(param, value);
417 void Network::removeSupport(const QString ¶m) {
418 if(_supports.contains(param)) {
419 _supports.remove(param);
420 emit supportRemoved(param);
424 QVariantMap Network::initSupports() const {
425 QVariantMap supports;
426 QHashIterator<QString, QString> iter(_supports);
427 while(iter.hasNext()) {
429 supports[iter.key()] = iter.value();
434 QVariantList Network::initServerList() const {
438 QStringList Network::initIrcUsers() const {
439 QStringList hostmasks;
440 foreach(IrcUser *ircuser, ircUsers()) {
441 hostmasks << ircuser->hostmask();
446 QStringList Network::initIrcChannels() const {
447 return _ircChannels.keys();
450 void Network::initSetSupports(const QVariantMap &supports) {
451 QMapIterator<QString, QVariant> iter(supports);
452 while(iter.hasNext()) {
454 addSupport(iter.key(), iter.value().toString());
458 void Network::initSetServerList(const QVariantList & serverList) {
459 setServerList(serverList);
462 void Network::initSetIrcUsers(const QStringList &hostmasks) {
463 if(!_ircUsers.empty())
465 foreach(QString hostmask, hostmasks) {
466 newIrcUser(hostmask);
470 void Network::initSetChannels(const QStringList &channels) {
471 if(!_ircChannels.empty())
473 foreach(QString channel, channels)
474 newIrcChannel(channel);
477 IrcUser *Network::updateNickFromMask(const QString &mask) {
478 QString nick(nickFromMask(mask).toLower());
481 if(_ircUsers.contains(nick)) {
482 ircuser = _ircUsers[nick];
483 ircuser->updateHostmask(mask);
485 ircuser = newIrcUser(mask);
490 void Network::ircUserNickChanged(QString newnick) {
491 QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
496 if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
498 if(myNick().toLower() == oldnick)
502 void Network::ircUserInitDone() {
503 IrcUser *ircuser = static_cast<IrcUser *>(sender());
505 emit ircUserInitDone(ircuser);
508 void Network::ircChannelInitDone() {
509 IrcChannel *ircchannel = static_cast<IrcChannel *>(sender());
510 Q_ASSERT(ircchannel);
511 emit ircChannelInitDone(ircchannel);
514 void Network::removeIrcChannel(IrcChannel *channel) {
515 QString chanName = _ircChannels.key(channel);
516 if(chanName.isNull())
519 _ircChannels.remove(chanName);
520 disconnect(channel, 0, this, 0);
521 emit ircChannelRemoved(chanName);
522 emit ircChannelRemoved(channel);
523 channel->deleteLater();
526 void Network::removeIrcChannel(const QString &channel) {
528 if((chan = ircChannel(channel)) != 0)
529 removeIrcChannel(chan);
532 void Network::channelDestroyed() {
533 IrcChannel *channel = static_cast<IrcChannel *>(sender());
535 _ircChannels.remove(_ircChannels.key(channel));
536 emit ircChannelRemoved(channel);
539 void Network::requestConnect() const {
541 if(proxy()->proxyMode() == SignalProxy::Client) emit connectRequested(); // on the client this triggers calling this slot on the core
543 if(connectionState() != Disconnected) {
544 qWarning() << "Requesting connect while not being disconnected!";
547 emit connectRequested(networkId()); // and this is for CoreSession :)
551 void Network::requestDisconnect() const {
553 if(proxy()->proxyMode() == SignalProxy::Client) emit disconnectRequested(); // on the client this triggers calling this slot on the core
555 if(connectionState() == Disconnected) {
556 qWarning() << "Requesting disconnect while not being connected!";
559 emit disconnectRequested(networkId()); // and this is for CoreSession :)
563 void Network::emitConnectionError(const QString &errorMsg) {
564 emit connectionError(errorMsg);
567 // ====================
569 // ====================
570 void Network::determinePrefixes() {
571 // seems like we have to construct them first
572 QString PREFIX = support("PREFIX");
574 if(PREFIX.startsWith("(") && PREFIX.contains(")")) {
575 _prefixes = PREFIX.section(")", 1);
576 _prefixModes = PREFIX.mid(1).section(")", 0, 0);
578 QString defaultPrefixes("~&@%+");
579 QString defaultPrefixModes("qaohv");
581 // we just assume that in PREFIX are only prefix chars stored
582 for(int i = 0; i < defaultPrefixes.size(); i++) {
583 if(PREFIX.contains(defaultPrefixes[i])) {
584 _prefixes += defaultPrefixes[i];
585 _prefixModes += defaultPrefixModes[i];
589 if(!_prefixes.isNull())
592 // well... our assumption was obviously wrong...
593 // check if it's only prefix modes
594 for(int i = 0; i < defaultPrefixes.size(); i++) {
595 if(PREFIX.contains(defaultPrefixModes[i])) {
596 _prefixes += defaultPrefixes[i];
597 _prefixModes += defaultPrefixModes[i];
600 // now we've done all we've could...
604 /************************************************************************
606 ************************************************************************/
608 bool NetworkInfo::operator==(const NetworkInfo &other) const {
609 if(networkId != other.networkId) return false;
610 if(networkName != other.networkName) return false;
611 if(identity != other.identity) return false;
612 if(codecForEncoding != other.codecForEncoding) return false;
613 if(codecForDecoding != other.codecForDecoding) return false;
614 if(serverList != other.serverList) return false;
618 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
619 return !(*this == other);
622 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) {
624 i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
625 i["NetworkName"] = info.networkName;
626 i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
627 i["CodecForEncoding"] = info.codecForEncoding;
628 i["CodecForDecoding"] = info.codecForDecoding;
629 i["ServerList"] = info.serverList;
634 QDataStream &operator>>(QDataStream &in, NetworkInfo &info) {
637 info.networkId = i["NetworkId"].value<NetworkId>();
638 info.networkName = i["NetworkName"].toString();
639 info.identity = i["Identity"].value<IdentityId>();
640 info.codecForEncoding = i["CodecForEncoding"].toByteArray();
641 info.codecForDecoding = i["CodecForDecoding"].toByteArray();
642 info.serverList = i["ServerList"].toList();