X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fcoresession.cpp;h=c34e89888710d41026c141ddff595e14d166ca14;hp=437200a8256e453aa0529e5ad630b57592f53476;hb=a9b3edc811552b39dafb8fb01699490e5bcfb014;hpb=4e9a619ab2a22ce3c933fbb36122632debfd415a diff --git a/src/core/coresession.cpp b/src/core/coresession.cpp index 437200a8..c34e8988 100644 --- a/src/core/coresession.cpp +++ b/src/core/coresession.cpp @@ -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,198 +18,336 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include "core.h" #include "coresession.h" -#include "server.h" +#include "networkconnection.h" + #include "signalproxy.h" #include "storage.h" + +#include "network.h" +#include "ircuser.h" +#include "ircchannel.h" +#include "identity.h" + #include "util.h" +#include "coreusersettings.h" -CoreSession::CoreSession(UserId uid, Storage *_storage, QObject *parent) : QObject(parent), user(uid), storage(_storage) { - _signalProxy = new SignalProxy(SignalProxy::Server, 0, this); +#include - QSettings s; - s.beginGroup(QString("SessionData/%1").arg(user)); - mutex.lock(); - foreach(QString key, s.allKeys()) { - sessionData[key] = s.value(key); - } - mutex.unlock(); +CoreSession::CoreSession(UserId uid, QObject *parent) : QObject(parent), + _user(uid), + _signalProxy(new SignalProxy(SignalProxy::Server, 0, this)), + scriptEngine(new QScriptEngine(this)) +{ SignalProxy *p = signalProxy(); - p->attachSlot(SIGNAL(requestNetworkStates()), this, SIGNAL(serverStateRequested())); + 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(BufferId, QString)), this, SLOT(msgFromGui(BufferId, QString))); - p->attachSlot(SIGNAL(importOldBacklog()), storage, SLOT(importOldBacklog())); - p->attachSlot(SIGNAL(requestBacklog(BufferId, QVariant, QVariant)), this, SLOT(sendBacklog(BufferId, QVariant, QVariant))); - p->attachSlot(SIGNAL(requestNetworkStates()), this, SLOT(sendServerStates())); + 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(BufferId, QVariantList, bool))); - p->attachSignal(this, SIGNAL(bufferIdUpdated(BufferId))); - p->attachSignal(storage, SIGNAL(bufferIdUpdated(BufferId))); + 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 &))); - /* Autoconnect. (When) do we actually do this? - QStringList 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); + } + + // Load and init networks. + // FIXME For now we use the old info from sessionData... + 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); - */ + 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 slist; + foreach(QVariant v, network["Servers"].toList()) slist << v.toMap(); + net->setServerList(slist); + net->setProxy(p); + _networks[netid] = net; + p->synchronize(net); + } + + emit initialized(); } CoreSession::~CoreSession() { +} + +UserId CoreSession::user() const { + return _user; +} + +Network *CoreSession::network(NetworkId id) const { + if(_networks.contains(id)) return _networks[id]; + return 0; +} +NetworkConnection *CoreSession::networkConnection(NetworkId id) const { + if(_connections.contains(id)) return _connections[id]; + return 0; +} + +Identity *CoreSession::identity(IdentityId id) const { + if(_identities.contains(id)) return _identities[id]; + return 0; +} + +QVariant CoreSession::state() const { // FIXME + QVariantMap res; + /* + QList conn; + foreach(NetworkConnection *net, connections.values()) { + if(net->isConnected()) { + QVariantMap m; + m["Network"] = net->networkName(); + m["State"] = net->state(); + conn << m; + } + } + res["ConnectedServers"] = conn; + */ + return res; } -UserId CoreSession::userId() const { - return user; +void CoreSession::restoreState(const QVariant &previousState) { // FIXME + // 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)); - mutex.lock(); + CoreUserSettings s(user()); + s.setSessionValue(key, data); sessionData[key] = data; - s.setValue(key, data); - mutex.unlock(); - s.endGroup(); emit sessionDataChanged(key, data); emit sessionDataChanged(key); } QVariant CoreSession::retrieveSessionData(const QString &key, const QVariant &def) { QVariant data; - mutex.lock(); if(!sessionData.contains(key)) data = def; else data = sessionData[key]; - mutex.unlock(); return data; } -void CoreSession::connectToNetwork(QString network) { - QStringList networks; networks << network; // FIXME obsolete crap - foreach(QString net, networks) { - if(servers.contains(net)) { - - } else { - Server *server = new Server(userId(), net); - connect(this, SIGNAL(serverStateRequested()), server, SLOT(sendState())); - connect(this, SIGNAL(connectToIrc(QString)), server, SLOT(connectToIrc(QString))); - connect(this, SIGNAL(disconnectFromIrc(QString)), server, SLOT(disconnectFromIrc(QString))); - connect(this, SIGNAL(msgFromGui(QString, QString, QString)), server, SLOT(userInput(QString, QString, QString))); - - connect(server, SIGNAL(connected(QString)), this, SLOT(serverConnected(QString))); - connect(server, SIGNAL(disconnected(QString)), this, SLOT(serverDisconnected(QString))); - connect(server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8))); - connect(server, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString))); - - SignalProxy *p = signalProxy(); - p->attachSignal(server, SIGNAL(serverState(QString, QVariantMap)), SIGNAL(networkState(QString, QVariantMap))); - p->attachSignal(server, SIGNAL(modeSet(QString, QString, QString))); - p->attachSignal(server, SIGNAL(nickAdded(QString, QString, QVariantMap))); - p->attachSignal(server, SIGNAL(nickRenamed(QString, QString, QString))); - p->attachSignal(server, SIGNAL(nickRemoved(QString, QString))); - p->attachSignal(server, SIGNAL(nickUpdated(QString, QString, QVariantMap))); - p->attachSignal(server, SIGNAL(ownNickSet(QString, QString))); - p->attachSignal(server, SIGNAL(queryRequested(QString, QString))); - // TODO add error handling - p->attachSignal(server, SIGNAL(connected(QString)), SIGNAL(networkConnected(QString))); - p->attachSignal(server, SIGNAL(disconnected(QString)), SIGNAL(networkDisconnected(QString))); - - server->start(); - servers[net] = server; +void CoreSession::updateBufferInfo(UserId uid, const BufferInfo &bufinfo) { + if(uid == user()) emit bufferInfoUpdated(bufinfo); +} + +// FIXME remove +void CoreSession::connectToNetwork(QString netname, const QVariant &previousState) { + Network *net = 0; + foreach(Network *n, _networks.values()) { + if(n->networkName() == netname) { + net = n; break; } - emit connectToIrc(net); } + if(!net) { + qWarning() << "Connect to unknown network requested, ignoring!"; + return; + } + connectToNetwork(net->networkId(), previousState); } -void CoreSession::addClient(QIODevice *device) { - signalProxy()->addPeer(device); +void CoreSession::connectToNetwork(NetworkId id, const QVariant &previousState) { + Network *net = network(id); + if(!net) { + qWarning() << "Connect to unknown network requested! net:" << id << "user:" << user(); + return; + } + + NetworkConnection *conn = networkConnection(id); + if(!conn) { + conn = new NetworkConnection(net, this, previousState); + _connections[id] = conn; + attachNetworkConnection(conn); + conn->connectToIrc(); + } +} + +void CoreSession::attachNetworkConnection(NetworkConnection *conn) { + //connect(this, SIGNAL(connectToIrc(QString)), network, SLOT(connectToIrc(QString))); + //connect(this, SIGNAL(disconnectFromIrc(QString)), network, SLOT(disconnectFromIrc(QString))); + //connect(this, SIGNAL(msgFromGui(uint, QString, QString)), network, SLOT(userInput(uint, QString, QString))); + + //connect(conn, SIGNAL(connected(NetworkId)), this, SLOT(networkConnected(NetworkId))); + //connect(conn, SIGNAL(disconnected(NetworkId)), this, SLOT(networkDisconnected(NetworkId))); + signalProxy()->attachSignal(conn, SIGNAL(connected(NetworkId)), SIGNAL(networkConnected(NetworkId))); + signalProxy()->attachSignal(conn, SIGNAL(disconnected(NetworkId)), SIGNAL(networkDisconnected(NetworkId))); + + connect(conn, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)), this, SLOT(recvMessageFromServer(Message::Type, QString, QString, QString, quint8))); + connect(conn, SIGNAL(displayStatusMsg(QString)), this, SLOT(recvStatusMsgFromServer(QString))); + + // TODO add error handling +} + +void CoreSession::networkStateRequested() { +} + +void CoreSession::addClient(QObject *dev) { // this is QObject* so we can use it in signal connections + QIODevice *device = qobject_cast(dev); + if(!device) { + qWarning() << "Invoking CoreSession::addClient with a QObject that is not a QIODevice!"; + } else { + signalProxy()->addPeer(device); + QVariantMap reply; + reply["MsgType"] = "SessionInit"; + reply["SessionState"] = sessionState(); + SignalProxy::writeDataToDevice(device, reply); + } } SignalProxy *CoreSession::signalProxy() const { return _signalProxy; } -void CoreSession::serverConnected(QString net) { - storage->getBufferId(userId(), net); // create status buffer +void CoreSession::networkConnected(uint networkid) { + Core::bufferInfo(user(), networkConnection(networkid)->networkName()); // create status buffer } -void CoreSession::serverDisconnected(QString net) { - delete servers[net]; - servers.remove(net); - signalProxy()->sendSignal(SIGNAL(networkDisconnected(QString)), net); // FIXME does this work? +void CoreSession::networkDisconnected(uint networkid) { + // FIXME + // connection should only go away on explicit /part, and handle reconnections etcpp internally otherwise + Q_ASSERT(_connections.contains(networkid)); + _connections.take(networkid)->deleteLater(); + Q_ASSERT(!_connections.contains(networkid)); } -void CoreSession::msgFromGui(BufferId bufid, QString msg) { - emit msgFromGui(bufid.network(), bufid.buffer(), msg); +// FIXME switch to BufferId +void CoreSession::msgFromClient(BufferInfo bufinfo, QString msg) { + NetworkConnection *conn = networkConnection(bufinfo.networkId()); + if(conn) { + conn->userInput(bufinfo.buffer(), msg); + } else { + qWarning() << "Trying to send to unconnected network!"; + } } // ALL messages coming pass through these functions before going to the GUI. // So this is the perfect place for storing the backlog and log stuff. - void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) { - Server *s = qobject_cast(this->sender()); + NetworkConnection *s = qobject_cast(this->sender()); Q_ASSERT(s); - BufferId buf; + BufferInfo buf; if((flags & Message::PrivMsg) && !(flags & Message::Self)) { - buf = storage->getBufferId(user, s->getNetwork(), nickFromMask(sender)); + buf = Core::bufferInfo(user(), s->networkName(), nickFromMask(sender)); } else { - buf = storage->getBufferId(user, s->getNetwork(), target); + buf = Core::bufferInfo(user(), s->networkName(), target); } Message msg(buf, type, text, sender, flags); - msg.msgId = storage->logMessage(msg); - Q_ASSERT(msg.msgId); + msg.setMsgId(Core::storeMessage(msg)); + Q_ASSERT(msg.msgId() != 0); emit displayMsg(msg); } void CoreSession::recvStatusMsgFromServer(QString msg) { - Server *s = qobject_cast(sender()); + NetworkConnection *s = qobject_cast(sender()); Q_ASSERT(s); - emit displayStatusMsg(s->getNetwork(), msg); + emit displayStatusMsg(s->networkName(), msg); } - -QList CoreSession::buffers() const { - return storage->requestBuffers(user); +QList CoreSession::buffers() const { + return Core::requestBuffers(user()); } QVariant CoreSession::sessionState() { QVariantMap v; - QList bufs; - foreach(BufferId id, storage->requestBuffers(user)) { bufs.append(QVariant::fromValue(id)); } - v["Buffers"] = bufs; - mutex.lock(); + + QVariantList bufs; + foreach(BufferInfo id, buffers()) bufs << qVariantFromValue(id); + v["BufferInfos"] = bufs; + QVariantList networkids; + foreach(NetworkId id, _networks.keys()) networkids << qVariantFromValue(id); + v["NetworkIds"] = networkids; + + quint32 ircusercount = 0; + quint32 ircchannelcount = 0; + foreach(Network *net, _networks.values()) { + ircusercount += net->ircUserCount(); + ircchannelcount += net->ircChannelCount(); + } + v["IrcUserCount"] = ircusercount; + v["IrcChannelCount"] = ircchannelcount; + + QList idlist; + foreach(Identity *i, _identities.values()) idlist << qVariantFromValue(*i); + v["Identities"] = idlist; + v["SessionData"] = sessionData; - mutex.unlock(); - v["Networks"] = QVariant(servers.keys()); - // v["Payload"] = QByteArray(100000000, 'a'); // for testing purposes - return v; -} -void CoreSession::sendServerStates() { - emit serverStateRequested(); + //v["Payload"] = QByteArray(100000000, 'a'); // for testing purposes + return v; } -void CoreSession::sendBacklog(BufferId id, QVariant v1, QVariant v2) { +void CoreSession::sendBacklog(BufferInfo id, QVariant v1, QVariant v2) { QList log; QList msglist; if(v1.type() == QVariant::DateTime) { } else { - msglist = storage->requestMsgs(id, v1.toInt(), v2.toInt()); + msglist = Core::requestMsgs(id, v1.toInt(), v2.toInt()); } // Send messages out in smaller packages - we don't want to make the signal data too large! for(int i = 0; i < msglist.count(); i++) { - log.append(QVariant::fromValue(msglist[i])); + log.append(qVariantFromValue(msglist[i])); if(log.count() >= 5) { emit backlogData(id, log, i >= msglist.count() - 1); log.clear(); @@ -217,3 +355,55 @@ void CoreSession::sendBacklog(BufferId 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))); + + // FIXME + //QScriptValue storage_ = scriptEngine->newQObject(storage); + //scriptEngine->globalObject().setProperty("storage", storage_); +} + +void CoreSession::scriptRequest(QString script) { + emit scriptResult(scriptEngine->evaluate(script).toString()); +} +#include +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(); + } +} +