Quassel IRC  Pre-Release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
client.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2015 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  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #include "client.h"
22 
24 #include "abstractui.h"
25 #include "bufferinfo.h"
26 #include "buffermodel.h"
27 #include "buffersettings.h"
28 #include "buffersyncer.h"
29 #include "bufferviewconfig.h"
30 #include "bufferviewoverlay.h"
31 #include "clientaliasmanager.h"
32 #include "clientbacklogmanager.h"
34 #include "clientirclisthelper.h"
35 #include "clientidentity.h"
37 #include "clienttransfermanager.h"
38 #include "clientuserinputhandler.h"
39 #include "coreaccountmodel.h"
40 #include "coreconnection.h"
41 #include "ircchannel.h"
42 #include "ircuser.h"
43 #include "message.h"
44 #include "messagemodel.h"
45 #include "network.h"
46 #include "networkconfig.h"
47 #include "networkmodel.h"
48 #include "quassel.h"
49 #include "signalproxy.h"
50 #include "util.h"
51 #include "clientauthhandler.h"
52 
53 #include <stdio.h>
54 #include <stdlib.h>
55 
56 QPointer<Client> Client::instanceptr = 0;
57 Quassel::Features Client::_coreFeatures = 0;
58 
59 /*** Initialization/destruction ***/
60 
62 {
63  return instanceptr;
64 }
65 
66 
68 {
69  if (!instanceptr)
70  instanceptr = new Client();
71  return instanceptr;
72 }
73 
74 
76 {
77  if (instanceptr) {
78  delete instanceptr->mainUi();
79  instanceptr->deleteLater();
80  instanceptr = 0;
81  }
82 }
83 
84 
86 {
87  instance()->_mainUi = ui;
88  instance()->init();
89 }
90 
91 
92 Client::Client(QObject *parent)
93  : QObject(parent),
94  _signalProxy(new SignalProxy(SignalProxy::Client, this)),
95  _mainUi(0),
96  _networkModel(0),
97  _bufferModel(0),
98  _bufferSyncer(0),
99  _aliasManager(0),
100  _backlogManager(new ClientBacklogManager(this)),
101  _bufferViewManager(0),
102  _bufferViewOverlay(new BufferViewOverlay(this)),
103  _ircListHelper(new ClientIrcListHelper(this)),
104  _inputHandler(0),
105  _networkConfig(0),
106  _ignoreListManager(0),
107  _transferManager(0),
108  _messageModel(0),
109  _messageProcessor(0),
110  _coreAccountModel(new CoreAccountModel(this)),
111  _coreConnection(new CoreConnection(this)),
112  _connected(false),
113  _debugLog(&_debugLogBuffer)
114 {
116 }
117 
118 
120 {
122 }
123 
124 
126 {
127  _networkModel = new NetworkModel(this);
128 
129  connect(this, SIGNAL(networkRemoved(NetworkId)),
131 
136 
137  SignalProxy *p = signalProxy();
138 
139  p->attachSlot(SIGNAL(displayMsg(const Message &)), this, SLOT(recvMessage(const Message &)));
140  p->attachSlot(SIGNAL(displayStatusMsg(QString, QString)), this, SLOT(recvStatusMsg(QString, QString)));
141 
142  p->attachSlot(SIGNAL(bufferInfoUpdated(BufferInfo)), _networkModel, SLOT(bufferUpdated(BufferInfo)));
143  p->attachSignal(inputHandler(), SIGNAL(sendInput(BufferInfo, QString)));
144  p->attachSignal(this, SIGNAL(requestNetworkStates()));
145 
146  p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &, const QVariantMap &)), SIGNAL(createIdentity(const Identity &, const QVariantMap &)));
148  p->attachSlot(SIGNAL(identityCreated(const Identity &)), this, SLOT(coreIdentityCreated(const Identity &)));
150 
151  p->attachSignal(this, SIGNAL(requestCreateNetwork(const NetworkInfo &, const QStringList &)), SIGNAL(createNetwork(const NetworkInfo &, const QStringList &)));
155 
156  p->attachSignal(this, SIGNAL(requestPasswordChange(PeerPtr,QString,QString,QString)), SIGNAL(changePassword(PeerPtr,QString,QString,QString)));
157  p->attachSlot(SIGNAL(passwordChanged(PeerPtr,bool)), this, SLOT(corePasswordChanged(PeerPtr,bool)));
158 
159  //connect(mainUi(), SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
160  connect(mainUi(), SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
161  connect(this, SIGNAL(connected()), mainUi(), SLOT(connectedToCore()));
162  connect(this, SIGNAL(disconnected()), mainUi(), SLOT(disconnectedFromCore()));
163 
164  // attach backlog manager
166  connect(backlogManager(), SIGNAL(messagesReceived(BufferId, int)), _messageModel, SLOT(messagesReceived(BufferId, int)));
167 
168  coreAccountModel()->load();
169 
171  coreConnection()->init();
172 }
173 
174 
175 /*** public static methods ***/
176 
178 {
179  return instance()->_mainUi;
180 }
181 
182 
183 void Client::setCoreFeatures(Quassel::Features features)
184 {
185  _coreFeatures = features;
186 }
187 
188 
190 {
191  return instance()->_connected;
192 }
193 
194 
196 {
197  return currentCoreAccount().isInternal();
198 }
199 
200 
201 /*** Network handling ***/
202 
203 QList<NetworkId> Client::networkIds()
204 {
205  return instance()->_networks.keys();
206 }
207 
208 
210 {
211  if (instance()->_networks.contains(networkid)) return instance()->_networks[networkid];
212  else return 0;
213 }
214 
215 
216 void Client::createNetwork(const NetworkInfo &info, const QStringList &persistentChannels)
217 {
218  emit instance()->requestCreateNetwork(info, persistentChannels);
219 }
220 
221 
223 {
224  emit instance()->requestRemoveNetwork(id);
225 }
226 
227 
229 {
230  Network *netptr = instance()->_networks.value(info.networkId, 0);
231  if (!netptr) {
232  qWarning() << "Update for unknown network requested:" << info;
233  return;
234  }
235  netptr->requestSetNetworkInfo(info);
236 }
237 
238 
240 {
241  net->setProxy(signalProxy());
242  signalProxy()->synchronize(net);
243  networkModel()->attachNetwork(net);
244  connect(net, SIGNAL(destroyed()), instance(), SLOT(networkDestroyed()));
245  instance()->_networks[net->networkId()] = net;
246  emit instance()->networkCreated(net->networkId());
247 }
248 
249 
251 {
252  if (_networks.contains(id)) {
253  qWarning() << "Creation of already existing network requested!";
254  return;
255  }
256  Network *net = new Network(id, this);
257  addNetwork(net);
258 }
259 
260 
262 {
263  if (!_networks.contains(id))
264  return;
265  Network *net = _networks.take(id);
266  emit networkRemoved(net->networkId());
267  net->deleteLater();
268 }
269 
270 
271 /*** Identity handling ***/
272 
273 QList<IdentityId> Client::identityIds()
274 {
275  return instance()->_identities.keys();
276 }
277 
278 
280 {
281  if (instance()->_identities.contains(id)) return instance()->_identities[id];
282  else return 0;
283 }
284 
285 
287 {
288  QVariantMap additional;
289 #ifdef HAVE_SSL
290  additional["KeyPem"] = id.sslKey().toPem();
291  additional["CertPem"] = id.sslCert().toPem();
292 #endif
293  emit instance()->requestCreateIdentity(id, additional);
294 }
295 
296 
297 void Client::updateIdentity(IdentityId id, const QVariantMap &ser)
298 {
299  Identity *idptr = instance()->_identities.value(id, 0);
300  if (!idptr) {
301  qWarning() << "Update for unknown identity requested:" << id;
302  return;
303  }
304  idptr->requestUpdate(ser);
305 }
306 
307 
309 {
310  emit instance()->requestRemoveIdentity(id);
311 }
312 
313 
315 {
316  if (!_identities.contains(other.id())) {
317  Identity *identity = new Identity(other, this);
318  _identities[other.id()] = identity;
319  identity->setInitialized();
320  signalProxy()->synchronize(identity);
321  emit identityCreated(other.id());
322  }
323  else {
324  qWarning() << tr("Identity already exists in client!");
325  }
326 }
327 
328 
330 {
331  if (_identities.contains(id)) {
332  emit identityRemoved(id);
333  Identity *i = _identities.take(id);
334  i->deleteLater();
335  }
336 }
337 
338 
339 /*** User input handling ***/
340 
341 void Client::userInput(const BufferInfo &bufferInfo, const QString &message)
342 {
343  // we need to make sure that AliasManager is ready before processing input
344  if (aliasManager() && aliasManager()->isInitialized())
345  inputHandler()->handleUserInput(bufferInfo, message);
346  else
347  instance()->_userInputBuffer.append(qMakePair(bufferInfo, message));
348 }
349 
350 
352 {
353  for (int i = 0; i < _userInputBuffer.count(); i++)
354  userInput(_userInputBuffer.at(i).first, _userInputBuffer.at(i).second);
355 
356  _userInputBuffer.clear();
357 }
358 
359 
360 /*** core connection stuff ***/
361 
363 {
364  switch (state) {
367  break;
369  setSyncedToCore();
370  break;
371  default:
372  break;
373  }
374 }
375 
376 
378 {
379  // create buffersyncer
380  Q_ASSERT(!_bufferSyncer);
381  _bufferSyncer = new BufferSyncer(this);
382  connect(bufferSyncer(), SIGNAL(lastSeenMsgSet(BufferId, MsgId)), _networkModel, SLOT(setLastSeenMsgId(BufferId, MsgId)));
383  connect(bufferSyncer(), SIGNAL(markerLineSet(BufferId, MsgId)), _networkModel, SLOT(setMarkerLineMsgId(BufferId, MsgId)));
384  connect(bufferSyncer(), SIGNAL(bufferRemoved(BufferId)), this, SLOT(bufferRemoved(BufferId)));
385  connect(bufferSyncer(), SIGNAL(bufferRenamed(BufferId, QString)), this, SLOT(bufferRenamed(BufferId, QString)));
388  connect(bufferSyncer(), SIGNAL(bufferMarkedAsRead(BufferId)), SIGNAL(bufferMarkedAsRead(BufferId)));
389  connect(networkModel(), SIGNAL(requestSetLastSeenMsg(BufferId, MsgId)), bufferSyncer(), SLOT(requestSetLastSeenMsg(BufferId, const MsgId &)));
390 
391  SignalProxy *p = signalProxy();
393 
394  // create a new BufferViewManager
395  Q_ASSERT(!_bufferViewManager);
397  connect(_bufferViewManager, SIGNAL(initDone()), _bufferViewOverlay, SLOT(restore()));
398 
399  // create AliasManager
400  Q_ASSERT(!_aliasManager);
401  _aliasManager = new ClientAliasManager(this);
402  connect(aliasManager(), SIGNAL(initDone()), SLOT(sendBufferedUserInput()));
404 
405  // create NetworkConfig
406  Q_ASSERT(!_networkConfig);
407  _networkConfig = new NetworkConfig("GlobalNetworkConfig", this);
409 
410  // create IgnoreListManager
411  Q_ASSERT(!_ignoreListManager);
414 
415  Q_ASSERT(!_transferManager);
418 
419  // trigger backlog request once all active bufferviews are initialized
420  connect(bufferViewOverlay(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
421 
422  _connected = true;
423  emit connected();
424  emit coreConnectionStateChanged(true);
425 }
426 
427 
429 {
430  // usually it _should_ take longer until the bufferViews are initialized, so that's what
431  // triggers this slot. But we have to make sure that we know all buffers yet.
432  // so we check the BufferSyncer and in case it wasn't initialized we wait for that instead
433  if (!bufferSyncer()->isInitialized()) {
434  disconnect(bufferViewOverlay(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
435  connect(bufferSyncer(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
436  return;
437  }
438  disconnect(bufferViewOverlay(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
439  disconnect(bufferSyncer(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
440 
442 }
443 
444 
446 {
447  if (!coreConnection()->isConnected())
448  return;
449 
451 }
452 
453 
455 {
456  _connected = false;
457  _coreFeatures = 0;
458 
459  emit disconnected();
460  emit coreConnectionStateChanged(false);
461 
462  backlogManager()->reset();
463  messageProcessor()->reset();
464 
465  // Clear internal data. Hopefully nothing relies on it at this point.
466 
467  if (_bufferSyncer) {
468  _bufferSyncer->deleteLater();
469  _bufferSyncer = 0;
470  }
471 
472  if (_bufferViewManager) {
473  _bufferViewManager->deleteLater();
474  _bufferViewManager = 0;
475  }
476 
478 
479  if (_aliasManager) {
480  _aliasManager->deleteLater();
481  _aliasManager = 0;
482  }
483 
484  if (_ignoreListManager) {
485  _ignoreListManager->deleteLater();
486  _ignoreListManager = 0;
487  }
488 
489  if (_transferManager) {
490  _transferManager->deleteLater();
491  _transferManager = 0;
492  }
493 
494  // we probably don't want to save pending input for reconnect
495  _userInputBuffer.clear();
496 
497  _messageModel->clear();
498  _networkModel->clear();
499 
500  QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
501  while (netIter != _networks.end()) {
502  Network *net = netIter.value();
503  emit networkRemoved(net->networkId());
504  disconnect(net, SIGNAL(destroyed()), this, 0);
505  netIter = _networks.erase(netIter);
506  net->deleteLater();
507  }
508  Q_ASSERT(_networks.isEmpty());
509 
510  QHash<IdentityId, Identity *>::iterator idIter = _identities.begin();
511  while (idIter != _identities.end()) {
512  emit identityRemoved(idIter.key());
513  Identity *id = idIter.value();
514  idIter = _identities.erase(idIter);
515  id->deleteLater();
516  }
517  Q_ASSERT(_identities.isEmpty());
518 
519  if (_networkConfig) {
520  _networkConfig->deleteLater();
521  _networkConfig = 0;
522  }
523 }
524 
525 
526 /*** ***/
527 
529 {
530  Network *net = static_cast<Network *>(sender());
531  QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
532  while (netIter != _networks.end()) {
533  if (*netIter == net) {
534  netIter = _networks.erase(netIter);
535  break;
536  }
537  else {
538  netIter++;
539  }
540  }
541 }
542 
543 
544 // Hmm... we never used this...
545 void Client::recvStatusMsg(QString /*net*/, QString /*msg*/)
546 {
547  //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
548 }
549 
550 
551 void Client::recvMessage(const Message &msg)
552 {
553  Message msg_ = msg;
554  messageProcessor()->process(msg_);
555 }
556 
557 
559 {
560  if (bufferSyncer())
561  bufferSyncer()->requestSetLastSeenMsg(id, msgId);
562 }
563 
564 
565 void Client::setMarkerLine(BufferId id, const MsgId &msgId)
566 {
567  if (bufferSyncer())
568  bufferSyncer()->requestSetMarkerLine(id, msgId);
569 }
570 
571 
573 {
574  if (id.isValid() && networkModel())
575  return networkModel()->markerLineMsgId(id);
576  return MsgId();
577 }
578 
579 
581 {
582  if (!bufferSyncer()) return;
584 }
585 
586 
587 void Client::renameBuffer(BufferId bufferId, const QString &newName)
588 {
589  if (!bufferSyncer())
590  return;
591  bufferSyncer()->requestRenameBuffer(bufferId, newName);
592 }
593 
594 
596 {
597  if (!bufferSyncer())
598  return;
599  bufferSyncer()->requestMergeBuffersPermanently(bufferId1, bufferId2);
600 }
601 
602 
604 {
605  if (!bufferSyncer())
606  return;
608 }
609 
610 
612 {
613  // select a sane buffer (status buffer)
614  /* we have to manually select a buffer because otherwise inconsitent changes
615  * to the model might occur:
616  * the result of a buffer removal triggers a change in the selection model.
617  * the newly selected buffer might be a channel that hasn't been selected yet
618  * and a new nickview would be created (which never heard of the "rowsAboutToBeRemoved").
619  * this new view (and/or) its sort filter will then only receive a "rowsRemoved" signal.
620  */
621  QModelIndex current = bufferModel()->currentIndex();
622  if (current.data(NetworkModel::BufferIdRole).value<BufferId>() == bufferId) {
623  bufferModel()->setCurrentIndex(current.sibling(0, 0));
624  }
625 
626  // and remove it from the model
627  networkModel()->removeBuffer(bufferId);
628 }
629 
630 
631 void Client::bufferRenamed(BufferId bufferId, const QString &newName)
632 {
633  QModelIndex bufferIndex = networkModel()->bufferIndex(bufferId);
634  if (bufferIndex.isValid()) {
635  networkModel()->setData(bufferIndex, newName, Qt::DisplayRole);
636  }
637 }
638 
639 
641 {
642  QModelIndex idx = networkModel()->bufferIndex(bufferId1);
643  bufferModel()->setCurrentIndex(bufferModel()->mapFromSource(idx));
644  networkModel()->removeBuffer(bufferId2);
645 }
646 
647 
649 {
650  if (bufferSyncer() && id.isValid())
652 }
653 
654 
655 void Client::changePassword(const QString &oldPassword, const QString &newPassword) {
656  CoreAccount account = currentCoreAccount();
657  account.setPassword(newPassword);
659  emit instance()->requestPasswordChange(nullptr, account.user(), oldPassword, newPassword);
660 }
661 
662 
664 {
665  if (success)
666  coreAccountModel()->save();
667  emit passwordChanged(success);
668 }
669 
670 
671 #if QT_VERSION < 0x050000
672 void Client::logMessage(QtMsgType type, const char *msg)
673 {
674  fprintf(stderr, "%s\n", msg);
675  fflush(stderr);
676  if (type == QtFatalMsg) {
678  }
679  else {
680  QString msgString = QString("%1\n").arg(msg);
681 
682  //Check to see if there is an instance around, else we risk recursions
683  //when calling instance() and creating new ones.
684  if (!instanceExists())
685  return;
686 
687  instance()->_debugLog << msgString;
688  emit instance()->logUpdated(msgString);
689  }
690 }
691 #else
692 void Client::logMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
693 {
694  Q_UNUSED(context);
695 
696  fprintf(stderr, "%s\n", msg.toLocal8Bit().constData());
697  fflush(stderr);
698  if (type == QtFatalMsg) {
699  Quassel::logFatalMessage(msg.toLocal8Bit().constData());
700  }
701  else {
702  QString msgString = QString("%1\n").arg(msg);
703 
704  //Check to see if there is an instance around, else we risk recursions
705  //when calling instance() and creating new ones.
706  if (!instanceExists())
707  return;
708 
709  instance()->_debugLog << msgString;
710  emit instance()->logUpdated(msgString);
711  }
712 }
713 #endif