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 "abstractmessageprocessor.h"
24 #include "bufferinfo.h"
25 #include "buffermodel.h"
26 #include "buffersettings.h"
27 #include "buffersyncer.h"
28 #include "bufferviewconfig.h"
29 #include "bufferviewmanager.h"
30 #include "clientbacklogmanager.h"
31 #include "clientirclisthelper.h"
32 #include "clientidentity.h"
33 #include "ircchannel.h"
36 #include "messagemodel.h"
38 #include "networkmodel.h"
40 #include "quasselui.h"
41 #include "signalproxy.h"
47 QPointer<Client> Client::instanceptr = 0;
48 AccountId Client::_currentCoreAccount = 0;
50 /*** Initialization/destruction ***/
52 Client *Client::instance() {
54 instanceptr = new Client();
58 void Client::destroy() {
60 delete instanceptr->mainUi();
61 instanceptr->deleteLater();
66 void Client::init(AbstractUi *ui) {
67 instance()->_mainUi = ui;
71 Client::Client(QObject *parent)
73 _signalProxy(new SignalProxy(SignalProxy::Client, this)),
78 _backlogManager(new ClientBacklogManager(this)),
79 _bufferViewManager(0),
80 _ircListHelper(new ClientIrcListHelper(this)),
83 _connectedToCore(false),
86 _debugLog(&_debugLogBuffer)
88 _signalProxy->synchronize(_ircListHelper);
96 _currentCoreAccount = 0;
97 _networkModel = new NetworkModel(this);
99 connect(this, SIGNAL(networkRemoved(NetworkId)),
100 _networkModel, SLOT(networkRemoved(NetworkId)));
102 _bufferModel = new BufferModel(_networkModel);
103 _messageModel = mainUi()->createMessageModel(this);
104 _messageProcessor = mainUi()->createMessageProcessor(this);
106 SignalProxy *p = signalProxy();
108 p->attachSlot(SIGNAL(displayMsg(const Message &)), this, SLOT(recvMessage(const Message &)));
109 p->attachSlot(SIGNAL(displayStatusMsg(QString, QString)), this, SLOT(recvStatusMsg(QString, QString)));
111 p->attachSlot(SIGNAL(bufferInfoUpdated(BufferInfo)), _networkModel, SLOT(bufferUpdated(BufferInfo)));
112 p->attachSignal(this, SIGNAL(sendInput(BufferInfo, QString)));
113 p->attachSignal(this, SIGNAL(requestNetworkStates()));
115 p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &, const QVariantMap &)), SIGNAL(createIdentity(const Identity &, const QVariantMap &)));
116 p->attachSignal(this, SIGNAL(requestRemoveIdentity(IdentityId)), SIGNAL(removeIdentity(IdentityId)));
117 p->attachSlot(SIGNAL(identityCreated(const Identity &)), this, SLOT(coreIdentityCreated(const Identity &)));
118 p->attachSlot(SIGNAL(identityRemoved(IdentityId)), this, SLOT(coreIdentityRemoved(IdentityId)));
120 p->attachSignal(this, SIGNAL(requestCreateNetwork(const NetworkInfo &)), SIGNAL(createNetwork(const NetworkInfo &)));
121 p->attachSignal(this, SIGNAL(requestRemoveNetwork(NetworkId)), SIGNAL(removeNetwork(NetworkId)));
122 p->attachSlot(SIGNAL(networkCreated(NetworkId)), this, SLOT(coreNetworkCreated(NetworkId)));
123 p->attachSlot(SIGNAL(networkRemoved(NetworkId)), this, SLOT(coreNetworkRemoved(NetworkId)));
125 connect(p, SIGNAL(disconnected()), this, SLOT(disconnectedFromCore()));
127 //connect(mainUi(), SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
128 connect(mainUi(), SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
129 connect(this, SIGNAL(connected()), mainUi(), SLOT(connectedToCore()));
130 connect(this, SIGNAL(disconnected()), mainUi(), SLOT(disconnectedFromCore()));
132 // attach backlog manager
133 p->synchronize(backlogManager());
134 connect(backlogManager(), SIGNAL(messagesReceived(BufferId, int)), _messageModel, SLOT(messagesReceived(BufferId, int)));
137 /*** public static methods ***/
139 AbstractUi *Client::mainUi() {
140 return instance()->_mainUi;
143 AccountId Client::currentCoreAccount() {
144 return _currentCoreAccount;
147 void Client::setCurrentCoreAccount(AccountId id) {
148 _currentCoreAccount = id;
151 bool Client::isConnected() {
152 return instance()->_connectedToCore;
155 bool Client::isSynced() {
156 return instance()->_syncedToCore;
159 /*** Network handling ***/
161 QList<NetworkId> Client::networkIds() {
162 return instance()->_networks.keys();
165 const Network * Client::network(NetworkId networkid) {
166 if(instance()->_networks.contains(networkid)) return instance()->_networks[networkid];
170 void Client::createNetwork(const NetworkInfo &info) {
171 emit instance()->requestCreateNetwork(info);
174 void Client::removeNetwork(NetworkId id) {
175 emit instance()->requestRemoveNetwork(id);
178 void Client::updateNetwork(const NetworkInfo &info) {
179 Network *netptr = instance()->_networks.value(info.networkId, 0);
181 qWarning() << "Update for unknown network requested:" << info;
184 netptr->requestSetNetworkInfo(info);
187 void Client::addNetwork(Network *net) {
188 net->setProxy(signalProxy());
189 signalProxy()->synchronize(net);
190 networkModel()->attachNetwork(net);
191 connect(net, SIGNAL(destroyed()), instance(), SLOT(networkDestroyed()));
192 instance()->_networks[net->networkId()] = net;
193 emit instance()->networkCreated(net->networkId());
196 void Client::coreNetworkCreated(NetworkId id) {
197 if(_networks.contains(id)) {
198 qWarning() << "Creation of already existing network requested!";
201 Network *net = new Network(id, this);
205 void Client::coreNetworkRemoved(NetworkId id) {
206 if(!_networks.contains(id))
208 Network *net = _networks.take(id);
209 emit networkRemoved(net->networkId());
213 /*** Identity handling ***/
215 QList<IdentityId> Client::identityIds() {
216 return instance()->_identities.keys();
219 const Identity *Client::identity(IdentityId id) {
220 if(instance()->_identities.contains(id)) return instance()->_identities[id];
224 void Client::createIdentity(const CertIdentity &id) {
225 QVariantMap additional;
227 additional["KeyPem"] = id.sslKey().toPem();
228 additional["CertPem"] = id.sslCert().toPem();
230 emit instance()->requestCreateIdentity(id, additional);
233 void Client::updateIdentity(IdentityId id, const QVariantMap &ser) {
234 Identity *idptr = instance()->_identities.value(id, 0);
236 qWarning() << "Update for unknown identity requested:" << id;
239 idptr->requestUpdate(ser);
242 void Client::removeIdentity(IdentityId id) {
243 emit instance()->requestRemoveIdentity(id);
246 void Client::coreIdentityCreated(const Identity &other) {
247 if(!_identities.contains(other.id())) {
248 Identity *identity = new Identity(other, this);
249 _identities[other.id()] = identity;
250 identity->setInitialized();
251 signalProxy()->synchronize(identity);
252 emit identityCreated(other.id());
254 qWarning() << tr("Identity already exists in client!");
258 void Client::coreIdentityRemoved(IdentityId id) {
259 if(_identities.contains(id)) {
260 emit identityRemoved(id);
261 Identity *i = _identities.take(id);
267 void Client::userInput(BufferInfo bufferInfo, QString message) {
268 emit instance()->sendInput(bufferInfo, message);
271 /*** core connection stuff ***/
273 void Client::setConnectedToCore(AccountId id, QIODevice *socket) {
274 if(socket) { // external core
275 // if the socket is an orphan, the signalProxy adopts it.
276 // -> we don't need to care about it anymore
277 socket->setParent(0);
278 signalProxy()->addPeer(socket);
280 _internalCore = !socket;
281 _connectedToCore = true;
282 setCurrentCoreAccount(id);
285 void Client::setSyncedToCore() {
286 // create buffersyncer
287 Q_ASSERT(!_bufferSyncer);
288 _bufferSyncer = new BufferSyncer(this);
289 connect(bufferSyncer(), SIGNAL(lastSeenMsgSet(BufferId, MsgId)), _networkModel, SLOT(setLastSeenMsgId(BufferId, MsgId)));
290 connect(bufferSyncer(), SIGNAL(bufferRemoved(BufferId)), this, SLOT(bufferRemoved(BufferId)));
291 connect(bufferSyncer(), SIGNAL(bufferRenamed(BufferId, QString)), this, SLOT(bufferRenamed(BufferId, QString)));
292 connect(bufferSyncer(), SIGNAL(buffersPermanentlyMerged(BufferId, BufferId)), this, SLOT(buffersPermanentlyMerged(BufferId, BufferId)));
293 connect(bufferSyncer(), SIGNAL(buffersPermanentlyMerged(BufferId, BufferId)), _messageModel, SLOT(buffersPermanentlyMerged(BufferId, BufferId)));
294 connect(bufferSyncer(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
295 connect(networkModel(), SIGNAL(setLastSeenMsg(BufferId, MsgId)), bufferSyncer(), SLOT(requestSetLastSeenMsg(BufferId, const MsgId &)));
296 signalProxy()->synchronize(bufferSyncer());
298 // create a new BufferViewManager
299 Q_ASSERT(!_bufferViewManager);
300 _bufferViewManager = new BufferViewManager(signalProxy(), this);
301 connect(bufferViewManager(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
302 connect(bufferViewManager(), SIGNAL(initDone()), this, SLOT(createDefautBufferView()));
304 createDefaultIdentity();
306 _syncedToCore = true;
308 emit coreConnectionStateChanged(true);
311 void Client::requestInitialBacklog() {
312 if(bufferViewManager()->isInitialized() && bufferSyncer()->isInitialized())
313 Client::backlogManager()->requestInitialBacklog();
316 void Client::createDefautBufferView() {
317 if(bufferViewManager()->bufferViewConfigs().isEmpty()) {
318 BufferViewConfig config(-1);
319 config.setBufferViewName(tr("All Buffers"));
320 config.initSetBufferList(networkModel()->allBufferIdsSorted());
321 bufferViewManager()->requestCreateBufferView(config.toVariantMap());
325 void Client::createDefaultIdentity() {
326 if(_identities.isEmpty()) {
328 identity.setToDefaults();
329 identity.setIdentityName(tr("Default Identity"));
330 createIdentity(identity);
334 void Client::setSecuredConnection() {
335 emit securedConnection();
338 void Client::disconnectFromCore() {
342 signalProxy()->removeAllPeers();
345 void Client::disconnectedFromCore() {
346 _connectedToCore = false;
347 _syncedToCore = false;
349 emit coreConnectionStateChanged(false);
351 backlogManager()->reset();
352 messageProcessor()->reset();
354 // Clear internal data. Hopefully nothing relies on it at this point.
355 setCurrentCoreAccount(0);
358 _bufferSyncer->deleteLater();
362 if(_bufferViewManager) {
363 _bufferViewManager->deleteLater();
364 _bufferViewManager = 0;
367 _messageModel->clear();
368 _networkModel->clear();
370 QHash<NetworkId, Network*>::iterator netIter = _networks.begin();
371 while(netIter != _networks.end()) {
372 Network *net = netIter.value();
373 emit networkRemoved(net->networkId());
374 disconnect(net, SIGNAL(destroyed()), this, 0);
375 netIter = _networks.erase(netIter);
378 Q_ASSERT(_networks.isEmpty());
380 QHash<IdentityId, Identity *>::iterator idIter = _identities.begin();
381 while(idIter != _identities.end()) {
382 emit identityRemoved(idIter.key());
383 Identity *id = idIter.value();
384 idIter = _identities.erase(idIter);
387 Q_ASSERT(_identities.isEmpty());
393 void Client::networkDestroyed() {
394 Network *net = static_cast<Network *>(sender());
395 QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
396 while(netIter != _networks.end()) {
397 if(*netIter == net) {
398 netIter = _networks.erase(netIter);
406 // Hmm... we never used this...
407 void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) {
408 //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
411 void Client::recvMessage(const Message &msg) {
413 messageProcessor()->process(msg_);
416 void Client::setBufferLastSeenMsg(BufferId id, const MsgId &msgId) {
419 bufferSyncer()->requestSetLastSeenMsg(id, msgId);
422 void Client::removeBuffer(BufferId id) {
423 if(!bufferSyncer()) return;
424 bufferSyncer()->requestRemoveBuffer(id);
427 void Client::renameBuffer(BufferId bufferId, const QString &newName) {
430 bufferSyncer()->requestRenameBuffer(bufferId, newName);
433 void Client::mergeBuffersPermanently(BufferId bufferId1, BufferId bufferId2) {
436 bufferSyncer()->requestMergeBuffersPermanently(bufferId1, bufferId2);
439 void Client::bufferRemoved(BufferId bufferId) {
440 // select a sane buffer (status buffer)
441 /* we have to manually select a buffer because otherwise inconsitent changes
442 * to the model might occur:
443 * the result of a buffer removal triggers a change in the selection model.
444 * the newly selected buffer might be a channel that hasn't been selected yet
445 * and a new nickview would be created (which never heard of the "rowsAboutToBeRemoved").
446 * this new view (and/or) its sort filter will then only receive a "rowsRemoved" signal.
448 QModelIndex current = bufferModel()->currentIndex();
449 if(current.data(NetworkModel::BufferIdRole).value<BufferId>() == bufferId) {
450 bufferModel()->setCurrentIndex(current.sibling(0,0));
453 // and remove it from the model
454 networkModel()->removeBuffer(bufferId);
457 void Client::bufferRenamed(BufferId bufferId, const QString &newName) {
458 QModelIndex bufferIndex = networkModel()->bufferIndex(bufferId);
459 if(bufferIndex.isValid()) {
460 networkModel()->setData(bufferIndex, newName, Qt::DisplayRole);
464 void Client::buffersPermanentlyMerged(BufferId bufferId1, BufferId bufferId2) {
465 QModelIndex idx = networkModel()->bufferIndex(bufferId1);
466 bufferModel()->setCurrentIndex(bufferModel()->mapFromSource(idx));
467 networkModel()->removeBuffer(bufferId2);
470 void Client::logMessage(QtMsgType type, const char *msg) {
471 fprintf(stderr, "%s\n", msg);
473 if(type == QtFatalMsg) {
474 Quassel::logFatalMessage(msg);
476 QString msgString = QString("%1\n").arg(msg);
477 instance()->_debugLog << msgString;
478 emit instance()->logUpdated(msgString);