#include "bufferviewmanager.h"
#include "clientbacklogmanager.h"
#include "clientirclisthelper.h"
-#include "global.h"
#include "identity.h"
#include "ircchannel.h"
#include "ircuser.h"
#include "messagemodel.h"
#include "network.h"
#include "networkmodel.h"
+#include "quassel.h"
#include "quasselui.h"
#include "signalproxy.h"
#include "util.h"
+#include <stdio.h>
+#include <stdlib.h>
+
QPointer<Client> Client::instanceptr = 0;
AccountId Client::_currentCoreAccount = 0;
}
void Client::destroy() {
- //delete instanceptr;
- instanceptr->deleteLater();
+ if(instanceptr) {
+ delete instanceptr->mainUi;
+ instanceptr->deleteLater();
+ instanceptr = 0;
+ }
}
void Client::init(AbstractUi *ui) {
Client::Client(QObject *parent)
: QObject(parent),
- socket(0),
_signalProxy(new SignalProxy(SignalProxy::Client, this)),
mainUi(0),
_networkModel(0),
_messageModel(0),
_messageProcessor(0),
_connectedToCore(false),
- _syncedToCore(false)
+ _syncedToCore(false),
+ _debugLog(&_debugLogBuffer)
{
_signalProxy->synchronize(_ircListHelper);
-
- connect(_backlogManager, SIGNAL(backlog(BufferId, const QVariantList &)),
- this, SLOT(receiveBacklog(BufferId, const QVariantList &)));
}
Client::~Client() {
_currentCoreAccount = 0;
_networkModel = new NetworkModel(this);
- connect(this, SIGNAL(bufferUpdated(BufferInfo)),
- _networkModel, SLOT(bufferUpdated(BufferInfo)));
connect(this, SIGNAL(networkRemoved(NetworkId)),
_networkModel, SLOT(networkRemoved(NetworkId)));
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(bufferInfoUpdated(BufferInfo)), this, SLOT(updateBufferInfo(BufferInfo)));
+ p->attachSlot(SIGNAL(bufferInfoUpdated(BufferInfo)), _networkModel, SLOT(bufferUpdated(BufferInfo)));
p->attachSignal(this, SIGNAL(sendInput(BufferInfo, QString)));
p->attachSignal(this, SIGNAL(requestNetworkStates()));
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(p, SIGNAL(disconnected()), this, SLOT(disconnectedFromCore()));
//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()));
+ // attach backlog manager
+ p->synchronize(backlogManager());
+ connect(backlogManager(), SIGNAL(messagesReceived(BufferId, int)), _messageModel, SLOT(messagesReceived(BufferId, int)));
}
/*** public static methods ***/
_currentCoreAccount = id;
}
-QList<BufferInfo> Client::allBufferInfos() {
- QList<BufferInfo> bufferids;
- foreach(Buffer *buffer, buffers()) {
- bufferids << buffer->bufferInfo();
- }
- return bufferids;
-}
-
-QList<Buffer *> Client::buffers() {
- return instance()->_buffers.values();
-}
-
-
-Buffer *Client::statusBuffer(const NetworkId &networkId) const {
- if(_statusBuffers.contains(networkId))
- return _statusBuffers[networkId];
- else
- return 0;
-}
-
-Buffer *Client::buffer(BufferInfo bufferInfo) {
- Buffer *buff = 0;
- if(instance()->_buffers.contains(bufferInfo.bufferId()))
- buff = instance()->_buffers[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;
- if(bufferInfo.type() == BufferInfo::StatusBuffer)
- client->_statusBuffers[bufferInfo.networkId()] = buff;
-
- emit client->bufferUpdated(bufferInfo);
-
- // I don't like this: but currently there isn't really a prettier way:
- if(isSynced()) { // this slows down syncing a lot, so disable it during sync
- QModelIndex bufferIdx = networkModel()->bufferIndex(bufferInfo.bufferId());
- bufferModel()->setCurrentIndex(bufferModel()->mapFromSource(bufferIdx));
- }
- }
- Q_ASSERT(buff);
- return buff;
-}
-
bool Client::isConnected() {
return instance()->_connectedToCore;
}
/*** core connection stuff ***/
-void Client::setConnectedToCore(QIODevice *sock, AccountId id) {
- socket = sock;
- signalProxy()->addPeer(socket);
+void Client::setConnectedToCore(AccountId id, QIODevice *socket) {
+ if(socket) { // external core
+ // if the socket is an orphan, the signalProxy adopts it.
+ // -> we don't need to care about it anymore
+ socket->setParent(0);
+ signalProxy()->addPeer(socket);
+ }
_connectedToCore = true;
setCurrentCoreAccount(id);
}
connect(bufferSyncer(), SIGNAL(lastSeenMsgSet(BufferId, MsgId)), _networkModel, SLOT(setLastSeenMsgId(BufferId, MsgId)));
connect(bufferSyncer(), SIGNAL(bufferRemoved(BufferId)), this, SLOT(bufferRemoved(BufferId)));
connect(bufferSyncer(), SIGNAL(bufferRenamed(BufferId, QString)), this, SLOT(bufferRenamed(BufferId, QString)));
+ connect(bufferSyncer(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
+ connect(networkModel(), SIGNAL(setLastSeenMsg(BufferId, MsgId)), bufferSyncer(), SLOT(requestSetLastSeenMsg(BufferId, const MsgId &)));
signalProxy()->synchronize(bufferSyncer());
- // attach backlog manager
- signalProxy()->synchronize(backlogManager());
-
// create a new BufferViewManager
+ Q_ASSERT(!_bufferViewManager);
_bufferViewManager = new BufferViewManager(signalProxy(), this);
+ connect(bufferViewManager(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
_syncedToCore = true;
emit connected();
emit coreConnectionStateChanged(true);
}
+void Client::requestInitialBacklog() {
+ if(bufferViewManager()->isInitialized() && bufferSyncer()->isInitialized())
+ Client::backlogManager()->requestInitialBacklog();
+}
+
void Client::setSecuredConnection() {
emit securedConnection();
}
void Client::disconnectFromCore() {
if(!isConnected())
return;
- _connectedToCore = false;
- if(socket) {
- socket->close();
- socket->deleteLater();
- }
+ signalProxy()->removeAllPeers();
+}
+
+void Client::disconnectedFromCore() {
+ _connectedToCore = false;
_syncedToCore = false;
emit disconnected();
emit coreConnectionStateChanged(false);
+ backlogManager()->reset();
messageProcessor()->reset();
// Clear internal data. Hopefully nothing relies on it at this point.
_messageModel->clear();
_networkModel->clear();
- QHash<BufferId, Buffer *>::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());
-
- _statusBuffers.clear();
-
QHash<NetworkId, Network*>::iterator netIter = _networks.begin();
while(netIter != _networks.end()) {
Network *net = netIter.value();
}
-void Client::setCoreConfiguration(const QVariantMap &settings) {
- SignalProxy::writeDataToDevice(socket, settings);
-}
-
/*** ***/
-void Client::updateBufferInfo(BufferInfo id) {
- emit bufferUpdated(id);
-}
-
-void Client::bufferDestroyed() {
- Buffer *buffer = static_cast<Buffer *>(sender());
- QHash<BufferId, Buffer *>::iterator iter = _buffers.begin();
- while(iter != _buffers.end()) {
- if(iter.value() == buffer) {
- iter = _buffers.erase(iter);
- break;
- }
- iter++;
- }
-
- QHash<NetworkId, Buffer *>::iterator statusIter = _statusBuffers.begin();
- while(statusIter != _statusBuffers.end()) {
- if(statusIter.value() == buffer) {
- statusIter = _statusBuffers.erase(statusIter);
- break;
- }
- statusIter++;
- }
-}
-
void Client::networkDestroyed() {
Network *net = static_cast<Network *>(sender());
QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
//recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
}
-void Client::recvMessage(const Message &msg_) {
- Message msg = msg_;
- messageProcessor()->process(msg);
-}
-
-void Client::receiveBacklog(BufferId bufferId, const QVariantList &msgs) {
- if(msgs.isEmpty()) return;
- //QTime start = QTime::currentTime();
- QList<Message> msglist;
- foreach(QVariant v, msgs) {
- Message msg = v.value<Message>();
- msg.setFlags(msg.flags() | Message::Backlog);
- msglist << msg;
- }
- messageProcessor()->process(msglist);
- //qDebug() << "processed" << msgs.count() << "backlog lines in" << start.msecsTo(QTime::currentTime());
+void Client::recvMessage(const Message &msg) {
+ Message msg_ = msg;
+ messageProcessor()->process(msg_);
}
void Client::setBufferLastSeenMsg(BufferId id, const MsgId &msgId) {
}
void Client::bufferRemoved(BufferId bufferId) {
- // first remove the buffer from hash. this prohibits further lastSeenUpdates
- Buffer *buff = 0;
- if(_buffers.contains(bufferId)) {
- buff = _buffers.take(bufferId);
- disconnect(buff, 0, this, 0);
- }
-
- // then we select a sane buffer (status buffer)
+ // select a sane buffer (status buffer)
/* we have to manually select a buffer because otherwise inconsitent changes
* to the model might occur:
* the result of a buffer removal triggers a change in the selection model.
// and remove it from the model
networkModel()->removeBuffer(bufferId);
-
- if(buff)
- buff->deleteLater();
}
void Client::bufferRenamed(BufferId bufferId, const QString &newName) {
networkModel()->setData(bufferIndex, newName, Qt::DisplayRole);
}
}
+
+void Client::logMessage(QtMsgType type, const char *msg) {
+ fprintf(stderr, "%s\n", msg);
+ fflush(stderr);
+ if(type == QtFatalMsg) {
+ Quassel::logFatalMessage(msg);
+ } else {
+ QString msgString = QString("%1\n").arg(msg);
+ instance()->_debugLog << msgString;
+ emit instance()->logUpdated(msgString);
+ }
+}
+