1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
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) version 3. *
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 ***************************************************************************/
23 #include "bufferinfo.h"
26 #include "ircchannel.h"
30 #include "networkmodel.h"
31 #include "buffermodel.h"
32 #include "nickmodel.h"
33 #include "quasselui.h"
34 #include "signalproxy.h"
37 QPointer<Client> Client::instanceptr = 0;
39 /*** Initialization/destruction ***/
41 Client *Client::instance() {
43 instanceptr = new Client();
47 void Client::destroy() {
49 instanceptr->deleteLater();
52 void Client::init(AbstractUi *ui) {
53 instance()->mainUi = ui;
57 Client::Client(QObject *parent)
60 _signalProxy(new SignalProxy(SignalProxy::Client, this)),
65 _connectedToCore(false),
76 _networkModel = new NetworkModel(this);
77 connect(this, SIGNAL(bufferUpdated(BufferInfo)),
78 _networkModel, SLOT(bufferUpdated(BufferInfo)));
80 _bufferModel = new BufferModel(_networkModel);
81 _nickModel = new NickModel(_networkModel);
83 SignalProxy *p = signalProxy();
84 p->attachSignal(this, SIGNAL(sendSessionData(const QString &, const QVariant &)),
85 SIGNAL(clientSessionDataChanged(const QString &, const QVariant &)));
86 p->attachSlot(SIGNAL(coreSessionDataChanged(const QString &, const QVariant &)),
87 this, SLOT(recvSessionData(const QString &, const QVariant &)));
88 //p->attachSlot(SIGNAL(networkConnected(uint)),
89 //FIXME this, SLOT(networkConnected(uint)));
90 //p->attachSlot(SIGNAL(networkDisconnected(uint)),
91 //FIXME this, SLOT(networkDisconnected(uint)));
92 p->attachSlot(SIGNAL(displayMsg(const Message &)),
93 this, SLOT(recvMessage(const Message &)));
94 p->attachSlot(SIGNAL(displayStatusMsg(QString, QString)),
95 this, SLOT(recvStatusMsg(QString, QString)));
98 p->attachSlot(SIGNAL(backlogData(BufferInfo, const QVariantList &, bool)), this, SLOT(recvBacklogData(BufferInfo, const QVariantList &, bool)));
99 p->attachSlot(SIGNAL(bufferInfoUpdated(BufferInfo)), this, SLOT(updateBufferInfo(BufferInfo)));
100 p->attachSignal(this, SIGNAL(sendInput(BufferInfo, QString)));
101 p->attachSignal(this, SIGNAL(requestNetworkStates()));
103 p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &)), SIGNAL(createIdentity(const Identity &)));
104 p->attachSignal(this, SIGNAL(requestUpdateIdentity(const Identity &)), SIGNAL(updateIdentity(const Identity &)));
105 p->attachSignal(this, SIGNAL(requestRemoveIdentity(IdentityId)), SIGNAL(removeIdentity(IdentityId)));
106 p->attachSlot(SIGNAL(identityCreated(const Identity &)), this, SLOT(coreIdentityCreated(const Identity &)));
107 p->attachSlot(SIGNAL(identityRemoved(IdentityId)), this, SLOT(coreIdentityRemoved(IdentityId)));
109 connect(p, SIGNAL(disconnected()), this, SLOT(disconnectFromCore()));
111 //connect(mainUi, SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
112 connect(mainUi, SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
113 connect(this, SIGNAL(connected()), mainUi, SLOT(connectedToCore()));
114 connect(this, SIGNAL(disconnected()), mainUi, SLOT(disconnectedFromCore()));
116 layoutTimer = new QTimer(this);
117 layoutTimer->setInterval(0);
118 layoutTimer->setSingleShot(false);
119 connect(layoutTimer, SIGNAL(timeout()), this, SLOT(layoutMsg()));
123 /*** public static methods ***/
126 QList<Network *> Client::networks() {
127 return instance()->_networks.values();
130 Network *Client::network(NetworkId networkid) {
131 if(instance()->_networks.contains(networkid))
132 return instance()->_networks[networkid];
137 QList<BufferInfo> Client::allBufferInfos() {
138 QList<BufferInfo> bufferids;
139 foreach(Buffer *buffer, buffers()) {
140 bufferids << buffer->bufferInfo();
145 QList<Buffer *> Client::buffers() {
146 return instance()->_buffers.values();
151 Buffer *Client::buffer(BufferId bufferUid) {
152 if(instance()->_buffers.contains(bufferUid))
153 return instance()->_buffers[bufferUid];
159 Buffer *Client::buffer(BufferInfo id) {
160 Buffer *buff = buffer(id.uid());
163 Client *client = Client::instance();
164 buff = new Buffer(id, client);
166 connect(buff, SIGNAL(userInput(BufferInfo, QString)),
167 client, SLOT(userInput(BufferInfo, QString)));
168 connect(buff, SIGNAL(destroyed()),
169 client, SLOT(bufferDestroyed()));
170 client->_buffers[id.uid()] = buff;
171 emit client->bufferUpdated(id);
178 NetworkModel *Client::networkModel() {
179 return instance()->_networkModel;
182 BufferModel *Client::bufferModel() {
183 return instance()->_bufferModel;
186 NickModel *Client::nickModel() {
187 return instance()->_nickModel;
191 SignalProxy *Client::signalProxy() {
192 return instance()->_signalProxy;
195 bool Client::isConnected() {
196 return instance()->_connectedToCore;
199 bool Client::isSynced() {
200 return instance()->_syncedToCore;
203 /*** Identity handling ***/
205 QList<IdentityId> Client::identityIds() {
206 return instance()->_identities.keys();
209 const Identity * Client::identity(IdentityId id) {
210 if(instance()->_identities.contains(id)) return instance()->_identities[id];
214 void Client::createIdentity(const Identity &id) {
215 emit instance()->requestCreateIdentity(id);
218 void Client::updateIdentity(const Identity &id) {
219 emit instance()->requestUpdateIdentity(id);
222 void Client::removeIdentity(IdentityId id) {
223 emit instance()->requestRemoveIdentity(id);
226 void Client::coreIdentityCreated(const Identity &other) {
227 if(!_identities.contains(other.id())) {
228 Identity *identity = new Identity(other, this);
229 _identities[other.id()] = identity;
230 identity->setInitialized();
231 signalProxy()->synchronize(identity);
232 emit identityCreated(other.id());
234 qWarning() << tr("Identity already exists in client!");
238 void Client::coreIdentityRemoved(IdentityId id) {
239 if(_identities.contains(id)) {
240 emit identityRemoved(id);
241 Identity *i = _identities.take(id);
248 void Client::fakeInput(BufferId bufferUid, QString message) {
249 Buffer *buff = buffer(bufferUid);
251 qWarning() << "No Buffer with uid" << bufferUid << "can't send Input" << message;
253 emit instance()->sendInput(buff->bufferInfo(), message);
256 void Client::fakeInput(BufferInfo bufferInfo, QString message) {
257 fakeInput(bufferInfo, message);
260 /*** core connection stuff ***/
262 void Client::setConnectedToCore(QIODevice *sock) {
264 signalProxy()->addPeer(socket);
265 _connectedToCore = true;
268 void Client::setSyncedToCore() {
269 _syncedToCore = true;
271 emit coreConnectionStateChanged(true);
274 void Client::disconnectFromCore() {
277 socket->deleteLater();
279 _connectedToCore = false;
280 _syncedToCore = false;
282 emit coreConnectionStateChanged(false);
284 // Clear internal data. Hopefully nothing relies on it at this point.
285 _networkModel->clear();
287 QHash<BufferId, Buffer *>::iterator bufferIter = _buffers.begin();
288 while(bufferIter != _buffers.end()) {
289 Buffer *buffer = bufferIter.value();
290 disconnect(buffer, SIGNAL(destroyed()), this, 0);
291 bufferIter = _buffers.erase(bufferIter);
292 buffer->deleteLater();
294 Q_ASSERT(_buffers.isEmpty());
297 QHash<NetworkId, Network*>::iterator netIter = _networks.begin();
298 while(netIter != _networks.end()) {
299 Network *net = netIter.value();
300 disconnect(net, SIGNAL(destroyed()), this, 0);
301 netIter = _networks.erase(netIter);
304 Q_ASSERT(_networks.isEmpty());
306 QHash<IdentityId, Identity*>::iterator idIter = _identities.begin();
307 while(idIter != _identities.end()) {
308 Identity *id = idIter.value();
309 emit identityRemoved(id->id());
310 idIter = _identities.erase(idIter);
313 Q_ASSERT(_identities.isEmpty());
320 void Client::setCoreConfiguration(const QVariantMap &settings) {
321 SignalProxy::writeDataToDevice(socket, settings);
324 /*** Session data ***/
326 void Client::recvSessionData(const QString &key, const QVariant &data) {
327 sessionData[key] = data;
328 emit sessionDataChanged(key, data);
329 emit sessionDataChanged(key);
332 void Client::storeSessionData(const QString &key, const QVariant &data) {
333 // Not sure if this is a good idea, but we'll try it anyway:
334 // Calling this function only sends a signal to core. Data is stored upon reception of the update signal,
335 // rather than immediately.
336 emit instance()->sendSessionData(key, data);
339 QVariant Client::retrieveSessionData(const QString &key, const QVariant &def) {
340 if(instance()->sessionData.contains(key)) return instance()->sessionData[key];
344 QStringList Client::sessionDataKeys() {
345 return instance()->sessionData.keys();
351 void Client::networkConnected(uint netid) {
352 // TODO: create statusBuffer / switch to networkids
353 //BufferInfo id = statusBufferInfo(net);
354 //Buffer *b = buffer(id);
355 //b->setActive(true);
357 Network *netinfo = new Network(netid, this);
358 netinfo->setProxy(signalProxy());
359 networkModel()->attachNetwork(netinfo);
360 connect(netinfo, SIGNAL(destroyed()), this, SLOT(networkDestroyed()));
361 _networks[netid] = netinfo;
364 void Client::networkDisconnected(NetworkId networkid) {
365 if(!_networks.contains(networkid)) {
366 qWarning() << "Client::networkDisconnected(uint): unknown Network" << networkid;
370 Network *net = _networks.take(networkid);
371 if(!net->isInitialized()) {
372 qDebug() << "Network" << networkid << "disconnected while not yet initialized!";
373 updateCoreConnectionProgress();
379 void Client::addNetwork(NetworkId netid) {
380 Network *net = new Network(netid, instance());
384 void Client::addNetwork(Network *net) {
385 net->setProxy(signalProxy());
386 signalProxy()->synchronize(net);
387 networkModel()->attachNetwork(net);
388 connect(net, SIGNAL(destroyed()), instance(), SLOT(networkDestroyed()));
389 instance()->_networks[net->networkId()] = net;
390 //if(net->networkId() == 1) net->requestConnect(); // FIXME
395 void Client::updateBufferInfo(BufferInfo id) {
396 emit bufferUpdated(id);
399 void Client::bufferDestroyed() {
400 Buffer *buffer = static_cast<Buffer *>(sender());
401 QHash<BufferId, Buffer *>::iterator iter = _buffers.begin();
402 while(iter != _buffers.end()) {
403 if(iter.value() == buffer) {
404 iter = _buffers.erase(iter);
411 void Client::networkDestroyed() {
412 Network *netinfo = static_cast<Network *>(sender());
413 NetworkId networkId = netinfo->networkId();
414 if(_networks.contains(networkId))
415 _networks.remove(networkId);
418 void Client::recvMessage(const Message &msg) {
419 Buffer *b = buffer(msg.buffer());
421 // Buffer::ActivityLevel level = Buffer::OtherActivity;
422 // if(msg.type() == Message::Plain || msg.type() == Message::Notice){
423 // level |= Buffer::NewMessage;
425 // if(msg.flags() & Message::Highlight){
426 // level |= Buffer::Highlight;
428 // emit bufferActivity(level, b);
433 void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) {
434 //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
437 void Client::recvBacklogData(BufferInfo id, QVariantList msgs, bool /*done*/) {
438 Buffer *b = buffer(id);
439 foreach(QVariant v, msgs) {
440 Message msg = v.value<Message>();
442 if(!layoutQueue.contains(b)) layoutQueue.append(b);
444 if(layoutQueue.count() && !layoutTimer->isActive()) layoutTimer->start();
447 void Client::layoutMsg() {
448 if(layoutQueue.count()) {
449 Buffer *b = layoutQueue.takeFirst(); // TODO make this the current buffer
451 layoutQueue.append(b); // Buffer has more messages in its queue --> Round Robin
454 if(!layoutQueue.count())
458 AbstractUiMsg *Client::layoutMsg(const Message &msg) {
459 return instance()->mainUi->layoutMsg(msg);
462 void Client::userInput(BufferInfo id, QString msg) {
463 emit sendInput(id, msg);