Syncing my current state, Network settings still not fully functional, so don't use...
[quassel.git] / src / common / network.cpp
index 780a5d8..e899969 100644 (file)
 // ====================
 //  Public:
 // ====================
-Network::Network(const uint &networkid, QObject *parent)
-  : SyncableObject(parent),
+Network::Network(const NetworkId &networkid, QObject *parent) : SyncableObject(parent),
     _networkId(networkid),
-    _initialized(false),
+    _identity(0),
     _myNick(QString()),
-    _networkName(QString()),
+    _networkName(QString("<not initialized>")),
     _currentServer(QString()),
+    _connected(false),
     _prefixes(QString()),
     _prefixModes(QString()),
     _proxy(0),
     _codecForEncoding(0),
     _codecForDecoding(0)
 {
-  setObjectName(QString::number(networkid));
+  setObjectName(QString::number(networkid.toInt()));
 }
 
 // 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();
-//   }
-//}
-
-uint Network::networkId() const {
-  return _networkId;
+/*
+Network::~Network() {
+  QHashIterator<QString, IrcUser *> ircuser(_ircUsers);
+  while (ircuser.hasNext()) {
+    ircuser.next();
+    delete ircuser.value();
+  }
 }
+*/
 
-bool Network::initialized() const {
-  return _initialized;
+NetworkId Network::networkId() const {
+  return _networkId;
 }
 
 SignalProxy *Network::proxy() const {
@@ -70,14 +68,14 @@ SignalProxy *Network::proxy() const {
 
 void Network::setProxy(SignalProxy *proxy) {
   _proxy = proxy;
-  proxy->synchronize(this);
+  //proxy->synchronize(this);  // we should to this explicitly from the outside!
 }
 
 bool Network::isMyNick(const QString &nick) const {
   return (myNick().toLower() == nick.toLower());
 }
 
-bool Network::isMyNick(IrcUser *ircuser) const {
+bool Network::isMe(IrcUser *ircuser) const {
   return (ircuser->nick().toLower() == myNick().toLower());
 }
 
@@ -91,6 +89,30 @@ bool Network::isChannelName(const QString &channelname) const {
     return QString("#&!+").contains(channelname[0]);
 }
 
+bool Network::isConnected() const {
+  return _connected;
+}
+
+NetworkInfo Network::networkInfo() const {
+  NetworkInfo info;
+  info.networkName = networkName();
+  info.networkId = networkId();
+  info.identity = identity();
+  info.codecForEncoding = codecForEncoding();
+  info.codecForDecoding = codecForDecoding();
+  info.serverList = serverList();
+  return info;
+}
+
+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);
+}
+
 QString Network::prefixToMode(const QString &prefix) {
   if(prefixes().contains(prefix))
     return QString(prefixModes()[prefixes().indexOf(prefix)]);
@@ -125,6 +147,10 @@ QString Network::myNick() const {
   return _myNick;
 }
 
+IdentityId Network::identity() const {
+  return _identity;
+}
+
 QStringList Network::nicks() const {
   // we don't use _ircUsers.keys() since the keys may be
   // not up to date after a nick change
@@ -139,6 +165,10 @@ QStringList Network::channels() const {
   return _ircChannels.keys();
 }
 
+QList<QVariantMap> Network::serverList() const {
+  return _serverList;
+}
+
 QString Network::prefixes() {
   if(_prefixes.isNull())
     determinePrefixes();
@@ -170,18 +200,19 @@ IrcUser *Network::newIrcUser(const QString &hostmask) {
   if(!_ircUsers.contains(nick)) {
     IrcUser *ircuser = new IrcUser(hostmask, this);
     // mark IrcUser as already initialized to keep the SignalProxy from requesting initData
-    if(initialized())
-      ircuser->setInitialized();
+    //if(isInitialized())
+    //  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, SIGNAL(ircUserInitDone()));
+    connect(ircuser, SIGNAL(initDone()), this, SLOT(ircUserInitDone()));
     connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
     _ircUsers[nick] = ircuser;
     emit ircUserAdded(hostmask);
+    emit ircUserAdded(ircuser);
   }
   return _ircUsers[nick];
 }
@@ -196,8 +227,9 @@ void Network::removeIrcUser(IrcUser *ircuser) {
     return;
 
   _ircUsers.remove(nick);
-  ircuser->deleteLater();
   emit ircUserRemoved(nick);
+  emit ircUserRemoved(ircuser);
+  ircuser->deleteLater();
 }
 
 void Network::removeIrcUser(QString nick) {
@@ -222,22 +254,27 @@ QList<IrcUser *> Network::ircUsers() const {
   return _ircUsers.values();
 }
 
+quint32 Network::ircUserCount() const {
+  return _ircUsers.count();
+}
+
 IrcChannel *Network::newIrcChannel(const QString &channelname) {
   if(!_ircChannels.contains(channelname.toLower())) {
     IrcChannel *channel = new IrcChannel(channelname, this);
     // mark IrcUser as already initialized to keep the SignalProxy from requesting initData
-    if(initialized())
-      channel->setInitialized();
+    //if(isInitialized())
+    //  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, SIGNAL(ircChannelInitDone()));
+    connect(channel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
     connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
     _ircChannels[channelname.toLower()] = channel;
     emit ircChannelAdded(channelname);
+    emit ircChannelAdded(channel);
   }
   return _ircChannels[channelname.toLower()];
 }
@@ -246,7 +283,7 @@ IrcChannel *Network::newIrcChannel(const QByteArray &channelname) {
   return newIrcChannel(decodeString(channelname));
 }
 
-IrcChannel *Network::ircChannel(QString channelname) {
+IrcChannel *Network::ircChannel(QString channelname) const {
   channelname = channelname.toLower();
   if(_ircChannels.contains(channelname))
     return _ircChannels[channelname];
@@ -254,7 +291,7 @@ IrcChannel *Network::ircChannel(QString channelname) {
     return 0;
 }
 
-IrcChannel *Network::ircChannel(const QByteArray &channelname) {
+IrcChannel *Network::ircChannel(const QByteArray &channelname) const {
   return ircChannel(decodeString(channelname));
 }
 
@@ -263,28 +300,36 @@ QList<IrcChannel *> Network::ircChannels() const {
   return _ircChannels.values();
 }
 
-QTextCodec *Network::codecForEncoding() const {
-  return _codecForEncoding;
+quint32 Network::ircChannelCount() const {
+  return _ircChannels.count();
 }
 
-void Network::setCodecForEncoding(const QString &name) {
-  setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
+QByteArray Network::codecForEncoding() const {
+  if(_codecForEncoding) return _codecForEncoding->name();
+  return QByteArray();
+}
+
+void Network::setCodecForEncoding(const QByteArray &name) {
+  setCodecForEncoding(QTextCodec::codecForName(name));
 }
 
 void Network::setCodecForEncoding(QTextCodec *codec) {
   _codecForEncoding = codec;
+  emit codecForEncodingSet(codecForEncoding());
 }
 
-QTextCodec *Network::codecForDecoding() const {
-  return _codecForDecoding;
+QByteArray Network::codecForDecoding() const {
+  if(_codecForDecoding) return _codecForDecoding->name();
+  else return QByteArray();
 }
 
-void Network::setCodecForDecoding(const QString &name) {
-  setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
+void Network::setCodecForDecoding(const QByteArray &name) {
+  setCodecForDecoding(QTextCodec::codecForName(name));
 }
 
 void Network::setCodecForDecoding(QTextCodec *codec) {
   _codecForDecoding = codec;
+  emit codecForDecodingSet(codecForDecoding());
 }
 
 QString Network::decodeString(const QByteArray &text) const {
@@ -311,11 +356,26 @@ void Network::setCurrentServer(const QString &currentServer) {
   emit currentServerSet(currentServer);
 }
 
+void Network::setConnected(bool connected) {
+  _connected = connected;
+  emit connectedSet(connected);
+}
+
 void Network::setMyNick(const QString &nickname) {
   _myNick = nickname;
   emit myNickSet(nickname);
 }
 
+void Network::setIdentity(IdentityId id) {
+  _identity = id;
+  emit identitySet(id);
+}
+
+void Network::setServerList(const QList<QVariantMap> &serverList) {
+  _serverList = serverList;
+  emit serverListSet(serverList);
+}
+
 void Network::addSupport(const QString &param, const QString &value) {
   if(!_supports.contains(param)) {
     _supports[param] = value;
@@ -340,6 +400,12 @@ QVariantMap Network::initSupports() const {
   return supports;
 }
 
+QVariantList Network::initServerList() const {
+  QList<QVariant> list;
+  foreach(QVariantMap serverdata, serverList()) list << QVariant(serverdata);
+  return list;
+}
+
 QStringList Network::initIrcUsers() const {
   QStringList hostmasks;
   foreach(IrcUser *ircuser, ircUsers()) {
@@ -360,6 +426,12 @@ void Network::initSetSupports(const QVariantMap &supports) {
   }
 }
 
+void Network::initSetServerList(const QVariantList & serverList) {
+  QList<QVariantMap> slist;
+  foreach(QVariant v, serverList) slist << v.toMap();
+  setServerList(slist);
+}
+
 void Network::initSetIrcUsers(const QStringList &hostmasks) {
   if(!_ircUsers.empty())
     return;
@@ -395,11 +467,23 @@ void Network::ircUserNickChanged(QString newnick) {
     return;
 
   if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
-  
+
   if(myNick().toLower() == oldnick)
     setMyNick(newnick);
 }
 
+void Network::ircUserInitDone() {
+  IrcUser *ircuser = static_cast<IrcUser *>(sender());
+  Q_ASSERT(ircuser);
+  emit ircUserInitDone(ircuser);
+}
+
+void Network::ircChannelInitDone() {
+  IrcChannel *ircchannel = static_cast<IrcChannel *>(sender());
+  Q_ASSERT(ircchannel);
+  emit ircChannelInitDone(ircchannel);
+}
+
 void Network::ircUserDestroyed() {
   IrcUser *ircuser = static_cast<IrcUser *>(sender());
   Q_ASSERT(ircuser);
@@ -409,12 +493,20 @@ void Network::ircUserDestroyed() {
 void Network::channelDestroyed() {
   IrcChannel *channel = static_cast<IrcChannel *>(sender());
   Q_ASSERT(channel);
+  emit ircChannelRemoved(sender());
   _ircChannels.remove(_ircChannels.key(channel));
 }
 
-void Network::setInitialized() {
-  _initialized = true;
-  emit initDone();
+void Network::requestConnect() {
+  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 :)
+}
+
+void Network::requestDisconnect() {
+  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 :)
 }
 
 // ====================
@@ -454,3 +546,20 @@ 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);
+}