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"
43 QPointer<Client> Client::instanceptr = 0;
44 AccountId Client::_currentCoreAccount = 0;
46 /*** Initialization/destruction ***/
48 Client *Client::instance() {
50 instanceptr = new Client();
54 void Client::destroy() {
56 delete instanceptr->mainUi;
57 instanceptr->deleteLater();
62 void Client::init(AbstractUi *ui) {
63 instance()->mainUi = ui;
67 Client::Client(QObject *parent)
69 _signalProxy(new SignalProxy(SignalProxy::Client, this)),
74 _backlogManager(new ClientBacklogManager(this)),
75 _bufferViewManager(0),
76 _ircListHelper(new ClientIrcListHelper(this)),
79 _connectedToCore(false),
81 _debugLog(&_debugLogBuffer)
83 _signalProxy->synchronize(_ircListHelper);
91 _currentCoreAccount = 0;
92 _networkModel = new NetworkModel(this);
94 connect(this, SIGNAL(networkRemoved(NetworkId)),
95 _networkModel, SLOT(networkRemoved(NetworkId)));
97 _bufferModel = new BufferModel(_networkModel);
98 _messageModel = mainUi->createMessageModel(this);
99 _messageProcessor = mainUi->createMessageProcessor(this);
101 SignalProxy *p = signalProxy();
103 p->attachSlot(SIGNAL(displayMsg(const Message &)), this, SLOT(recvMessage(const Message &)));
104 p->attachSlot(SIGNAL(displayStatusMsg(QString, QString)), this, SLOT(recvStatusMsg(QString, QString)));
106 p->attachSlot(SIGNAL(bufferInfoUpdated(BufferInfo)), _networkModel, SLOT(bufferUpdated(BufferInfo)));
107 p->attachSignal(this, SIGNAL(sendInput(BufferInfo, QString)));
108 p->attachSignal(this, SIGNAL(requestNetworkStates()));
110 p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &)), SIGNAL(createIdentity(const Identity &)));
111 p->attachSignal(this, SIGNAL(requestRemoveIdentity(IdentityId)), SIGNAL(removeIdentity(IdentityId)));
112 p->attachSlot(SIGNAL(identityCreated(const Identity &)), this, SLOT(coreIdentityCreated(const Identity &)));
113 p->attachSlot(SIGNAL(identityRemoved(IdentityId)), this, SLOT(coreIdentityRemoved(IdentityId)));
115 p->attachSignal(this, SIGNAL(requestCreateNetwork(const NetworkInfo &)), SIGNAL(createNetwork(const NetworkInfo &)));
116 p->attachSignal(this, SIGNAL(requestRemoveNetwork(NetworkId)), SIGNAL(removeNetwork(NetworkId)));
117 p->attachSlot(SIGNAL(networkCreated(NetworkId)), this, SLOT(coreNetworkCreated(NetworkId)));
118 p->attachSlot(SIGNAL(networkRemoved(NetworkId)), this, SLOT(coreNetworkRemoved(NetworkId)));
120 connect(p, SIGNAL(disconnected()), this, SLOT(disconnectedFromCore()));
122 //connect(mainUi, SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
123 connect(mainUi, SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
124 connect(this, SIGNAL(connected()), mainUi, SLOT(connectedToCore()));
125 connect(this, SIGNAL(disconnected()), mainUi, SLOT(disconnectedFromCore()));
127 // attach backlog manager
128 p->synchronize(backlogManager());
129 connect(backlogManager(), SIGNAL(messagesReceived(BufferId, int)), _messageModel, SLOT(messagesReceived(BufferId, int)));
132 /*** public static methods ***/
134 AccountId Client::currentCoreAccount() {
135 return _currentCoreAccount;
138 void Client::setCurrentCoreAccount(AccountId id) {
139 _currentCoreAccount = id;
142 bool Client::isConnected() {
143 return instance()->_connectedToCore;
146 bool Client::isSynced() {
147 return instance()->_syncedToCore;
150 /*** Network handling ***/
152 QList<NetworkId> Client::networkIds() {
153 return instance()->_networks.keys();
156 const Network * Client::network(NetworkId networkid) {
157 if(instance()->_networks.contains(networkid)) return instance()->_networks[networkid];
161 void Client::createNetwork(const NetworkInfo &info) {
162 emit instance()->requestCreateNetwork(info);
165 void Client::removeNetwork(NetworkId id) {
166 emit instance()->requestRemoveNetwork(id);
169 void Client::updateNetwork(const NetworkInfo &info) {
170 Network *netptr = instance()->_networks.value(info.networkId, 0);
172 qWarning() << "Update for unknown network requested:" << info;
175 netptr->requestSetNetworkInfo(info);
178 void Client::addNetwork(Network *net) {
179 net->setProxy(signalProxy());
180 signalProxy()->synchronize(net);
181 networkModel()->attachNetwork(net);
182 connect(net, SIGNAL(destroyed()), instance(), SLOT(networkDestroyed()));
183 instance()->_networks[net->networkId()] = net;
184 emit instance()->networkCreated(net->networkId());
187 void Client::coreNetworkCreated(NetworkId id) {
188 if(_networks.contains(id)) {
189 qWarning() << "Creation of already existing network requested!";
192 Network *net = new Network(id, this);
196 void Client::coreNetworkRemoved(NetworkId id) {
197 if(!_networks.contains(id))
199 Network *net = _networks.take(id);
200 emit networkRemoved(net->networkId());
204 /*** Identity handling ***/
206 QList<IdentityId> Client::identityIds() {
207 return instance()->_identities.keys();
210 const Identity * Client::identity(IdentityId id) {
211 if(instance()->_identities.contains(id)) return instance()->_identities[id];
215 void Client::createIdentity(const Identity &id) {
216 emit instance()->requestCreateIdentity(id);
219 void Client::updateIdentity(IdentityId id, const QVariantMap &ser) {
220 Identity *idptr = instance()->_identities.value(id, 0);
222 qWarning() << "Update for unknown identity requested:" << id;
225 idptr->requestUpdate(ser);
228 void Client::removeIdentity(IdentityId id) {
229 emit instance()->requestRemoveIdentity(id);
232 void Client::coreIdentityCreated(const Identity &other) {
233 if(!_identities.contains(other.id())) {
234 Identity *identity = new Identity(other, this);
235 _identities[other.id()] = identity;
236 identity->setInitialized();
237 signalProxy()->synchronize(identity);
238 emit identityCreated(other.id());
240 qWarning() << tr("Identity already exists in client!");
244 void Client::coreIdentityRemoved(IdentityId id) {
245 if(_identities.contains(id)) {
246 emit identityRemoved(id);
247 Identity *i = _identities.take(id);
253 void Client::userInput(BufferInfo bufferInfo, QString message) {
254 emit instance()->sendInput(bufferInfo, message);
257 /*** core connection stuff ***/
259 void Client::setConnectedToCore(QIODevice *socket, AccountId id) {
260 // if the socket is an orphan, the signalProxy adopts it.
261 // -> we don't need to care about it anymore
262 socket->setParent(0);
263 signalProxy()->addPeer(socket);
264 _connectedToCore = true;
265 setCurrentCoreAccount(id);
268 void Client::setConnectedToInternalCore() {
269 _connectedToCore = true;
270 setCurrentCoreAccount(AccountId());
273 void Client::setSyncedToCore() {
274 // create buffersyncer
275 Q_ASSERT(!_bufferSyncer);
276 _bufferSyncer = new BufferSyncer(this);
277 connect(bufferSyncer(), SIGNAL(lastSeenMsgSet(BufferId, MsgId)), _networkModel, SLOT(setLastSeenMsgId(BufferId, MsgId)));
278 connect(bufferSyncer(), SIGNAL(bufferRemoved(BufferId)), this, SLOT(bufferRemoved(BufferId)));
279 connect(bufferSyncer(), SIGNAL(bufferRenamed(BufferId, QString)), this, SLOT(bufferRenamed(BufferId, QString)));
280 signalProxy()->synchronize(bufferSyncer());
282 // create a new BufferViewManager
283 _bufferViewManager = new BufferViewManager(signalProxy(), this);
285 _syncedToCore = true;
287 emit coreConnectionStateChanged(true);
290 void Client::setSecuredConnection() {
291 emit securedConnection();
294 void Client::disconnectFromCore() {
298 signalProxy()->removeAllPeers();
301 void Client::disconnectedFromCore() {
302 _connectedToCore = false;
303 _syncedToCore = false;
305 emit coreConnectionStateChanged(false);
307 backlogManager()->reset();
308 messageProcessor()->reset();
310 // Clear internal data. Hopefully nothing relies on it at this point.
311 setCurrentCoreAccount(0);
314 _bufferSyncer->deleteLater();
318 if(_bufferViewManager) {
319 _bufferViewManager->deleteLater();
320 _bufferViewManager = 0;
323 _messageModel->clear();
324 _networkModel->clear();
326 QHash<NetworkId, Network*>::iterator netIter = _networks.begin();
327 while(netIter != _networks.end()) {
328 Network *net = netIter.value();
329 emit networkRemoved(net->networkId());
330 disconnect(net, SIGNAL(destroyed()), this, 0);
331 netIter = _networks.erase(netIter);
334 Q_ASSERT(_networks.isEmpty());
336 QHash<IdentityId, Identity*>::iterator idIter = _identities.begin();
337 while(idIter != _identities.end()) {
338 Identity *id = idIter.value();
339 emit identityRemoved(id->id());
340 idIter = _identities.erase(idIter);
343 Q_ASSERT(_identities.isEmpty());
349 void Client::networkDestroyed() {
350 Network *net = static_cast<Network *>(sender());
351 QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
352 while(netIter != _networks.end()) {
353 if(*netIter == net) {
354 netIter = _networks.erase(netIter);
362 // Hmm... we never used this...
363 void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) {
364 //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
367 void Client::recvMessage(const Message &msg) {
369 messageProcessor()->process(msg_);
372 void Client::setBufferLastSeenMsg(BufferId id, const MsgId &msgId) {
375 bufferSyncer()->requestSetLastSeenMsg(id, msgId);
378 void Client::removeBuffer(BufferId id) {
379 if(!bufferSyncer()) return;
380 bufferSyncer()->requestRemoveBuffer(id);
383 void Client::bufferRemoved(BufferId bufferId) {
384 // select a sane buffer (status buffer)
385 /* we have to manually select a buffer because otherwise inconsitent changes
386 * to the model might occur:
387 * the result of a buffer removal triggers a change in the selection model.
388 * the newly selected buffer might be a channel that hasn't been selected yet
389 * and a new nickview would be created (which never heard of the "rowsAboutToBeRemoved").
390 * this new view (and/or) its sort filter will then only receive a "rowsRemoved" signal.
392 QModelIndex current = bufferModel()->currentIndex();
393 if(current.data(NetworkModel::BufferIdRole).value<BufferId>() == bufferId) {
394 bufferModel()->setCurrentIndex(current.sibling(0,0));
397 // and remove it from the model
398 networkModel()->removeBuffer(bufferId);
401 void Client::bufferRenamed(BufferId bufferId, const QString &newName) {
402 QModelIndex bufferIndex = networkModel()->bufferIndex(bufferId);
403 if(bufferIndex.isValid()) {
404 networkModel()->setData(bufferIndex, newName, Qt::DisplayRole);
408 void Client::logMessage(QtMsgType type, const char *msg) {
421 Quassel::logFatalMessage(msg);
424 instance()->_debugLog << prefix << ": " << msg << "\n";
425 emit instance()->logUpdated();