X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fnetwork.cpp;h=0d367af292ef99884cc35583b58ff07439957c66;hp=e899969a217a0bce4ef19daa75dbfccb876a8914;hb=5b083e1f67c77ceb7d1d134e976cd6081b70bf12;hpb=59912f14782c193a2394a2b0d044902a59c96870 diff --git a/src/common/network.cpp b/src/common/network.cpp index e899969a..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,15 +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(); - } + emit aboutToBeDestroyed(); +// QHashIterator ircuser(_ircUsers); +// while (ircuser.hasNext()) { +// ircuser.next(); +// delete ircuser.value(); +// } +// qDebug() << "Destroying net" << networkName() << networkId(); } -*/ + NetworkId Network::networkId() const { return _networkId; @@ -93,6 +96,11 @@ 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(); @@ -106,11 +114,13 @@ NetworkInfo Network::networkInfo() const { void Network::setNetworkInfo(const NetworkInfo &info) { // we don't set our ID! - if(!info.networkName.isEmpty()) setNetworkName(info.networkName); - if(info.identity > 0) setIdentity(info.identity); - if(!info.codecForEncoding.isEmpty()) setCodecForEncoding(QTextCodec::codecForName(info.codecForEncoding)); - if(!info.codecForDecoding.isEmpty()) setCodecForDecoding(QTextCodec::codecForName(info.codecForDecoding)); - if(info.serverList.count()) setServerList(info.serverList); + 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) { @@ -165,7 +175,7 @@ QStringList Network::channels() const { return _ircChannels.keys(); } -QList Network::serverList() const { +QVariantList Network::serverList() const { return _serverList; } @@ -209,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); @@ -227,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); @@ -358,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); @@ -371,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); } @@ -401,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 { @@ -427,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) { @@ -484,29 +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() { +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 emit disconnectRequested(networkId()); // and this is for CoreSession :) + 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); } // ==================== @@ -563,3 +618,27 @@ bool NetworkInfo::operator==(const NetworkInfo &other) const { 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; +}