Merging NetworkConnection into CoreNetwork.
[quassel.git] / src / common / network.cpp
index c020b69..df2a837 100644 (file)
 QTextCodec *Network::_defaultCodecForServer = 0;
 QTextCodec *Network::_defaultCodecForEncoding = 0;
 QTextCodec *Network::_defaultCodecForDecoding = 0;
+
+Network::Server Network::Server::fromVariant(const QVariant &variant) {
+  QVariantMap serverMap = variant.toMap();
+  return Server(serverMap["Host"].toString(), serverMap["Port"].toUInt(), serverMap["Password"].toString(), serverMap["UseSSL"].toBool());
+}
+
+QVariant Network::Server::toVariant() const {
+  QVariantMap serverMap;
+  serverMap["Host"] = host;
+  serverMap["Port"] = port;
+  serverMap["Password"] = password;
+  serverMap["UseSSL"] = useSsl;
+  return QVariant::fromValue<QVariantMap>(serverMap);
+}
+
 // ====================
 //  Public:
 // ====================
@@ -79,7 +94,7 @@ NetworkInfo Network::networkInfo() const {
   info.codecForServer = codecForServer();
   info.codecForEncoding = codecForEncoding();
   info.codecForDecoding = codecForDecoding();
-  info.serverList = serverList();
+  info.serverList = variantServerList();
   info.useRandomServer = useRandomServer();
   info.perform = perform();
   info.useAutoIdentify = useAutoIdentify();
@@ -137,6 +152,16 @@ QStringList Network::nicks() const {
   return nicks;
 }
 
+QVariantList Network::variantServerList() const {
+  QVariantList servers;
+  ServerList::const_iterator serverIter = _serverList.constBegin();
+  while(serverIter != _serverList.constEnd()) {
+    servers << serverIter->toVariant();
+    serverIter++;
+  }
+  return servers;
+}
+
 QString Network::prefixes() {
   if(_prefixes.isNull())
     determinePrefixes();
@@ -186,7 +211,7 @@ IrcUser *Network::newIrcUser(const QString &hostmask, const QVariantMap &initDat
   QString nick(nickFromMask(hostmask).toLower());
   if(!_ircUsers.contains(nick)) {
     IrcUser *ircuser = new IrcUser(hostmask, this);
-    if(initData.isEmpty()) {
+    if(!initData.isEmpty()) {
       ircuser->fromVariantMap(initData);
       ircuser->setInitialized();
     }
@@ -197,34 +222,22 @@ IrcUser *Network::newIrcUser(const QString &hostmask, const QVariantMap &initDat
       qWarning() << "unable to synchronize new IrcUser" << hostmask << "forgot to call Network::setProxy(SignalProxy *)?";
 
     connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
-    connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
-    if(!ircuser->isInitialized())
-      connect(ircuser, SIGNAL(initDone()), this, SLOT(ircUserInitDone()));
 
     _ircUsers[nick] = ircuser;
 
     emit ircUserAdded(hostmask);
     emit ircUserAdded(ircuser);
-    if(ircuser->isInitialized())
-      emit ircUserInitDone(ircuser);
   }
 
   return _ircUsers[nick];
 }
 
-void Network::ircUserDestroyed() {
-  IrcUser *ircUser = static_cast<IrcUser *>(sender());
-  if(!ircUser)
-    return;
-
-  QHash<QString, IrcUser *>::iterator ircUserIter = _ircUsers.begin();
-  while(ircUserIter != _ircUsers.end()) {
-    if(ircUser == *ircUserIter) {
-      ircUserIter = _ircUsers.erase(ircUserIter);
-      break;
-    }
-    ircUserIter++;
-  }
+IrcUser *Network::ircUser(QString nickname) const {
+  nickname = nickname.toLower();
+  if(_ircUsers.contains(nickname))
+    return _ircUsers[nickname];
+  else
+    return 0;
 }
 
 void Network::removeIrcUser(IrcUser *ircuser) {
@@ -234,40 +247,47 @@ void Network::removeIrcUser(IrcUser *ircuser) {
 
   _ircUsers.remove(nick);
   disconnect(ircuser, 0, this, 0);
-  emit ircUserRemoved(nick);
-  emit ircUserRemoved(ircuser);
   ircuser->deleteLater();
 }
 
-void Network::removeIrcUser(const QString &nick) {
-  IrcUser *ircuser;
-  if((ircuser = ircUser(nick)) != 0)
-    removeIrcUser(ircuser);
+void Network::removeIrcChannel(IrcChannel *channel) {
+  QString chanName = _ircChannels.key(channel);
+  if(chanName.isNull())
+    return;
+
+  _ircChannels.remove(chanName);
+  disconnect(channel, 0, this, 0);
+  channel->deleteLater();
 }
 
 void Network::removeChansAndUsers() {
   QList<IrcUser *> users = ircUsers();
-  foreach(IrcUser *user, users) {
-    removeIrcUser(user);
-  }
+  _ircUsers.clear();
   QList<IrcChannel *> channels = ircChannels();
+  _ircChannels.clear();
+
   foreach(IrcChannel *channel, channels) {
-    removeIrcChannel(channel);
+    proxy()->detachObject(channel);
+    disconnect(channel, 0, this, 0);
+  }
+  foreach(IrcUser *user, users) {
+    proxy()->detachObject(user);
+    disconnect(user, 0, this, 0);
   }
-}
 
-IrcUser *Network::ircUser(QString nickname) const {
-  nickname = nickname.toLower();
-  if(_ircUsers.contains(nickname))
-    return _ircUsers[nickname];
-  else
-    return 0;
+  // the second loop is needed because quit can have sideffects
+  foreach(IrcUser *user, users) {
+    user->quit();
+  }
+
+  qDeleteAll(users);
+  qDeleteAll(channels);
 }
 
 IrcChannel *Network::newIrcChannel(const QString &channelname, const QVariantMap &initData) {
   if(!_ircChannels.contains(channelname.toLower())) {
     IrcChannel *channel = ircChannelFactory(channelname);
-    if(initData.isEmpty()) {
+    if(!initData.isEmpty()) {
       channel->fromVariantMap(initData);
       channel->setInitialized();
     }
@@ -277,16 +297,10 @@ IrcChannel *Network::newIrcChannel(const QString &channelname, const QVariantMap
     else
       qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
 
-    connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
-    if(!channel->isInitialized())
-      connect(channel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
-
     _ircChannels[channelname.toLower()] = channel;
 
     emit ircChannelAdded(channelname);
     emit ircChannelAdded(channel);
-    if(channel->isInitialized())
-      emit ircChannelInitDone(channel);
   }
   return _ircChannels[channelname.toLower()];
 }
@@ -463,7 +477,10 @@ void Network::setIdentity(IdentityId id) {
 }
 
 void Network::setServerList(const QVariantList &serverList) {
-  _serverList = serverList;
+  _serverList.clear();
+  foreach(QVariant variant, serverList) {
+    _serverList << Server::fromVariant(variant);
+  }
   emit serverListSet(serverList);
 }
 
@@ -587,7 +604,6 @@ void Network::initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels) {
     newIrcChannel(channelIter.key(), channelIter.value().toMap());
     channelIter++;
   }
-
 }
 
 void Network::initSetSupports(const QVariantMap &supports) {
@@ -598,6 +614,12 @@ void Network::initSetSupports(const QVariantMap &supports) {
   }
 }
 
+void Network::initSetServerList(const QVariantList &serverList) {
+  foreach(QVariant variant, serverList) {
+    _serverList << Server::fromVariant(variant);
+  }
+}
+
 IrcUser *Network::updateNickFromMask(const QString &mask) {
   QString nick(nickFromMask(mask).toLower());
   IrcUser *ircuser;
@@ -623,45 +645,6 @@ void Network::ircUserNickChanged(QString newnick) {
     setMyNick(newnick);
 }
 
-void Network::ircUserInitDone() {
-  IrcUser *ircuser = static_cast<IrcUser *>(sender());
-  Q_ASSERT(ircuser);
-  connect(ircuser, SIGNAL(initDone()), this, SLOT(ircUserInitDone()));
-  emit ircUserInitDone(ircuser);
-}
-
-void Network::ircChannelInitDone() {
-  IrcChannel *ircChannel = static_cast<IrcChannel *>(sender());
-  Q_ASSERT(ircChannel);
-  disconnect(ircChannel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
-  emit ircChannelInitDone(ircChannel);
-}
-
-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);
-  _ircChannels.remove(_ircChannels.key(channel));
-  emit ircChannelRemoved(channel);
-}
-
 void Network::emitConnectionError(const QString &errorMsg) {
   emit connectionError(errorMsg);
 }