fixing a bug that caused the client to request init data for ircUsers and ircChannel...
[quassel.git] / src / common / network.cpp
index c813ebb..23ce589 100644 (file)
@@ -36,6 +36,7 @@ Network::Network(const NetworkId &networkid, QObject *parent)
     _networkId(networkid),
     _identity(0),
     _myNick(QString()),
+    _latency(0),
     _networkName(QString("<not initialized>")),
     _currentServer(QString()),
     _connected(false),
@@ -50,7 +51,8 @@ Network::Network(const NetworkId &networkid, QObject *parent)
     _unlimitedReconnectRetries(false),
     _codecForServer(0),
     _codecForEncoding(0),
-    _codecForDecoding(0)
+    _codecForDecoding(0),
+    _autoAwayActive(false)
 {
   setObjectName(QString::number(networkid.toInt()));
 }
@@ -62,7 +64,7 @@ Network::~Network() {
 bool Network::isChannelName(const QString &channelname) const {
   if(channelname.isEmpty())
     return false;
-  
+
   if(supports("CHANTYPES"))
     return support("CHANTYPES").contains(channelname[0]);
   else
@@ -138,7 +140,7 @@ QStringList Network::nicks() const {
 QString Network::prefixes() {
   if(_prefixes.isNull())
     determinePrefixes();
-  
+
   return _prefixes;
 }
 
@@ -149,7 +151,7 @@ QString Network::prefixModes() {
   return _prefixModes;
 }
 
-// example Unreal IRCD: CHANMODES=beI,kfL,lj,psmntirRcOAQKVCuzNSMTG 
+// example Unreal IRCD: CHANMODES=beI,kfL,lj,psmntirRcOAQKVCuzNSMTG
 Network::ChannelModeType Network::channelModeType(const QString &mode) {
   if(mode.isEmpty())
     return NOT_A_CHANMODE;
@@ -180,23 +182,33 @@ QString Network::support(const QString &param) const {
     return QString();
 }
 
-IrcUser *Network::newIrcUser(const QString &hostmask) {
+IrcUser *Network::newIrcUser(const QString &hostmask, const QVariantMap &initData) {
   QString nick(nickFromMask(hostmask).toLower());
   if(!_ircUsers.contains(nick)) {
     IrcUser *ircuser = new IrcUser(hostmask, this);
+    if(!initData.isEmpty()) {
+      ircuser->fromVariantMap(initData);
+      ircuser->setInitialized();
+    }
 
     if(proxy())
       proxy()->synchronize(ircuser);
     else
       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(initDone()), this, SLOT(ircUserInitDone()));
     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];
 }
 
@@ -252,20 +264,29 @@ IrcUser *Network::ircUser(QString nickname) const {
     return 0;
 }
 
-IrcChannel *Network::newIrcChannel(const QString &channelname) {
+IrcChannel *Network::newIrcChannel(const QString &channelname, const QVariantMap &initData) {
   if(!_ircChannels.contains(channelname.toLower())) {
-    IrcChannel *channel = new IrcChannel(channelname, this);
+    IrcChannel *channel = ircChannelFactory(channelname);
+    if(!initData.isEmpty()) {
+      channel->fromVariantMap(initData);
+      channel->setInitialized();
+    }
 
     if(proxy())
       proxy()->synchronize(channel);
     else
       qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
 
-    connect(channel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
     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()];
 }
@@ -403,7 +424,7 @@ void Network::setCurrentServer(const QString &currentServer) {
 void Network::setConnected(bool connected) {
   if(_connected == connected)
     return;
-  
+
   _connected = connected;
   if(!connected) {
     setMyNick(QString());
@@ -429,6 +450,13 @@ void Network::setMyNick(const QString &nickname) {
   emit myNickSet(nickname);
 }
 
+void Network::setLatency(int latency) {
+  if(_latency == latency)
+    return;
+  _latency = latency;
+  emit latencySet(latency);
+}
+
 void Network::setIdentity(IdentityId id) {
   _identity = id;
   emit identitySet(id);
@@ -529,7 +557,7 @@ QVariantMap Network::initIrcUsersAndChannels() const {
   QHash<QString, IrcChannel *>::const_iterator channelIter = _ircChannels.constBegin();
   QHash<QString, IrcChannel *>::const_iterator channelIterEnd = _ircChannels.constEnd();
   while(channelIter != channelIterEnd) {
-    channels[channelIter.key()] = channelIter.value()->toVariantMap();
+    channels[channelIter.value()->name()] = channelIter.value()->toVariantMap();
     channelIter++;
   }
   usersAndChannels["channels"] = channels;
@@ -539,60 +567,27 @@ QVariantMap Network::initIrcUsersAndChannels() const {
 
 void Network::initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels) {
   Q_ASSERT(proxy());
-  if(!_ircUsers.isEmpty() || !_ircChannels.isEmpty()) {
+  if(isInitialized()) {
     qWarning() << "Network" << networkId() << "received init data for users and channels allthough there allready are known users or channels!";
     return;
   }
-    
-  QVariantMap users = usersAndChannels.value("users").toMap();
 
+  QVariantMap users = usersAndChannels.value("users").toMap();
   QVariantMap::const_iterator userIter = users.constBegin();
   QVariantMap::const_iterator userIterEnd = users.constEnd();
-  IrcUser *ircUser = 0;
-  QString hostmask;
   while(userIter != userIterEnd) {
-    hostmask = userIter.key();
-    ircUser = new IrcUser(hostmask, this);
-    ircUser->fromVariantMap(userIter.value().toMap());
-    ircUser->setInitialized();
-    proxy()->synchronize(ircUser);
-
-    connect(ircUser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
-    connect(ircUser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
-
-    _ircUsers[nickFromMask(hostmask).toLower()] = ircUser;
-
-    emit ircUserAdded(hostmask);
-    emit ircUserAdded(ircUser);
-    emit ircUserInitDone(ircUser);
-
+    newIrcUser(userIter.key(), userIter.value().toMap());
     userIter++;
   }
 
-
   QVariantMap channels = usersAndChannels.value("channels").toMap();
   QVariantMap::const_iterator channelIter = channels.constBegin();
   QVariantMap::const_iterator channelIterEnd = channels.constEnd();
-  IrcChannel *ircChannel = 0;
-  QString channelName;
-
   while(channelIter != channelIterEnd) {
-    channelName = channelIter.key();
-    ircChannel = new IrcChannel(channelName, this);
-    ircChannel->fromVariantMap(channelIter.value().toMap());
-    ircChannel->setInitialized();
-    proxy()->synchronize(ircChannel);
-      
-    connect(ircChannel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
-    _ircChannels[channelName.toLower()] = ircChannel;
-
-    emit ircChannelAdded(channelName);
-    emit ircChannelAdded(ircChannel);
-    emit ircChannelInitDone(ircChannel);
-
+    newIrcChannel(channelIter.key(), channelIter.value().toMap());
     channelIter++;
   }
-  
+
 }
 
 void Network::initSetSupports(const QVariantMap &supports) {
@@ -606,7 +601,7 @@ void Network::initSetSupports(const QVariantMap &supports) {
 IrcUser *Network::updateNickFromMask(const QString &mask) {
   QString nick(nickFromMask(mask).toLower());
   IrcUser *ircuser;
-  
+
   if(_ircUsers.contains(nick)) {
     ircuser = _ircUsers[nick];
     ircuser->updateHostmask(mask);
@@ -646,7 +641,7 @@ 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);
@@ -676,16 +671,16 @@ void Network::emitConnectionError(const QString &errorMsg) {
 // ====================
 void Network::determinePrefixes() {
   // seems like we have to construct them first
-  QString PREFIX = support("PREFIX");
-  
-  if(PREFIX.startsWith("(") && PREFIX.contains(")")) {
-    _prefixes = PREFIX.section(")", 1);
-    _prefixModes = PREFIX.mid(1).section(")", 0, 0);
+  QString prefix = support("PREFIX");
+
+  if(prefix.startsWith("(") && prefix.contains(")")) {
+    _prefixes = prefix.section(")", 1);
+    _prefixModes = prefix.mid(1).section(")", 0, 0);
   } else {
     QString defaultPrefixes("~&@%+");
     QString defaultPrefixModes("qaohv");
 
-    if(PREFIX.isEmpty()) {
+    if(prefix.isEmpty()) {
       _prefixes = defaultPrefixes;
       _prefixModes = defaultPrefixModes;
       return;
@@ -693,7 +688,7 @@ void Network::determinePrefixes() {
 
     // we just assume that in PREFIX are only prefix chars stored
     for(int i = 0; i < defaultPrefixes.size(); i++) {
-      if(PREFIX.contains(defaultPrefixes[i])) {
+      if(prefix.contains(defaultPrefixes[i])) {
        _prefixes += defaultPrefixes[i];
        _prefixModes += defaultPrefixModes[i];
       }
@@ -701,11 +696,11 @@ void Network::determinePrefixes() {
     // check for success
     if(!_prefixes.isNull())
       return;
-    
+
     // well... our assumption was obviously wrong...
     // check if it's only prefix modes
     for(int i = 0; i < defaultPrefixes.size(); i++) {
-      if(PREFIX.contains(defaultPrefixModes[i])) {
+      if(prefix.contains(defaultPrefixModes[i])) {
        _prefixes += defaultPrefixes[i];
        _prefixModes += defaultPrefixModes[i];
       }