X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fnetwork.cpp;h=0d367af292ef99884cc35583b58ff07439957c66;hp=982011becaa5c5ce711243f6d6b658bcd3550769;hb=5b083e1f67c77ceb7d1d134e976cd6081b70bf12;hpb=6f2f1723f5bb3d26908f6dd297890f6fba43793b diff --git a/src/common/network.cpp b/src/common/network.cpp index 982011be..0d367af2 100644 --- a/src/common/network.cpp +++ b/src/common/network.cpp @@ -38,6 +38,7 @@ Network::Network(const NetworkId &networkid, QObject *parent) : SyncableObject(p _networkName(QString("")), _currentServer(QString()), _connected(false), + _connectionState(Disconnected), _prefixes(QString()), _prefixModes(QString()), _proxy(0), @@ -48,13 +49,17 @@ Network::Network(const NetworkId &networkid, QObject *parent) : SyncableObject(p } // I think this is unnecessary since IrcUsers have us as their daddy :) -//Network::~Network() { -// QHashIterator ircuser(_ircUsers); -// while (ircuser.hasNext()) { -// ircuser.next(); -// delete ircuser.value(); -// } -//} + +Network::~Network() { + emit aboutToBeDestroyed(); +// QHashIterator ircuser(_ircUsers); +// while (ircuser.hasNext()) { +// ircuser.next(); +// delete ircuser.value(); +// } +// qDebug() << "Destroying net" << networkName() << networkId(); +} + NetworkId Network::networkId() const { return _networkId; @@ -91,6 +96,33 @@ bool Network::isConnected() const { return _connected; } +//Network::ConnectionState Network::connectionState() const { +int Network::connectionState() const { + return _connectionState; +} + +NetworkInfo Network::networkInfo() const { + NetworkInfo info; + info.networkName = networkName(); + info.networkId = networkId(); + info.identity = identity(); + info.codecForEncoding = codecForEncoding(); + info.codecForDecoding = codecForDecoding(); + info.serverList = serverList(); + return info; +} + +void Network::setNetworkInfo(const NetworkInfo &info) { + // we don't set our ID! + if(!info.networkName.isEmpty() && info.networkName != networkName()) setNetworkName(info.networkName); + if(info.identity > 0 && info.identity != identity()) setIdentity(info.identity); + if(!info.codecForEncoding.isEmpty() && info.codecForEncoding != codecForEncoding()) + setCodecForEncoding(QTextCodec::codecForName(info.codecForEncoding)); + if(!info.codecForDecoding.isEmpty() && info.codecForDecoding != codecForDecoding()) + setCodecForDecoding(QTextCodec::codecForName(info.codecForDecoding)); + if(info.serverList.count()) setServerList(info.serverList); // FIXME compare components +} + QString Network::prefixToMode(const QString &prefix) { if(prefixes().contains(prefix)) return QString(prefixModes()[prefixes().indexOf(prefix)]); @@ -143,7 +175,7 @@ QStringList Network::channels() const { return _ircChannels.keys(); } -QList Network::serverList() const { +QVariantList Network::serverList() const { return _serverList; } @@ -187,7 +219,6 @@ IrcUser *Network::newIrcUser(const QString &hostmask) { connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString))); connect(ircuser, SIGNAL(initDone()), this, SLOT(ircUserInitDone())); - connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed())); _ircUsers[nick] = ircuser; emit ircUserAdded(hostmask); emit ircUserAdded(ircuser); @@ -205,12 +236,24 @@ void Network::removeIrcUser(IrcUser *ircuser) { return; _ircUsers.remove(nick); + disconnect(ircuser, 0, this, 0); emit ircUserRemoved(nick); emit ircUserRemoved(ircuser); ircuser->deleteLater(); } -void Network::removeIrcUser(QString nick) { +void Network::removeChansAndUsers() { + QList users = ircUsers(); + foreach(IrcUser *user, users) { + removeIrcUser(user); + } + QList channels = ircChannels(); + foreach(IrcChannel *channel, channels) { + removeIrcChannel(channel); + } +} + +void Network::removeIrcUser(const QString &nick) { IrcUser *ircuser; if((ircuser = ircUser(nick)) != 0) removeIrcUser(ircuser); @@ -261,7 +304,7 @@ IrcChannel *Network::newIrcChannel(const QByteArray &channelname) { return newIrcChannel(decodeString(channelname)); } -IrcChannel *Network::ircChannel(QString channelname) { +IrcChannel *Network::ircChannel(QString channelname) const { channelname = channelname.toLower(); if(_ircChannels.contains(channelname)) return _ircChannels[channelname]; @@ -269,7 +312,7 @@ IrcChannel *Network::ircChannel(QString channelname) { return 0; } -IrcChannel *Network::ircChannel(const QByteArray &channelname) { +IrcChannel *Network::ircChannel(const QByteArray &channelname) const { return ircChannel(decodeString(channelname)); } @@ -293,6 +336,7 @@ void Network::setCodecForEncoding(const QByteArray &name) { void Network::setCodecForEncoding(QTextCodec *codec) { _codecForEncoding = codec; + emit codecForEncodingSet(codecForEncoding()); } QByteArray Network::codecForDecoding() const { @@ -306,6 +350,7 @@ void Network::setCodecForDecoding(const QByteArray &name) { void Network::setCodecForDecoding(QTextCodec *codec) { _codecForDecoding = codec; + emit codecForDecodingSet(codecForDecoding()); } QString Network::decodeString(const QByteArray &text) const { @@ -334,9 +379,19 @@ void Network::setCurrentServer(const QString ¤tServer) { void Network::setConnected(bool connected) { _connected = connected; + if(!connected) + removeChansAndUsers(); emit connectedSet(connected); } +//void Network::setConnectionState(ConnectionState state) { +void Network::setConnectionState(int state) { + _connectionState = (ConnectionState)state; + //qDebug() << "netstate" << networkId() << networkName() << state; + emit connectionStateSet(state); + emit connectionStateSet(_connectionState); +} + void Network::setMyNick(const QString &nickname) { _myNick = nickname; emit myNickSet(nickname); @@ -347,7 +402,7 @@ void Network::setIdentity(IdentityId id) { emit identitySet(id); } -void Network::setServerList(const QList &serverList) { +void Network::setServerList(const QVariantList &serverList) { _serverList = serverList; emit serverListSet(serverList); } @@ -377,9 +432,7 @@ QVariantMap Network::initSupports() const { } QVariantList Network::initServerList() const { - QList list; - foreach(QVariantMap serverdata, serverList()) list << QVariant(serverdata); - return list; + return serverList(); } QStringList Network::initIrcUsers() const { @@ -403,9 +456,7 @@ void Network::initSetSupports(const QVariantMap &supports) { } void Network::initSetServerList(const QVariantList & serverList) { - QList slist; - foreach(QVariant v, serverList) slist << v.toMap(); - setServerList(slist); + setServerList(serverList); } void Network::initSetIrcUsers(const QStringList &hostmasks) { @@ -460,23 +511,57 @@ void Network::ircChannelInitDone() { emit ircChannelInitDone(ircchannel); } -void Network::ircUserDestroyed() { - IrcUser *ircuser = static_cast(sender()); - Q_ASSERT(ircuser); - removeIrcUser(ircuser); +void Network::removeIrcChannel(IrcChannel *channel) { + QString chanName = _ircChannels.key(channel); + if(chanName.isNull()) + return; + + _ircChannels.remove(chanName); + disconnect(channel, 0, this, 0); + emit ircChannelRemoved(chanName); + emit ircChannelRemoved(channel); + channel->deleteLater(); +} + +void Network::removeIrcChannel(const QString &channel) { + IrcChannel *chan; + if((chan = ircChannel(channel)) != 0) + removeIrcChannel(chan); } void Network::channelDestroyed() { IrcChannel *channel = static_cast(sender()); Q_ASSERT(channel); - emit ircChannelRemoved(sender()); _ircChannels.remove(_ircChannels.key(channel)); + emit ircChannelRemoved(channel); } -void Network::requestConnect() { +void Network::requestConnect() const { if(!proxy()) return; if(proxy()->proxyMode() == SignalProxy::Client) emit connectRequested(); // on the client this triggers calling this slot on the core - else emit connectRequested(networkId()); // and this is for CoreSession :) + else { + if(connectionState() != Disconnected) { + qWarning() << "Requesting connect while not being disconnected!"; + return; + } + emit connectRequested(networkId()); // and this is for CoreSession :) + } +} + +void Network::requestDisconnect() const { + if(!proxy()) return; + if(proxy()->proxyMode() == SignalProxy::Client) emit disconnectRequested(); // on the client this triggers calling this slot on the core + else { + if(connectionState() == Disconnected) { + qWarning() << "Requesting disconnect while not being connected!"; + return; + } + emit disconnectRequested(networkId()); // and this is for CoreSession :) + } +} + +void Network::emitConnectionError(const QString &errorMsg) { + emit connectionError(errorMsg); } // ==================== @@ -516,3 +601,44 @@ void Network::determinePrefixes() { } } +/************************************************************************ + * NetworkInfo + ************************************************************************/ + +bool NetworkInfo::operator==(const NetworkInfo &other) const { + if(networkId != other.networkId) return false; + if(networkName != other.networkName) return false; + if(identity != other.identity) return false; + if(codecForEncoding != other.codecForEncoding) return false; + if(codecForDecoding != other.codecForDecoding) return false; + if(serverList != other.serverList) return false; + return true; +} + +bool NetworkInfo::operator!=(const NetworkInfo &other) const { + return !(*this == other); +} + +QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) { + QVariantMap i; + i["NetworkId"] = QVariant::fromValue(info.networkId); + i["NetworkName"] = info.networkName; + i["Identity"] = QVariant::fromValue(info.identity); + i["CodecForEncoding"] = info.codecForEncoding; + i["CodecForDecoding"] = info.codecForDecoding; + i["ServerList"] = info.serverList; + out << i; + return out; +} + +QDataStream &operator>>(QDataStream &in, NetworkInfo &info) { + QVariantMap i; + in >> i; + info.networkId = i["NetworkId"].value(); + info.networkName = i["NetworkName"].toString(); + info.identity = i["Identity"].value(); + info.codecForEncoding = i["CodecForEncoding"].toByteArray(); + info.codecForDecoding = i["CodecForDecoding"].toByteArray(); + info.serverList = i["ServerList"].toList(); + return in; +}