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()));
125 // attach backlog manager
126 p->synchronize(backlogManager());
127 connect(backlogManager(), SIGNAL(messagesReceived(BufferId, int)), _messageModel, SLOT(messagesReceived(BufferId, int)));
130 /*** public static methods ***/
132 AccountId Client::currentCoreAccount() {
133 return _currentCoreAccount;
136 void Client::setCurrentCoreAccount(AccountId id) {
137 _currentCoreAccount = id;
140 bool Client::isConnected() {
141 return instance()->_connectedToCore;
144 bool Client::isSynced() {
145 return instance()->_syncedToCore;
148 /*** Network handling ***/
150 QList<NetworkId> Client::networkIds() {
151 return instance()->_networks.keys();
154 const Network * Client::network(NetworkId networkid) {
155 if(instance()->_networks.contains(networkid)) return instance()->_networks[networkid];
159 void Client::createNetwork(const NetworkInfo &info) {
160 emit instance()->requestCreateNetwork(info);
163 void Client::removeNetwork(NetworkId id) {
164 emit instance()->requestRemoveNetwork(id);
167 void Client::updateNetwork(const NetworkInfo &info) {
168 Network *netptr = instance()->_networks.value(info.networkId, 0);
170 qWarning() << "Update for unknown network requested:" << info;
173 netptr->requestSetNetworkInfo(info);
176 void Client::addNetwork(Network *net) {
177 net->setProxy(signalProxy());
178 signalProxy()->synchronize(net);
179 networkModel()->attachNetwork(net);
180 connect(net, SIGNAL(destroyed()), instance(), SLOT(networkDestroyed()));
181 instance()->_networks[net->networkId()] = net;
182 emit instance()->networkCreated(net->networkId());
185 void Client::coreNetworkCreated(NetworkId id) {
186 if(_networks.contains(id)) {
187 qWarning() << "Creation of already existing network requested!";
190 Network *net = new Network(id, this);
194 void Client::coreNetworkRemoved(NetworkId id) {
195 if(!_networks.contains(id))
197 Network *net = _networks.take(id);
198 emit networkRemoved(net->networkId());
202 /*** Identity handling ***/
204 QList<IdentityId> Client::identityIds() {
205 return instance()->_identities.keys();
208 const Identity * Client::identity(IdentityId id) {
209 if(instance()->_identities.contains(id)) return instance()->_identities[id];
213 void Client::createIdentity(const Identity &id) {
214 emit instance()->requestCreateIdentity(id);
217 void Client::updateIdentity(IdentityId id, const QVariantMap &ser) {
218 Identity *idptr = instance()->_identities.value(id, 0);
220 qWarning() << "Update for unknown identity requested:" << id;
223 idptr->requestUpdate(ser);
226 void Client::removeIdentity(IdentityId id) {
227 emit instance()->requestRemoveIdentity(id);
230 void Client::coreIdentityCreated(const Identity &other) {
231 if(!_identities.contains(other.id())) {
232 Identity *identity = new Identity(other, this);
233 _identities[other.id()] = identity;
234 identity->setInitialized();
235 signalProxy()->synchronize(identity);
236 emit identityCreated(other.id());
238 qWarning() << tr("Identity already exists in client!");
242 void Client::coreIdentityRemoved(IdentityId id) {
243 if(_identities.contains(id)) {
244 emit identityRemoved(id);
245 Identity *i = _identities.take(id);
251 void Client::userInput(BufferInfo bufferInfo, QString message) {
252 emit instance()->sendInput(bufferInfo, message);
255 /*** core connection stuff ***/
257 void Client::setConnectedToCore(QIODevice *socket, AccountId id) {
258 // if the socket is an orphan, the signalProxy adopts it.
259 // -> we don't need to care about it anymore
260 socket->setParent(0);
261 signalProxy()->addPeer(socket);
262 _connectedToCore = true;
263 setCurrentCoreAccount(id);
266 void Client::setConnectedToInternalCore() {
267 _connectedToCore = true;
268 setCurrentCoreAccount(AccountId());
271 void Client::setSyncedToCore() {
272 // create buffersyncer
273 Q_ASSERT(!_bufferSyncer);
274 _bufferSyncer = new BufferSyncer(this);
275 connect(bufferSyncer(), SIGNAL(lastSeenMsgSet(BufferId, MsgId)), _networkModel, SLOT(setLastSeenMsgId(BufferId, MsgId)));
276 connect(bufferSyncer(), SIGNAL(bufferRemoved(BufferId)), this, SLOT(bufferRemoved(BufferId)));
277 connect(bufferSyncer(), SIGNAL(bufferRenamed(BufferId, QString)), this, SLOT(bufferRenamed(BufferId, QString)));
278 signalProxy()->synchronize(bufferSyncer());
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 messageProcessor()->reset();
307 // Clear internal data. Hopefully nothing relies on it at this point.
308 setCurrentCoreAccount(0);
311 _bufferSyncer->deleteLater();
315 if(_bufferViewManager) {
316 _bufferViewManager->deleteLater();
317 _bufferViewManager = 0;
320 _messageModel->clear();
321 _networkModel->clear();
323 QHash<NetworkId, Network*>::iterator netIter = _networks.begin();
324 while(netIter != _networks.end()) {
325 Network *net = netIter.value();
326 emit networkRemoved(net->networkId());
327 disconnect(net, SIGNAL(destroyed()), this, 0);
328 netIter = _networks.erase(netIter);
331 Q_ASSERT(_networks.isEmpty());
333 QHash<IdentityId, Identity*>::iterator idIter = _identities.begin();
334 while(idIter != _identities.end()) {
335 Identity *id = idIter.value();
336 emit identityRemoved(id->id());
337 idIter = _identities.erase(idIter);
340 Q_ASSERT(_identities.isEmpty());
346 void Client::networkDestroyed() {
347 Network *net = static_cast<Network *>(sender());
348 QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
349 while(netIter != _networks.end()) {
350 if(*netIter == net) {
351 netIter = _networks.erase(netIter);
359 // Hmm... we never used this...
360 void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) {
361 //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
364 void Client::recvMessage(const Message &msg_) {
366 messageProcessor()->process(msg);
369 void Client::setBufferLastSeenMsg(BufferId id, const MsgId &msgId) {
372 bufferSyncer()->requestSetLastSeenMsg(id, msgId);
375 void Client::removeBuffer(BufferId id) {
376 if(!bufferSyncer()) return;
377 bufferSyncer()->requestRemoveBuffer(id);
380 void Client::bufferRemoved(BufferId bufferId) {
381 // select a sane buffer (status buffer)
382 /* we have to manually select a buffer because otherwise inconsitent changes
383 * to the model might occur:
384 * the result of a buffer removal triggers a change in the selection model.
385 * the newly selected buffer might be a channel that hasn't been selected yet
386 * and a new nickview would be created (which never heard of the "rowsAboutToBeRemoved").
387 * this new view (and/or) its sort filter will then only receive a "rowsRemoved" signal.
389 QModelIndex current = bufferModel()->currentIndex();
390 if(current.data(NetworkModel::BufferIdRole).value<BufferId>() == bufferId) {
391 bufferModel()->setCurrentIndex(current.sibling(0,0));
394 // and remove it from the model
395 networkModel()->removeBuffer(bufferId);
398 void Client::bufferRenamed(BufferId bufferId, const QString &newName) {
399 QModelIndex bufferIndex = networkModel()->bufferIndex(bufferId);
400 if(bufferIndex.isValid()) {
401 networkModel()->setData(bufferIndex, newName, Qt::DisplayRole);