+
+
+void CoreSession::initScriptEngine() {
+ signalProxy()->attachSlot(SIGNAL(scriptRequest(QString)), this, SLOT(scriptRequest(QString)));
+ signalProxy()->attachSignal(this, SIGNAL(scriptResult(QString)));
+
+ // FIXME
+ //QScriptValue storage_ = scriptEngine->newQObject(storage);
+ //scriptEngine->globalObject().setProperty("storage", storage_);
+}
+
+void CoreSession::scriptRequest(QString script) {
+ emit scriptResult(scriptEngine->evaluate(script).toString());
+}
+
+/*** 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);
+ 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();
+ }
+}
+
+/*** Network Handling ***/
+
+void CoreSession::createNetwork(const NetworkInfo &info_) {
+ NetworkInfo info = info_;
+ int id;
+
+ if(!info.networkId.isValid())
+ Core::createNetwork(user(), info);
+
+ Q_ASSERT(info.networkId.isValid());
+
+ id = info.networkId.toInt();
+ Q_ASSERT(!_networks.contains(id));
+
+ Network *net = new Network(id, this);
+ connect(net, SIGNAL(connectRequested(NetworkId)), this, SLOT(connectToNetwork(NetworkId)));
+ connect(net, SIGNAL(disconnectRequested(NetworkId)), this, SLOT(disconnectFromNetwork(NetworkId)));
+ net->setNetworkInfo(info);
+ net->setProxy(signalProxy());
+ _networks[id] = net;
+ signalProxy()->synchronize(net);
+ emit networkCreated(id);
+}
+
+void CoreSession::updateNetwork(const NetworkInfo &info) {
+ if(!_networks.contains(info.networkId)) {
+ qWarning() << "Update request for unknown network received!";
+ return;
+ }
+ _networks[info.networkId]->setNetworkInfo(info);
+ Core::updateNetwork(user(), info);
+}
+
+void CoreSession::removeNetwork(NetworkId id) {
+ // Make sure the network is disconnected!
+ NetworkConnection *conn = _connections.value(id, 0);
+ if(conn) {
+ if(conn->connectionState() != Network::Disconnected) {
+ connect(conn, SIGNAL(disconnected(NetworkId)), this, SLOT(destroyNetwork(NetworkId)));
+ conn->disconnectFromIrc();
+ } else {
+ _connections.take(id)->deleteLater(); // TODO make this saner
+ destroyNetwork(id);
+ }
+ } else {
+ destroyNetwork(id);
+ }
+}
+
+void CoreSession::destroyNetwork(NetworkId id) {
+ Q_ASSERT(!_connections.contains(id));
+ Network *net = _networks.take(id);
+ if(net && Core::removeNetwork(user(), id)) {
+ emit networkRemoved(id);
+ net->deleteLater();
+ }
+}