X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fclient%2Fclient.cpp;h=b561fd12737cd6904210d86905b1691fff6fe548;hp=6be7e0778efc9f634831ee2ec7cbf26562c24925;hb=999dd26d04c96a142ee0ee8e1d066d939b7c4499;hpb=0ac9ce4d7cf768d13993d6aa1d6b791c4149a843 diff --git a/src/client/client.cpp b/src/client/client.cpp index 6be7e077..b561fd12 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -22,18 +22,20 @@ #include "buffer.h" #include "buffertreemodel.h" -#include "clientproxy.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::nicks; QHash Client::netConnected; +QStringList Client::netsAwaitingInit; QHash Client::ownNick; Client *Client::instance() { @@ -48,18 +50,10 @@ void Client::destroy() { } Client::Client() { - clientProxy = ClientProxy::instance(); + _signalProxy = new SignalProxy(SignalProxy::Client, 0, this); - _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 *))); - - // 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) { @@ -70,33 +64,33 @@ 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))); - connect(this, SIGNAL(requestNetworkStates()), clientProxy, SLOT(gsRequestNetworkStates())); - - connect(mainUi, SIGNAL(connectToCore(const VarMap &)), this, SLOT(connectToCore(const VarMap &))); + _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())); @@ -105,75 +99,127 @@ void Client::init() { layoutTimer->setInterval(0); layoutTimer->setSingleShot(false); connect(layoutTimer, SIGNAL(timeout()), this, SLOT(layoutMsg())); - } 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; } -bool Client::isConnected() { return connectedToCore; } +SignalProxy *Client::signalProxy() { + return instance()->_signalProxy; +} + +bool Client::isConnected() { + return connectedToCore; +} -void Client::connectToCore(const VarMap &conn) { +void Client::connectToCore(const QVariantMap &conn) { // TODO implement SSL + coreConnectionInfo = conn; if(isConnected()) { - qDebug() << "Already connected to core!"; + emit coreConnectionError(tr("Already connected to Core!")); return; } + if(socket != 0) socket->deleteLater(); if(conn["Host"].toString().isEmpty()) { clientMode = LocalCore; - syncToCore(); // TODO send user and pw from conn info + 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; - socket.connectToHost(conn["Host"].toString(), conn["Port"].toUInt()); + 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::disconnectFromCore() { if(clientMode == RemoteCore) { - socket.close(); + socket->close(); + //QAbstractSocket *sock = qobject_cast(socket); + //Q_ASSERT(sock); + //sock->disconnectFromHost(); } else { - disconnectFromLocalCore(); - coreDisconnected(); + socket->close(); + //disconnectFromLocalCore(); + coreSocketDisconnected(); } - // TODO clear internal data } -void Client::coreConnected() { - syncToCore(); - +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::coreDisconnected() { +void Client::coreSocketDisconnected() { connectedToCore = false; emit disconnected(); -} - -void Client::syncToCore() { - VarMap state; - if(clientMode == LocalCore) { - state = connectToLocalCore("Default", "password").toMap(); // TODO make this configurable - } else { + 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(); - VarMap data = state["CoreData"].toMap(); - foreach(QString key, data.keys()) { - Global::updateData(key, data[key]); + foreach(QString key, sessData.keys()) { + recvSessionData(key, sessData[key]); } - //if(!Global::data("CoreReady").toBool()) { - // qFatal("Something is wrong, getting invalid data from core!"); - //} - - VarMap sessionState = state["SessionState"].toMap(); QList coreBuffers = sessionState["Buffers"].toList(); /* make lookups by id faster */ foreach(QVariant vid, coreBuffers) { @@ -181,48 +227,61 @@ void Client::syncToCore() { 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; - emit connected(); - emit requestNetworkStates(); + 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::recvSessionData(const QString &key, const QVariant &data) { + sessionData[key] = data; + emit sessionDataChanged(key, data); + emit sessionDataChanged(key); } -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::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::updateLocalData(QString key, QVariant data) { - Global::updateData(key, data); +QVariant Client::retrieveSessionData(const QString &key, const QVariant &def) { + if(instance()->sessionData.contains(key)) return instance()->sessionData[key]; + else return def; } -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)); +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) { + Q_ASSERT(!netsAwaitingInit.contains(net)); netConnected[net] = true; BufferId id = statusBufferId(net); Buffer *b = buffer(id); @@ -239,6 +298,12 @@ void Client::networkDisconnected(QString net) { b->setActive(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) { @@ -267,6 +332,7 @@ 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); } @@ -277,12 +343,18 @@ 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) { + 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); @@ -292,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); @@ -314,7 +388,7 @@ void Client::recvStatusMsg(QString /*net*/, QString /*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(); @@ -351,10 +425,10 @@ void Client::setTopic(QString net, QString buf, QString topic) { //} } -void Client::addNick(QString net, QString nick, VarMap props) { +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); @@ -370,7 +444,7 @@ 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) { +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(); @@ -386,7 +460,7 @@ void Client::updateNick(QString net, QString nick, VarMap props) { void Client::removeNick(QString net, QString nick) { if(!netConnected[net]) return; - VarMap chans = nicks[net][nick]["Channels"].toMap(); + QVariantMap chans = nicks[net][nick]["Channels"].toMap(); foreach(QString bufname, chans.keys()) { buffer(bufferId(net, bufname))->removeNick(nick); }