Added a fancy menu to connect to and disconnect from your networks directly.
[quassel.git] / src / client / client.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "client.h"
22
23 #include "bufferinfo.h"
24 #include "global.h"
25 #include "identity.h"
26 #include "ircchannel.h"
27 #include "ircuser.h"
28 #include "message.h"
29 #include "network.h"
30 #include "networkmodel.h"
31 #include "buffermodel.h"
32 #include "quasselui.h"
33 #include "signalproxy.h"
34 #include "util.h"
35
36 QPointer<Client> Client::instanceptr = 0;
37
38 /*** Initialization/destruction ***/
39
40 Client *Client::instance() {
41   if(!instanceptr)
42     instanceptr = new Client();
43   return instanceptr;
44 }
45
46 void Client::destroy() {
47   //delete instanceptr;
48   instanceptr->deleteLater();
49 }
50
51 void Client::init(AbstractUi *ui) {
52   instance()->mainUi = ui;
53   instance()->init();
54 }
55
56 Client::Client(QObject *parent)
57   : QObject(parent),
58     socket(0),
59     _signalProxy(new SignalProxy(SignalProxy::Client, this)),
60     mainUi(0),
61     _networkModel(0),
62     _bufferModel(0),
63     _connectedToCore(false),
64     _syncedToCore(false)
65 {
66   _monitorBuffer = new Buffer(BufferInfo(), this);
67 }
68
69 Client::~Client() {
70   disconnectFromCore();
71 }
72
73 void Client::init() {
74
75   _networkModel = new NetworkModel(this);
76   connect(this, SIGNAL(bufferUpdated(BufferInfo)),
77           _networkModel, SLOT(bufferUpdated(BufferInfo)));
78
79   _bufferModel = new BufferModel(_networkModel);
80
81   SignalProxy *p = signalProxy();
82
83   p->attachSlot(SIGNAL(displayMsg(const Message &)), this, SLOT(recvMessage(const Message &)));
84   p->attachSlot(SIGNAL(displayStatusMsg(QString, QString)), this, SLOT(recvStatusMsg(QString, QString)));
85
86   p->attachSlot(SIGNAL(backlogData(BufferInfo, const QVariantList &, bool)), this, SLOT(recvBacklogData(BufferInfo, const QVariantList &, bool)));
87   p->attachSlot(SIGNAL(bufferInfoUpdated(BufferInfo)), this, SLOT(updateBufferInfo(BufferInfo)));
88   p->attachSignal(this, SIGNAL(sendInput(BufferInfo, QString)));
89   p->attachSignal(this, SIGNAL(requestNetworkStates()));
90
91   p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &)), SIGNAL(createIdentity(const Identity &)));
92   p->attachSignal(this, SIGNAL(requestUpdateIdentity(const Identity &)), SIGNAL(updateIdentity(const Identity &)));
93   p->attachSignal(this, SIGNAL(requestRemoveIdentity(IdentityId)), SIGNAL(removeIdentity(IdentityId)));
94   p->attachSlot(SIGNAL(identityCreated(const Identity &)), this, SLOT(coreIdentityCreated(const Identity &)));
95   p->attachSlot(SIGNAL(identityRemoved(IdentityId)), this, SLOT(coreIdentityRemoved(IdentityId)));
96
97   p->attachSignal(this, SIGNAL(requestCreateNetwork(const NetworkInfo &)), SIGNAL(createNetwork(const NetworkInfo &)));
98   p->attachSignal(this, SIGNAL(requestUpdateNetwork(const NetworkInfo &)), SIGNAL(updateNetwork(const NetworkInfo &)));
99   p->attachSignal(this, SIGNAL(requestRemoveNetwork(NetworkId)), SIGNAL(removeNetwork(NetworkId)));
100   p->attachSlot(SIGNAL(networkCreated(NetworkId)), this, SLOT(coreNetworkCreated(NetworkId)));
101   p->attachSlot(SIGNAL(networkRemoved(NetworkId)), this, SLOT(coreNetworkRemoved(NetworkId)));
102
103   connect(p, SIGNAL(disconnected()), this, SLOT(disconnectFromCore()));
104
105   //connect(mainUi, SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
106   connect(mainUi, SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
107   connect(this, SIGNAL(connected()), mainUi, SLOT(connectedToCore()));
108   connect(this, SIGNAL(disconnected()), mainUi, SLOT(disconnectedFromCore()));
109
110   layoutTimer = new QTimer(this);
111   layoutTimer->setInterval(0);
112   layoutTimer->setSingleShot(false);
113   connect(layoutTimer, SIGNAL(timeout()), this, SLOT(layoutMsg()));
114
115 }
116
117 /*** public static methods ***/
118
119 QList<BufferInfo> Client::allBufferInfos() {
120   QList<BufferInfo> bufferids;
121   foreach(Buffer *buffer, buffers()) {
122     bufferids << buffer->bufferInfo();
123   }
124   return bufferids;
125 }
126
127 QList<Buffer *> Client::buffers() {
128   return instance()->_buffers.values();
129 }
130
131
132 // FIXME remove
133 Buffer *Client::buffer(BufferId bufferUid) {
134   if(instance()->_buffers.contains(bufferUid))
135     return instance()->_buffers[bufferUid];
136   else
137     return 0;
138 }
139
140 // FIXME remove
141 Buffer *Client::buffer(BufferInfo id) {
142   Buffer *buff = buffer(id.uid());
143
144   if(!buff) {
145     Client *client = Client::instance();
146     buff = new Buffer(id, client);
147     connect(buff, SIGNAL(destroyed()),
148             client, SLOT(bufferDestroyed()));
149     client->_buffers[id.uid()] = buff;
150     emit client->bufferUpdated(id);
151   }
152   Q_ASSERT(buff);
153   return buff;
154 }
155
156 Buffer *Client::monitorBuffer() {
157   return instance()->_monitorBuffer;
158 }
159
160
161 NetworkModel *Client::networkModel() {
162   return instance()->_networkModel;
163 }
164
165 BufferModel *Client::bufferModel() {
166   return instance()->_bufferModel;
167 }
168
169 SignalProxy *Client::signalProxy() {
170   return instance()->_signalProxy;
171 }
172
173 bool Client::isConnected() {
174   return instance()->_connectedToCore;
175 }
176
177 bool Client::isSynced() {
178   return instance()->_syncedToCore;
179 }
180
181 /*** Network handling ***/
182
183 QList<NetworkId> Client::networkIds() {
184   return instance()->_networks.keys();
185 }
186
187 const Network * Client::network(NetworkId networkid) {
188   if(instance()->_networks.contains(networkid)) return instance()->_networks[networkid];
189   else return 0;
190 }
191
192 void Client::createNetwork(const NetworkInfo &info) {
193   emit instance()->requestCreateNetwork(info);
194 }
195
196 void Client::updateNetwork(const NetworkInfo &info) {
197   emit instance()->requestUpdateNetwork(info);
198 }
199
200 void Client::removeNetwork(NetworkId id) {
201   emit instance()->requestRemoveNetwork(id);
202 }
203
204 void Client::addNetwork(Network *net) {
205   net->setProxy(signalProxy());
206   signalProxy()->synchronize(net);
207   networkModel()->attachNetwork(net);
208   connect(net, SIGNAL(destroyed()), instance(), SLOT(networkDestroyed()));
209   instance()->_networks[net->networkId()] = net;
210   emit instance()->networkCreated(net->networkId());
211 }
212
213 void Client::coreNetworkCreated(NetworkId id) {
214   if(_networks.contains(id)) {
215     qWarning() << "Creation of already existing network requested!";
216     return;
217   }
218   Network *net = new Network(id, this);
219   addNetwork(net);
220 }
221
222 void Client::coreNetworkRemoved(NetworkId id) {
223   if(!_networks.contains(id)) return;
224   Network *net = _networks.take(id);
225   emit networkRemoved(net->networkId());
226   net->deleteLater();
227 }
228
229 /*** Identity handling ***/
230
231 QList<IdentityId> Client::identityIds() {
232   return instance()->_identities.keys();
233 }
234
235 const Identity * Client::identity(IdentityId id) {
236   if(instance()->_identities.contains(id)) return instance()->_identities[id];
237   else return 0;
238 }
239
240 void Client::createIdentity(const Identity &id) {
241   emit instance()->requestCreateIdentity(id);
242 }
243
244 void Client::updateIdentity(const Identity &id) {
245   emit instance()->requestUpdateIdentity(id);
246 }
247
248 void Client::removeIdentity(IdentityId id) {
249   emit instance()->requestRemoveIdentity(id);
250 }
251
252 void Client::coreIdentityCreated(const Identity &other) {
253   if(!_identities.contains(other.id())) {
254     Identity *identity = new Identity(other, this);
255     _identities[other.id()] = identity;
256     identity->setInitialized();
257     signalProxy()->synchronize(identity);
258     emit identityCreated(other.id());
259   } else {
260     qWarning() << tr("Identity already exists in client!");
261   }
262 }
263
264 void Client::coreIdentityRemoved(IdentityId id) {
265   if(_identities.contains(id)) {
266     emit identityRemoved(id);
267     Identity *i = _identities.take(id);
268     i->deleteLater();
269   }
270 }
271
272 /***  ***/
273 void Client::userInput(BufferInfo bufferInfo, QString message) {
274   emit instance()->sendInput(bufferInfo, message);
275 }
276
277 /*** core connection stuff ***/
278
279 void Client::setConnectedToCore(QIODevice *sock) {
280   socket = sock;
281   signalProxy()->addPeer(socket);
282   _connectedToCore = true;
283 }
284
285 void Client::setSyncedToCore() {
286   _syncedToCore = true;
287   emit connected();
288   emit coreConnectionStateChanged(true);
289 }
290
291 void Client::disconnectFromCore() {
292   if(socket) {
293     socket->close();
294     socket->deleteLater();
295   }
296   _connectedToCore = false;
297   _syncedToCore = false;
298   emit disconnected();
299   emit coreConnectionStateChanged(false);
300
301   // Clear internal data. Hopefully nothing relies on it at this point.
302   _networkModel->clear();
303
304   QHash<NetworkId, Network*>::iterator netIter = _networks.begin();
305   while(netIter != _networks.end()) {
306     Network *net = netIter.value();
307     emit networkRemoved(net->networkId());
308     disconnect(net, SIGNAL(destroyed()), this, 0);
309     netIter = _networks.erase(netIter);
310     net->deleteLater();
311   }
312   Q_ASSERT(_networks.isEmpty());
313
314   QHash<BufferId, Buffer *>::iterator bufferIter =  _buffers.begin();
315   while(bufferIter != _buffers.end()) {
316     Buffer *buffer = bufferIter.value();
317     disconnect(buffer, SIGNAL(destroyed()), this, 0);
318     bufferIter = _buffers.erase(bufferIter);
319     buffer->deleteLater();
320   }
321   Q_ASSERT(_buffers.isEmpty());
322
323   QHash<IdentityId, Identity*>::iterator idIter = _identities.begin();
324   while(idIter != _identities.end()) {
325     Identity *id = idIter.value();
326     emit identityRemoved(id->id());
327     idIter = _identities.erase(idIter);
328     id->deleteLater();
329   }
330   Q_ASSERT(_identities.isEmpty());
331
332   layoutQueue.clear();
333   layoutTimer->stop();
334 }
335
336 void Client::setCoreConfiguration(const QVariantMap &settings) {
337   SignalProxy::writeDataToDevice(socket, settings);
338 }
339
340 /*** ***/
341
342 void Client::updateBufferInfo(BufferInfo id) {
343   emit bufferUpdated(id);
344 }
345
346 void Client::bufferDestroyed() {
347   Buffer *buffer = static_cast<Buffer *>(sender());
348   QHash<BufferId, Buffer *>::iterator iter = _buffers.begin();
349   while(iter != _buffers.end()) {
350     if(iter.value() == buffer) {
351       iter = _buffers.erase(iter);
352       break;
353     }
354     iter++;
355   }
356 }
357
358 void Client::networkDestroyed() {
359   // FIXME this is not gonna work, net is a QObject here already!
360   Network *net = static_cast<Network *>(sender());
361   NetworkId networkId = net->networkId();
362   if(_networks.contains(networkId))
363     _networks.remove(networkId);
364 }
365
366 void Client::recvMessage(const Message &msg) {
367   Buffer *b = buffer(msg.buffer());
368   b->appendMsg(msg);
369   networkModel()->updateBufferActivity(msg);
370
371   if(msg.type() == Message::Plain || msg.type() == Message::Notice || msg.type() == Message::Action) {
372     QString sender = msg.buffer().network() + ":" + msg.buffer().buffer() + ":" + msg.sender();
373     Message mmsg = Message(msg.timestamp(), msg.buffer(), msg.type(), msg.text(), sender, msg.flags());
374     monitorBuffer()->appendMsg(mmsg);
375   }
376
377 }
378
379 void Client::recvStatusMsg(QString /*net*/, QString /*msg*/) {
380   //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
381 }
382
383 void Client::recvBacklogData(BufferInfo id, QVariantList msgs, bool /*done*/) {
384   Buffer *b = buffer(id);
385   foreach(QVariant v, msgs) {
386     Message msg = v.value<Message>();
387     b->prependMsg(msg);
388     // networkModel()->updateBufferActivity(msg);
389     if(!layoutQueue.contains(b)) layoutQueue.append(b);
390   }
391   if(layoutQueue.count() && !layoutTimer->isActive()) layoutTimer->start();
392 }
393
394 void Client::layoutMsg() {
395   if(layoutQueue.count()) {
396     Buffer *b = layoutQueue.takeFirst();  // TODO make this the current buffer
397     if(b->layoutMsg())
398       layoutQueue.append(b);  // Buffer has more messages in its queue --> Round Robin
399   }
400   
401   if(!layoutQueue.count())
402     layoutTimer->stop();
403 }
404
405 AbstractUiMsg *Client::layoutMsg(const Message &msg) {
406   return instance()->mainUi->layoutMsg(msg);
407 }
408