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 "bufferviewmanager.h"
29 #include "clientbacklogmanager.h"
30 #include "clientirclisthelper.h"
31 #include "clientidentity.h"
32 #include "ircchannel.h"
35 #include "messagemodel.h"
37 #include "networkmodel.h"
39 #include "quasselui.h"
40 #include "signalproxy.h"
46 QPointer<Client> Client::instanceptr = 0;
47 AccountId Client::_currentCoreAccount = 0;
49 /*** Initialization/destruction ***/
51 Client *Client::instance() {
53 instanceptr = new Client();
57 void Client::destroy() {
59 delete instanceptr->mainUi();
60 instanceptr->deleteLater();
65 void Client::init(AbstractUi *ui) {
66 instance()->_mainUi = ui;
70 Client::Client(QObject *parent)
72 _signalProxy(new SignalProxy(SignalProxy::Client, this)),
77 _backlogManager(new ClientBacklogManager(this)),
78 _bufferViewManager(0),
79 _ircListHelper(new ClientIrcListHelper(this)),
82 _connectedToCore(false),
84 _debugLog(&_debugLogBuffer)
86 _signalProxy->synchronize(_ircListHelper);
94 _currentCoreAccount = 0;
95 _networkModel = new NetworkModel(this);
97 connect(this, SIGNAL(networkRemoved(NetworkId)),
98 _networkModel, SLOT(networkRemoved(NetworkId)));
100 _bufferModel = new BufferModel(_networkModel);
101 _messageModel = mainUi()->createMessageModel(this);
102 _messageProcessor = mainUi()->createMessageProcessor(this);
104 SignalProxy *p = signalProxy();
106 p->attachSlot(SIGNAL(displayMsg(const Message &)), this, SLOT(recvMessage(const Message &)));
107 p->attachSlot(SIGNAL(displayStatusMsg(QString, QString)), this, SLOT(recvStatusMsg(QString, QString)));
109 p->attachSlot(SIGNAL(bufferInfoUpdated(BufferInfo)), _networkModel, SLOT(bufferUpdated(BufferInfo)));
110 p->attachSignal(this, SIGNAL(sendInput(BufferInfo, QString)));
111 p->attachSignal(this, SIGNAL(requestNetworkStates()));
113 p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &, const QVariantMap &)), SIGNAL(createIdentity(const Identity &, const QVariantMap &)));
114 p->attachSignal(this, SIGNAL(requestRemoveIdentity(IdentityId)), SIGNAL(removeIdentity(IdentityId)));
115 p->attachSlot(SIGNAL(identityCreated(const Identity &)), this, SLOT(coreIdentityCreated(const Identity &)));
116 p->attachSlot(SIGNAL(identityRemoved(IdentityId)), this, SLOT(coreIdentityRemoved(IdentityId)));
118 p->attachSignal(this, SIGNAL(requestCreateNetwork(const NetworkInfo &)), SIGNAL(createNetwork(const NetworkInfo &)));
119 p->attachSignal(this, SIGNAL(requestRemoveNetwork(NetworkId)), SIGNAL(removeNetwork(NetworkId)));
120 p->attachSlot(SIGNAL(networkCreated(NetworkId)), this, SLOT(coreNetworkCreated(NetworkId)));
121 p->attachSlot(SIGNAL(networkRemoved(NetworkId)), this, SLOT(coreNetworkRemoved(NetworkId)));
123 connect(p, SIGNAL(disconnected()), this, SLOT(disconnectedFromCore()));
125 //connect(mainUi(), SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
126 connect(mainUi(), SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
127 connect(this, SIGNAL(connected()), mainUi(), SLOT(connectedToCore()));
128 connect(this, SIGNAL(disconnected()), mainUi(), SLOT(disconnectedFromCore()));
130 // attach backlog manager
131 p->synchronize(backlogManager());
132 connect(backlogManager(), SIGNAL(messagesReceived(BufferId, int)), _messageModel, SLOT(messagesReceived(BufferId, int)));
135 /*** public static methods ***/
137 AbstractUi *Client::mainUi() {
138 return instance()->_mainUi;
141 AccountId Client::currentCoreAccount() {
142 return _currentCoreAccount;
145 void Client::setCurrentCoreAccount(AccountId id) {
146 _currentCoreAccount = id;
149 bool Client::isConnected() {
150 return instance()->_connectedToCore;
153 bool Client::isSynced() {
154 return instance()->_syncedToCore;
157 /*** Network handling ***/
159 QList<NetworkId> Client::networkIds() {
160 return instance()->_networks.keys();
163 const Network * Client::network(NetworkId networkid) {
164 if(instance()->_networks.contains(networkid)) return instance()->_networks[networkid];
168 void Client::createNetwork(const NetworkInfo &info) {
169 emit instance()->requestCreateNetwork(info);
172 void Client::removeNetwork(NetworkId id) {
173 emit instance()->requestRemoveNetwork(id);
176 void Client::updateNetwork(const NetworkInfo &info) {
177 Network *netptr = instance()->_networks.value(info.networkId, 0);
179 qWarning() << "Update for unknown network requested:" << info;
182 netptr->requestSetNetworkInfo(info);
185 void Client::addNetwork(Network *net) {
186 net->setProxy(signalProxy());
187 signalProxy()->synchronize(net);
188 networkModel()->attachNetwork(net);
189 connect(net, SIGNAL(destroyed()), instance(), SLOT(networkDestroyed()));
190 instance()->_networks[net->networkId()] = net;
191 emit instance()->networkCreated(net->networkId());
194 void Client::coreNetworkCreated(NetworkId id) {
195 if(_networks.contains(id)) {
196 qWarning() << "Creation of already existing network requested!";
199 Network *net = new Network(id, this);
203 void Client::coreNetworkRemoved(NetworkId id) {
204 if(!_networks.contains(id))
206 Network *net = _networks.take(id);
207 emit networkRemoved(net->networkId());
211 /*** Identity handling ***/
213 QList<IdentityId> Client::identityIds() {
214 return instance()->_identities.keys();
217 const Identity *Client::identity(IdentityId id) {
218 if(instance()->_identities.contains(id)) return instance()->_identities[id];
222 void Client::createIdentity(const CertIdentity &id) {
223 QVariantMap additional;
225 additional["KeyPem"] = id.sslKey().toPem();
226 additional["CertPem"] = id.sslCert().toPem();
228 emit instance()->requestCreateIdentity(id, additional);
231 void Client::updateIdentity(IdentityId id, const QVariantMap &ser) {
232 Identity *idptr = instance()->_identities.value(id, 0);
234 qWarning() << "Update for unknown identity requested:" << id;
237 idptr->requestUpdate(ser);
240 void Client::removeIdentity(IdentityId id) {
241 emit instance()->requestRemoveIdentity(id);
244 void Client::coreIdentityCreated(const Identity &other) {
245 if(!_identities.contains(other.id())) {
246 Identity *identity = new Identity(other, this);
247 _identities[other.id()] = identity;
248 identity->setInitialized();
249 signalProxy()->synchronize(identity);
250 emit identityCreated(other.id());
252 qWarning() << tr("Identity already exists in client!");
256 void Client::coreIdentityRemoved(IdentityId id) {
257 if(_identities.contains(id)) {
258 emit identityRemoved(id);
259 Identity *i = _identities.take(id);
265 void Client::userInput(BufferInfo bufferInfo, QString message) {
266 emit instance()->sendInput(bufferInfo, message);
269 /*** core connection stuff ***/
271 void Client::setConnectedToCore(AccountId id, QIODevice *socket) {
272 if(socket) { // external core
273 // if the socket is an orphan, the signalProxy adopts it.
274 // -> we don't need to care about it anymore
275 socket->setParent(0);
276 signalProxy()->addPeer(socket);
278 _connectedToCore = true;
279 setCurrentCoreAccount(id);
282 void Client::setSyncedToCore() {
283 // create buffersyncer
284 Q_ASSERT(!_bufferSyncer);
285 _bufferSyncer = new BufferSyncer(this);
286 connect(bufferSyncer(), SIGNAL(lastSeenMsgSet(BufferId, MsgId)), _networkModel, SLOT(setLastSeenMsgId(BufferId, MsgId)));
287 connect(bufferSyncer(), SIGNAL(bufferRemoved(BufferId)), this, SLOT(bufferRemoved(BufferId)));
288 connect(bufferSyncer(), SIGNAL(bufferRenamed(BufferId, QString)), this, SLOT(bufferRenamed(BufferId, QString)));
289 connect(bufferSyncer(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
290 connect(networkModel(), SIGNAL(setLastSeenMsg(BufferId, MsgId)), bufferSyncer(), SLOT(requestSetLastSeenMsg(BufferId, const MsgId &)));
291 signalProxy()->synchronize(bufferSyncer());
293 // create a new BufferViewManager
294 Q_ASSERT(!_bufferViewManager);
295 _bufferViewManager = new BufferViewManager(signalProxy(), this);
296 connect(bufferViewManager(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
298 _syncedToCore = true;
300 emit coreConnectionStateChanged(true);
303 void Client::requestInitialBacklog() {
304 if(bufferViewManager()->isInitialized() && bufferSyncer()->isInitialized())
305 Client::backlogManager()->requestInitialBacklog();
308 void Client::setSecuredConnection() {
309 emit securedConnection();
312 void Client::disconnectFromCore() {
316 signalProxy()->removeAllPeers();
319 void Client::disconnectedFromCore() {
320 _connectedToCore = false;
321 _syncedToCore = false;
323 emit coreConnectionStateChanged(false);
325 backlogManager()->reset();
326 messageProcessor()->reset();
328 // Clear internal data. Hopefully nothing relies on it at this point.
329 setCurrentCoreAccount(0);
332 _bufferSyncer->deleteLater();
336 if(_bufferViewManager) {
337 _bufferViewManager->deleteLater();
338 _bufferViewManager = 0;
341 _messageModel->clear();
342 _networkModel->clear();
344 QHash<NetworkId, Network*>::iterator netIter = _networks.begin();
345 while(netIter != _networks.end()) {
346 Network *net = netIter.value();
347 emit networkRemoved(net->networkId());
348 disconnect(net, SIGNAL(destroyed()), this, 0);
349 netIter = _networks.erase(netIter);
352 Q_ASSERT(_networks.isEmpty());
354 QHash<IdentityId, Identity *>::iterator idIter = _identities.begin();
355 while(idIter != _identities.end()) {
356 emit identityRemoved(idIter.key());
357 Identity *id = idIter.value();
358 idIter = _identities.erase(idIter);
361 Q_ASSERT(_identities.isEmpty());
367 void Client::networkDestroyed() {
368 Network *net = static_cast<Network *>(sender());
369 QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
370 while(netIter != _networks.end()) {
371 if(*netIter == net) {
372 netIter = _networks.erase(netIter);
380 // Hmm... we never used this...
381 void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) {
382 //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
385 void Client::recvMessage(const Message &msg) {
387 messageProcessor()->process(msg_);
390 void Client::setBufferLastSeenMsg(BufferId id, const MsgId &msgId) {
393 bufferSyncer()->requestSetLastSeenMsg(id, msgId);
396 void Client::removeBuffer(BufferId id) {
397 if(!bufferSyncer()) return;
398 bufferSyncer()->requestRemoveBuffer(id);
401 void Client::bufferRemoved(BufferId bufferId) {
402 // select a sane buffer (status buffer)
403 /* we have to manually select a buffer because otherwise inconsitent changes
404 * to the model might occur:
405 * the result of a buffer removal triggers a change in the selection model.
406 * the newly selected buffer might be a channel that hasn't been selected yet
407 * and a new nickview would be created (which never heard of the "rowsAboutToBeRemoved").
408 * this new view (and/or) its sort filter will then only receive a "rowsRemoved" signal.
410 QModelIndex current = bufferModel()->currentIndex();
411 if(current.data(NetworkModel::BufferIdRole).value<BufferId>() == bufferId) {
412 bufferModel()->setCurrentIndex(current.sibling(0,0));
415 // and remove it from the model
416 networkModel()->removeBuffer(bufferId);
419 void Client::bufferRenamed(BufferId bufferId, const QString &newName) {
420 QModelIndex bufferIndex = networkModel()->bufferIndex(bufferId);
421 if(bufferIndex.isValid()) {
422 networkModel()->setData(bufferIndex, newName, Qt::DisplayRole);
426 void Client::logMessage(QtMsgType type, const char *msg) {
427 fprintf(stderr, "%s\n", msg);
429 if(type == QtFatalMsg) {
430 Quassel::logFatalMessage(msg);
432 QString msgString = QString("%1\n").arg(msg);
433 instance()->_debugLog << msgString;
434 emit instance()->logUpdated(msgString);