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"
33 #include "ircchannel.h"
36 #include "messagemodel.h"
38 #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 instanceptr->deleteLater();
59 void Client::init(AbstractUi *ui) {
60 instance()->mainUi = ui;
64 Client::Client(QObject *parent)
67 _signalProxy(new SignalProxy(SignalProxy::Client, this)),
72 _backlogManager(new ClientBacklogManager(this)),
73 _bufferViewManager(0),
74 _ircListHelper(new ClientIrcListHelper(this)),
77 _connectedToCore(false),
80 _signalProxy->synchronize(_ircListHelper);
88 _currentCoreAccount = 0;
89 _networkModel = new NetworkModel(this);
91 connect(this, SIGNAL(networkRemoved(NetworkId)),
92 _networkModel, SLOT(networkRemoved(NetworkId)));
94 _bufferModel = new BufferModel(_networkModel);
95 _messageModel = mainUi->createMessageModel(this);
96 _messageProcessor = mainUi->createMessageProcessor(this);
98 SignalProxy *p = signalProxy();
100 p->attachSlot(SIGNAL(displayMsg(const Message &)), this, SLOT(recvMessage(const Message &)));
101 p->attachSlot(SIGNAL(displayStatusMsg(QString, QString)), this, SLOT(recvStatusMsg(QString, QString)));
103 p->attachSlot(SIGNAL(bufferInfoUpdated(BufferInfo)), _networkModel, SLOT(bufferUpdated(BufferInfo)));
104 p->attachSignal(this, SIGNAL(sendInput(BufferInfo, QString)));
105 p->attachSignal(this, SIGNAL(requestNetworkStates()));
107 p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &)), SIGNAL(createIdentity(const Identity &)));
108 p->attachSignal(this, SIGNAL(requestRemoveIdentity(IdentityId)), SIGNAL(removeIdentity(IdentityId)));
109 p->attachSlot(SIGNAL(identityCreated(const Identity &)), this, SLOT(coreIdentityCreated(const Identity &)));
110 p->attachSlot(SIGNAL(identityRemoved(IdentityId)), this, SLOT(coreIdentityRemoved(IdentityId)));
112 p->attachSignal(this, SIGNAL(requestCreateNetwork(const NetworkInfo &)), SIGNAL(createNetwork(const NetworkInfo &)));
113 p->attachSignal(this, SIGNAL(requestRemoveNetwork(NetworkId)), SIGNAL(removeNetwork(NetworkId)));
114 p->attachSlot(SIGNAL(networkCreated(NetworkId)), this, SLOT(coreNetworkCreated(NetworkId)));
115 p->attachSlot(SIGNAL(networkRemoved(NetworkId)), this, SLOT(coreNetworkRemoved(NetworkId)));
117 connect(p, SIGNAL(disconnected()), this, SLOT(disconnectFromCore()));
119 //connect(mainUi, SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
120 connect(mainUi, SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
121 connect(this, SIGNAL(connected()), mainUi, SLOT(connectedToCore()));
122 connect(this, SIGNAL(disconnected()), mainUi, SLOT(disconnectedFromCore()));
126 /*** public static methods ***/
128 AccountId Client::currentCoreAccount() {
129 return _currentCoreAccount;
132 void Client::setCurrentCoreAccount(AccountId id) {
133 _currentCoreAccount = id;
136 bool Client::isConnected() {
137 return instance()->_connectedToCore;
140 bool Client::isSynced() {
141 return instance()->_syncedToCore;
144 /*** Network handling ***/
146 QList<NetworkId> Client::networkIds() {
147 return instance()->_networks.keys();
150 const Network * Client::network(NetworkId networkid) {
151 if(instance()->_networks.contains(networkid)) return instance()->_networks[networkid];
155 void Client::createNetwork(const NetworkInfo &info) {
156 emit instance()->requestCreateNetwork(info);
159 void Client::removeNetwork(NetworkId id) {
160 emit instance()->requestRemoveNetwork(id);
163 void Client::updateNetwork(const NetworkInfo &info) {
164 Network *netptr = instance()->_networks.value(info.networkId, 0);
166 qWarning() << "Update for unknown network requested:" << info;
169 netptr->requestSetNetworkInfo(info);
172 void Client::addNetwork(Network *net) {
173 net->setProxy(signalProxy());
174 signalProxy()->synchronize(net);
175 networkModel()->attachNetwork(net);
176 connect(net, SIGNAL(destroyed()), instance(), SLOT(networkDestroyed()));
177 instance()->_networks[net->networkId()] = net;
178 emit instance()->networkCreated(net->networkId());
181 void Client::coreNetworkCreated(NetworkId id) {
182 if(_networks.contains(id)) {
183 qWarning() << "Creation of already existing network requested!";
186 Network *net = new Network(id, this);
190 void Client::coreNetworkRemoved(NetworkId id) {
191 if(!_networks.contains(id))
193 Network *net = _networks.take(id);
194 emit networkRemoved(net->networkId());
198 /*** Identity handling ***/
200 QList<IdentityId> Client::identityIds() {
201 return instance()->_identities.keys();
204 const Identity * Client::identity(IdentityId id) {
205 if(instance()->_identities.contains(id)) return instance()->_identities[id];
209 void Client::createIdentity(const Identity &id) {
210 emit instance()->requestCreateIdentity(id);
213 void Client::updateIdentity(IdentityId id, const QVariantMap &ser) {
214 Identity *idptr = instance()->_identities.value(id, 0);
216 qWarning() << "Update for unknown identity requested:" << id;
219 idptr->requestUpdate(ser);
222 void Client::removeIdentity(IdentityId id) {
223 emit instance()->requestRemoveIdentity(id);
226 void Client::coreIdentityCreated(const Identity &other) {
227 if(!_identities.contains(other.id())) {
228 Identity *identity = new Identity(other, this);
229 _identities[other.id()] = identity;
230 identity->setInitialized();
231 signalProxy()->synchronize(identity);
232 emit identityCreated(other.id());
234 qWarning() << tr("Identity already exists in client!");
238 void Client::coreIdentityRemoved(IdentityId id) {
239 if(_identities.contains(id)) {
240 emit identityRemoved(id);
241 Identity *i = _identities.take(id);
247 void Client::userInput(BufferInfo bufferInfo, QString message) {
248 emit instance()->sendInput(bufferInfo, message);
251 /*** core connection stuff ***/
253 void Client::setConnectedToCore(QIODevice *sock, AccountId id) {
255 signalProxy()->addPeer(socket);
256 _connectedToCore = true;
257 setCurrentCoreAccount(id);
260 void Client::setSyncedToCore() {
261 // create buffersyncer
262 Q_ASSERT(!_bufferSyncer);
263 _bufferSyncer = new BufferSyncer(this);
264 connect(bufferSyncer(), SIGNAL(lastSeenMsgSet(BufferId, MsgId)), _networkModel, SLOT(setLastSeenMsgId(BufferId, MsgId)));
265 connect(bufferSyncer(), SIGNAL(bufferRemoved(BufferId)), this, SLOT(bufferRemoved(BufferId)));
266 connect(bufferSyncer(), SIGNAL(bufferRenamed(BufferId, QString)), this, SLOT(bufferRenamed(BufferId, QString)));
267 signalProxy()->synchronize(bufferSyncer());
269 // attach backlog manager
270 signalProxy()->synchronize(backlogManager());
272 // create a new BufferViewManager
273 _bufferViewManager = new BufferViewManager(signalProxy(), this);
275 _syncedToCore = true;
277 emit coreConnectionStateChanged(true);
280 void Client::setSecuredConnection() {
281 emit securedConnection();
284 void Client::disconnectFromCore() {
287 _connectedToCore = false;
291 socket->deleteLater();
293 _syncedToCore = false;
295 emit coreConnectionStateChanged(false);
297 messageProcessor()->reset();
299 // Clear internal data. Hopefully nothing relies on it at this point.
300 setCurrentCoreAccount(0);
303 _bufferSyncer->deleteLater();
307 if(_bufferViewManager) {
308 _bufferViewManager->deleteLater();
309 _bufferViewManager = 0;
312 _messageModel->clear();
313 _networkModel->clear();
315 QHash<NetworkId, Network*>::iterator netIter = _networks.begin();
316 while(netIter != _networks.end()) {
317 Network *net = netIter.value();
318 emit networkRemoved(net->networkId());
319 disconnect(net, SIGNAL(destroyed()), this, 0);
320 netIter = _networks.erase(netIter);
323 Q_ASSERT(_networks.isEmpty());
325 QHash<IdentityId, Identity*>::iterator idIter = _identities.begin();
326 while(idIter != _identities.end()) {
327 Identity *id = idIter.value();
328 emit identityRemoved(id->id());
329 idIter = _identities.erase(idIter);
332 Q_ASSERT(_identities.isEmpty());
336 void Client::setCoreConfiguration(const QVariantMap &settings) {
337 SignalProxy::writeDataToDevice(socket, settings);
342 void Client::networkDestroyed() {
343 Network *net = static_cast<Network *>(sender());
344 QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
345 while(netIter != _networks.end()) {
346 if(*netIter == net) {
347 netIter = _networks.erase(netIter);
355 // Hmm... we never used this...
356 void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) {
357 //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
360 void Client::recvMessage(const Message &msg_) {
362 messageProcessor()->process(msg);
365 void Client::setBufferLastSeenMsg(BufferId id, const MsgId &msgId) {
368 bufferSyncer()->requestSetLastSeenMsg(id, msgId);
371 void Client::removeBuffer(BufferId id) {
372 if(!bufferSyncer()) return;
373 bufferSyncer()->requestRemoveBuffer(id);
376 void Client::bufferRemoved(BufferId bufferId) {
377 // select a sane buffer (status buffer)
378 /* we have to manually select a buffer because otherwise inconsitent changes
379 * to the model might occur:
380 * the result of a buffer removal triggers a change in the selection model.
381 * the newly selected buffer might be a channel that hasn't been selected yet
382 * and a new nickview would be created (which never heard of the "rowsAboutToBeRemoved").
383 * this new view (and/or) its sort filter will then only receive a "rowsRemoved" signal.
385 QModelIndex current = bufferModel()->currentIndex();
386 if(current.data(NetworkModel::BufferIdRole).value<BufferId>() == bufferId) {
387 bufferModel()->setCurrentIndex(current.sibling(0,0));
390 // and remove it from the model
391 networkModel()->removeBuffer(bufferId);
394 void Client::bufferRenamed(BufferId bufferId, const QString &newName) {
395 QModelIndex bufferIndex = networkModel()->bufferIndex(bufferId);
396 if(bufferIndex.isValid()) {
397 networkModel()->setData(bufferIndex, newName, Qt::DisplayRole);