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 "quasselui.h"
33 #include "signalproxy.h"
36 QPointer<Client> Client::instanceptr = 0;
37 AccountId Client::_currentCoreAccount = 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)),
64 _connectedToCore(false),
67 _monitorBuffer = new Buffer(BufferInfo(), this);
75 _currentCoreAccount = 0;
76 _networkModel = new NetworkModel(this);
77 connect(this, SIGNAL(bufferUpdated(BufferInfo)),
78 _networkModel, SLOT(bufferUpdated(BufferInfo)));
80 _bufferModel = new BufferModel(_networkModel);
82 SignalProxy *p = signalProxy();
84 p->attachSlot(SIGNAL(displayMsg(const Message &)), this, SLOT(recvMessage(const Message &)));
85 p->attachSlot(SIGNAL(displayStatusMsg(QString, QString)), this, SLOT(recvStatusMsg(QString, QString)));
87 p->attachSlot(SIGNAL(backlogData(BufferInfo, const QVariantList &, bool)), this, SLOT(recvBacklogData(BufferInfo, const QVariantList &, bool)));
88 p->attachSlot(SIGNAL(bufferInfoUpdated(BufferInfo)), this, SLOT(updateBufferInfo(BufferInfo)));
89 p->attachSignal(this, SIGNAL(sendInput(BufferInfo, QString)));
90 p->attachSignal(this, SIGNAL(requestNetworkStates()));
92 p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &)), SIGNAL(createIdentity(const Identity &)));
93 p->attachSignal(this, SIGNAL(requestUpdateIdentity(const Identity &)), SIGNAL(updateIdentity(const Identity &)));
94 p->attachSignal(this, SIGNAL(requestRemoveIdentity(IdentityId)), SIGNAL(removeIdentity(IdentityId)));
95 p->attachSlot(SIGNAL(identityCreated(const Identity &)), this, SLOT(coreIdentityCreated(const Identity &)));
96 p->attachSlot(SIGNAL(identityRemoved(IdentityId)), this, SLOT(coreIdentityRemoved(IdentityId)));
98 p->attachSignal(this, SIGNAL(requestCreateNetwork(const NetworkInfo &)), SIGNAL(createNetwork(const NetworkInfo &)));
99 p->attachSignal(this, SIGNAL(requestUpdateNetwork(const NetworkInfo &)), SIGNAL(updateNetwork(const NetworkInfo &)));
100 p->attachSignal(this, SIGNAL(requestRemoveNetwork(NetworkId)), SIGNAL(removeNetwork(NetworkId)));
101 p->attachSlot(SIGNAL(networkCreated(NetworkId)), this, SLOT(coreNetworkCreated(NetworkId)));
102 p->attachSlot(SIGNAL(networkRemoved(NetworkId)), this, SLOT(coreNetworkRemoved(NetworkId)));
104 connect(p, SIGNAL(disconnected()), this, SLOT(disconnectFromCore()));
106 //connect(mainUi, SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
107 connect(mainUi, SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
108 connect(this, SIGNAL(connected()), mainUi, SLOT(connectedToCore()));
109 connect(this, SIGNAL(disconnected()), mainUi, SLOT(disconnectedFromCore()));
111 layoutTimer = new QTimer(this);
112 layoutTimer->setInterval(0);
113 layoutTimer->setSingleShot(false);
114 connect(layoutTimer, SIGNAL(timeout()), this, SLOT(layoutMsg()));
118 /*** public static methods ***/
120 AccountId Client::currentCoreAccount() {
121 return _currentCoreAccount;
124 void Client::setCurrentCoreAccount(AccountId id) {
125 _currentCoreAccount = id;
128 QList<BufferInfo> Client::allBufferInfos() {
129 QList<BufferInfo> bufferids;
130 foreach(Buffer *buffer, buffers()) {
131 bufferids << buffer->bufferInfo();
136 QList<Buffer *> Client::buffers() {
137 return instance()->_buffers.values();
142 Buffer *Client::buffer(BufferId bufferId) {
143 if(instance()->_buffers.contains(bufferId))
144 return instance()->_buffers[bufferId];
150 Buffer *Client::buffer(BufferInfo bufferInfo) {
151 Buffer *buff = buffer(bufferInfo.bufferId());
154 Client *client = Client::instance();
155 buff = new Buffer(bufferInfo, client);
156 connect(buff, SIGNAL(destroyed()),
157 client, SLOT(bufferDestroyed()));
158 client->_buffers[bufferInfo.bufferId()] = buff;
159 emit client->bufferUpdated(bufferInfo);
165 Buffer *Client::monitorBuffer() {
166 return instance()->_monitorBuffer;
170 NetworkModel *Client::networkModel() {
171 return instance()->_networkModel;
174 BufferModel *Client::bufferModel() {
175 return instance()->_bufferModel;
178 SignalProxy *Client::signalProxy() {
179 return instance()->_signalProxy;
182 bool Client::isConnected() {
183 return instance()->_connectedToCore;
186 bool Client::isSynced() {
187 return instance()->_syncedToCore;
190 /*** Network handling ***/
192 QList<NetworkId> Client::networkIds() {
193 return instance()->_networks.keys();
196 const Network * Client::network(NetworkId networkid) {
197 if(instance()->_networks.contains(networkid)) return instance()->_networks[networkid];
201 void Client::createNetwork(const NetworkInfo &info) {
202 emit instance()->requestCreateNetwork(info);
205 void Client::updateNetwork(const NetworkInfo &info) {
206 emit instance()->requestUpdateNetwork(info);
209 void Client::removeNetwork(NetworkId id) {
210 emit instance()->requestRemoveNetwork(id);
213 void Client::addNetwork(Network *net) {
214 net->setProxy(signalProxy());
215 signalProxy()->synchronize(net);
216 networkModel()->attachNetwork(net);
217 connect(net, SIGNAL(destroyed()), instance(), SLOT(networkDestroyed()));
218 instance()->_networks[net->networkId()] = net;
219 emit instance()->networkCreated(net->networkId());
222 void Client::coreNetworkCreated(NetworkId id) {
223 if(_networks.contains(id)) {
224 qWarning() << "Creation of already existing network requested!";
227 Network *net = new Network(id, this);
231 void Client::coreNetworkRemoved(NetworkId id) {
232 if(!_networks.contains(id)) return;
233 Network *net = _networks.take(id);
234 emit networkRemoved(net->networkId());
238 /*** Identity handling ***/
240 QList<IdentityId> Client::identityIds() {
241 return instance()->_identities.keys();
244 const Identity * Client::identity(IdentityId id) {
245 if(instance()->_identities.contains(id)) return instance()->_identities[id];
249 void Client::createIdentity(const Identity &id) {
250 emit instance()->requestCreateIdentity(id);
253 void Client::updateIdentity(const Identity &id) {
254 emit instance()->requestUpdateIdentity(id);
257 void Client::removeIdentity(IdentityId id) {
258 emit instance()->requestRemoveIdentity(id);
261 void Client::coreIdentityCreated(const Identity &other) {
262 if(!_identities.contains(other.id())) {
263 Identity *identity = new Identity(other, this);
264 _identities[other.id()] = identity;
265 identity->setInitialized();
266 signalProxy()->synchronize(identity);
267 emit identityCreated(other.id());
269 qWarning() << tr("Identity already exists in client!");
273 void Client::coreIdentityRemoved(IdentityId id) {
274 if(_identities.contains(id)) {
275 emit identityRemoved(id);
276 Identity *i = _identities.take(id);
282 void Client::userInput(BufferInfo bufferInfo, QString message) {
283 emit instance()->sendInput(bufferInfo, message);
286 /*** core connection stuff ***/
288 void Client::setConnectedToCore(QIODevice *sock, AccountId id) {
290 signalProxy()->addPeer(socket);
291 _connectedToCore = true;
292 setCurrentCoreAccount(id);
295 void Client::setSyncedToCore() {
296 _syncedToCore = true;
298 emit coreConnectionStateChanged(true);
301 void Client::disconnectFromCore() {
304 socket->deleteLater();
306 _connectedToCore = false;
307 _syncedToCore = false;
308 setCurrentCoreAccount(0);
310 emit coreConnectionStateChanged(false);
312 // Clear internal data. Hopefully nothing relies on it at this point.
313 _networkModel->clear();
315 QHash<NetworkId, Network*>::iterator netIter = _networks.begin();
316 while(netIter != _networks.end()) {
317 Network *net = netIter.value();
318 emit networkRemoved(net->networkId());
319 disconnect(net, SIGNAL(destroyed()), this, 0);
320 netIter = _networks.erase(netIter);
323 Q_ASSERT(_networks.isEmpty());
325 QHash<BufferId, Buffer *>::iterator bufferIter = _buffers.begin();
326 while(bufferIter != _buffers.end()) {
327 Buffer *buffer = bufferIter.value();
328 disconnect(buffer, SIGNAL(destroyed()), this, 0);
329 bufferIter = _buffers.erase(bufferIter);
330 buffer->deleteLater();
332 Q_ASSERT(_buffers.isEmpty());
334 QHash<IdentityId, Identity*>::iterator idIter = _identities.begin();
335 while(idIter != _identities.end()) {
336 Identity *id = idIter.value();
337 emit identityRemoved(id->id());
338 idIter = _identities.erase(idIter);
341 Q_ASSERT(_identities.isEmpty());
347 void Client::setCoreConfiguration(const QVariantMap &settings) {
348 SignalProxy::writeDataToDevice(socket, settings);
353 void Client::updateBufferInfo(BufferInfo id) {
354 emit bufferUpdated(id);
357 void Client::bufferDestroyed() {
358 Buffer *buffer = static_cast<Buffer *>(sender());
359 QHash<BufferId, Buffer *>::iterator iter = _buffers.begin();
360 while(iter != _buffers.end()) {
361 if(iter.value() == buffer) {
362 iter = _buffers.erase(iter);
369 void Client::networkDestroyed() {
370 Network *net = static_cast<Network *>(sender());
371 QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
372 while(netIter != _networks.end()) {
373 if(*netIter == net) {
374 netIter = _networks.erase(netIter);
382 void Client::recvMessage(const Message &msg) {
383 if(msg.type() == Message::Error) {
384 Buffer *b = buffer(msg.bufferInfo().bufferId());
387 networkModel()->updateBufferActivity(msg);
389 //TODO: display message in the status buffer
390 b = buffer(msg.bufferInfo());
392 networkModel()->updateBufferActivity(msg);
395 Buffer *b = buffer(msg.bufferInfo());
397 networkModel()->updateBufferActivity(msg);
400 if(msg.type() == Message::Plain || msg.type() == Message::Notice || msg.type() == Message::Action) {
401 const Network *net = network(msg.bufferInfo().networkId());
402 QString networkName = net != 0
403 ? net->networkName() + ":"
405 QString sender = networkName + msg.bufferInfo().bufferName() + ":" + msg.sender();
406 Message mmsg = Message(msg.timestamp(), msg.bufferInfo(), msg.type(), msg.text(), sender, msg.flags());
407 monitorBuffer()->appendMsg(mmsg);
412 void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) {
413 //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
416 void Client::recvBacklogData(BufferInfo id, QVariantList msgs, bool /*done*/) {
417 Buffer *b = buffer(id);
418 foreach(QVariant v, msgs) {
419 Message msg = v.value<Message>();
421 networkModel()->updateBufferActivity(msg);
422 if(!layoutQueue.contains(b)) layoutQueue.append(b);
424 if(layoutQueue.count() && !layoutTimer->isActive()) layoutTimer->start();
427 void Client::layoutMsg() {
428 if(layoutQueue.count()) {
429 Buffer *b = layoutQueue.takeFirst(); // TODO make this the current buffer
431 layoutQueue.append(b); // Buffer has more messages in its queue --> Round Robin
434 if(!layoutQueue.count())
438 AbstractUiMsg *Client::layoutMsg(const Message &msg) {
439 return instance()->mainUi->layoutMsg(msg);