1 /***************************************************************************
2 * Copyright (C) 2005-07 by The Quassel Team *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "buffertreemodel.h"
25 #include "clientproxy.h"
26 #include "quasselui.h"
29 Client * Client::instanceptr = 0;
31 bool Client::connectedToCore = false;
32 Client::ClientMode Client::clientMode;
33 QHash<BufferId, Buffer *> Client::buffers;
34 QHash<uint, BufferId> Client::bufferIds;
35 QHash<QString, QHash<QString, VarMap> > Client::nicks;
36 QHash<QString, bool> Client::netConnected;
37 QHash<QString, QString> Client::ownNick;
39 Client *Client::instance() {
40 if(instanceptr) return instanceptr;
41 instanceptr = new Client();
45 void Client::destroy() {
51 clientProxy = ClientProxy::instance();
53 _bufferModel = new BufferTreeModel(this);
55 connect(this, SIGNAL(bufferSelected(Buffer *)), _bufferModel, SLOT(selectBuffer(Buffer *)));
56 connect(this, SIGNAL(bufferUpdated(Buffer *)), _bufferModel, SLOT(bufferUpdated(Buffer *)));
57 connect(this, SIGNAL(bufferActivity(Buffer::ActivityLevel, Buffer *)), _bufferModel, SLOT(bufferActivity(Buffer::ActivityLevel, Buffer *)));
59 // TODO: make this configurable (allow monolithic client to connect to remote cores)
60 if(Global::runMode == Global::Monolithic) clientMode = LocalCore;
61 else clientMode = RemoteCore;
62 connectedToCore = false;
65 void Client::init(AbstractUi *ui) {
66 instance()->mainUi = ui;
73 connect(&socket, SIGNAL(readyRead()), this, SLOT(serverHasData()));
74 connect(&socket, SIGNAL(connected()), this, SLOT(coreConnected()));
75 connect(&socket, SIGNAL(disconnected()), this, SLOT(coreDisconnected()));
76 connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(serverError(QAbstractSocket::SocketError)));
78 connect(Global::instance(), SIGNAL(dataPutLocally(UserId, QString)), this, SLOT(updateCoreData(UserId, QString)));
79 connect(clientProxy, SIGNAL(csUpdateGlobalData(QString, QVariant)), this, SLOT(updateLocalData(QString, QVariant)));
81 connect(clientProxy, SIGNAL(send(ClientSignal, QVariant, QVariant, QVariant)), this, SLOT(recvProxySignal(ClientSignal, QVariant, QVariant, QVariant)));
82 connect(clientProxy, SIGNAL(csServerState(QString, QVariant)), this, SLOT(recvNetworkState(QString, QVariant)));
83 connect(clientProxy, SIGNAL(csServerConnected(QString)), this, SLOT(networkConnected(QString)));
84 connect(clientProxy, SIGNAL(csServerDisconnected(QString)), this, SLOT(networkDisconnected(QString)));
85 connect(clientProxy, SIGNAL(csDisplayMsg(Message)), this, SLOT(recvMessage(const Message &)));
86 connect(clientProxy, SIGNAL(csDisplayStatusMsg(QString, QString)), this, SLOT(recvStatusMsg(QString, QString)));
87 connect(clientProxy, SIGNAL(csTopicSet(QString, QString, QString)), this, SLOT(setTopic(QString, QString, QString)));
88 connect(clientProxy, SIGNAL(csNickAdded(QString, QString, VarMap)), this, SLOT(addNick(QString, QString, VarMap)));
89 connect(clientProxy, SIGNAL(csNickRemoved(QString, QString)), this, SLOT(removeNick(QString, QString)));
90 connect(clientProxy, SIGNAL(csNickRenamed(QString, QString, QString)), this, SLOT(renameNick(QString, QString, QString)));
91 connect(clientProxy, SIGNAL(csNickUpdated(QString, QString, VarMap)), this, SLOT(updateNick(QString, QString, VarMap)));
92 connect(clientProxy, SIGNAL(csOwnNickSet(QString, QString)), this, SLOT(setOwnNick(QString, QString)));
93 connect(clientProxy, SIGNAL(csBacklogData(BufferId, const QList<QVariant> &, bool)), this, SLOT(recvBacklogData(BufferId, QList<QVariant>, bool)));
94 connect(clientProxy, SIGNAL(csUpdateBufferId(BufferId)), this, SLOT(updateBufferId(BufferId)));
95 connect(this, SIGNAL(sendInput(BufferId, QString)), clientProxy, SLOT(gsUserInput(BufferId, QString)));
96 connect(this, SIGNAL(requestBacklog(BufferId, QVariant, QVariant)), clientProxy, SLOT(gsRequestBacklog(BufferId, QVariant, QVariant)));
97 connect(this, SIGNAL(requestNetworkStates()), clientProxy, SLOT(gsRequestNetworkStates()));
99 connect(mainUi, SIGNAL(connectToCore(const VarMap &)), this, SLOT(connectToCore(const VarMap &)));
100 connect(mainUi, SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
101 connect(this, SIGNAL(connected()), mainUi, SLOT(connectedToCore()));
102 connect(this, SIGNAL(disconnected()), mainUi, SLOT(disconnectedFromCore()));
104 layoutTimer = new QTimer(this);
105 layoutTimer->setInterval(0);
106 layoutTimer->setSingleShot(false);
107 connect(layoutTimer, SIGNAL(timeout()), this, SLOT(layoutMsg()));
113 //delete _bufferModel;
114 foreach(Buffer *buf, buffers.values()) delete buf;
115 ClientProxy::destroy();
119 BufferTreeModel *Client::bufferModel() {
120 return instance()->_bufferModel;
123 bool Client::isConnected() { return connectedToCore; }
125 void Client::connectToCore(const VarMap &conn) {
126 // TODO implement SSL
128 qDebug() << "Already connected to core!";
131 if(conn["Host"].toString().isEmpty()) {
132 clientMode = LocalCore;
133 syncToCore(); // TODO send user and pw from conn info
135 clientMode = RemoteCore;
136 socket.connectToHost(conn["Host"].toString(), conn["Port"].toUInt());
140 void Client::disconnectFromCore() {
141 if(clientMode == RemoteCore) {
144 disconnectFromLocalCore();
147 // TODO clear internal data
150 void Client::coreConnected() {
155 void Client::coreDisconnected() {
156 connectedToCore = false;
160 void Client::syncToCore() {
162 if(clientMode == LocalCore) {
163 state = connectToLocalCore("Default", "password").toMap(); // TODO make this configurable
168 VarMap data = state["CoreData"].toMap();
169 foreach(QString key, data.keys()) {
170 Global::updateData(key, data[key]);
172 //if(!Global::data("CoreReady").toBool()) {
173 // qFatal("Something is wrong, getting invalid data from core!");
176 VarMap sessionState = state["SessionState"].toMap();
177 QList<QVariant> coreBuffers = sessionState["Buffers"].toList();
178 /* make lookups by id faster */
179 foreach(QVariant vid, coreBuffers) {
180 BufferId id = vid.value<BufferId>();
181 bufferIds[id.uid()] = id; // make lookups by id faster
182 buffer(id); // create all buffers, so we see them in the network views
184 connectedToCore = true;
186 emit requestNetworkStates();
189 void Client::updateCoreData(UserId, QString key) {
190 if(clientMode == LocalCore) return;
191 QVariant data = Global::data(key);
192 recvProxySignal(GS_UPDATE_GLOBAL_DATA, key, data, QVariant());
195 void Client::updateLocalData(QString key, QVariant data) {
196 Global::updateData(key, data);
199 void Client::recvProxySignal(ClientSignal sig, QVariant arg1, QVariant arg2, QVariant arg3) {
200 if(clientMode == LocalCore) return;
201 QList<QVariant> sigdata;
202 sigdata.append(sig); sigdata.append(arg1); sigdata.append(arg2); sigdata.append(arg3);
203 //qDebug() << "Sending signal: " << sigdata;
204 writeDataToDevice(&socket, QVariant(sigdata));
207 void Client::serverError(QAbstractSocket::SocketError) {
208 emit coreConnectionError(socket.errorString());
211 void Client::serverHasData() {
213 while(readDataFromDevice(&socket, blockSize, item)) {
214 emit recvPartialItem(1,1);
215 QList<QVariant> sigdata = item.toList();
216 Q_ASSERT(sigdata.size() == 4);
217 ClientProxy::instance()->recv((CoreSignal)sigdata[0].toInt(), sigdata[1], sigdata[2], sigdata[3]);
221 emit recvPartialItem(socket.bytesAvailable(), blockSize);
225 void Client::networkConnected(QString net) {
226 netConnected[net] = true;
227 BufferId id = statusBufferId(net);
228 Buffer *b = buffer(id);
230 //b->displayMsg(Message(id, Message::Server, tr("Connected.")));
231 // TODO buffersUpdated();
234 void Client::networkDisconnected(QString net) {
235 foreach(BufferId id, buffers.keys()) {
236 if(id.network() != net) continue;
237 Buffer *b = buffer(id);
238 //b->displayMsg(Message(id, Message::Server, tr("Server disconnected."))); FIXME
241 netConnected[net] = false;
244 void Client::updateBufferId(BufferId id) {
245 bufferIds[id.uid()] = id; // make lookups by id faster
249 BufferId Client::bufferId(QString net, QString buf) {
250 foreach(BufferId id, buffers.keys()) {
251 if(id.network() == net && id.buffer() == buf) return id;
253 Q_ASSERT(false); // should never happen!
257 BufferId Client::statusBufferId(QString net) {
258 return bufferId(net, "");
262 Buffer * Client::buffer(BufferId id) {
263 Client *client = Client::instance();
264 if(!buffers.contains(id)) {
265 Buffer *b = new Buffer(id);
266 b->setOwnNick(ownNick[id.network()]);
267 connect(b, SIGNAL(userInput(BufferId, QString)), client, SLOT(userInput(BufferId, QString)));
268 connect(b, SIGNAL(bufferUpdated(Buffer *)), client, SIGNAL(bufferUpdated(Buffer *)));
269 connect(b, SIGNAL(bufferDestroyed(Buffer *)), client, SIGNAL(bufferDestroyed(Buffer *)));
271 emit client->bufferUpdated(b);
276 QList<BufferId> Client::allBufferIds() {
277 return buffers.keys();
280 void Client::recvNetworkState(QString net, QVariant state) {
281 netConnected[net] = true;
282 setOwnNick(net, state.toMap()["OwnNick"].toString());
283 buffer(statusBufferId(net))->setActive(true);
284 VarMap t = state.toMap()["Topics"].toMap();
285 VarMap n = state.toMap()["Nicks"].toMap();
286 foreach(QVariant v, t.keys()) {
287 QString buf = v.toString();
288 BufferId id = bufferId(net, buf);
289 buffer(id)->setActive(true);
290 setTopic(net, buf, t[buf].toString());
292 foreach(QString nick, n.keys()) {
293 addNick(net, nick, n[nick].toMap());
297 void Client::recvMessage(const Message &msg) {
298 Buffer *b = buffer(msg.buffer);
300 Buffer::ActivityLevel level = Buffer::OtherActivity;
301 if(msg.type == Message::Plain || msg.type == Message::Notice){
302 level |= Buffer::NewMessage;
304 if(msg.flags & Message::Highlight){
305 level |= Buffer::Highlight;
307 emit bufferActivity(level, b);
312 void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) {
313 //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
317 void Client::recvBacklogData(BufferId id, const QList<QVariant> &msgs, bool /*done*/) {
318 Buffer *b = buffer(id);
319 foreach(QVariant v, msgs) {
320 Message msg = v.value<Message>();
322 if(!layoutQueue.contains(b)) layoutQueue.append(b);
324 if(layoutQueue.count() && !layoutTimer->isActive()) layoutTimer->start();
327 void Client::layoutMsg() {
328 if(layoutQueue.count()) {
329 Buffer *b = layoutQueue.takeFirst(); // TODO make this the current buffer
330 if(b->layoutMsg()) layoutQueue.append(b); // Buffer has more messages in its queue --> Round Robin
332 if(!layoutQueue.count()) layoutTimer->stop();
335 AbstractUiMsg *Client::layoutMsg(const Message &msg) {
336 return instance()->mainUi->layoutMsg(msg);
339 void Client::userInput(BufferId id, QString msg) {
340 emit sendInput(id, msg);
343 void Client::setTopic(QString net, QString buf, QString topic) {
344 BufferId id = bufferId(net, buf);
345 if(!netConnected[id.network()]) return;
346 Buffer *b = buffer(id);
348 //if(!b->isActive()) {
349 // b->setActive(true);
354 void Client::addNick(QString net, QString nick, VarMap props) {
355 if(!netConnected[net]) return;
356 nicks[net][nick] = props;
357 VarMap chans = props["Channels"].toMap();
358 QStringList c = chans.keys();
359 foreach(QString bufname, c) {
360 buffer(bufferId(net, bufname))->addNick(nick, props);
364 void Client::renameNick(QString net, QString oldnick, QString newnick) {
365 if(!netConnected[net]) return;
366 QStringList chans = nicks[net][oldnick]["Channels"].toMap().keys();
367 foreach(QString c, chans) {
368 buffer(bufferId(net, c))->renameNick(oldnick, newnick);
370 nicks[net][newnick] = nicks[net].take(oldnick);
373 void Client::updateNick(QString net, QString nick, VarMap props) {
374 if(!netConnected[net]) return;
375 QStringList oldchans = nicks[net][nick]["Channels"].toMap().keys();
376 QStringList newchans = props["Channels"].toMap().keys();
377 foreach(QString c, newchans) {
378 if(oldchans.contains(c)) buffer(bufferId(net, c))->updateNick(nick, props);
379 else buffer(bufferId(net, c))->addNick(nick, props);
381 foreach(QString c, oldchans) {
382 if(!newchans.contains(c)) buffer(bufferId(net, c))->removeNick(nick);
384 nicks[net][nick] = props;
387 void Client::removeNick(QString net, QString nick) {
388 if(!netConnected[net]) return;
389 VarMap chans = nicks[net][nick]["Channels"].toMap();
390 foreach(QString bufname, chans.keys()) {
391 buffer(bufferId(net, bufname))->removeNick(nick);
393 nicks[net].remove(nick);
396 void Client::setOwnNick(QString net, QString nick) {
397 if(!netConnected[net]) return;
399 foreach(BufferId id, buffers.keys()) {
400 if(id.network() == net) {
401 buffers[id]->setOwnNick(nick);