_Almost_ finished the identity dialog (which is still not used and not visible yet...
[quassel.git] / src / core / coresession.cpp
index bf542c2..bbe4a1f 100644 (file)
@@ -1,11 +1,11 @@
 /***************************************************************************
- *   Copyright (C) 2005-07 by The Quassel IRC Development Team             *
+ *   Copyright (C) 2005-08 by the Quassel Project                          *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
+ *   (at your option) version 3.                                           *
  *                                                                         *
  *   This program is distributed in the hope that it will be useful,       *
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
@@ -18,6 +18,8 @@
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
+#define SPUTDEV
+
 #include "coresession.h"
 #include "server.h"
 
 #include "networkinfo.h"
 #include "ircuser.h"
 #include "ircchannel.h"
+#include "identity.h"
 
 #include "util.h"
+#include "coreusersettings.h"
 
+#include <QtScript>
 
 CoreSession::CoreSession(UserId uid, Storage *_storage, QObject *parent)
   : QObject(parent),
     user(uid),
     _signalProxy(new SignalProxy(SignalProxy::Server, 0, this)),
-    storage(_storage)
+    storage(_storage),
+    scriptEngine(new QScriptEngine(this))
 {
-  
-  QSettings s;
-  s.beginGroup(QString("SessionData/%1").arg(user));
-  mutex.lock();
-  foreach(QString key, s.allKeys()) {
-    sessionData[key] = s.value(key);
-  }
-  mutex.unlock();
 
   SignalProxy *p = signalProxy();
 
+  CoreUserSettings s(user);
+  sessionData = s.sessionData();
+#ifdef SPUTDEV
+  foreach(IdentityId id, s.identityIds()) {
+    Identity *i = new Identity(s.identity(id), this);
+    if(!i->isValid()) {
+      qWarning() << QString("Invalid identity! Removing...");
+      s.removeIdentity(id);
+      delete i;
+      continue;
+    }
+    if(_identities.contains(i->id())) {
+      qWarning() << "Duplicate identity, ignoring!";
+      delete i;
+      continue;
+    }
+    _identities[i->id()] = i;
+  }
+  if(!_identities.count()) {
+    Identity i(1);
+    i.setToDefaults();
+    i.setIdentityName(tr("Default Identity"));
+    createIdentity(i);
+  }
+#endif
+
   p->attachSlot(SIGNAL(requestNetworkStates()), this, SLOT(serverStateRequested()));
   p->attachSlot(SIGNAL(requestConnect(QString)), this, SLOT(connectToNetwork(QString)));
   p->attachSlot(SIGNAL(sendInput(BufferInfo, QString)), this, SLOT(msgFromGui(BufferInfo, QString)));
@@ -59,16 +83,18 @@ CoreSession::CoreSession(UserId uid, Storage *_storage, QObject *parent)
   p->attachSignal(storage, SIGNAL(bufferInfoUpdated(BufferInfo)));
   p->attachSignal(this, SIGNAL(sessionDataChanged(const QString &, const QVariant &)), SIGNAL(coreSessionDataChanged(const QString &, const QVariant &)));
   p->attachSlot(SIGNAL(clientSessionDataChanged(const QString &, const QVariant &)), this, SLOT(storeSessionData(const QString &, const QVariant &)));
-  /* Autoconnect. (When) do we actually do this?
-  QStringList list;
-  QVariantMap networks = retrieveSessionData("Networks").toMap();
-  foreach(QString net, networks.keys()) {
-    if(networks[net].toMap()["AutoConnect"].toBool()) {
-      list << net;
-    }
-  } qDebug() << list;
-  if(list.count()) connectToIrc(list);
-  */
+
+  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(updateIdentity(const Identity &)), this, SLOT(updateIdentity(const Identity &)));
+  p->attachSlot(SIGNAL(removeIdentity(IdentityId)), this, SLOT(removeIdentity(IdentityId)));
+
+  initScriptEngine();
+
+  foreach(Identity *id, _identities.values()) {
+    p->synchronize(id);
+  }
 }
 
 CoreSession::~CoreSession() {
@@ -78,14 +104,40 @@ UserId CoreSession::userId() const {
   return user;
 }
 
+QVariant CoreSession::state() const {
+  QVariantMap res;
+  QList<QVariant> conn;
+  foreach(Server *server, servers.values()) {
+    if(server->isConnected()) {
+      QVariantMap m;
+      m["Network"] = server->networkName();
+      m["State"] = server->state();
+      conn << m;
+    }
+  }
+  res["ConnectedServers"] = conn;
+  return res;
+}
+
+void CoreSession::restoreState(const QVariant &previousState) {
+  // Session restore
+  QVariantMap state = previousState.toMap();
+  if(state.contains("ConnectedServers")) {
+    foreach(QVariant v, state["ConnectedServers"].toList()) {
+      QVariantMap m = v.toMap();
+      QString net = m["Network"].toString();
+      if(!net.isEmpty()) connectToNetwork(net, m["State"]);
+    }
+  }
+}
+
+
 void CoreSession::storeSessionData(const QString &key, const QVariant &data) {
-  QSettings s;
-  s.beginGroup(QString("SessionData/%1").arg(user));
+  CoreUserSettings s(user);
   mutex.lock();
+  s.setSessionValue(key, data);
   sessionData[key] = data;
-  s.setValue(key, data);
   mutex.unlock();
-  s.endGroup();
   emit sessionDataChanged(key, data);
   emit sessionDataChanged(key);
 }
@@ -100,10 +152,14 @@ QVariant CoreSession::retrieveSessionData(const QString &key, const QVariant &de
 }
 
 // FIXME switch to NetworkIDs
-void CoreSession::connectToNetwork(QString network) {
+void CoreSession::connectToNetwork(QString network, const QVariant &previousState) {
   uint networkid = getNetworkId(network);
+  if(networkid == 0) {
+    qWarning() << "unable to connect to Network" << network << "(User:" << userId() << "): unable to determine NetworkId";
+    return;
+  }
   if(!servers.contains(networkid)) {
-    Server *server = new Server(userId(), networkid, network);
+    Server *server = new Server(userId(), networkid, network, previousState);
     servers[networkid] = server;
     attachServer(server);
     server->start();
@@ -199,10 +255,14 @@ QVariant CoreSession::sessionState() {
   mutex.unlock();
 
   QVariantList networks;
-  foreach(uint networkid, servers.keys())
+  foreach(NetworkId networkid, servers.keys())
     networks.append(QVariant(networkid));
   v["Networks"] = QVariant(networks);
 
+  QList<QVariant> idlist;
+  foreach(Identity *i, _identities.values()) idlist << QVariant::fromValue<Identity>(*i);
+  v["Identities"] = idlist;
+
   // v["Payload"] = QByteArray(100000000, 'a');  // for testing purposes
   return v;
 }
@@ -227,3 +287,54 @@ void CoreSession::sendBacklog(BufferInfo id, QVariant v1, QVariant v2) {
   }
   if(log.count() > 0) emit backlogData(id, log, true);
 }
+
+
+void CoreSession::initScriptEngine() {
+  signalProxy()->attachSlot(SIGNAL(scriptRequest(QString)), this, SLOT(scriptRequest(QString)));
+  signalProxy()->attachSignal(this, SIGNAL(scriptResult(QString)));
+  
+  QScriptValue storage_ = scriptEngine->newQObject(storage);
+  scriptEngine->globalObject().setProperty("storage", storage_);
+}
+
+void CoreSession::scriptRequest(QString script) {
+  emit scriptResult(scriptEngine->evaluate(script).toString());
+}
+#include <QDebug>
+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);
+  emit identityCreated(*newId);
+}
+
+void CoreSession::updateIdentity(const Identity &id) {
+  if(!_identities.contains(id.id())) {
+    qWarning() << "Update request for unknown identity received!";
+    return;
+  }
+  _identities[id.id()]->update(id);
+
+  CoreUserSettings s(user);
+  s.storeIdentity(id);
+}
+
+void CoreSession::removeIdentity(IdentityId id) {
+  Identity *i = _identities.take(id);
+  if(i) {
+    emit identityRemoved(id);
+    CoreUserSettings s(user);
+    s.removeIdentity(id);
+    i->deleteLater();
+  }
+}
+