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"
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 &)), SIGNAL(createIdentity(const Identity &)));
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 Identity &id) {
223 emit instance()->requestCreateIdentity(id);
226 void Client::updateIdentity(IdentityId id, const QVariantMap &ser) {
227 Identity *idptr = instance()->_identities.value(id, 0);
229 qWarning() << "Update for unknown identity requested:" << id;
232 idptr->requestUpdate(ser);
235 void Client::removeIdentity(IdentityId id) {
236 emit instance()->requestRemoveIdentity(id);
239 void Client::coreIdentityCreated(const Identity &other) {
240 if(!_identities.contains(other.id())) {
241 Identity *identity = new Identity(other, this);
242 _identities[other.id()] = identity;
243 identity->setInitialized();
244 signalProxy()->synchronize(identity);
245 emit identityCreated(other.id());
247 qWarning() << tr("Identity already exists in client!");
251 void Client::coreIdentityRemoved(IdentityId id) {
252 if(_identities.contains(id)) {
253 emit identityRemoved(id);
254 Identity *i = _identities.take(id);
260 void Client::userInput(BufferInfo bufferInfo, QString message) {
261 emit instance()->sendInput(bufferInfo, message);
264 /*** core connection stuff ***/
266 void Client::setConnectedToCore(AccountId id, QIODevice *socket) {
267 if(socket) { // external core
268 // if the socket is an orphan, the signalProxy adopts it.
269 // -> we don't need to care about it anymore
270 socket->setParent(0);
271 signalProxy()->addPeer(socket);
273 _connectedToCore = true;
274 setCurrentCoreAccount(id);
277 void Client::setSyncedToCore() {
278 // create buffersyncer
279 Q_ASSERT(!_bufferSyncer);
280 _bufferSyncer = new BufferSyncer(this);
281 connect(bufferSyncer(), SIGNAL(lastSeenMsgSet(BufferId, MsgId)), _networkModel, SLOT(setLastSeenMsgId(BufferId, MsgId)));
282 connect(bufferSyncer(), SIGNAL(bufferRemoved(BufferId)), this, SLOT(bufferRemoved(BufferId)));
283 connect(bufferSyncer(), SIGNAL(bufferRenamed(BufferId, QString)), this, SLOT(bufferRenamed(BufferId, QString)));
284 connect(bufferSyncer(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
285 connect(networkModel(), SIGNAL(setLastSeenMsg(BufferId, MsgId)), bufferSyncer(), SLOT(requestSetLastSeenMsg(BufferId, const MsgId &)));
286 signalProxy()->synchronize(bufferSyncer());
288 // create a new BufferViewManager
289 Q_ASSERT(!_bufferViewManager);
290 _bufferViewManager = new BufferViewManager(signalProxy(), this);
291 connect(bufferViewManager(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
293 _syncedToCore = true;
295 emit coreConnectionStateChanged(true);
298 void Client::requestInitialBacklog() {
299 if(bufferViewManager()->isInitialized() && bufferSyncer()->isInitialized())
300 Client::backlogManager()->requestInitialBacklog();
303 void Client::setSecuredConnection() {
304 emit securedConnection();
307 void Client::disconnectFromCore() {
311 signalProxy()->removeAllPeers();
314 void Client::disconnectedFromCore() {
315 _connectedToCore = false;
316 _syncedToCore = false;
318 emit coreConnectionStateChanged(false);
320 backlogManager()->reset();
321 messageProcessor()->reset();
323 // Clear internal data. Hopefully nothing relies on it at this point.
324 setCurrentCoreAccount(0);
327 _bufferSyncer->deleteLater();
331 if(_bufferViewManager) {
332 _bufferViewManager->deleteLater();
333 _bufferViewManager = 0;
336 _messageModel->clear();
337 _networkModel->clear();
339 QHash<NetworkId, Network*>::iterator netIter = _networks.begin();
340 while(netIter != _networks.end()) {
341 Network *net = netIter.value();
342 emit networkRemoved(net->networkId());
343 disconnect(net, SIGNAL(destroyed()), this, 0);
344 netIter = _networks.erase(netIter);
347 Q_ASSERT(_networks.isEmpty());
349 QHash<IdentityId, Identity*>::iterator idIter = _identities.begin();
350 while(idIter != _identities.end()) {
351 Identity *id = idIter.value();
352 emit identityRemoved(id->id());
353 idIter = _identities.erase(idIter);
356 Q_ASSERT(_identities.isEmpty());
362 void Client::networkDestroyed() {
363 Network *net = static_cast<Network *>(sender());
364 QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
365 while(netIter != _networks.end()) {
366 if(*netIter == net) {
367 netIter = _networks.erase(netIter);
375 // Hmm... we never used this...
376 void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) {
377 //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
380 void Client::recvMessage(const Message &msg) {
382 messageProcessor()->process(msg_);
385 void Client::setBufferLastSeenMsg(BufferId id, const MsgId &msgId) {
388 bufferSyncer()->requestSetLastSeenMsg(id, msgId);
391 void Client::removeBuffer(BufferId id) {
392 if(!bufferSyncer()) return;
393 bufferSyncer()->requestRemoveBuffer(id);
396 void Client::bufferRemoved(BufferId bufferId) {
397 // select a sane buffer (status buffer)
398 /* we have to manually select a buffer because otherwise inconsitent changes
399 * to the model might occur:
400 * the result of a buffer removal triggers a change in the selection model.
401 * the newly selected buffer might be a channel that hasn't been selected yet
402 * and a new nickview would be created (which never heard of the "rowsAboutToBeRemoved").
403 * this new view (and/or) its sort filter will then only receive a "rowsRemoved" signal.
405 QModelIndex current = bufferModel()->currentIndex();
406 if(current.data(NetworkModel::BufferIdRole).value<BufferId>() == bufferId) {
407 bufferModel()->setCurrentIndex(current.sibling(0,0));
410 // and remove it from the model
411 networkModel()->removeBuffer(bufferId);
414 void Client::bufferRenamed(BufferId bufferId, const QString &newName) {
415 QModelIndex bufferIndex = networkModel()->bufferIndex(bufferId);
416 if(bufferIndex.isValid()) {
417 networkModel()->setData(bufferIndex, newName, Qt::DisplayRole);
421 void Client::logMessage(QtMsgType type, const char *msg) {
422 fprintf(stderr, "%s\n", msg);
424 if(type == QtFatalMsg) {
425 Quassel::logFatalMessage(msg);
427 QString msgString = QString("%1\n").arg(msg);
428 instance()->_debugLog << msgString;
429 emit instance()->logUpdated(msgString);