X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fclient%2Fclient.cpp;h=2500b27334264df32bcf4b9f6570f50e572957e7;hp=1924001bdb75e63a392e3ce3805e00920f19b488;hb=0ff076706c3d353ec9b098b1eca270195288774e;hpb=130ccf5033c21ca3ad0e0357a82953b5727bd0e9 diff --git a/src/client/client.cpp b/src/client/client.cpp index 1924001b..2500b273 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -18,337 +18,505 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -#include "gui.h" -#include "guiproxy.h" -#include "mainwin.h" -#include "buffer.h" -//#include "bufferview.h" -#include "bufferviewwidget.h" -#include "util.h" +#include "client.h" + +#include "networkinfo.h" +#include "ircuser.h" +#include "ircchannel.h" -Client * Client::instanceptr = 0; +#include "message.h" -Client::ClientMode Client::clientMode; -QHash Client::buffers; -QHash Client::bufferIds; -QHash > Client::nicks; -QHash Client::connected; -QHash Client::ownNick; -QList Client::coreBuffers; +#include "bufferinfo.h" +#include "buffertreemodel.h" +#include "quasselui.h" +#include "signalproxy.h" +#include "synchronizer.h" +#include "util.h" +QPointer Client::instanceptr = 0; +// ============================== +// public Static Methods +// ============================== Client *Client::instance() { - if(instanceptr) return instanceptr; - instanceptr = new Client(); - instanceptr->init(); + if(!instanceptr) + instanceptr = new Client(); return instanceptr; } void Client::destroy() { delete instanceptr; - instanceptr = 0; } -Client::Client() { - clientProxy = ClientProxy::instance(); +void Client::init(AbstractUi *ui) { + instance()->mainUi = ui; + instance()->init(); +} - //mainWin = new MainWin(); +QList Client::networkInfos() { + return instance()->_networkInfo.values(); +} - _bufferModel = new BufferTreeModel(0); // FIXME +NetworkInfo *Client::networkInfo(uint networkid) { + if(instance()->_networkInfo.contains(networkid)) + return instance()->_networkInfo[networkid]; + else + return 0; +} - 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 *))); +QList Client::allBufferInfos() { + QList bufferids; + foreach(Buffer *buffer, buffers()) { + bufferids << buffer->bufferInfo(); + } + return bufferids; +} - // TODO: make this configurable (allow monolithic client to connect to remote cores) - if(Global::runMode == Global::Monolithic) clientMode = LocalCore; - else clientMode = RemoteCore; +QList Client::buffers() { + return instance()->_buffers.values(); } -void Client::init() { - blockSize = 0; +Buffer *Client::buffer(uint bufferUid) { + if(instance()->_buffers.contains(bufferUid)) + return instance()->_buffers[bufferUid]; + else + return 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(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, 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(); +Buffer *Client::buffer(BufferInfo id) { + Buffer *buff = buffer(id.uid()); - layoutTimer = new QTimer(this); - layoutTimer->setInterval(0); - layoutTimer->setSingleShot(false); - connect(layoutTimer, SIGNAL(timeout()), this, SLOT(layoutMsg())); + if(!buff) { + Client *client = Client::instance(); + buff = new Buffer(id, client); - /* 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 + connect(buff, SIGNAL(userInput(BufferInfo, QString)), + client, SLOT(userInput(BufferInfo, QString))); + connect(buff, SIGNAL(bufferUpdated(Buffer *)), + client, SIGNAL(bufferUpdated(Buffer *))); + connect(buff, SIGNAL(destroyed()), + client, SLOT(bufferDestroyed())); + client->_buffers[id.uid()] = buff; + emit client->bufferUpdated(buff); } - - mainWin = new MainWin(); - mainWin->init(); - + Q_ASSERT(buff); + return buff; } -Client::~Client() { - delete mainWin; - delete _bufferModel; - foreach(Buffer *buf, buffers.values()) delete buf; - ClientProxy::destroy(); +// FIXME switch to netids! +// WHEN IS THIS NEEDED ANYHOW!? +BufferInfo Client::bufferInfo(QString net, QString buf) { + foreach(Buffer *buffer_, buffers()) { + BufferInfo bufferInfo = buffer_->bufferInfo(); + if(bufferInfo.network() == net && bufferInfo.buffer() == buf) + return bufferInfo; + } + Q_ASSERT(false); // should never happen! + return BufferInfo(); +} +BufferInfo Client::statusBufferInfo(QString net) { + return bufferInfo(net, ""); } BufferTreeModel *Client::bufferModel() { return instance()->_bufferModel; } -void Client::coreConnected() { +SignalProxy *Client::signalProxy() { + return instance()->_signalProxy; +} +// ============================== +// Constructor / Decon +// ============================== +Client::Client(QObject *parent) + : QObject(parent), + socket(0), + _signalProxy(new SignalProxy(SignalProxy::Client, this)), + mainUi(0), + _bufferModel(0), + connectedToCore(false) +{ } -void Client::coreDisconnected() { +Client::~Client() { +} + +void Client::init() { + blockSize = 0; + + _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(networkConnected(uint)), + this, SLOT(networkConnected(uint))); + p->attachSlot(SIGNAL(networkDisconnected(uint)), + this, SLOT(networkDisconnected(uint))); + 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(backlogData(BufferInfo, const QVariantList &, bool)), this, SLOT(recvBacklogData(BufferInfo, const QVariantList &, bool))); + p->attachSlot(SIGNAL(bufferInfoUpdated(BufferInfo)), this, SLOT(updateBufferInfo(BufferInfo))); + p->attachSignal(this, SIGNAL(sendInput(BufferInfo, 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())); } -void Client::updateCoreData(UserId, QString key) { - if(clientMode == LocalCore) return; - QVariant data = Global::data(key); - recvProxySignal(GS_UPDATE_GLOBAL_DATA, key, data, QVariant()); +bool Client::isConnected() { + return instance()->connectedToCore; } -void Client::updateLocalData(QString key, QVariant data) { - Global::updateData(key, data); +void Client::fakeInput(uint bufferUid, QString message) { + Buffer *buff = buffer(bufferUid); + if(!buff) + qWarning() << "No Buffer with uid" << bufferUid << "can't send Input" << message; + else + emit instance()->sendInput(buff->bufferInfo(), message); } -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::fakeInput(BufferInfo bufferInfo, QString message) { + fakeInput(bufferInfo, message); } -void Client::connectToCore(QString host, quint16 port) { +void Client::connectToCore(const QVariantMap &conn) { // TODO implement SSL - socket.connectToHost(host, port); + 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(disconnected()), 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() { - socket.close(); + socket->close(); + if(clientMode == LocalCore) { + coreSocketDisconnected(); + } } -void Client::serverError(QAbstractSocket::SocketError) { - emit coreConnectionError(socket.errorString()); +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::serverHasData() { - QVariant item; - while(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]); - blockSize = 0; +void Client::coreSocketDisconnected() { + instance()->connectedToCore = false; + emit disconnected(); + socket->deleteLater(); + blockSize = 0; + + /* Clear internal data. Hopefully nothing relies on it at this point. */ + _bufferModel->clear(); + foreach(Buffer *buffer, _buffers.values()) { + buffer->deleteLater(); } - if(blockSize > 0) { - emit recvPartialItem(socket.bytesAvailable(), blockSize); + _buffers.clear(); + + foreach(NetworkInfo *networkinfo, _networkInfo.values()) { + networkinfo->deleteLater(); } + _networkInfo.clear(); + + coreConnectionInfo.clear(); + sessionData.clear(); + layoutQueue.clear(); + layoutTimer->stop(); } -/*******************************************************************************************************************/ +void Client::coreSocketStateChanged(QAbstractSocket::SocketState state) { + if(state == QAbstractSocket::UnconnectedState) coreSocketDisconnected(); +} -void Client::networkConnected(QString net) { - connected[net] = true; - BufferId id = statusBufferId(net); - Buffer *b = buffer(id); - b->setActive(true); - //b->displayMsg(Message(id, Message::Server, tr("Connected."))); - // TODO buffersUpdated(); +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); } -void Client::networkDisconnected(QString net) { - foreach(BufferId id, buffers.keys()) { - if(id.network() != net) continue; - Buffer *b = buffer(id); - //b->displayMsg(Message(id, Message::Server, tr("Server disconnected."))); FIXME - b->setActive(false); +// TODO: auth errors +void Client::syncToCore(const QVariant &coreState) { + if(!coreState.toMap().contains("SessionState")) { + emit coreConnectionError(tr("Invalid data received from core!")); + disconnectFromCore(); + return; } - connected[net] = false; -} -void Client::updateBufferId(BufferId id) { - bufferIds[id.uid()] = id; // make lookups by id faster - buffer(id); -} + QVariantMap sessionState = coreState.toMap()["SessionState"].toMap(); -BufferId Client::bufferId(QString net, QString buf) { - foreach(BufferId id, buffers.keys()) { - if(id.network() == net && id.buffer() == buf) return id; + // store sessionData + QVariantMap sessData = sessionState["SessionData"].toMap(); + foreach(QString key, sessData.keys()) + recvSessionData(key, sessData[key]); + + // store Buffer details + QVariantList coreBuffers = sessionState["Buffers"].toList(); + /* make lookups by id faster */ + foreach(QVariant vid, coreBuffers) { + buffer(vid.value()); // create all buffers, so we see them in the network views } - Q_ASSERT(false); // should never happen! - return BufferId(); -} -BufferId Client::statusBufferId(QString net) { - return bufferId(net, ""); + // create networkInfo objects + QVariantList networkids = sessionState["Networks"].toList(); + foreach(QVariant networkid, networkids) { + networkConnected(networkid.toUInt()); + } + + instance()->connectedToCore = true; + updateCoreConnectionProgress(); } +void Client::updateCoreConnectionProgress() { + // we'll do this in three steps: + // 1.) networks + // 2.) channels + // 3.) ircusers -Buffer * Client::buffer(BufferId id) { - Client *client = Client::instance(); - if(!buffers.contains(id)) { - Buffer *b = new Buffer(id); - b->setOwnNick(ownNick[id.network()]); - 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 *))); - buffers[id] = b; - emit client->bufferUpdated(b); - } - return buffers[id]; -} - -void Client::recvNetworkState(QString net, QVariant state) { - connected[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(); - foreach(QVariant v, t.keys()) { - QString buf = v.toString(); - BufferId id = bufferId(net, buf); - buffer(id)->setActive(true); - setTopic(net, buf, t[buf].toString()); + int numNets = networkInfos().count(); + int numNetsWaiting = 0; + + int numIrcUsers = 0; + int numIrcUsersWaiting = 0; + + int numChannels = 0; + int numChannelsWaiting = 0; + + foreach(NetworkInfo *net, networkInfos()) { + if(! net->initialized()) + numNetsWaiting++; + + numIrcUsers += net->ircUsers().count(); + foreach(IrcUser *user, net->ircUsers()) { + if(! user->initialized()) + numIrcUsersWaiting++; + } + + numChannels += net->ircChannels().count(); + foreach(IrcChannel *channel, net->ircChannels()) { + if(! channel->initialized()) + numChannelsWaiting++; + } } - foreach(QString nick, n.keys()) { - addNick(net, nick, n[nick].toMap()); + + if(numNetsWaiting > 0) { + emit coreConnectionMsg(tr("Requesting network states...")); + emit coreConnectionProgress(numNets - numNetsWaiting, numNets); + return; } -} -void Client::recvMessage(Message msg) { - Buffer *b = buffer(msg.buffer); + if(numIrcUsersWaiting > 0) { + emit coreConnectionMsg(tr("Requesting User states...")); + emit coreConnectionProgress(numIrcUsers - numIrcUsersWaiting, numIrcUsers); + return; + } - Buffer::ActivityLevel level = Buffer::OtherActivity; - if(msg.type == Message::Plain || msg.type == Message::Notice){ - level |= Buffer::NewMessage; + if(numChannelsWaiting > 0) { + emit coreConnectionMsg(tr("Requesting Channel states...")); + emit coreConnectionProgress(numChannels - numChannelsWaiting, numChannels); + return; } - if(msg.flags & Message::Highlight){ - level |= Buffer::Highlight; + + emit coreConnectionProgress(1,1); + emit connected(); + + foreach(NetworkInfo *net, networkInfos()) { + disconnect(net, 0, this, SLOT(updateCoreConnectionProgress())); } - emit bufferActivity(level, b); + + // signalProxy()->dumpProxyStats(); +} - //b->displayMsg(msg); - b->appendChatLine(new ChatLine(msg)); +void Client::recvSessionData(const QString &key, const QVariant &data) { + sessionData[key] = data; + emit sessionDataChanged(key, data); + emit sessionDataChanged(key); } -void Client::recvStatusMsg(QString net, QString msg) { - //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg))); +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); +} +QVariant Client::retrieveSessionData(const QString &key, const QVariant &def) { + if(instance()->sessionData.contains(key)) return instance()->sessionData[key]; + else return def; } -void Client::recvBacklogData(BufferId id, QList msgs, bool done) { - foreach(QVariant v, msgs) { - layoutQueue.append(v.value()); - } - if(!layoutTimer->isActive()) layoutTimer->start(); +QStringList Client::sessionDataKeys() { + return instance()->sessionData.keys(); } +void Client::coreSocketError(QAbstractSocket::SocketError) { + emit coreConnectionError(socket->errorString()); + socket->deleteLater(); +} -void Client::layoutMsg() { - if(layoutQueue.count()) { - ChatLine *line = new ChatLine(layoutQueue.takeFirst()); - buffer(line->bufferId())->prependChatLine(line); +void Client::coreHasData() { + QVariant item; + if(readDataFromDevice(socket, blockSize, item)) { + emit recvPartialItem(1,1); + recvCoreState(item); + blockSize = 0; + return; + } + if(blockSize > 0) { + emit recvPartialItem(socket->bytesAvailable(), blockSize); } - if(!layoutQueue.count()) layoutTimer->stop(); } -void Client::userInput(BufferId id, QString msg) { - emit sendInput(id, msg); +void Client::networkConnected(uint netid) { + // TODO: create statusBuffer / switch to networkids + //BufferInfo id = statusBufferInfo(net); + //Buffer *b = buffer(id); + //b->setActive(true); + + NetworkInfo *netinfo = new NetworkInfo(netid, signalProxy(), this); + connect(netinfo, SIGNAL(initDone()), this, SLOT(updateCoreConnectionProgress())); + connect(netinfo, SIGNAL(ircUserInitDone()), this, SLOT(updateCoreConnectionProgress())); + connect(netinfo, SIGNAL(ircChannelInitDone()), this, SLOT(updateCoreConnectionProgress())); + connect(netinfo, SIGNAL(destroyed()), this, SLOT(networkInfoDestroyed())); + _networkInfo[netid] = netinfo; } -void Client::setTopic(QString net, QString buf, QString topic) { - BufferId id = bufferId(net, buf); - if(!connected[id.network()]) return; - Buffer *b = buffer(id); - b->setTopic(topic); - //if(!b->isActive()) { - // b->setActive(true); - // buffersUpdated(); - //} -} - -void Client::addNick(QString net, QString nick, VarMap props) { - if(!connected[net]) return; - nicks[net][nick] = props; - VarMap chans = props["Channels"].toMap(); - QStringList c = chans.keys(); - foreach(QString bufname, c) { - buffer(bufferId(net, bufname))->addNick(nick, props); +void Client::networkDisconnected(uint networkid) { + foreach(Buffer *buffer, buffers()) { + if(buffer->bufferInfo().networkId() != networkid) + continue; + + //buffer->displayMsg(Message(bufferid, Message::Server, tr("Server disconnected."))); FIXME + buffer->setActive(false); + } + + Q_ASSERT(networkInfo(networkid)); + if(!networkInfo(networkid)->initialized()) { + qDebug() << "Network" << networkid << "disconnected while not yet initialized!"; + updateCoreConnectionProgress(); } } -void Client::renameNick(QString net, QString oldnick, QString newnick) { - if(!connected[net]) return; - QStringList chans = nicks[net][oldnick]["Channels"].toMap().keys(); - foreach(QString c, chans) { - buffer(bufferId(net, c))->renameNick(oldnick, newnick); - } - nicks[net][newnick] = nicks[net].take(oldnick); +void Client::updateBufferInfo(BufferInfo id) { + buffer(id)->updateBufferInfo(id); +} + +void Client::bufferDestroyed() { + Buffer *buffer = static_cast(sender()); + uint bufferUid = buffer->uid(); + if(_buffers.contains(bufferUid)) + _buffers.remove(bufferUid); +} + +void Client::networkInfoDestroyed() { + NetworkInfo *netinfo = static_cast(sender()); + uint networkId = netinfo->networkId(); + if(_networkInfo.contains(networkId)) + _networkInfo.remove(networkId); } -void Client::updateNick(QString net, QString nick, VarMap props) { - if(!connected[net]) return; - QStringList oldchans = nicks[net][nick]["Channels"].toMap().keys(); - QStringList newchans = props["Channels"].toMap().keys(); - foreach(QString c, newchans) { - if(oldchans.contains(c)) buffer(bufferId(net, c))->updateNick(nick, props); - else buffer(bufferId(net, c))->addNick(nick, props); +void Client::recvMessage(const Message &msg) { + Buffer *b = buffer(msg.buffer()); + + Buffer::ActivityLevel level = Buffer::OtherActivity; + if(msg.type() == Message::Plain || msg.type() == Message::Notice){ + level |= Buffer::NewMessage; } - foreach(QString c, oldchans) { - if(!newchans.contains(c)) buffer(bufferId(net, c))->removeNick(nick); + if(msg.flags() & Message::Highlight){ + level |= Buffer::Highlight; } - nicks[net][nick] = props; + emit bufferActivity(level, b); + + b->appendMsg(msg); } -void Client::removeNick(QString net, QString nick) { - if(!connected[net]) return; - VarMap chans = nicks[net][nick]["Channels"].toMap(); - foreach(QString bufname, chans.keys()) { - buffer(bufferId(net, bufname))->removeNick(nick); +void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) { + //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg))); +} + +void Client::recvBacklogData(BufferInfo id, QVariantList msgs, bool /*done*/) { + Buffer *b = buffer(id); + foreach(QVariant v, msgs) { + Message msg = v.value(); + b->prependMsg(msg); + if(!layoutQueue.contains(b)) layoutQueue.append(b); } - nicks[net].remove(nick); + if(layoutQueue.count() && !layoutTimer->isActive()) layoutTimer->start(); } -void Client::setOwnNick(QString net, QString nick) { - if(!connected[net]) return; - ownNick[net] = nick; - foreach(BufferId id, buffers.keys()) { - if(id.network() == net) { - buffers[id]->setOwnNick(nick); - } +void Client::layoutMsg() { + if(layoutQueue.count()) { + Buffer *b = layoutQueue.takeFirst(); // TODO make this the current buffer + if(b->layoutMsg()) + layoutQueue.append(b); // Buffer has more messages in its queue --> Round Robin } + + if(!layoutQueue.count()) + layoutTimer->stop(); } +AbstractUiMsg *Client::layoutMsg(const Message &msg) { + return instance()->mainUi->layoutMsg(msg); +} + +void Client::userInput(BufferInfo id, QString msg) { + emit sendInput(id, msg); +}