We require cmake-2.6 now. Also LINGUAS should work again...
[quassel.git] / src / common / network.cpp
index 2e10c04..1098b0a 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),
@@ -208,7 +209,6 @@ void Network::ircUserDestroyed() {
   QHash<QString, IrcUser *>::iterator ircUserIter = _ircUsers.begin();
   while(ircUserIter != _ircUsers.end()) {
     if(ircUser == *ircUserIter) {
-      emit deletedIrcUserRemoved(ircUserIter.key());
       ircUserIter = _ircUsers.erase(ircUserIter);
       break;
     }
@@ -216,30 +216,6 @@ void Network::ircUserDestroyed() {
   }
 }
 
-void Network::removeDeletedIrcUser(const QString &username) {
-  // DO NOT CALL THIS SLOT EVER!!!
-  
-  // this slots purpose is only to remove deleted users that haven't been synced yet.
-  // Reason:
-  // as a user parting a channel results in it's deletion if it is no longer in any known channel
-  // this action can only be communicated if the slaves are allready in sync.
-  // so if such a deleted user isn't synced in slave mode, we kill and remove it.
-
-  Q_ASSERT(proxy());
-
-  if(!_ircUsers.contains(username))
-    return;
-
-  IrcUser *ircUser = _ircUsers[username];
-
-  if(ircUser->isInitialized())
-    return;
-
-  _ircUsers.remove(username);
-  emit ircUserRemoved(username);
-  emit ircUserRemoved(ircUser);
-}
-
 void Network::removeIrcUser(IrcUser *ircuser) {
   QString nick = _ircUsers.key(ircuser);
   if(nick.isNull())
@@ -454,6 +430,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);
@@ -538,22 +521,86 @@ QVariantMap Network::initSupports() const {
   return supports;
 }
 
-QStringList Network::initIrcUsers() const {
-  QStringList hostmasks;
-  foreach(IrcUser *ircuser, ircUsers()) {
-    hostmasks << ircuser->hostmask();
+QVariantMap Network::initIrcUsersAndChannels() const {
+  QVariantMap usersAndChannels;
+  QVariantMap users;
+  QVariantMap channels;
+
+  QHash<QString, IrcUser *>::const_iterator userIter = _ircUsers.constBegin();
+  QHash<QString, IrcUser *>::const_iterator userIterEnd = _ircUsers.constEnd();
+  while(userIter != userIterEnd) {
+    users[userIter.value()->hostmask()] = userIter.value()->toVariantMap();
+    userIter++;
   }
-  return hostmasks;
+  usersAndChannels["users"] = users;
+
+  QHash<QString, IrcChannel *>::const_iterator channelIter = _ircChannels.constBegin();
+  QHash<QString, IrcChannel *>::const_iterator channelIterEnd = _ircChannels.constEnd();
+  while(channelIter != channelIterEnd) {
+    channels[channelIter.value()->name()] = channelIter.value()->toVariantMap();
+    channelIter++;
+  }
+  usersAndChannels["channels"] = channels;
+
+  return usersAndChannels;
 }
 
-QStringList Network::initIrcChannels() const {
-  QStringList channels;
-  QHash<QString, IrcChannel *>::const_iterator iter = _ircChannels.constBegin();
-  while(iter != _ircChannels.constEnd()) {
-    channels << iter.value()->name();
-    iter++;
+void Network::initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels) {
+  Q_ASSERT(proxy());
+  if(isInitialized()) {
+    qWarning() << "Network" << networkId() << "received init data for users and channels allthough there allready are known users or channels!";
+    return;
   }
-  return channels;
+    
+  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);
+
+    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);
+
+    channelIter++;
+  }
+  
 }
 
 void Network::initSetSupports(const QVariantMap &supports) {
@@ -564,21 +611,6 @@ void Network::initSetSupports(const QVariantMap &supports) {
   }
 }
 
-void Network::initSetIrcUsers(const QStringList &hostmasks) {
-  if(!_ircUsers.empty())
-    return;
-  foreach(QString hostmask, hostmasks) {
-    newIrcUser(hostmask);
-  }
-}
-
-void Network::initSetIrcChannels(const QStringList &channels) {
-  if(!_ircChannels.empty())
-    return;
-  foreach(QString channel, channels)
-    newIrcChannel(channel);
-}
-
 IrcUser *Network::updateNickFromMask(const QString &mask) {
   QString nick(nickFromMask(mask).toLower());
   IrcUser *ircuser;
@@ -607,13 +639,15 @@ void Network::ircUserNickChanged(QString 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);
-  emit ircChannelInitDone(ircchannel);
+  IrcChannel *ircChannel = static_cast<IrcChannel *>(sender());
+  Q_ASSERT(ircChannel);
+  disconnect(ircChannel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
+  emit ircChannelInitDone(ircChannel);
 }
 
 void Network::removeIrcChannel(IrcChannel *channel) {
@@ -650,18 +684,24 @@ void Network::emitConnectionError(const QString &errorMsg) {
 // ====================
 void Network::determinePrefixes() {
   // seems like we have to construct them first
-  QString PREFIX = support("PREFIX");
+  QString prefix = support("PREFIX");
   
-  if(PREFIX.startsWith("(") && PREFIX.contains(")")) {
-    _prefixes = PREFIX.section(")", 1);
-    _prefixModes = PREFIX.mid(1).section(")", 0, 0);
+  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()) {
+      _prefixes = defaultPrefixes;
+      _prefixModes = defaultPrefixModes;
+      return;
+    }
+
     // 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];
       }
@@ -673,7 +713,7 @@ void Network::determinePrefixes() {
     // 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];
       }