Networks can now be removed even when they're connected.
[quassel.git] / src / common / network.cpp
index f1cff6c..0d367af 100644 (file)
@@ -38,6 +38,7 @@ Network::Network(const NetworkId &networkid, QObject *parent) : SyncableObject(p
     _networkName(QString("<not initialized>")),
     _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<QString, IrcUser *> ircuser(_ircUsers);
-  while (ircuser.hasNext()) {
-    ircuser.next();
-    delete ircuser.value();
-  }
+  emit aboutToBeDestroyed();
+//  QHashIterator<QString, IrcUser *> 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<QVariantMap> 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<IrcUser *> users = ircUsers();
+  foreach(IrcUser *user, users) {
+    removeIrcUser(user);
+  }
+  QList<IrcChannel *> 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 &currentServer) {
 
 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<QVariantMap> &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<QVariant> 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<QVariantMap> slist;
-  foreach(QVariant v, serverList) slist << v.toMap();
-  setServerList(slist);
+  setServerList(serverList);
 }
 
 void Network::initSetIrcUsers(const QStringList &hostmasks) {
@@ -484,23 +511,57 @@ void Network::ircChannelInitDone() {
   emit ircChannelInitDone(ircchannel);
 }
 
-void Network::ircUserDestroyed() {
-  IrcUser *ircuser = static_cast<IrcUser *>(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<IrcChannel *>(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);
 }
 
 // ====================
@@ -540,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<NetworkId>(info.networkId);
+  i["NetworkName"] = info.networkName;
+  i["Identity"] = QVariant::fromValue<IdentityId>(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<NetworkId>();
+  info.networkName = i["NetworkName"].toString();
+  info.identity = i["Identity"].value<IdentityId>();
+  info.codecForEncoding = i["CodecForEncoding"].toByteArray();
+  info.codecForDecoding = i["CodecForDecoding"].toByteArray();
+  info.serverList = i["ServerList"].toList();
+  return in;
+}