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"
38 #include "quasselui.h"
39 #include "signalproxy.h"
42 QPointer<Client> Client::instanceptr = 0;
43 AccountId Client::_currentCoreAccount = 0;
45 /*** Initialization/destruction ***/
47 Client *Client::instance() {
49 instanceptr = new Client();
53 void Client::destroy() {
55 delete instanceptr->mainUi;
56 instanceptr->deleteLater();
61 void Client::init(AbstractUi *ui) {
62 instance()->mainUi = ui;
66 Client::Client(QObject *parent)
68 _signalProxy(new SignalProxy(SignalProxy::Client, this)),
73 _backlogManager(new ClientBacklogManager(this)),
74 _bufferViewManager(0),
75 _ircListHelper(new ClientIrcListHelper(this)),
78 _connectedToCore(false),
81 _signalProxy->synchronize(_ircListHelper);
89 _currentCoreAccount = 0;
90 _networkModel = new NetworkModel(this);
92 connect(this, SIGNAL(networkRemoved(NetworkId)),
93 _networkModel, SLOT(networkRemoved(NetworkId)));
95 _bufferModel = new BufferModel(_networkModel);
96 _messageModel = mainUi->createMessageModel(this);
97 _messageProcessor = mainUi->createMessageProcessor(this);
99 SignalProxy *p = signalProxy();
101 p->attachSlot(SIGNAL(displayMsg(const Message &)), this, SLOT(recvMessage(const Message &)));
102 p->attachSlot(SIGNAL(displayStatusMsg(QString, QString)), this, SLOT(recvStatusMsg(QString, QString)));
104 p->attachSlot(SIGNAL(bufferInfoUpdated(BufferInfo)), _networkModel, SLOT(bufferUpdated(BufferInfo)));
105 p->attachSignal(this, SIGNAL(sendInput(BufferInfo, QString)));
106 p->attachSignal(this, SIGNAL(requestNetworkStates()));
108 p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &)), SIGNAL(createIdentity(const Identity &)));
109 p->attachSignal(this, SIGNAL(requestRemoveIdentity(IdentityId)), SIGNAL(removeIdentity(IdentityId)));
110 p->attachSlot(SIGNAL(identityCreated(const Identity &)), this, SLOT(coreIdentityCreated(const Identity &)));
111 p->attachSlot(SIGNAL(identityRemoved(IdentityId)), this, SLOT(coreIdentityRemoved(IdentityId)));
113 p->attachSignal(this, SIGNAL(requestCreateNetwork(const NetworkInfo &)), SIGNAL(createNetwork(const NetworkInfo &)));
114 p->attachSignal(this, SIGNAL(requestRemoveNetwork(NetworkId)), SIGNAL(removeNetwork(NetworkId)));
115 p->attachSlot(SIGNAL(networkCreated(NetworkId)), this, SLOT(coreNetworkCreated(NetworkId)));
116 p->attachSlot(SIGNAL(networkRemoved(NetworkId)), this, SLOT(coreNetworkRemoved(NetworkId)));
118 connect(p, SIGNAL(disconnected()), this, SLOT(disconnectedFromCore()));
120 //connect(mainUi, SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
121 connect(mainUi, SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
122 connect(this, SIGNAL(connected()), mainUi, SLOT(connectedToCore()));
123 connect(this, SIGNAL(disconnected()), mainUi, SLOT(disconnectedFromCore()));
127 /*** public static methods ***/
129 AccountId Client::currentCoreAccount() {
130 return _currentCoreAccount;
133 void Client::setCurrentCoreAccount(AccountId id) {
134 _currentCoreAccount = id;
137 bool Client::isConnected() {
138 return instance()->_connectedToCore;
141 bool Client::isSynced() {
142 return instance()->_syncedToCore;
145 /*** Network handling ***/
147 QList<NetworkId> Client::networkIds() {
148 return instance()->_networks.keys();
151 const Network * Client::network(NetworkId networkid) {
152 if(instance()->_networks.contains(networkid)) return instance()->_networks[networkid];
156 void Client::createNetwork(const NetworkInfo &info) {
157 emit instance()->requestCreateNetwork(info);
160 void Client::removeNetwork(NetworkId id) {
161 emit instance()->requestRemoveNetwork(id);
164 void Client::updateNetwork(const NetworkInfo &info) {
165 Network *netptr = instance()->_networks.value(info.networkId, 0);
167 qWarning() << "Update for unknown network requested:" << info;
170 netptr->requestSetNetworkInfo(info);
173 void Client::addNetwork(Network *net) {
174 net->setProxy(signalProxy());
175 signalProxy()->synchronize(net);
176 networkModel()->attachNetwork(net);
177 connect(net, SIGNAL(destroyed()), instance(), SLOT(networkDestroyed()));
178 instance()->_networks[net->networkId()] = net;
179 emit instance()->networkCreated(net->networkId());
182 void Client::coreNetworkCreated(NetworkId id) {
183 if(_networks.contains(id)) {
184 qWarning() << "Creation of already existing network requested!";
187 Network *net = new Network(id, this);
191 void Client::coreNetworkRemoved(NetworkId id) {
192 if(!_networks.contains(id))
194 Network *net = _networks.take(id);
195 emit networkRemoved(net->networkId());
199 /*** Identity handling ***/
201 QList<IdentityId> Client::identityIds() {
202 return instance()->_identities.keys();
205 const Identity * Client::identity(IdentityId id) {
206 if(instance()->_identities.contains(id)) return instance()->_identities[id];
210 void Client::createIdentity(const Identity &id) {
211 emit instance()->requestCreateIdentity(id);
214 void Client::updateIdentity(IdentityId id, const QVariantMap &ser) {
215 Identity *idptr = instance()->_identities.value(id, 0);
217 qWarning() << "Update for unknown identity requested:" << id;
220 idptr->requestUpdate(ser);
223 void Client::removeIdentity(IdentityId id) {
224 emit instance()->requestRemoveIdentity(id);
227 void Client::coreIdentityCreated(const Identity &other) {
228 if(!_identities.contains(other.id())) {
229 Identity *identity = new Identity(other, this);
230 _identities[other.id()] = identity;
231 identity->setInitialized();
232 signalProxy()->synchronize(identity);
233 emit identityCreated(other.id());
235 qWarning() << tr("Identity already exists in client!");
239 void Client::coreIdentityRemoved(IdentityId id) {
240 if(_identities.contains(id)) {
241 emit identityRemoved(id);
242 Identity *i = _identities.take(id);
248 void Client::userInput(BufferInfo bufferInfo, QString message) {
249 emit instance()->sendInput(bufferInfo, message);
252 /*** core connection stuff ***/
254 void Client::setConnectedToCore(QIODevice *socket, AccountId id) {
255 // if the socket is an orphan, the signalProxy adopts it.
256 // -> we don't need to care about it anymore
257 socket->setParent(0);
258 signalProxy()->addPeer(socket);
259 _connectedToCore = true;
260 setCurrentCoreAccount(id);
263 void Client::setConnectedToInternalCore() {
264 _connectedToCore = true;
265 setCurrentCoreAccount(AccountId());
268 void Client::setSyncedToCore() {
269 // create buffersyncer
270 Q_ASSERT(!_bufferSyncer);
271 _bufferSyncer = new BufferSyncer(this);
272 connect(bufferSyncer(), SIGNAL(lastSeenMsgSet(BufferId, MsgId)), _networkModel, SLOT(setLastSeenMsgId(BufferId, MsgId)));
273 connect(bufferSyncer(), SIGNAL(bufferRemoved(BufferId)), this, SLOT(bufferRemoved(BufferId)));
274 connect(bufferSyncer(), SIGNAL(bufferRenamed(BufferId, QString)), this, SLOT(bufferRenamed(BufferId, QString)));
275 signalProxy()->synchronize(bufferSyncer());
277 // attach backlog manager
278 signalProxy()->synchronize(backlogManager());
280 // create a new BufferViewManager
281 _bufferViewManager = new BufferViewManager(signalProxy(), this);
283 _syncedToCore = true;
285 emit coreConnectionStateChanged(true);
288 void Client::setSecuredConnection() {
289 emit securedConnection();
292 void Client::disconnectFromCore() {
296 signalProxy()->removeAllPeers();
299 void Client::disconnectedFromCore() {
300 _connectedToCore = false;
301 _syncedToCore = false;
303 emit coreConnectionStateChanged(false);
305 backlogManager()->reset();
306 messageProcessor()->reset();
308 // Clear internal data. Hopefully nothing relies on it at this point.
309 setCurrentCoreAccount(0);
312 _bufferSyncer->deleteLater();
316 if(_bufferViewManager) {
317 _bufferViewManager->deleteLater();
318 _bufferViewManager = 0;
321 _messageModel->clear();
322 _networkModel->clear();
324 QHash<NetworkId, Network*>::iterator netIter = _networks.begin();
325 while(netIter != _networks.end()) {
326 Network *net = netIter.value();
327 emit networkRemoved(net->networkId());
328 disconnect(net, SIGNAL(destroyed()), this, 0);
329 netIter = _networks.erase(netIter);
332 Q_ASSERT(_networks.isEmpty());
334 QHash<IdentityId, Identity*>::iterator idIter = _identities.begin();
335 while(idIter != _identities.end()) {
336 Identity *id = idIter.value();
337 emit identityRemoved(id->id());
338 idIter = _identities.erase(idIter);
341 Q_ASSERT(_identities.isEmpty());
347 void Client::networkDestroyed() {
348 Network *net = static_cast<Network *>(sender());
349 QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
350 while(netIter != _networks.end()) {
351 if(*netIter == net) {
352 netIter = _networks.erase(netIter);
360 // Hmm... we never used this...
361 void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) {
362 //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
365 void Client::recvMessage(const Message &msg_) {
367 messageProcessor()->process(msg);
370 void Client::setBufferLastSeenMsg(BufferId id, const MsgId &msgId) {
373 bufferSyncer()->requestSetLastSeenMsg(id, msgId);
376 void Client::removeBuffer(BufferId id) {
377 if(!bufferSyncer()) return;
378 bufferSyncer()->requestRemoveBuffer(id);
381 void Client::bufferRemoved(BufferId bufferId) {
382 // select a sane buffer (status buffer)
383 /* we have to manually select a buffer because otherwise inconsitent changes
384 * to the model might occur:
385 * the result of a buffer removal triggers a change in the selection model.
386 * the newly selected buffer might be a channel that hasn't been selected yet
387 * and a new nickview would be created (which never heard of the "rowsAboutToBeRemoved").
388 * this new view (and/or) its sort filter will then only receive a "rowsRemoved" signal.
390 QModelIndex current = bufferModel()->currentIndex();
391 if(current.data(NetworkModel::BufferIdRole).value<BufferId>() == bufferId) {
392 bufferModel()->setCurrentIndex(current.sibling(0,0));
395 // and remove it from the model
396 networkModel()->removeBuffer(bufferId);
399 void Client::bufferRenamed(BufferId bufferId, const QString &newName) {
400 QModelIndex bufferIndex = networkModel()->bufferIndex(bufferId);
401 if(bufferIndex.isValid()) {
402 networkModel()->setData(bufferIndex, newName, Qt::DisplayRole);