***************************************************************************/
#include "client.h"
-#include "clientproxy.h"
-#include "mainwin.h"
+
#include "buffer.h"
-#include "bufferviewwidget.h"
+#include "buffertreemodel.h"
+#include "clientproxy.h"
+#include "quasselui.h"
#include "util.h"
Client * Client::instanceptr = 0;
+bool Client::connectedToCore = false;
Client::ClientMode Client::clientMode;
+VarMap Client::coreConnectionInfo;
QHash<BufferId, Buffer *> Client::buffers;
QHash<uint, BufferId> Client::bufferIds;
QHash<QString, QHash<QString, VarMap> > Client::nicks;
-QHash<QString, bool> Client::connected;
+QHash<QString, bool> Client::netConnected;
+QStringList Client::netsAwaitingInit;
QHash<QString, QString> Client::ownNick;
-QList<BufferId> Client::coreBuffers;
-
Client *Client::instance() {
if(instanceptr) return instanceptr;
instanceptr = new Client();
- instanceptr->init();
return instanceptr;
}
Client::Client() {
clientProxy = ClientProxy::instance();
- //mainWin = new MainWin();
-
- _bufferModel = new BufferTreeModel(0); // FIXME
+ _bufferModel = new BufferTreeModel(this);
connect(this, SIGNAL(bufferSelected(Buffer *)), _bufferModel, SLOT(selectBuffer(Buffer *)));
connect(this, SIGNAL(bufferUpdated(Buffer *)), _bufferModel, SLOT(bufferUpdated(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;
+}
+
+void Client::init(AbstractUi *ui) {
+ instance()->mainUi = ui;
+ instance()->init();
}
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(connected()), this, SLOT(coreSocketConnected()));
+ connect(&socket, SIGNAL(disconnected()), this, SLOT(coreSocketDisconnected()));
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(this, SIGNAL(sendSessionData(const QString &, const QVariant &)), clientProxy, SLOT(gsSessionDataChanged(const QString &, const QVariant &)));
+ connect(clientProxy, SIGNAL(csSessionDataChanged(const QString &, const QVariant &)), this, SLOT(recvSessionData(const QString &, const QVariant &)));
connect(clientProxy, SIGNAL(send(ClientSignal, QVariant, QVariant, QVariant)), this, SLOT(recvProxySignal(ClientSignal, QVariant, QVariant, QVariant)));
+ connect(clientProxy, SIGNAL(csCoreState(QVariant)), this, SLOT(recvCoreState(const 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(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(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<QVariant>, bool)), this, SLOT(recvBacklogData(BufferId, QList<QVariant>, bool)));
+ connect(clientProxy, SIGNAL(csBacklogData(BufferId, const QList<QVariant> &, bool)), this, SLOT(recvBacklogData(BufferId, QList<QVariant>, 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()));
- syncToCore();
+ connect(mainUi, SIGNAL(connectToCore(const VarMap &)), this, SLOT(connectToCore(const VarMap &)));
+ 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()));
- /* 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
- }
-
- mainWin = new MainWin();
- mainWin->init();
-
}
Client::~Client() {
- delete mainWin;
- delete _bufferModel;
+ //delete mainUi;
+ //delete _bufferModel;
foreach(Buffer *buf, buffers.values()) delete buf;
ClientProxy::destroy();
return instance()->_bufferModel;
}
-void Client::coreConnected() {
+bool Client::isConnected() { return connectedToCore; }
+void Client::connectToCore(const VarMap &conn) {
+ // TODO implement SSL
+ coreConnectionInfo = conn;
+ if(isConnected()) {
+ emit coreConnectionError(tr("Already connected to Core!"));
+ return;
+ }
+ if(conn["Host"].toString().isEmpty()) {
+ clientMode = LocalCore;
+ QVariant state = connectToLocalCore(coreConnectionInfo["User"].toString(), coreConnectionInfo["Password"].toString());
+ syncToCore(state);
+ } else {
+ clientMode = RemoteCore;
+ emit coreConnectionMsg(tr("Connecting..."));
+ socket.connectToHost(conn["Host"].toString(), conn["Port"].toUInt());
+ }
}
-void Client::coreDisconnected() {
+void Client::disconnectFromCore() {
+ if(clientMode == RemoteCore) {
+ socket.close();
+ } else {
+ disconnectFromLocalCore();
+ coreSocketDisconnected();
+ }
+ /* Clear internal data. Hopefully nothing relies on it at this point. */
+ coreConnectionInfo.clear();
+ sessionData.clear();
+ //foreach(Buffer *buf, buffers.values()) delete buf;
+ qDebug() << "barfoo";
+ _bufferModel->clear();
+ //qDeleteAll(buffers);
+ qDebug() << "foobar";
+}
+void Client::coreSocketConnected() {
+ VarMap clientInit;
+ clientInit["GuiProtocol"] = GUI_PROTOCOL;
+ clientInit["User"] = coreConnectionInfo["User"].toString();
+ clientInit["Password"] = coreConnectionInfo["Password"].toString();
+ writeDataToDevice(&socket, clientInit);
}
-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::coreSocketDisconnected() {
+ connectedToCore = false;
+ emit disconnected();
+}
+
+void Client::recvCoreState(const QVariant &state) {
+ syncToCore(state);
+
+}
+
+void Client::syncToCore(const QVariant &coreState) {
+ VarMap sessionState = coreState.toMap()["SessionState"].toMap();
+ VarMap sessData = sessionState["SessionData"].toMap();
+
+ foreach(QString key, sessData.keys()) {
+ recvSessionData(key, sessData[key]);
+ }
+ QList<QVariant> coreBuffers = sessionState["Buffers"].toList();
+ /* make lookups by id faster */
+ foreach(QVariant vid, coreBuffers) {
+ BufferId id = vid.value<BufferId>();
+ 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;
+ 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::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;
+}
+
+QStringList Client::sessionDataKeys() {
+ return instance()->sessionData.keys();
}
void Client::recvProxySignal(ClientSignal sig, QVariant arg1, QVariant arg2, QVariant arg3) {
writeDataToDevice(&socket, QVariant(sigdata));
}
-void Client::connectToCore(QString host, quint16 port) {
- // TODO implement SSL
- socket.connectToHost(host, port);
-}
-
-void Client::disconnectFromCore() {
- socket.close();
-}
-
void Client::serverError(QAbstractSocket::SocketError) {
emit coreConnectionError(socket.errorString());
}
}
}
-/*******************************************************************************************************************/
-
void Client::networkConnected(QString net) {
- connected[net] = true;
+ Q_ASSERT(!netsAwaitingInit.contains(net));
+ netConnected[net] = true;
BufferId id = statusBufferId(net);
Buffer *b = buffer(id);
b->setActive(true);
//b->displayMsg(Message(id, Message::Server, tr("Server disconnected."))); FIXME
b->setActive(false);
}
- connected[net] = 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) {
return buffers[id];
}
+QList<BufferId> Client::allBufferIds() {
+ return buffers.keys();
+}
+
void Client::recvNetworkState(QString net, QVariant state) {
- connected[net] = true;
+ netsAwaitingInit.removeAll(net);
+ netConnected[net] = true;
setOwnNick(net, state.toMap()["OwnNick"].toString());
buffer(statusBufferId(net))->setActive(true);
VarMap t = state.toMap()["Topics"].toMap();
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(Message msg) {
+void Client::recvMessage(const Message &msg) {
Buffer *b = buffer(msg.buffer);
Buffer::ActivityLevel level = Buffer::OtherActivity;
}
emit bufferActivity(level, b);
- //b->displayMsg(msg);
- b->appendChatLine(new ChatLine(msg));
+ b->appendMsg(msg);
}
-void Client::recvStatusMsg(QString net, QString msg) {
+void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) {
//recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
}
-void Client::recvBacklogData(BufferId id, QList<QVariant> msgs, bool done) {
+void Client::recvBacklogData(BufferId id, const QList<QVariant> &msgs, bool /*done*/) {
+ Buffer *b = buffer(id);
foreach(QVariant v, msgs) {
- layoutQueue.append(v.value<Message>());
+ Message msg = v.value<Message>();
+ b->prependMsg(msg);
+ if(!layoutQueue.contains(b)) layoutQueue.append(b);
}
- if(!layoutTimer->isActive()) layoutTimer->start();
+ if(layoutQueue.count() && !layoutTimer->isActive()) layoutTimer->start();
}
-
void Client::layoutMsg() {
if(layoutQueue.count()) {
- ChatLine *line = new ChatLine(layoutQueue.takeFirst());
- buffer(line->bufferId())->prependChatLine(line);
+ 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(BufferId id, QString msg) {
emit sendInput(id, msg);
}
void Client::setTopic(QString net, QString buf, QString topic) {
BufferId id = bufferId(net, buf);
- if(!connected[id.network()]) return;
+ if(!netConnected[id.network()]) return;
Buffer *b = buffer(id);
b->setTopic(topic);
//if(!b->isActive()) {
}
void Client::addNick(QString net, QString nick, VarMap props) {
- if(!connected[net]) return;
+ if(!netConnected[net]) return;
nicks[net][nick] = props;
VarMap chans = props["Channels"].toMap();
QStringList c = chans.keys();
}
void Client::renameNick(QString net, QString oldnick, QString newnick) {
- if(!connected[net]) return;
+ if(!netConnected[net]) return;
QStringList chans = nicks[net][oldnick]["Channels"].toMap().keys();
foreach(QString c, chans) {
buffer(bufferId(net, c))->renameNick(oldnick, newnick);
}
void Client::updateNick(QString net, QString nick, VarMap props) {
- if(!connected[net]) return;
+ if(!netConnected[net]) return;
QStringList oldchans = nicks[net][nick]["Channels"].toMap().keys();
QStringList newchans = props["Channels"].toMap().keys();
foreach(QString c, newchans) {
}
void Client::removeNick(QString net, QString nick) {
- if(!connected[net]) return;
+ if(!netConnected[net]) return;
VarMap chans = nicks[net][nick]["Channels"].toMap();
foreach(QString bufname, chans.keys()) {
buffer(bufferId(net, bufname))->removeNick(nick);
}
void Client::setOwnNick(QString net, QString nick) {
- if(!connected[net]) return;
+ if(!netConnected[net]) return;
ownNick[net] = nick;
foreach(BufferId id, buffers.keys()) {
if(id.network() == net) {
}
}
-