X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fclient%2Fclient.cpp;h=b561fd12737cd6904210d86905b1691fff6fe548;hp=419a8a2a47333c7bf22a9ba29229f53db3221cc5;hb=999dd26d04c96a142ee0ee8e1d066d939b7c4499;hpb=73edffb5f0f6ecae4118c36a7ca2c0d479b7f8c6 diff --git a/src/client/client.cpp b/src/client/client.cpp index 419a8a2a..b561fd12 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -19,21 +19,24 @@ ***************************************************************************/ #include "client.h" -#include "clientproxy.h" + #include "buffer.h" #include "buffertreemodel.h" +#include "quasselui.h" +#include "signalproxy.h" #include "util.h" Client * Client::instanceptr = 0; +bool Client::connectedToCore = false; Client::ClientMode Client::clientMode; +QVariantMap Client::coreConnectionInfo; QHash Client::buffers; QHash Client::bufferIds; -QHash > Client::nicks; -QHash Client::connected; +QHash > Client::nicks; +QHash Client::netConnected; +QStringList Client::netsAwaitingInit; QHash Client::ownNick; -QList Client::coreBuffers; - Client *Client::instance() { if(instanceptr) return instanceptr; @@ -47,17 +50,10 @@ void Client::destroy() { } Client::Client() { - clientProxy = ClientProxy::instance(); - - _bufferModel = new BufferTreeModel(this); - - connect(this, SIGNAL(bufferSelected(Buffer *)), _bufferModel, SLOT(selectBuffer(Buffer *))); - connect(this, SIGNAL(bufferUpdated(Buffer *)), _bufferModel, SLOT(bufferUpdated(Buffer *))); - connect(this, SIGNAL(bufferActivity(Buffer::ActivityLevel, Buffer *)), _bufferModel, SLOT(bufferActivity(Buffer::ActivityLevel, Buffer *))); + _signalProxy = new SignalProxy(SignalProxy::Client, 0, this); - // TODO: make this configurable (allow monolithic client to connect to remote cores) - if(Global::runMode == Global::Monolithic) clientMode = LocalCore; - else clientMode = RemoteCore; + connectedToCore = false; + socket = 0; } void Client::init(AbstractUi *ui) { @@ -68,113 +64,225 @@ void Client::init(AbstractUi *ui) { void Client::init() { blockSize = 0; - connect(&socket, SIGNAL(readyRead()), this, SLOT(serverHasData())); - connect(&socket, SIGNAL(connected()), this, SLOT(coreConnected())); - connect(&socket, SIGNAL(disconnected()), this, SLOT(coreDisconnected())); - connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(serverError(QAbstractSocket::SocketError))); - - connect(Global::instance(), SIGNAL(dataPutLocally(UserId, QString)), this, SLOT(updateCoreData(UserId, QString))); - connect(clientProxy, SIGNAL(csUpdateGlobalData(QString, QVariant)), this, SLOT(updateLocalData(QString, QVariant))); - - connect(clientProxy, SIGNAL(send(ClientSignal, QVariant, QVariant, QVariant)), this, SLOT(recvProxySignal(ClientSignal, QVariant, QVariant, QVariant))); - connect(clientProxy, SIGNAL(csServerState(QString, QVariant)), this, SLOT(recvNetworkState(QString, QVariant))); - connect(clientProxy, SIGNAL(csServerConnected(QString)), this, SLOT(networkConnected(QString))); - connect(clientProxy, SIGNAL(csServerDisconnected(QString)), this, SLOT(networkDisconnected(QString))); - connect(clientProxy, SIGNAL(csDisplayMsg(Message)), this, SLOT(recvMessage(const Message &))); - connect(clientProxy, SIGNAL(csDisplayStatusMsg(QString, QString)), this, SLOT(recvStatusMsg(QString, QString))); - connect(clientProxy, SIGNAL(csTopicSet(QString, QString, QString)), this, SLOT(setTopic(QString, QString, QString))); - connect(clientProxy, SIGNAL(csNickAdded(QString, QString, VarMap)), this, SLOT(addNick(QString, QString, VarMap))); - connect(clientProxy, SIGNAL(csNickRemoved(QString, QString)), this, SLOT(removeNick(QString, QString))); - connect(clientProxy, SIGNAL(csNickRenamed(QString, QString, QString)), this, SLOT(renameNick(QString, QString, QString))); - connect(clientProxy, SIGNAL(csNickUpdated(QString, QString, VarMap)), this, SLOT(updateNick(QString, QString, VarMap))); - connect(clientProxy, SIGNAL(csOwnNickSet(QString, QString)), this, SLOT(setOwnNick(QString, QString))); - connect(clientProxy, SIGNAL(csBacklogData(BufferId, const QList &, bool)), this, SLOT(recvBacklogData(BufferId, QList, bool))); - connect(clientProxy, SIGNAL(csUpdateBufferId(BufferId)), this, SLOT(updateBufferId(BufferId))); - connect(this, SIGNAL(sendInput(BufferId, QString)), clientProxy, SLOT(gsUserInput(BufferId, QString))); - connect(this, SIGNAL(requestBacklog(BufferId, QVariant, QVariant)), clientProxy, SLOT(gsRequestBacklog(BufferId, QVariant, QVariant))); - - syncToCore(); + _bufferModel = new BufferTreeModel(this); + + connect(this, SIGNAL(bufferSelected(Buffer *)), _bufferModel, SLOT(selectBuffer(Buffer *))); + connect(this, SIGNAL(bufferUpdated(Buffer *)), _bufferModel, SLOT(bufferUpdated(Buffer *))); + connect(this, SIGNAL(bufferActivity(Buffer::ActivityLevel, Buffer *)), _bufferModel, SLOT(bufferActivity(Buffer::ActivityLevel, Buffer *))); + + SignalProxy *p = signalProxy(); + p->attachSignal(this, SIGNAL(sendSessionData(const QString &, const QVariant &)), SIGNAL(clientSessionDataChanged(const QString &, const QVariant &))); + p->attachSlot(SIGNAL(coreSessionDataChanged(const QString &, const QVariant &)), this, SLOT(recvSessionData(const QString &, const QVariant &))); + p->attachSlot(SIGNAL(coreState(const QVariant &)), this, SLOT(recvCoreState(const QVariant &))); + p->attachSlot(SIGNAL(networkState(QString, QVariant)), this, SLOT(recvNetworkState(QString, QVariant))); + p->attachSlot(SIGNAL(networkConnected(QString)), this, SLOT(networkConnected(QString))); + p->attachSlot(SIGNAL(networkDisconnected(QString)), this, SLOT(networkDisconnected(QString))); + p->attachSlot(SIGNAL(displayMsg(const Message &)), this, SLOT(recvMessage(const Message &))); + p->attachSlot(SIGNAL(displayStatusMsg(QString, QString)), this, SLOT(recvStatusMsg(QString, QString))); + p->attachSlot(SIGNAL(topicSet(QString, QString, QString)), this, SLOT(setTopic(QString, QString, QString))); + p->attachSlot(SIGNAL(nickAdded(QString, QString, QVariantMap)), this, SLOT(addNick(QString, QString, QVariantMap))); + p->attachSlot(SIGNAL(nickRemoved(QString, QString)), this, SLOT(removeNick(QString, QString))); + p->attachSlot(SIGNAL(nickRenamed(QString, QString, QString)), this, SLOT(renameNick(QString, QString, QString))); + p->attachSlot(SIGNAL(nickUpdated(QString, QString, QVariantMap)), this, SLOT(updateNick(QString, QString, QVariantMap))); + p->attachSlot(SIGNAL(ownNickSet(QString, QString)), this, SLOT(setOwnNick(QString, QString))); + p->attachSlot(SIGNAL(backlogData(BufferId, const QVariantList &, bool)), this, SLOT(recvBacklogData(BufferId, const QVariantList &, bool))); + p->attachSlot(SIGNAL(bufferIdUpdated(BufferId)), this, SLOT(updateBufferId(BufferId))); + p->attachSignal(this, SIGNAL(sendInput(BufferId, QString))); + p->attachSignal(this, SIGNAL(requestNetworkStates())); + + connect(mainUi, SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &))); + connect(mainUi, SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore())); + connect(this, SIGNAL(connected()), mainUi, SLOT(connectedToCore())); + connect(this, SIGNAL(disconnected()), mainUi, SLOT(disconnectedFromCore())); layoutTimer = new QTimer(this); layoutTimer->setInterval(0); layoutTimer->setSingleShot(false); connect(layoutTimer, SIGNAL(timeout()), this, SLOT(layoutMsg())); - - /* make lookups by id faster */ - foreach(BufferId id, coreBuffers) { - bufferIds[id.uid()] = id; // make lookups by id faster - buffer(id); // create all buffers, so we see them in the network views - emit requestBacklog(id, -1, -1); // TODO: use custom settings for backlog request - } } Client::~Client() { - //delete mainUi; - //delete _bufferModel; - foreach(Buffer *buf, buffers.values()) delete buf; - ClientProxy::destroy(); - + foreach(Buffer *buf, buffers.values()) delete buf; // this is done by disconnectFromCore()! FIXME? + Q_ASSERT(!buffers.count()); } BufferTreeModel *Client::bufferModel() { return instance()->_bufferModel; } -void Client::coreConnected() { +SignalProxy *Client::signalProxy() { + return instance()->_signalProxy; +} + +bool Client::isConnected() { + return connectedToCore; +} +void Client::connectToCore(const QVariantMap &conn) { + // TODO implement SSL + coreConnectionInfo = conn; + if(isConnected()) { + emit coreConnectionError(tr("Already connected to Core!")); + return; + } + if(socket != 0) socket->deleteLater(); + if(conn["Host"].toString().isEmpty()) { + clientMode = LocalCore; + socket = new QBuffer(this); + connect(socket, SIGNAL(readyRead()), this, SLOT(coreHasData())); + socket->open(QIODevice::ReadWrite); + //QVariant state = connectToLocalCore(coreConnectionInfo["User"].toString(), coreConnectionInfo["Password"].toString()); + //syncToCore(state); + coreSocketConnected(); + } else { + clientMode = RemoteCore; + emit coreConnectionMsg(tr("Connecting...")); + Q_ASSERT(!socket); + QTcpSocket *sock = new QTcpSocket(this); + socket = sock; + connect(sock, SIGNAL(readyRead()), this, SLOT(coreHasData())); + connect(sock, SIGNAL(connected()), this, SLOT(coreSocketConnected())); + connect(sock, SIGNAL(disconnected()), this, SLOT(coreSocketDisconnected())); + connect(signalProxy(), SIGNAL(peerDisconnected()), this, SLOT(coreSocketDisconnected())); + //connect(sock, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(coreSocketStateChanged(QAbstractSocket::SocketState))); + connect(sock, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(coreSocketError(QAbstractSocket::SocketError))); + sock->connectToHost(conn["Host"].toString(), conn["Port"].toUInt()); + } } -void Client::coreDisconnected() { +void Client::disconnectFromCore() { + if(clientMode == RemoteCore) { + socket->close(); + //QAbstractSocket *sock = qobject_cast(socket); + //Q_ASSERT(sock); + //sock->disconnectFromHost(); + } else { + socket->close(); + //disconnectFromLocalCore(); + coreSocketDisconnected(); + } +} +void Client::coreSocketConnected() { + connect(this, SIGNAL(recvPartialItem(uint, uint)), this, SIGNAL(coreConnectionProgress(uint, uint))); + emit coreConnectionMsg(tr("Synchronizing to core...")); + QVariantMap clientInit; + clientInit["GuiProtocol"] = GUI_PROTOCOL; + clientInit["User"] = coreConnectionInfo["User"].toString(); + clientInit["Password"] = coreConnectionInfo["Password"].toString(); + writeDataToDevice(socket, clientInit); } -void Client::updateCoreData(UserId, QString key) { - if(clientMode == LocalCore) return; - QVariant data = Global::data(key); - recvProxySignal(GS_UPDATE_GLOBAL_DATA, key, data, QVariant()); +void Client::coreSocketDisconnected() { + connectedToCore = false; + emit disconnected(); + socket->deleteLater(); + blockSize = 0; + + /* Clear internal data. Hopefully nothing relies on it at this point. */ + _bufferModel->clear(); + // Buffers, if deleted, send a signal that causes their removal from buffers and bufferIds. + // So we cannot simply go through the array in a loop (or use qDeleteAll) for deletion... + while(buffers.count()) { delete buffers.take(buffers.keys()[0]); } + Q_ASSERT(!buffers.count()); // should be empty now! + Q_ASSERT(!bufferIds.count()); + coreConnectionInfo.clear(); + sessionData.clear(); + nicks.clear(); + netConnected.clear(); + netsAwaitingInit.clear(); + ownNick.clear(); + layoutQueue.clear(); + layoutTimer->stop(); +} + +void Client::coreSocketStateChanged(QAbstractSocket::SocketState state) { + if(state == QAbstractSocket::UnconnectedState) coreSocketDisconnected(); +} + +void Client::recvCoreState(const QVariant &state) { + disconnect(this, SIGNAL(recvPartialItem(uint, uint)), this, SIGNAL(coreConnectionProgress(uint, uint))); + disconnect(socket, 0, this, 0); // rest of communication happens through SignalProxy + signalProxy()->addPeer(socket); + syncToCore(state); +} + +// TODO: auth errors +void Client::syncToCore(const QVariant &coreState) { + if(!coreState.toMap().contains("SessionState")) { + emit coreConnectionError(tr("Invalid data received from core!")); + disconnectFromCore(); + return; + } + QVariantMap sessionState = coreState.toMap()["SessionState"].toMap(); + QVariantMap sessData = sessionState["SessionData"].toMap(); + + foreach(QString key, sessData.keys()) { + recvSessionData(key, sessData[key]); + } + QList coreBuffers = sessionState["Buffers"].toList(); + /* make lookups by id faster */ + foreach(QVariant vid, coreBuffers) { + BufferId id = vid.value(); + bufferIds[id.uid()] = id; // make lookups by id faster + buffer(id); // create all buffers, so we see them in the network views + } + netsAwaitingInit = sessionState["Networks"].toStringList(); + connectedToCore = true; + if(netsAwaitingInit.count()) { + emit coreConnectionMsg(tr("Requesting network states...")); + emit coreConnectionProgress(0, netsAwaitingInit.count()); + emit requestNetworkStates(); + } + else { + emit coreConnectionProgress(1, 1); + emit connected(); + } } -void Client::updateLocalData(QString key, QVariant data) { - Global::updateData(key, data); +void Client::recvSessionData(const QString &key, const QVariant &data) { + sessionData[key] = data; + emit sessionDataChanged(key, data); + emit sessionDataChanged(key); } -void Client::recvProxySignal(ClientSignal sig, QVariant arg1, QVariant arg2, QVariant arg3) { - if(clientMode == LocalCore) return; - QList sigdata; - sigdata.append(sig); sigdata.append(arg1); sigdata.append(arg2); sigdata.append(arg3); - //qDebug() << "Sending signal: " << sigdata; - writeDataToDevice(&socket, QVariant(sigdata)); +void Client::storeSessionData(const QString &key, const QVariant &data) { + // Not sure if this is a good idea, but we'll try it anyway: + // Calling this function only sends a signal to core. Data is stored upon reception of the update signal, + // rather than immediately. + emit instance()->sendSessionData(key, data); } -void Client::connectToCore(QString host, quint16 port) { - // TODO implement SSL - socket.connectToHost(host, port); +QVariant Client::retrieveSessionData(const QString &key, const QVariant &def) { + if(instance()->sessionData.contains(key)) return instance()->sessionData[key]; + else return def; } -void Client::disconnectFromCore() { - socket.close(); +QStringList Client::sessionDataKeys() { + return instance()->sessionData.keys(); } -void Client::serverError(QAbstractSocket::SocketError) { - emit coreConnectionError(socket.errorString()); +void Client::coreSocketError(QAbstractSocket::SocketError) { + emit coreConnectionError(socket->errorString()); + socket->deleteLater(); } -void Client::serverHasData() { +void Client::coreHasData() { QVariant item; - while(readDataFromDevice(&socket, blockSize, item)) { + if(readDataFromDevice(socket, blockSize, item)) { emit recvPartialItem(1,1); - QList sigdata = item.toList(); - Q_ASSERT(sigdata.size() == 4); - ClientProxy::instance()->recv((CoreSignal)sigdata[0].toInt(), sigdata[1], sigdata[2], sigdata[3]); + recvCoreState(item); blockSize = 0; + return; } if(blockSize > 0) { - emit recvPartialItem(socket.bytesAvailable(), blockSize); + emit recvPartialItem(socket->bytesAvailable(), blockSize); } } void Client::networkConnected(QString net) { - connected[net] = true; + Q_ASSERT(!netsAwaitingInit.contains(net)); + netConnected[net] = true; BufferId id = statusBufferId(net); Buffer *b = buffer(id); b->setActive(true); @@ -189,7 +297,13 @@ void Client::networkDisconnected(QString net) { //b->displayMsg(Message(id, Message::Server, tr("Server disconnected."))); FIXME b->setActive(false); } - connected[net] = false; + netConnected[net] = false; + if(netsAwaitingInit.contains(net)) { + qDebug() << "Network" << net << "disconnected while not yet initialized!"; + netsAwaitingInit.removeAll(net); + emit coreConnectionProgress(netConnected.count(), netConnected.count() + netsAwaitingInit.count()); + if(!netsAwaitingInit.count()) emit connected(); + } } void Client::updateBufferId(BufferId id) { @@ -218,18 +332,29 @@ Buffer * Client::buffer(BufferId id) { connect(b, SIGNAL(userInput(BufferId, QString)), client, SLOT(userInput(BufferId, QString))); connect(b, SIGNAL(bufferUpdated(Buffer *)), client, SIGNAL(bufferUpdated(Buffer *))); connect(b, SIGNAL(bufferDestroyed(Buffer *)), client, SIGNAL(bufferDestroyed(Buffer *))); + connect(b, SIGNAL(bufferDestroyed(Buffer *)), client, SLOT(removeBuffer(Buffer *))); buffers[id] = b; emit client->bufferUpdated(b); } return buffers[id]; } +QList Client::allBufferIds() { + return buffers.keys(); +} + +void Client::removeBuffer(Buffer *b) { + buffers.remove(b->bufferId()); + bufferIds.remove(b->bufferId().uid()); +} + void Client::recvNetworkState(QString net, QVariant state) { - connected[net] = true; + netsAwaitingInit.removeAll(net); + netConnected[net] = true; setOwnNick(net, state.toMap()["OwnNick"].toString()); buffer(statusBufferId(net))->setActive(true); - VarMap t = state.toMap()["Topics"].toMap(); - VarMap n = state.toMap()["Nicks"].toMap(); + QVariantMap t = state.toMap()["Topics"].toMap(); + QVariantMap n = state.toMap()["Nicks"].toMap(); foreach(QVariant v, t.keys()) { QString buf = v.toString(); BufferId id = bufferId(net, buf); @@ -239,16 +364,18 @@ void Client::recvNetworkState(QString net, QVariant state) { foreach(QString nick, n.keys()) { addNick(net, nick, n[nick].toMap()); } + emit coreConnectionProgress(netConnected.count(), netConnected.count() + netsAwaitingInit.count()); + if(!netsAwaitingInit.count()) emit connected(); } void Client::recvMessage(const Message &msg) { - Buffer *b = buffer(msg.buffer); + Buffer *b = buffer(msg.buffer()); Buffer::ActivityLevel level = Buffer::OtherActivity; - if(msg.type == Message::Plain || msg.type == Message::Notice){ + if(msg.type() == Message::Plain || msg.type() == Message::Notice){ level |= Buffer::NewMessage; } - if(msg.flags & Message::Highlight){ + if(msg.flags() & Message::Highlight){ level |= Buffer::Highlight; } emit bufferActivity(level, b); @@ -256,12 +383,12 @@ void Client::recvMessage(const Message &msg) { b->appendMsg(msg); } -void Client::recvStatusMsg(QString net, QString msg) { +void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) { //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg))); } -void Client::recvBacklogData(BufferId id, const QList &msgs, bool done) { +void Client::recvBacklogData(BufferId id, QVariantList msgs, bool /*done*/) { Buffer *b = buffer(id); foreach(QVariant v, msgs) { Message msg = v.value(); @@ -289,7 +416,7 @@ void Client::userInput(BufferId id, QString msg) { void Client::setTopic(QString net, QString buf, QString topic) { BufferId id = bufferId(net, buf); - if(!connected[id.network()]) return; + if(!netConnected[id.network()]) return; Buffer *b = buffer(id); b->setTopic(topic); //if(!b->isActive()) { @@ -298,10 +425,10 @@ void Client::setTopic(QString net, QString buf, QString topic) { //} } -void Client::addNick(QString net, QString nick, VarMap props) { - if(!connected[net]) return; +void Client::addNick(QString net, QString nick, QVariantMap props) { + if(!netConnected[net]) return; nicks[net][nick] = props; - VarMap chans = props["Channels"].toMap(); + QVariantMap chans = props["Channels"].toMap(); QStringList c = chans.keys(); foreach(QString bufname, c) { buffer(bufferId(net, bufname))->addNick(nick, props); @@ -309,7 +436,7 @@ void Client::addNick(QString net, QString nick, VarMap props) { } void Client::renameNick(QString net, QString oldnick, QString newnick) { - if(!connected[net]) return; + if(!netConnected[net]) return; QStringList chans = nicks[net][oldnick]["Channels"].toMap().keys(); foreach(QString c, chans) { buffer(bufferId(net, c))->renameNick(oldnick, newnick); @@ -317,8 +444,8 @@ void Client::renameNick(QString net, QString oldnick, QString newnick) { nicks[net][newnick] = nicks[net].take(oldnick); } -void Client::updateNick(QString net, QString nick, VarMap props) { - if(!connected[net]) return; +void Client::updateNick(QString net, QString nick, QVariantMap props) { + if(!netConnected[net]) return; QStringList oldchans = nicks[net][nick]["Channels"].toMap().keys(); QStringList newchans = props["Channels"].toMap().keys(); foreach(QString c, newchans) { @@ -332,8 +459,8 @@ void Client::updateNick(QString net, QString nick, VarMap props) { } void Client::removeNick(QString net, QString nick) { - if(!connected[net]) return; - VarMap chans = nicks[net][nick]["Channels"].toMap(); + if(!netConnected[net]) return; + QVariantMap chans = nicks[net][nick]["Channels"].toMap(); foreach(QString bufname, chans.keys()) { buffer(bufferId(net, bufname))->removeNick(nick); } @@ -341,7 +468,7 @@ void Client::removeNick(QString net, QString nick) { } void Client::setOwnNick(QString net, QString nick) { - if(!connected[net]) return; + if(!netConnected[net]) return; ownNick[net] = nick; foreach(BufferId id, buffers.keys()) { if(id.network() == net) { @@ -350,4 +477,3 @@ void Client::setOwnNick(QString net, QString nick) { } } -