/***************************************************************************
- * Copyright (C) 2005-07 by the Quassel IRC 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 *
#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()) {
+ qDebug() << QString("Invalid identity! Removing...");
+ s.removeIdentity(id);
+ delete i;
+ continue;
+ }
+ if(_identities.contains(i->id())) {
+ qDebug() << "Duplicate identity, ignoring!";
+ delete i;
+ continue;
+ }
+ _identities[i->id()] = i;
+ }
+ if(!_identities.count()) {
+ Identity i(1);
+ i.setToDefaults();
+ 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)));
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?
- --> session restore should be enough!
- 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() {
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);
}
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;
}
}
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());
+}
+
+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(i);
+}
+
+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();
+ }
+}
+