/***************************************************************************
- * 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 *
#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> Client::instanceptr = 0;
-// ==============================
-// public Static Methods
-// ==============================
+/*** Initialization/destruction ***/
+
Client *Client::instance() {
if(!instanceptr)
instanceptr = new Client();
instance()->init();
}
-QList<NetworkInfo *> Client::networkInfos() {
- return instance()->_networkInfo.values();
+Client::Client(QObject *parent)
+ : QObject(parent),
+ socket(0),
+ _signalProxy(new SignalProxy(SignalProxy::Client, this)),
+ mainUi(0),
+ _networkModel(0),
+ _bufferModel(0),
+ connectedToCore(false)
+{
+}
+
+Client::~Client() {
}
-NetworkInfo *Client::networkInfo(uint networkid) {
- if(instance()->_networkInfo.contains(networkid))
- return instance()->_networkInfo[networkid];
+void Client::init() {
+ blockSize = 0;
+
+ _networkModel = new NetworkModel(this);
+ connect(this, SIGNAL(bufferUpdated(BufferInfo)),
+ _networkModel, SLOT(bufferUpdated(BufferInfo)));
+
+ _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(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()));
+
+ 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)));
+
+ 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()));
+
+}
+
+/*** public static methods ***/
+
+
+QList<Network *> Client::networks() {
+ return instance()->_network.values();
+}
+
+Network *Client::network(uint networkid) {
+ if(instance()->_network.contains(networkid))
+ return instance()->_network[networkid];
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);
+ 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 *)));
-
+ connect(buff, SIGNAL(destroyed()),
+ client, SLOT(bufferDestroyed()));
client->_buffers[id.uid()] = buff;
- emit client->bufferUpdated(buff);
+ emit client->bufferUpdated(id);
}
-
+ Q_ASSERT(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, "");
+NetworkModel *Client::networkModel() {
+ return instance()->_networkModel;
}
-BufferTreeModel *Client::bufferModel() {
+BufferModel *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)),
- mainUi(0),
- _bufferModel(0),
- connectedToCore(false)
-{
-}
-Client::~Client() {
+/*** Identity handling ***/
+
+QList<IdentityId> Client::identityIds() {
+ return instance()->_identities.keys();
}
-void Client::init() {
- blockSize = 0;
+const Identity * Client::identity(IdentityId id) {
+ if(instance()->_identities.contains(id)) return instance()->_identities[id];
+ else return 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 *)));
+void Client::createIdentity(const Identity &id) {
+ emit instance()->requestCreateIdentity(id);
+}
- 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)));
+void Client::updateIdentity(const Identity &id) {
+ emit instance()->requestUpdateIdentity(id);
+}
+void Client::removeIdentity(IdentityId id) {
+ emit instance()->requestRemoveIdentity(id);
+}
- 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()));
+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!");
+ }
+}
- 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()));
+void Client::coreIdentityRemoved(IdentityId id) {
+ if(_identities.contains(id)) {
+ emit identityRemoved(id);
+ Identity *i = _identities.take(id);
+ i->deleteLater();
+ }
+}
- layoutTimer = new QTimer(this);
- layoutTimer->setInterval(0);
- layoutTimer->setSingleShot(false);
- connect(layoutTimer, SIGNAL(timeout()), this, SLOT(layoutMsg()));
+/*** ***/
-}
bool Client::isConnected() {
return instance()->connectedToCore;
}
+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::fakeInput(BufferInfo bufferInfo, QString message) {
+ fakeInput(bufferInfo, message);
+}
+
void Client::connectToCore(const QVariantMap &conn) {
// TODO implement SSL
coreConnectionInfo = conn;
-
if(isConnected()) {
emit coreConnectionError(tr("Already connected to Core!"));
return;
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(signalProxy(), SIGNAL(disconnected()), this, SLOT(coreSocketDisconnected()));
connect(sock, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(coreSocketError(QAbstractSocket::SocketError)));
sock->connectToHost(conn["Host"].toString(), conn["Port"].toUInt());
}
void Client::disconnectFromCore() {
socket->close();
- if(clientMode == LocalCore)
- coreSocketDisconnected();
+}
+
+void Client::setCoreConfiguration(const QVariantMap &settings) {
+ SignalProxy::writeDataToDevice(socket, settings);
}
void Client::coreSocketConnected() {
clientInit["GuiProtocol"] = GUI_PROTOCOL;
clientInit["User"] = coreConnectionInfo["User"].toString();
clientInit["Password"] = coreConnectionInfo["Password"].toString();
- writeDataToDevice(socket, clientInit);
+ SignalProxy::writeDataToDevice(socket, clientInit);
}
void Client::coreSocketDisconnected() {
instance()->connectedToCore = false;
emit disconnected();
+ emit coreConnectionStateChanged(false);
socket->deleteLater();
blockSize = 0;
/* Clear internal data. Hopefully nothing relies on it at this point. */
- _bufferModel->clear();
-
- foreach(Buffer *buffer, _buffers.values()) {
- delete buffer;
+ _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.empty());
+ Q_ASSERT(_buffers.isEmpty());
- foreach(NetworkInfo *networkinfo, _networkInfo.values()) {
- delete networkinfo;
+
+ QHash<NetworkId, Network*>::iterator netIter = _network.begin();
+ while(netIter != _network.end()) {
+ Network *net = netIter.value();
+ disconnect(net, SIGNAL(destroyed()), this, 0);
+ netIter = _network.erase(netIter);
+ net->deleteLater();
+ }
+ Q_ASSERT(_network.isEmpty());
+
+ QHash<IdentityId, Identity*>::iterator idIter = _identities.begin();
+ while(idIter != _identities.end()) {
+ Identity *id = idIter.value();
+ emit identityRemoved(id->id());
+ idIter = _identities.erase(idIter);
+ id->deleteLater();
}
- Q_ASSERT(_networkInfo.empty());
+ Q_ASSERT(_identities.isEmpty());
coreConnectionInfo.clear();
sessionData.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
foreach(QString key, sessData.keys())
recvSessionData(key, sessData[key]);
+ // create identities
+ foreach(QVariant vid, sessionState["Identities"].toList()) {
+ coreIdentityCreated(vid.value<Identity>());
+ }
+
// store Buffer details
QVariantList coreBuffers = sessionState["Buffers"].toList();
/* make lookups by id faster */
buffer(vid.value<BufferInfo>()); // create all buffers, so we see them in the network views
}
- // create networkInfo objects
+ // create network objects
QVariantList networkids = sessionState["Networks"].toList();
foreach(QVariant networkid, networkids) {
networkConnected(networkid.toUInt());
}
-
+
instance()->connectedToCore = true;
updateCoreConnectionProgress();
+
}
void Client::updateCoreConnectionProgress() {
// 2.) channels
// 3.) ircusers
- int numNets = networkInfos().count();
+ int numNets = networks().count();
int numNetsWaiting = 0;
int numIrcUsers = 0;
int numChannels = 0;
int numChannelsWaiting = 0;
- foreach(NetworkInfo *net, networkInfos()) {
+ foreach(Network *net, networks()) {
if(! net->initialized())
numNetsWaiting++;
if(! channel->initialized())
numChannelsWaiting++;
}
-
}
if(numNetsWaiting > 0) {
emit coreConnectionProgress(1,1);
emit connected();
+ emit coreConnectionStateChanged(true);
+ foreach(Network *net, networks()) {
+ disconnect(net, 0, this, SLOT(updateCoreConnectionProgress()));
+ }
+
+ // signalProxy()->dumpProxyStats();
}
void Client::recvSessionData(const QString &key, const QVariant &data) {
void Client::coreHasData() {
QVariant item;
- if(readDataFromDevice(socket, blockSize, item)) {
+ if(SignalProxy::readDataFromDevice(socket, blockSize, item)) {
emit recvPartialItem(1,1);
- recvCoreState(item);
+ QVariantMap msg = item.toMap();
+ if (!msg["StartWizard"].toBool()) {
+ recvCoreState(msg["Reply"]);
+ } else {
+ qWarning("Core not configured!");
+ qDebug() << "Available storage providers: " << msg["StorageProviders"].toStringList();
+ emit showConfigWizard(msg);
+ }
blockSize = 0;
return;
}
//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()));
- _networkInfo[netid] = netinfo;
+ Network *netinfo = new Network(netid, this);
+ netinfo->setProxy(signalProxy());
+ networkModel()->attachNetwork(netinfo);
+
+ if(!isConnected()) {
+ 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(networkDestroyed()));
+ _network[netid] = netinfo;
}
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);
+ if(!_network.contains(networkid)) {
+ qWarning() << "Client::networkDisconnected(uint): unknown Network" << networkid;
+ return;
}
-
- Q_ASSERT(networkInfo(networkid));
- if(!networkInfo(networkid)->initialized()) {
+
+ Network *net = _network.take(networkid);
+ if(!net->initialized()) {
qDebug() << "Network" << networkid << "disconnected while not yet initialized!";
updateCoreConnectionProgress();
}
+ net->deleteLater();
}
void Client::updateBufferInfo(BufferInfo id) {
- buffer(id)->updateBufferInfo(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++;
+ }
+}
-void Client::removeBuffer(Buffer *b) {
- _buffers.remove(b->bufferInfo().uid());
+void Client::networkDestroyed() {
+ Network *netinfo = static_cast<Network *>(sender());
+ uint networkId = netinfo->networkId();
+ if(_network.contains(networkId))
+ _network.remove(networkId);
}
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;
- }
- if(msg.flags() & Message::Highlight){
- level |= Buffer::Highlight;
- }
- emit bufferActivity(level, b);
+// Buffer::ActivityLevel level = Buffer::OtherActivity;
+// if(msg.type() == Message::Plain || msg.type() == Message::Notice){
+// level |= Buffer::NewMessage;
+// }
+// if(msg.flags() & Message::Highlight){
+// level |= Buffer::Highlight;
+// }
+// emit bufferActivity(level, b);
b->appendMsg(msg);
}