Deuglify channel state icons
[quassel.git] / src / core / coresession.cpp
index 2e9184c..926fc97 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   Copyright (C) 2005-09 by the Quassel Project                          *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
 #include "coresession.h"
 #include "userinputhandler.h"
 #include "signalproxy.h"
-#include "buffersyncer.h"
+#include "corebuffersyncer.h"
 #include "corebacklogmanager.h"
 #include "corebufferviewmanager.h"
 #include "coreirclisthelper.h"
 #include "storage.h"
 
+#include "coreidentity.h"
 #include "corenetwork.h"
 #include "ircuser.h"
 #include "ircchannel.h"
-#include "identity.h"
 
 #include "util.h"
 #include "coreusersettings.h"
@@ -44,14 +44,13 @@ CoreSession::CoreSession(UserId uid, bool restoreState, QObject *parent)
     _user(uid),
     _signalProxy(new SignalProxy(SignalProxy::Server, 0, this)),
     _aliasManager(this),
-    _bufferSyncer(new BufferSyncer(this)),
+    _bufferSyncer(new CoreBufferSyncer(this)),
     _backlogManager(new CoreBacklogManager(this)),
     _bufferViewManager(new CoreBufferViewManager(_signalProxy, this)),
     _ircListHelper(new CoreIrcListHelper(this)),
     _coreInfo(this),
     scriptEngine(new QScriptEngine(this))
 {
-
   SignalProxy *p = signalProxy();
   connect(p, SIGNAL(peerRemoved(QIODevice *)), this, SLOT(removeClient(QIODevice *)));
 
@@ -64,43 +63,29 @@ CoreSession::CoreSession(UserId uid, bool restoreState, QObject *parent)
 
   p->attachSignal(this, SIGNAL(identityCreated(const Identity &)));
   p->attachSignal(this, SIGNAL(identityRemoved(IdentityId)));
-  p->attachSlot(SIGNAL(createIdentity(const Identity &)), this, SLOT(createIdentity(const Identity &)));
+  p->attachSlot(SIGNAL(createIdentity(const Identity &, const QVariantMap &)), this, SLOT(createIdentity(const Identity &, const QVariantMap &)));
   p->attachSlot(SIGNAL(removeIdentity(IdentityId)), this, SLOT(removeIdentity(IdentityId)));
 
   p->attachSignal(this, SIGNAL(networkCreated(NetworkId)));
   p->attachSignal(this, SIGNAL(networkRemoved(NetworkId)));
-  p->attachSlot(SIGNAL(createNetwork(const NetworkInfo &)), this, SLOT(createNetwork(const NetworkInfo &)));
+  p->attachSlot(SIGNAL(createNetwork(const NetworkInfo &, const QStringList &)), this, SLOT(createNetwork(const NetworkInfo &, const QStringList &)));
   p->attachSlot(SIGNAL(removeNetwork(NetworkId)), this, SLOT(removeNetwork(NetworkId)));
 
   loadSettings();
   initScriptEngine();
 
-  // init BufferSyncer
-  QHash<BufferId, MsgId> lastSeenHash = Core::bufferLastSeenMsgIds(user());
-  foreach(BufferId id, lastSeenHash.keys())
-    _bufferSyncer->requestSetLastSeenMsg(id, lastSeenHash[id]);
+  connect(&(Core::instance()->syncTimer()), SIGNAL(timeout()), _bufferSyncer, SLOT(storeDirtyIds()));
+  connect(&(Core::instance()->syncTimer()), SIGNAL(timeout()), _bufferViewManager, SLOT(saveBufferViews()));
 
-  connect(_bufferSyncer, SIGNAL(lastSeenMsgSet(BufferId, MsgId)), this, SLOT(storeBufferLastSeenMsg(BufferId, MsgId)));
-  connect(_bufferSyncer, SIGNAL(removeBufferRequested(BufferId)), this, SLOT(removeBufferRequested(BufferId)));
-  connect(this, SIGNAL(bufferRemoved(BufferId)), _bufferSyncer, SLOT(removeBuffer(BufferId)));
-  connect(this, SIGNAL(bufferRenamed(BufferId, QString)), _bufferSyncer, SLOT(renameBuffer(BufferId, QString)));
   p->synchronize(_bufferSyncer);
-
-
-  // init alias manager
   p->synchronize(&aliasManager());
-
-  // init BacklogManager
   p->synchronize(_backlogManager);
-
-  // init IrcListHelper
   p->synchronize(ircListHelper());
-
-  // init CoreInfo
   p->synchronize(&_coreInfo);
 
   // Restore session state
-  if(restoreState) restoreSessionState();
+  if(restoreState)
+    restoreSessionState();
 
   emit initialized();
 }
@@ -117,7 +102,7 @@ CoreNetwork *CoreSession::network(NetworkId id) const {
   return 0;
 }
 
-Identity *CoreSession::identity(IdentityId id) const {
+CoreIdentity *CoreSession::identity(IdentityId id) const {
   if(_identities.contains(id)) return _identities[id];
   return 0;
 }
@@ -125,28 +110,28 @@ Identity *CoreSession::identity(IdentityId id) const {
 void CoreSession::loadSettings() {
   CoreUserSettings s(user());
 
-  foreach(IdentityId id, s.identityIds()) {
-    Identity *i = new Identity(s.identity(id), this);
-    if(!i->isValid()) {
-      qWarning() << "Invalid identity! Removing...";
-      s.removeIdentity(id);
-      delete i;
-      continue;
+  // migrate to db
+  QList<IdentityId> ids = s.identityIds();
+  QList<NetworkInfo> networkInfos = Core::networks(user());
+  foreach(IdentityId id, ids) {
+    CoreIdentity identity(s.identity(id));
+    IdentityId newId = Core::createIdentity(user(), identity);
+    QList<NetworkInfo>::iterator networkIter = networkInfos.begin();
+    while(networkIter != networkInfos.end()) {
+      if(networkIter->identity == id) {
+       networkIter->identity = newId;
+       Core::updateNetwork(user(), *networkIter);
+       networkIter = networkInfos.erase(networkIter);
+      } else {
+       networkIter++;
+      }
     }
-    if(_identities.contains(i->id())) {
-      qWarning() << "Duplicate identity, ignoring!";
-      delete i;
-      continue;
-    }
-    connect(i, SIGNAL(updated(const QVariantMap &)), this, SLOT(identityUpdated(const QVariantMap &)));
-    _identities[i->id()] = i;
-    signalProxy()->synchronize(i);
+    s.removeIdentity(id);
   }
-  if(!_identities.count()) {
-    Identity i(1);
-    i.setToDefaults();
-    i.setIdentityName(tr("Default Identity"));
-    createIdentity(i);
+  // end of migration
+
+  foreach(CoreIdentity identity, Core::identities(user())) {
+    createIdentity(identity);
   }
 
   foreach(NetworkInfo info, Core::networks(user())) {
@@ -155,6 +140,8 @@ void CoreSession::loadSettings() {
 }
 
 void CoreSession::saveSessionState() const {
+  _bufferSyncer->storeDirtyIds();
+  _bufferViewManager->saveBufferViews();
 }
 
 void CoreSession::restoreSessionState() {
@@ -195,7 +182,6 @@ void CoreSession::removeClient(QIODevice *iodev) {
 
 QHash<QString, QString> CoreSession::persistentChannels(NetworkId id) const {
   return Core::persistentChannels(user(), id);
-  return QHash<QString, QString>();
 }
 
 // FIXME switch to BufferId
@@ -260,10 +246,6 @@ QVariant CoreSession::sessionState() {
   return v;
 }
 
-void CoreSession::storeBufferLastSeenMsg(BufferId buffer, const MsgId &msgId) {
-  Core::setBufferLastSeenMsg(user(), buffer, msgId);
-}
-
 void CoreSession::initScriptEngine() {
   signalProxy()->attachSlot(SIGNAL(scriptRequest(QString)), this, SLOT(scriptRequest(QString)));
   signalProxy()->attachSignal(this, SIGNAL(scriptResult(QString)));
@@ -278,47 +260,53 @@ void CoreSession::scriptRequest(QString script) {
 }
 
 /*** Identity Handling ***/
-
-void CoreSession::createIdentity(const Identity &id) {
-  // find free ID
-  int i;
-  for(i = 1; i <= _identities.count(); i++) {
-    if(!_identities.keys().contains(i)) break;
-  }
-  //qDebug() << "found free id" << i;
-  Identity *newId = new Identity(id, this);
-  newId->setId(i);
-  _identities[i] = newId;
-  signalProxy()->synchronize(newId);
-  CoreUserSettings s(user());
-  s.storeIdentity(*newId);
-  connect(newId, SIGNAL(updated(const QVariantMap &)), this, SLOT(identityUpdated(const QVariantMap &)));
-  emit identityCreated(*newId);
+void CoreSession::createIdentity(const Identity &identity, const QVariantMap &additional) {
+#ifndef HAVE_SSL
+  Q_UNUSED(additional)
+#endif
+
+  CoreIdentity coreIdentity(identity);
+#ifdef HAVE_SSL
+  if(additional.contains("KeyPem"))
+    coreIdentity.setSslKey(additional["KeyPem"].toByteArray());
+  if(additional.contains("CertPem"))
+    coreIdentity.setSslCert(additional["CertPem"].toByteArray());
+#endif
+  IdentityId id = Core::createIdentity(user(), coreIdentity);
+  if(!id.isValid())
+    return;
+  else
+    createIdentity(coreIdentity);
 }
 
-void CoreSession::removeIdentity(IdentityId id) {
-  Identity *i = _identities.take(id);
-  if(i) {
-    emit identityRemoved(id);
-    CoreUserSettings s(user());
-    s.removeIdentity(id);
-    i->deleteLater();
-  }
+void CoreSession::createIdentity(const CoreIdentity &identity) {
+  CoreIdentity *coreIdentity = new CoreIdentity(identity, this);
+  _identities[identity.id()] = coreIdentity;
+  // CoreIdentity has it's own synchronize method since it's "private" sslManager needs to be synced aswell
+  coreIdentity->synchronize(signalProxy());
+  connect(coreIdentity, SIGNAL(updated(const QVariantMap &)), this, SLOT(updateIdentityBySender()));
+  emit identityCreated(*coreIdentity);
 }
 
-void CoreSession::identityUpdated(const QVariantMap &data) {
-  IdentityId id = data.value("identityId", 0).value<IdentityId>();
-  if(!id.isValid() || !_identities.contains(id)) {
-    qWarning() << "Update request for unknown identity received!";
+void CoreSession::updateIdentityBySender() {
+  CoreIdentity *identity = qobject_cast<CoreIdentity *>(sender());
+  if(!identity)
     return;
+  Core::updateIdentity(user(), *identity);
+}
+
+void CoreSession::removeIdentity(IdentityId id) {
+  CoreIdentity *identity = _identities.take(id);
+  if(identity) {
+    emit identityRemoved(id);
+    Core::removeIdentity(user(), id);
+    identity->deleteLater();
   }
-  CoreUserSettings s(user());
-  s.storeIdentity(*_identities.value(id));
 }
 
 /*** Network Handling ***/
 
-void CoreSession::createNetwork(const NetworkInfo &info_) {
+void CoreSession::createNetwork(const NetworkInfo &info_, const QStringList &persistentChans) {
   NetworkInfo info = info_;
   int id;
 
@@ -342,6 +330,11 @@ void CoreSession::createNetwork(const NetworkInfo &info_) {
     _networks[id] = net;
     signalProxy()->synchronize(net);
     emit networkCreated(id);
+    // create persistent chans
+    foreach(QString channel, persistentChans) {
+      Core::bufferInfo(user(), info.networkId, BufferInfo::ChannelBuffer, channel, true);
+      Core::setChannelPersistent(user(), info.networkId, channel, true);
+    }
   } else {
     qWarning() << qPrintable(tr("CoreSession::createNetwork(): Trying to create a network that already exists, updating instead!"));
     _networks[info.networkId]->requestSetNetworkInfo(info);
@@ -374,39 +367,10 @@ void CoreSession::destroyNetwork(NetworkId id) {
   }
 }
 
-void CoreSession::removeBufferRequested(BufferId bufferId) {
-  BufferInfo bufferInfo = Core::getBufferInfo(user(), bufferId);
-  if(!bufferInfo.isValid()) {
-    qWarning() << "CoreSession::removeBufferRequested(): invalid BufferId:" << bufferId << "for User:" << user();
-    return;
-  }
-
-  if(bufferInfo.type() == BufferInfo::StatusBuffer) {
-    qWarning() << "CoreSession::removeBufferRequested(): Status Buffers cannot be removed!";
-    return;
-  }
-
-  if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
-    CoreNetwork *net = network(bufferInfo.networkId());
-    if(!net) {
-      qWarning() << "CoreSession::removeBufferRequested(): Received BufferInfo with unknown networkId!";
-      return;
-    }
-    IrcChannel *chan = net->ircChannel(bufferInfo.bufferName());
-    if(chan) {
-      qWarning() << "CoreSession::removeBufferRequested(): Unable to remove Buffer for joined Channel:" << bufferInfo.bufferName();
-      return;
-    }
-  }
-  if(Core::removeBuffer(user(), bufferId))
-    emit bufferRemoved(bufferId);
-}
-
-// FIXME: use a coreBufferSyncer for this
 void CoreSession::renameBuffer(const NetworkId &networkId, const QString &newName, const QString &oldName) {
-  BufferId bufferId = Core::renameBuffer(user(), networkId, newName, oldName);
-  if(bufferId.isValid()) {
-    emit bufferRenamed(bufferId, newName);
+  BufferInfo bufferInfo = Core::bufferInfo(user(), networkId, BufferInfo::QueryBuffer, oldName, false);
+  if(bufferInfo.isValid()) {
+    _bufferSyncer->renameBuffer(bufferInfo.bufferId(), newName);
   }
 }
 
@@ -415,7 +379,6 @@ void CoreSession::clientsConnected() {
   Identity *identity = 0;
   CoreNetwork *net = 0;
   IrcUser *me = 0;
-  QString awayReason;
   while(netIter != _networks.end()) {
     net = *netIter;
     netIter++;
@@ -457,8 +420,6 @@ void CoreSession::clientsDisconnected() {
     if(identity->detachAwayEnabled() && !me->isAway()) {
       if(identity->detachAwayReasonEnabled())
        awayReason = identity->detachAwayReason();
-      else
-       awayReason = identity->awayReason();
       net->setAutoAwayActive(true);
       net->userInputHandler()->handleAway(BufferInfo(), awayReason);
     }