+#include "coreusersettings.h"
+
+#include <QtScript>
+
+CoreSession::CoreSession(UserId uid, bool restoreState, QObject *parent) : QObject(parent),
+ _user(uid),
+ _signalProxy(new SignalProxy(SignalProxy::Server, 0, this)),
+ scriptEngine(new QScriptEngine(this))
+{
+
+ SignalProxy *p = signalProxy();
+
+ CoreUserSettings s(user());
+ sessionData = s.sessionData();
+
+ 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);
+ }
+
+ //p->attachSlot(SIGNAL(requestNetworkStates()), this, SLOT(networkStateRequested()));
+ p->attachSlot(SIGNAL(requestConnect(QString)), this, SLOT(connectToNetwork(QString)));
+ p->attachSlot(SIGNAL(sendInput(BufferInfo, QString)), this, SLOT(msgFromClient(BufferInfo, QString)));
+ p->attachSlot(SIGNAL(requestBacklog(BufferInfo, QVariant, QVariant)), this, SLOT(sendBacklog(BufferInfo, QVariant, QVariant)));
+ p->attachSignal(this, SIGNAL(displayMsg(Message)));
+ p->attachSignal(this, SIGNAL(displayStatusMsg(QString, QString)));
+ p->attachSignal(this, SIGNAL(backlogData(BufferInfo, QVariantList, bool)));
+ p->attachSignal(this, 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 &)));
+
+ 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);
+ }
+
+ // Load and init networks.
+ // FIXME For now we use the old info from sessionData...
+
+ QVariantMap networks = retrieveSessionData("Networks").toMap();
+ foreach(QString netname, networks.keys()) {
+ QVariantMap network = networks[netname].toMap();
+ NetworkId netid = Core::networkId(user(), netname);
+ Network *net = new Network(netid, this);
+ connect(net, SIGNAL(connectRequested(NetworkId)), this, SLOT(connectToNetwork(NetworkId)));
+ net->setNetworkName(netname);
+ net->setIdentity(1); // FIXME default identity for now
+ net->setCodecForEncoding("ISO-8859-15"); // FIXME
+ net->setCodecForDecoding("ISO-8859-15"); // FIXME
+ QList<QVariantMap> slist;
+ foreach(QVariant v, network["Servers"].toList()) slist << v.toMap();
+ net->setServerList(slist);
+ net->setProxy(p);
+ _networks[netid] = net;
+ p->synchronize(net);
+ }
+
+ // Restore session state
+ if(restoreState) restoreSessionState();