1 /***************************************************************************
2 * Copyright (C) 2005-2014 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
23 #include "abstractmessageprocessor.h"
24 #include "abstractui.h"
25 #include "bufferinfo.h"
26 #include "buffermodel.h"
27 #include "buffersettings.h"
28 #include "buffersyncer.h"
29 #include "bufferviewconfig.h"
30 #include "bufferviewoverlay.h"
31 #include "clientaliasmanager.h"
32 #include "clientbacklogmanager.h"
33 #include "clientbufferviewmanager.h"
34 #include "clientirclisthelper.h"
35 #include "clientidentity.h"
36 #include "clientignorelistmanager.h"
37 #include "clienttransfermanager.h"
38 #include "clientuserinputhandler.h"
39 #include "coreaccountmodel.h"
40 #include "coreconnection.h"
41 #include "ircchannel.h"
44 #include "messagemodel.h"
46 #include "networkconfig.h"
47 #include "networkmodel.h"
49 #include "signalproxy.h"
55 QPointer<Client> Client::instanceptr = 0;
56 Quassel::Features Client::_coreFeatures = 0;
58 /*** Initialization/destruction ***/
60 bool Client::instanceExists()
66 Client *Client::instance()
69 instanceptr = new Client();
74 void Client::destroy()
77 delete instanceptr->mainUi();
78 instanceptr->deleteLater();
84 void Client::init(AbstractUi *ui)
86 instance()->_mainUi = ui;
91 Client::Client(QObject *parent)
93 _signalProxy(new SignalProxy(SignalProxy::Client, this)),
99 _backlogManager(new ClientBacklogManager(this)),
100 _bufferViewManager(0),
101 _bufferViewOverlay(new BufferViewOverlay(this)),
102 _ircListHelper(new ClientIrcListHelper(this)),
105 _ignoreListManager(0),
108 _messageProcessor(0),
109 _coreAccountModel(new CoreAccountModel(this)),
110 _coreConnection(new CoreConnection(this)),
112 _debugLog(&_debugLogBuffer)
114 _signalProxy->synchronize(_ircListHelper);
120 disconnectFromCore();
126 _networkModel = new NetworkModel(this);
128 connect(this, SIGNAL(networkRemoved(NetworkId)),
129 _networkModel, SLOT(networkRemoved(NetworkId)));
131 _bufferModel = new BufferModel(_networkModel);
132 _messageModel = mainUi()->createMessageModel(this);
133 _messageProcessor = mainUi()->createMessageProcessor(this);
134 _inputHandler = new ClientUserInputHandler(this);
136 SignalProxy *p = signalProxy();
138 p->attachSlot(SIGNAL(displayMsg(const Message &)), this, SLOT(recvMessage(const Message &)));
139 p->attachSlot(SIGNAL(displayStatusMsg(QString, QString)), this, SLOT(recvStatusMsg(QString, QString)));
141 p->attachSlot(SIGNAL(bufferInfoUpdated(BufferInfo)), _networkModel, SLOT(bufferUpdated(BufferInfo)));
142 p->attachSignal(inputHandler(), SIGNAL(sendInput(BufferInfo, QString)));
143 p->attachSignal(this, SIGNAL(requestNetworkStates()));
145 p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &, const QVariantMap &)), SIGNAL(createIdentity(const Identity &, const QVariantMap &)));
146 p->attachSignal(this, SIGNAL(requestRemoveIdentity(IdentityId)), SIGNAL(removeIdentity(IdentityId)));
147 p->attachSlot(SIGNAL(identityCreated(const Identity &)), this, SLOT(coreIdentityCreated(const Identity &)));
148 p->attachSlot(SIGNAL(identityRemoved(IdentityId)), this, SLOT(coreIdentityRemoved(IdentityId)));
150 p->attachSignal(this, SIGNAL(requestCreateNetwork(const NetworkInfo &, const QStringList &)), SIGNAL(createNetwork(const NetworkInfo &, const QStringList &)));
151 p->attachSignal(this, SIGNAL(requestRemoveNetwork(NetworkId)), SIGNAL(removeNetwork(NetworkId)));
152 p->attachSlot(SIGNAL(networkCreated(NetworkId)), this, SLOT(coreNetworkCreated(NetworkId)));
153 p->attachSlot(SIGNAL(networkRemoved(NetworkId)), this, SLOT(coreNetworkRemoved(NetworkId)));
155 //connect(mainUi(), SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
156 connect(mainUi(), SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
157 connect(this, SIGNAL(connected()), mainUi(), SLOT(connectedToCore()));
158 connect(this, SIGNAL(disconnected()), mainUi(), SLOT(disconnectedFromCore()));
160 // attach backlog manager
161 p->synchronize(backlogManager());
162 connect(backlogManager(), SIGNAL(messagesReceived(BufferId, int)), _messageModel, SLOT(messagesReceived(BufferId, int)));
164 coreAccountModel()->load();
166 connect(coreConnection(), SIGNAL(stateChanged(CoreConnection::ConnectionState)), SLOT(connectionStateChanged(CoreConnection::ConnectionState)));
167 coreConnection()->init();
171 /*** public static methods ***/
173 AbstractUi *Client::mainUi()
175 return instance()->_mainUi;
179 void Client::setCoreFeatures(Quassel::Features features)
181 _coreFeatures = features;
185 bool Client::isConnected()
187 return instance()->_connected;
191 bool Client::internalCore()
193 return currentCoreAccount().isInternal();
197 /*** Network handling ***/
199 QList<NetworkId> Client::networkIds()
201 return instance()->_networks.keys();
205 const Network *Client::network(NetworkId networkid)
207 if (instance()->_networks.contains(networkid)) return instance()->_networks[networkid];
212 void Client::createNetwork(const NetworkInfo &info, const QStringList &persistentChannels)
214 emit instance()->requestCreateNetwork(info, persistentChannels);
218 void Client::removeNetwork(NetworkId id)
220 emit instance()->requestRemoveNetwork(id);
224 void Client::updateNetwork(const NetworkInfo &info)
226 Network *netptr = instance()->_networks.value(info.networkId, 0);
228 qWarning() << "Update for unknown network requested:" << info;
231 netptr->requestSetNetworkInfo(info);
235 void Client::addNetwork(Network *net)
237 net->setProxy(signalProxy());
238 signalProxy()->synchronize(net);
239 networkModel()->attachNetwork(net);
240 connect(net, SIGNAL(destroyed()), instance(), SLOT(networkDestroyed()));
241 instance()->_networks[net->networkId()] = net;
242 emit instance()->networkCreated(net->networkId());
246 void Client::coreNetworkCreated(NetworkId id)
248 if (_networks.contains(id)) {
249 qWarning() << "Creation of already existing network requested!";
252 Network *net = new Network(id, this);
257 void Client::coreNetworkRemoved(NetworkId id)
259 if (!_networks.contains(id))
261 Network *net = _networks.take(id);
262 emit networkRemoved(net->networkId());
267 /*** Identity handling ***/
269 QList<IdentityId> Client::identityIds()
271 return instance()->_identities.keys();
275 const Identity *Client::identity(IdentityId id)
277 if (instance()->_identities.contains(id)) return instance()->_identities[id];
282 void Client::createIdentity(const CertIdentity &id)
284 QVariantMap additional;
286 additional["KeyPem"] = id.sslKey().toPem();
287 additional["CertPem"] = id.sslCert().toPem();
289 emit instance()->requestCreateIdentity(id, additional);
293 void Client::updateIdentity(IdentityId id, const QVariantMap &ser)
295 Identity *idptr = instance()->_identities.value(id, 0);
297 qWarning() << "Update for unknown identity requested:" << id;
300 idptr->requestUpdate(ser);
304 void Client::removeIdentity(IdentityId id)
306 emit instance()->requestRemoveIdentity(id);
310 void Client::coreIdentityCreated(const Identity &other)
312 if (!_identities.contains(other.id())) {
313 Identity *identity = new Identity(other, this);
314 _identities[other.id()] = identity;
315 identity->setInitialized();
316 signalProxy()->synchronize(identity);
317 emit identityCreated(other.id());
320 qWarning() << tr("Identity already exists in client!");
325 void Client::coreIdentityRemoved(IdentityId id)
327 if (_identities.contains(id)) {
328 emit identityRemoved(id);
329 Identity *i = _identities.take(id);
335 /*** User input handling ***/
337 void Client::userInput(const BufferInfo &bufferInfo, const QString &message)
339 // we need to make sure that AliasManager is ready before processing input
340 if (aliasManager() && aliasManager()->isInitialized())
341 inputHandler()->handleUserInput(bufferInfo, message);
343 instance()->_userInputBuffer.append(qMakePair(bufferInfo, message));
347 void Client::sendBufferedUserInput()
349 for (int i = 0; i < _userInputBuffer.count(); i++)
350 userInput(_userInputBuffer.at(i).first, _userInputBuffer.at(i).second);
352 _userInputBuffer.clear();
356 /*** core connection stuff ***/
358 void Client::connectionStateChanged(CoreConnection::ConnectionState state)
361 case CoreConnection::Disconnected:
362 setDisconnectedFromCore();
364 case CoreConnection::Synchronized:
373 void Client::setSyncedToCore()
375 // create buffersyncer
376 Q_ASSERT(!_bufferSyncer);
377 _bufferSyncer = new BufferSyncer(this);
378 connect(bufferSyncer(), SIGNAL(lastSeenMsgSet(BufferId, MsgId)), _networkModel, SLOT(setLastSeenMsgId(BufferId, MsgId)));
379 connect(bufferSyncer(), SIGNAL(markerLineSet(BufferId, MsgId)), _networkModel, SLOT(setMarkerLineMsgId(BufferId, MsgId)));
380 connect(bufferSyncer(), SIGNAL(bufferRemoved(BufferId)), this, SLOT(bufferRemoved(BufferId)));
381 connect(bufferSyncer(), SIGNAL(bufferRenamed(BufferId, QString)), this, SLOT(bufferRenamed(BufferId, QString)));
382 connect(bufferSyncer(), SIGNAL(buffersPermanentlyMerged(BufferId, BufferId)), this, SLOT(buffersPermanentlyMerged(BufferId, BufferId)));
383 connect(bufferSyncer(), SIGNAL(buffersPermanentlyMerged(BufferId, BufferId)), _messageModel, SLOT(buffersPermanentlyMerged(BufferId, BufferId)));
384 connect(bufferSyncer(), SIGNAL(bufferMarkedAsRead(BufferId)), SIGNAL(bufferMarkedAsRead(BufferId)));
385 connect(networkModel(), SIGNAL(requestSetLastSeenMsg(BufferId, MsgId)), bufferSyncer(), SLOT(requestSetLastSeenMsg(BufferId, const MsgId &)));
386 signalProxy()->synchronize(bufferSyncer());
388 // create a new BufferViewManager
389 Q_ASSERT(!_bufferViewManager);
390 _bufferViewManager = new ClientBufferViewManager(signalProxy(), this);
391 connect(_bufferViewManager, SIGNAL(initDone()), _bufferViewOverlay, SLOT(restore()));
393 // create AliasManager
394 Q_ASSERT(!_aliasManager);
395 _aliasManager = new ClientAliasManager(this);
396 connect(aliasManager(), SIGNAL(initDone()), SLOT(sendBufferedUserInput()));
397 signalProxy()->synchronize(aliasManager());
399 // create NetworkConfig
400 Q_ASSERT(!_networkConfig);
401 _networkConfig = new NetworkConfig("GlobalNetworkConfig", this);
402 signalProxy()->synchronize(networkConfig());
404 // create IgnoreListManager
405 Q_ASSERT(!_ignoreListManager);
406 _ignoreListManager = new ClientIgnoreListManager(this);
407 signalProxy()->synchronize(ignoreListManager());
409 Q_ASSERT(!_transferManager);
410 _transferManager = new ClientTransferManager(this);
411 signalProxy()->synchronize(transferManager());
413 // trigger backlog request once all active bufferviews are initialized
414 connect(bufferViewOverlay(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
418 emit coreConnectionStateChanged(true);
422 void Client::requestInitialBacklog()
424 // usually it _should_ take longer until the bufferViews are initialized, so that's what
425 // triggers this slot. But we have to make sure that we know all buffers yet.
426 // so we check the BufferSyncer and in case it wasn't initialized we wait for that instead
427 if (!bufferSyncer()->isInitialized()) {
428 disconnect(bufferViewOverlay(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
429 connect(bufferSyncer(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
432 disconnect(bufferViewOverlay(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
433 disconnect(bufferSyncer(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
435 _backlogManager->requestInitialBacklog();
439 void Client::disconnectFromCore()
441 if (!coreConnection()->isConnected())
444 coreConnection()->disconnectFromCore();
448 void Client::setDisconnectedFromCore()
454 emit coreConnectionStateChanged(false);
456 backlogManager()->reset();
457 messageProcessor()->reset();
459 // Clear internal data. Hopefully nothing relies on it at this point.
462 _bufferSyncer->deleteLater();
466 if (_bufferViewManager) {
467 _bufferViewManager->deleteLater();
468 _bufferViewManager = 0;
471 _bufferViewOverlay->reset();
474 _aliasManager->deleteLater();
478 if (_ignoreListManager) {
479 _ignoreListManager->deleteLater();
480 _ignoreListManager = 0;
483 if (_transferManager) {
484 _transferManager->deleteLater();
485 _transferManager = 0;
488 // we probably don't want to save pending input for reconnect
489 _userInputBuffer.clear();
491 _messageModel->clear();
492 _networkModel->clear();
494 QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
495 while (netIter != _networks.end()) {
496 Network *net = netIter.value();
497 emit networkRemoved(net->networkId());
498 disconnect(net, SIGNAL(destroyed()), this, 0);
499 netIter = _networks.erase(netIter);
502 Q_ASSERT(_networks.isEmpty());
504 QHash<IdentityId, Identity *>::iterator idIter = _identities.begin();
505 while (idIter != _identities.end()) {
506 emit identityRemoved(idIter.key());
507 Identity *id = idIter.value();
508 idIter = _identities.erase(idIter);
511 Q_ASSERT(_identities.isEmpty());
513 if (_networkConfig) {
514 _networkConfig->deleteLater();
522 void Client::networkDestroyed()
524 Network *net = static_cast<Network *>(sender());
525 QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
526 while (netIter != _networks.end()) {
527 if (*netIter == net) {
528 netIter = _networks.erase(netIter);
538 // Hmm... we never used this...
539 void Client::recvStatusMsg(QString /*net*/, QString /*msg*/)
541 //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
545 void Client::recvMessage(const Message &msg)
548 messageProcessor()->process(msg_);
552 void Client::setBufferLastSeenMsg(BufferId id, const MsgId &msgId)
555 bufferSyncer()->requestSetLastSeenMsg(id, msgId);
559 void Client::setMarkerLine(BufferId id, const MsgId &msgId)
562 bufferSyncer()->requestSetMarkerLine(id, msgId);
566 MsgId Client::markerLine(BufferId id)
568 if (id.isValid() && networkModel())
569 return networkModel()->markerLineMsgId(id);
574 void Client::removeBuffer(BufferId id)
576 if (!bufferSyncer()) return;
577 bufferSyncer()->requestRemoveBuffer(id);
581 void Client::renameBuffer(BufferId bufferId, const QString &newName)
585 bufferSyncer()->requestRenameBuffer(bufferId, newName);
589 void Client::mergeBuffersPermanently(BufferId bufferId1, BufferId bufferId2)
593 bufferSyncer()->requestMergeBuffersPermanently(bufferId1, bufferId2);
597 void Client::purgeKnownBufferIds()
601 bufferSyncer()->requestPurgeBufferIds();
605 void Client::bufferRemoved(BufferId bufferId)
607 // select a sane buffer (status buffer)
608 /* we have to manually select a buffer because otherwise inconsitent changes
609 * to the model might occur:
610 * the result of a buffer removal triggers a change in the selection model.
611 * the newly selected buffer might be a channel that hasn't been selected yet
612 * and a new nickview would be created (which never heard of the "rowsAboutToBeRemoved").
613 * this new view (and/or) its sort filter will then only receive a "rowsRemoved" signal.
615 QModelIndex current = bufferModel()->currentIndex();
616 if (current.data(NetworkModel::BufferIdRole).value<BufferId>() == bufferId) {
617 bufferModel()->setCurrentIndex(current.sibling(0, 0));
620 // and remove it from the model
621 networkModel()->removeBuffer(bufferId);
625 void Client::bufferRenamed(BufferId bufferId, const QString &newName)
627 QModelIndex bufferIndex = networkModel()->bufferIndex(bufferId);
628 if (bufferIndex.isValid()) {
629 networkModel()->setData(bufferIndex, newName, Qt::DisplayRole);
634 void Client::buffersPermanentlyMerged(BufferId bufferId1, BufferId bufferId2)
636 QModelIndex idx = networkModel()->bufferIndex(bufferId1);
637 bufferModel()->setCurrentIndex(bufferModel()->mapFromSource(idx));
638 networkModel()->removeBuffer(bufferId2);
642 void Client::markBufferAsRead(BufferId id)
644 if (bufferSyncer() && id.isValid())
645 bufferSyncer()->requestMarkBufferAsRead(id);
649 void Client::logMessage(QtMsgType type, const char *msg)
651 fprintf(stderr, "%s\n", msg);
653 if (type == QtFatalMsg) {
654 Quassel::logFatalMessage(msg);
657 QString msgString = QString("%1\n").arg(msg);
659 //Check to see if there is an instance around, else we risk recursions
660 //when calling instance() and creating new ones.
661 if (!instanceExists())
664 instance()->_debugLog << msgString;
665 emit instance()->logUpdated(msgString);