X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fclient%2Fclient.cpp;h=e65006ecf989c0cef3c5731d6015de9418ffa969;hp=6b941c65f34eaf947a0c4c1a0e68cb0c8dca943f;hb=6261af690fbf7f3aca06acc8f1268488cdfb566f;hpb=017582b02dcf25ed4027c54fbcd652004b103f91 diff --git a/src/client/client.cpp b/src/client/client.cpp index 6b941c65..e65006ec 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -1,11 +1,11 @@ /*************************************************************************** - * Copyright (C) 2005-07 by The Quassel 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 * @@ -20,24 +20,24 @@ #include "client.h" -#include "networkinfo.h" -#include "ircuser.h" +#include "bufferinfo.h" +#include "global.h" +#include "identity.h" #include "ircchannel.h" - +#include "ircuser.h" #include "message.h" - -#include "bufferinfo.h" -#include "buffertreemodel.h" +#include "network.h" +#include "networkmodel.h" +#include "buffermodel.h" #include "quasselui.h" #include "signalproxy.h" -#include "synchronizer.h" #include "util.h" QPointer Client::instanceptr = 0; +AccountId Client::_currentCoreAccount = 0; + +/*** Initialization/destruction ***/ -// ============================== -// public Static Methods -// ============================== Client *Client::instance() { if(!instanceptr) instanceptr = new Client(); @@ -45,7 +45,8 @@ Client *Client::instance() { } void Client::destroy() { - delete instanceptr; + //delete instanceptr; + instanceptr->deleteLater(); } void Client::init(AbstractUi *ui) { @@ -53,138 +54,56 @@ void Client::init(AbstractUi *ui) { instance()->init(); } -QList Client::networkInfos() { - return instance()->_networkInfo.values(); -} - -NetworkInfo *Client::networkInfo(uint networkid) { - if(instance()->_networkInfo.contains(networkid)) - return instance()->_networkInfo[networkid]; - else - return 0; -} - -QList Client::allBufferInfos() { - QList bufferids; - foreach(Buffer *buffer, buffers()) { - bufferids << buffer->bufferInfo(); - } - return bufferids; -} - -QList Client::buffers() { - return instance()->_buffers.values(); -} - -Buffer *Client::buffer(uint bufferUid) { - if(instance()->_buffers.contains(bufferUid)) - return instance()->_buffers[bufferUid]; - else - return 0; -} - -Buffer *Client::buffer(BufferInfo id) { - Buffer *buff = buffer(id.uid()); - - if(!buff) { - Client *client = Client::instance(); - Buffer *buff = new Buffer(id, client); - - connect(buff, SIGNAL(userInput(BufferInfo, QString)), - client, SLOT(userInput(BufferInfo, QString))); - connect(buff, SIGNAL(bufferUpdated(Buffer *)), - client, SIGNAL(bufferUpdated(Buffer *))); - connect(buff, SIGNAL(bufferDestroyed(Buffer *)), - client, SIGNAL(bufferDestroyed(Buffer *))); - connect(buff, SIGNAL(bufferDestroyed(Buffer *)), - client, SLOT(removeBuffer(Buffer *))); - - client->_buffers[id.uid()] = buff; - emit client->bufferUpdated(buff); - } - - return buff; -} - -// 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; -} - -SignalProxy *Client::signalProxy() { - return instance()->_signalProxy; -} - -// ============================== -// Constructor / Decon -// ============================== Client::Client(QObject *parent) : QObject(parent), socket(0), - _signalProxy(new SignalProxy(SignalProxy::Client, 0, this)), + _signalProxy(new SignalProxy(SignalProxy::Client, this)), mainUi(0), + _networkModel(0), _bufferModel(0), - connectedToCore(false) + _connectedToCore(false), + _syncedToCore(false) { + _monitorBuffer = new Buffer(BufferInfo(), this); } Client::~Client() { -// since we're now the parent of buffers this should be no longer needed - -// foreach(Buffer *buf, buffers.values()) delete buf; // this is done by disconnectFromCore()! FIXME? -// Q_ASSERT(!buffers.count()); + disconnectFromCore(); } void Client::init() { - blockSize = 0; - - _bufferModel = new BufferTreeModel(this); + _currentCoreAccount = 0; + _networkModel = new NetworkModel(this); + connect(this, SIGNAL(bufferUpdated(BufferInfo)), + _networkModel, SLOT(bufferUpdated(BufferInfo))); - 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 *))); + _bufferModel = new BufferModel(_networkModel); 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(displayMsg(const Message &)), this, SLOT(recvMessage(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 &))); + p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &)), SIGNAL(createIdentity(const Identity &))); + p->attachSignal(this, SIGNAL(requestUpdateIdentity(const Identity &)), SIGNAL(updateIdentity(const Identity &))); + p->attachSignal(this, SIGNAL(requestRemoveIdentity(IdentityId)), SIGNAL(removeIdentity(IdentityId))); + p->attachSlot(SIGNAL(identityCreated(const Identity &)), this, SLOT(coreIdentityCreated(const Identity &))); + p->attachSlot(SIGNAL(identityRemoved(IdentityId)), this, SLOT(coreIdentityRemoved(IdentityId))); + + p->attachSignal(this, SIGNAL(requestCreateNetwork(const NetworkInfo &)), SIGNAL(createNetwork(const NetworkInfo &))); + p->attachSignal(this, SIGNAL(requestUpdateNetwork(const NetworkInfo &)), SIGNAL(updateNetwork(const NetworkInfo &))); + p->attachSignal(this, SIGNAL(requestRemoveNetwork(NetworkId)), SIGNAL(removeNetwork(NetworkId))); + p->attachSlot(SIGNAL(networkCreated(NetworkId)), this, SLOT(coreNetworkCreated(NetworkId))); + p->attachSlot(SIGNAL(networkRemoved(NetworkId)), this, SLOT(coreNetworkRemoved(NetworkId))); + + connect(p, SIGNAL(disconnected()), this, SLOT(disconnectFromCore())); + + //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())); @@ -196,283 +115,297 @@ void Client::init() { } -bool Client::isConnected() { - return instance()->connectedToCore; +/*** public static methods ***/ + +AccountId Client::currentCoreAccount() { + return _currentCoreAccount; } -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::setCurrentCoreAccount(AccountId id) { + _currentCoreAccount = id; } -void Client::disconnectFromCore() { - if(clientMode == RemoteCore) { - socket->close(); - //QAbstractSocket *sock = qobject_cast(socket); - //Q_ASSERT(sock); - //sock->disconnectFromHost(); - } else { - socket->close(); - //disconnectFromLocalCore(); - coreSocketDisconnected(); +QList Client::allBufferInfos() { + QList bufferids; + foreach(Buffer *buffer, buffers()) { + bufferids << buffer->bufferInfo(); } + return bufferids; } -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); +QList Client::buffers() { + return instance()->_buffers.values(); } -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(); - // Buffers, if deleted, send a signal that causes their removal from buffers and bufferInfos. - // So we cannot simply go through the array in a loop (or use qDeleteAll) for deletion... - while(!_buffers.empty()) { - delete _buffers.take(_buffers.keys()[0]); - } - Q_ASSERT(_buffers.empty()); - while(!_networkInfo.empty()) { - delete _networkInfo.take(_networkInfo.keys()[0]); - } - - coreConnectionInfo.clear(); - sessionData.clear(); - layoutQueue.clear(); - layoutTimer->stop(); +// FIXME remove +Buffer *Client::buffer(BufferId bufferId) { + if(instance()->_buffers.contains(bufferId)) + return instance()->_buffers[bufferId]; + else + return 0; } -void Client::coreSocketStateChanged(QAbstractSocket::SocketState state) { - if(state == QAbstractSocket::UnconnectedState) coreSocketDisconnected(); +// FIXME remove +Buffer *Client::buffer(BufferInfo bufferInfo) { + Buffer *buff = buffer(bufferInfo.bufferId()); + + if(!buff) { + Client *client = Client::instance(); + buff = new Buffer(bufferInfo, client); + connect(buff, SIGNAL(destroyed()), + client, SLOT(bufferDestroyed())); + client->_buffers[bufferInfo.bufferId()] = buff; + emit client->bufferUpdated(bufferInfo); + } + Q_ASSERT(buff); + return buff; } -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); +Buffer *Client::monitorBuffer() { + return instance()->_monitorBuffer; } -// 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(); +NetworkModel *Client::networkModel() { + return instance()->_networkModel; +} - // store sessionData - QVariantMap sessData = sessionState["SessionData"].toMap(); - foreach(QString key, sessData.keys()) - recvSessionData(key, sessData[key]); +BufferModel *Client::bufferModel() { + return instance()->_bufferModel; +} - // 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 - } +SignalProxy *Client::signalProxy() { + return instance()->_signalProxy; +} - // create networkInfo objects - QVariantList networkids = sessionState["Networks"].toList(); - foreach(QVariant networkid, networkids) { - networkConnected(networkid.toUInt()); - } - - instance()->connectedToCore = true; - updateCoreConnectionProgress(); +bool Client::isConnected() { + return instance()->_connectedToCore; } -void Client::updateCoreConnectionProgress() { - // we'll do this in three steps: - // 1.) networks - // 2.) channels - // 3.) ircusers +bool Client::isSynced() { + return instance()->_syncedToCore; +} - int numNets = networkInfos().count(); - int numNetsWaiting = 0; +/*** Network handling ***/ - int numIrcUsers = 0; - int numIrcUsersWaiting = 0; +QList Client::networkIds() { + return instance()->_networks.keys(); +} - int numChannels = 0; - int numChannelsWaiting = 0; +const Network * Client::network(NetworkId networkid) { + if(instance()->_networks.contains(networkid)) return instance()->_networks[networkid]; + else return 0; +} - foreach(NetworkInfo *net, networkInfos()) { - if(! net->initialized()) - numNetsWaiting++; +void Client::createNetwork(const NetworkInfo &info) { + emit instance()->requestCreateNetwork(info); +} - numIrcUsers += net->ircUsers().count(); - foreach(IrcUser *user, net->ircUsers()) { - if(! user->initialized()) - numIrcUsersWaiting++; - } +void Client::updateNetwork(const NetworkInfo &info) { + emit instance()->requestUpdateNetwork(info); +} - numChannels += net->ircChannels().count(); - foreach(IrcChannel *channel, net->ircChannels()) { - if(! channel->initialized()) - numChannelsWaiting++; - } +void Client::removeNetwork(NetworkId id) { + emit instance()->requestRemoveNetwork(id); +} - } +void Client::addNetwork(Network *net) { + net->setProxy(signalProxy()); + signalProxy()->synchronize(net); + networkModel()->attachNetwork(net); + connect(net, SIGNAL(destroyed()), instance(), SLOT(networkDestroyed())); + instance()->_networks[net->networkId()] = net; + emit instance()->networkCreated(net->networkId()); +} - if(numNetsWaiting > 0) { - emit coreConnectionMsg(tr("Requesting network states...")); - emit coreConnectionProgress(numNets - numNetsWaiting, numNets); +void Client::coreNetworkCreated(NetworkId id) { + if(_networks.contains(id)) { + qWarning() << "Creation of already existing network requested!"; return; } + Network *net = new Network(id, this); + addNetwork(net); +} - if(numIrcUsersWaiting > 0) { - emit coreConnectionMsg(tr("Requesting User states...")); - emit coreConnectionProgress(numIrcUsers - numIrcUsersWaiting, numIrcUsers); - return; - } +void Client::coreNetworkRemoved(NetworkId id) { + if(!_networks.contains(id)) return; + Network *net = _networks.take(id); + emit networkRemoved(net->networkId()); + net->deleteLater(); +} - if(numChannelsWaiting > 0) { - emit coreConnectionMsg(tr("Requesting Channel states...")); - emit coreConnectionProgress(numChannels - numChannelsWaiting, numChannels); - return; - } +/*** Identity handling ***/ - emit coreConnectionProgress(1,1); - emit connected(); +QList Client::identityIds() { + return instance()->_identities.keys(); } -void Client::recvSessionData(const QString &key, const QVariant &data) { - sessionData[key] = data; - emit sessionDataChanged(key, data); - emit sessionDataChanged(key); +const Identity * Client::identity(IdentityId id) { + if(instance()->_identities.contains(id)) return instance()->_identities[id]; + else return 0; } -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::createIdentity(const Identity &id) { + emit instance()->requestCreateIdentity(id); } -QVariant Client::retrieveSessionData(const QString &key, const QVariant &def) { - if(instance()->sessionData.contains(key)) return instance()->sessionData[key]; - else return def; +void Client::updateIdentity(const Identity &id) { + emit instance()->requestUpdateIdentity(id); } -QStringList Client::sessionDataKeys() { - return instance()->sessionData.keys(); +void Client::removeIdentity(IdentityId id) { + emit instance()->requestRemoveIdentity(id); } -void Client::coreSocketError(QAbstractSocket::SocketError) { - emit coreConnectionError(socket->errorString()); - socket->deleteLater(); +void Client::coreIdentityCreated(const Identity &other) { + if(!_identities.contains(other.id())) { + Identity *identity = new Identity(other, this); + _identities[other.id()] = identity; + identity->setInitialized(); + signalProxy()->synchronize(identity); + emit identityCreated(other.id()); + } else { + qWarning() << tr("Identity already exists in client!"); + } } -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); +void Client::coreIdentityRemoved(IdentityId id) { + if(_identities.contains(id)) { + emit identityRemoved(id); + Identity *i = _identities.take(id); + i->deleteLater(); } } -void Client::networkConnected(uint netid) { - // TODO: create statusBuffer / switch to networkids - //BufferInfo id = statusBufferInfo(net); - //Buffer *b = buffer(id); - //b->setActive(true); +/*** ***/ +void Client::userInput(BufferInfo bufferInfo, QString message) { + emit instance()->sendInput(bufferInfo, message); +} + +/*** core connection stuff ***/ - 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())); - _networkInfo[netid] = netinfo; +void Client::setConnectedToCore(QIODevice *sock, AccountId id) { + socket = sock; + signalProxy()->addPeer(socket); + _connectedToCore = true; + setCurrentCoreAccount(id); } -void Client::networkDisconnected(uint networkid) { - foreach(Buffer *buffer, buffers()) { - if(buffer->bufferInfo().networkId() != networkid) - continue; +void Client::setSyncedToCore() { + _syncedToCore = true; + emit connected(); + emit coreConnectionStateChanged(true); +} - //buffer->displayMsg(Message(bufferid, Message::Server, tr("Server disconnected."))); FIXME - buffer->setActive(false); +void Client::disconnectFromCore() { + if(socket) { + socket->close(); + socket->deleteLater(); } - - Q_ASSERT(networkInfo(networkid)); - if(!networkInfo(networkid)->initialized()) { - qDebug() << "Network" << networkid << "disconnected while not yet initialized!"; - updateCoreConnectionProgress(); + _connectedToCore = false; + _syncedToCore = false; + setCurrentCoreAccount(0); + emit disconnected(); + emit coreConnectionStateChanged(false); + + // Clear internal data. Hopefully nothing relies on it at this point. + _networkModel->clear(); + + QHash::iterator netIter = _networks.begin(); + while(netIter != _networks.end()) { + Network *net = netIter.value(); + emit networkRemoved(net->networkId()); + disconnect(net, SIGNAL(destroyed()), this, 0); + netIter = _networks.erase(netIter); + net->deleteLater(); } + Q_ASSERT(_networks.isEmpty()); + + QHash::iterator bufferIter = _buffers.begin(); + while(bufferIter != _buffers.end()) { + Buffer *buffer = bufferIter.value(); + disconnect(buffer, SIGNAL(destroyed()), this, 0); + bufferIter = _buffers.erase(bufferIter); + buffer->deleteLater(); + } + Q_ASSERT(_buffers.isEmpty()); + + QHash::iterator idIter = _identities.begin(); + while(idIter != _identities.end()) { + Identity *id = idIter.value(); + emit identityRemoved(id->id()); + idIter = _identities.erase(idIter); + id->deleteLater(); + } + Q_ASSERT(_identities.isEmpty()); + + layoutQueue.clear(); + layoutTimer->stop(); } -void Client::updateBufferInfo(BufferInfo id) { - buffer(id)->updateBufferInfo(id); +void Client::setCoreConfiguration(const QVariantMap &settings) { + SignalProxy::writeDataToDevice(socket, settings); } +/*** ***/ -void Client::removeBuffer(Buffer *b) { - _buffers.remove(b->bufferInfo().uid()); +void Client::updateBufferInfo(BufferInfo id) { + emit bufferUpdated(id); } -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; +void Client::bufferDestroyed() { + Buffer *buffer = static_cast(sender()); + QHash::iterator iter = _buffers.begin(); + while(iter != _buffers.end()) { + if(iter.value() == buffer) { + iter = _buffers.erase(iter); + break; + } + iter++; } - if(msg.flags() & Message::Highlight){ - level |= Buffer::Highlight; +} + +void Client::networkDestroyed() { + Network *net = static_cast(sender()); + QHash::iterator netIter = _networks.begin(); + while(netIter != _networks.end()) { + if(*netIter == net) { + netIter = _networks.erase(netIter); + break; + } else { + netIter++; + } } - emit bufferActivity(level, b); +} +void Client::recvMessage(Message &msg) { + Buffer *b; + + if(msg.type() == Message::Error) { + b = buffer(msg.bufferInfo().bufferId()); + if(!b) { + // FIXME: if buffer doesn't exist, forward the message to the status or current buffer + b = buffer(msg.bufferInfo()); + } + } else { + b = buffer(msg.bufferInfo()); + } + + checkForHighlight(msg); b->appendMsg(msg); + networkModel()->updateBufferActivity(msg); + + if(msg.type() == Message::Plain || msg.type() == Message::Notice || msg.type() == Message::Action) { + const Network *net = network(msg.bufferInfo().networkId()); + QString networkName = net != 0 + ? net->networkName() + ":" + : QString(); + QString sender = networkName + msg.bufferInfo().bufferName() + ":" + msg.sender(); + Message mmsg = Message(msg.timestamp(), msg.bufferInfo(), msg.type(), msg.text(), sender, msg.flags()); + monitorBuffer()->appendMsg(mmsg); + } + } void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) { @@ -483,7 +416,9 @@ void Client::recvBacklogData(BufferInfo id, QVariantList msgs, bool /*done*/) { Buffer *b = buffer(id); foreach(QVariant v, msgs) { Message msg = v.value(); + checkForHighlight(msg); b->prependMsg(msg); + networkModel()->updateBufferActivity(msg); if(!layoutQueue.contains(b)) layoutQueue.append(b); } if(layoutQueue.count() && !layoutTimer->isActive()) layoutTimer->start(); @@ -504,7 +439,11 @@ AbstractUiMsg *Client::layoutMsg(const Message &msg) { return instance()->mainUi->layoutMsg(msg); } -void Client::userInput(BufferInfo id, QString msg) { - emit sendInput(id, msg); +void Client::checkForHighlight(Message &msg) const { + const Network *net = network(msg.bufferInfo().networkId()); + if(net && !net->myNick().isEmpty()) { + QRegExp nickRegExp("^(.*\\W)?" + QRegExp::escape(net->myNick()) + "(\\W.*)?$"); + if((msg.type() == Message::Plain || msg.type() == Message::Notice || msg.type() == Message::Action) && nickRegExp.exactMatch(msg.text())) + msg.setFlags(msg.flags() | Message::Highlight); + } } -