modernize: Use C++ versions of system headers
[quassel.git] / src / client / client.cpp
index 7b94d93..82e7fd4 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2012 by the Quassel Project                        *
+ *   Copyright (C) 2005-2018 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
 #include "clientirclisthelper.h"
 #include "clientidentity.h"
 #include "clientignorelistmanager.h"
+#include "clienttransfermanager.h"
 #include "clientuserinputhandler.h"
 #include "coreaccountmodel.h"
 #include "coreconnection.h"
+#include "dccconfig.h"
 #include "ircchannel.h"
 #include "ircuser.h"
 #include "message.h"
 #include "networkmodel.h"
 #include "quassel.h"
 #include "signalproxy.h"
+#include "transfermodel.h"
 #include "util.h"
+#include "clientauthhandler.h"
 
-#include <stdio.h>
-#include <stdlib.h>
+#include <cstdio>
+#include <cstdlib>
 
-QPointer<Client> Client::instanceptr = 0;
-Quassel::Features Client::_coreFeatures = 0;
-
-/*** Initialization/destruction ***/
-
-bool Client::instanceExists()
-{
-    return instanceptr;
-}
-
-
-Client *Client::instance()
-{
-    if (!instanceptr)
-        instanceptr = new Client();
-    return instanceptr;
-}
-
-
-void Client::destroy()
-{
-    if (instanceptr) {
-        delete instanceptr->mainUi();
-        instanceptr->deleteLater();
-        instanceptr = 0;
-    }
-}
-
-
-void Client::init(AbstractUi *ui)
-{
-    instance()->_mainUi = ui;
-    instance()->init();
-}
-
-
-Client::Client(QObject *parent)
-    : QObject(parent),
+Client::Client(std::unique_ptr<AbstractUi> ui, QObject *parent)
+    : QObject(parent), Singleton<Client>(this),
     _signalProxy(new SignalProxy(SignalProxy::Client, this)),
-    _mainUi(0),
-    _networkModel(0),
-    _bufferModel(0),
-    _bufferSyncer(0),
-    _aliasManager(0),
+    _mainUi(std::move(ui)),
+    _networkModel(new NetworkModel(this)),
+    _bufferModel(new BufferModel(_networkModel)),
     _backlogManager(new ClientBacklogManager(this)),
-    _bufferViewManager(0),
     _bufferViewOverlay(new BufferViewOverlay(this)),
+    _coreInfo(new CoreInfo(this)),
     _ircListHelper(new ClientIrcListHelper(this)),
-    _inputHandler(0),
-    _networkConfig(0),
-    _ignoreListManager(0),
-    _messageModel(0),
-    _messageProcessor(0),
+    _inputHandler(new ClientUserInputHandler(this)),
+    _transferModel(new TransferModel(this)),
+    _messageModel(_mainUi->createMessageModel(this)),
+    _messageProcessor(_mainUi->createMessageProcessor(this)),
     _coreAccountModel(new CoreAccountModel(this)),
-    _coreConnection(new CoreConnection(_coreAccountModel, this)),
-    _connected(false),
-    _debugLog(&_debugLogBuffer)
+    _coreConnection(new CoreConnection(this))
 {
-    _signalProxy->synchronize(_ircListHelper);
-}
-
-
-Client::~Client()
-{
-    disconnectFromCore();
-}
-
+#ifdef EMBED_DATA
+    Q_INIT_RESOURCE(data);
+#endif
 
-void Client::init()
-{
-    _networkModel = new NetworkModel(this);
+    //connect(mainUi(), SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
+    connect(mainUi(), SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
+    connect(this, SIGNAL(connected()), mainUi(), SLOT(connectedToCore()));
+    connect(this, SIGNAL(disconnected()), mainUi(), SLOT(disconnectedFromCore()));
 
-    connect(this, SIGNAL(networkRemoved(NetworkId)),
-        _networkModel, SLOT(networkRemoved(NetworkId)));
+    connect(this, SIGNAL(networkRemoved(NetworkId)), _networkModel, SLOT(networkRemoved(NetworkId)));
+    connect(this, SIGNAL(networkRemoved(NetworkId)), _messageProcessor, SLOT(networkRemoved(NetworkId)));
 
-    _bufferModel = new BufferModel(_networkModel);
-    _messageModel = mainUi()->createMessageModel(this);
-    _messageProcessor = mainUi()->createMessageProcessor(this);
-    _inputHandler = new ClientUserInputHandler(this);
+    connect(backlogManager(), SIGNAL(messagesReceived(BufferId, int)), _messageModel, SLOT(messagesReceived(BufferId, int)));
+    connect(coreConnection(), SIGNAL(stateChanged(CoreConnection::ConnectionState)), SLOT(connectionStateChanged(CoreConnection::ConnectionState)));
 
     SignalProxy *p = signalProxy();
 
@@ -150,33 +106,36 @@ void Client::init()
     p->attachSlot(SIGNAL(networkCreated(NetworkId)), this, SLOT(coreNetworkCreated(NetworkId)));
     p->attachSlot(SIGNAL(networkRemoved(NetworkId)), this, SLOT(coreNetworkRemoved(NetworkId)));
 
-    //connect(mainUi(), SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
-    connect(mainUi(), SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
-    connect(this, SIGNAL(connected()), mainUi(), SLOT(connectedToCore()));
-    connect(this, SIGNAL(disconnected()), mainUi(), SLOT(disconnectedFromCore()));
+    p->attachSignal(this, SIGNAL(requestPasswordChange(PeerPtr,QString,QString,QString)), SIGNAL(changePassword(PeerPtr,QString,QString,QString)));
+    p->attachSlot(SIGNAL(passwordChanged(PeerPtr,bool)), this, SLOT(corePasswordChanged(PeerPtr,bool)));
+
+    p->attachSignal(this, SIGNAL(requestKickClient(int)), SIGNAL(kickClient(int)));
+    p->attachSlot(SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
 
-    // attach backlog manager
     p->synchronize(backlogManager());
-    connect(backlogManager(), SIGNAL(messagesReceived(BufferId, int)), _messageModel, SLOT(messagesReceived(BufferId, int)));
+    p->synchronize(coreInfo());
+    p->synchronize(_ircListHelper);
 
     coreAccountModel()->load();
-
-    connect(coreConnection(), SIGNAL(stateChanged(CoreConnection::ConnectionState)), SLOT(connectionStateChanged(CoreConnection::ConnectionState)));
     coreConnection()->init();
 }
 
 
-/*** public static methods ***/
+Client::~Client()
+{
+    disconnectFromCore();
+}
+
 
 AbstractUi *Client::mainUi()
 {
-    return instance()->_mainUi;
+    return instance()->_mainUi.get();
 }
 
 
-void Client::setCoreFeatures(Quassel::Features features)
+bool Client::isCoreFeatureEnabled(Quassel::Feature feature)
 {
-    _coreFeatures = features;
+    return coreConnection()->peer() ? coreConnection()->peer()->hasFeature(feature) : false;
 }
 
 
@@ -192,6 +151,22 @@ bool Client::internalCore()
 }
 
 
+void Client::onDbUpgradeInProgress(bool inProgress)
+{
+    emit dbUpgradeInProgress(inProgress);
+}
+
+
+void Client::onExitRequested(int exitCode, const QString &reason)
+{
+    if (!reason.isEmpty()) {
+        qCritical() << reason;
+        emit exitRequested(reason);
+    }
+    QCoreApplication::exit(exitCode);
+}
+
+
 /*** Network handling ***/
 
 QList<NetworkId> Client::networkIds()
@@ -203,7 +178,7 @@ QList<NetworkId> Client::networkIds()
 const Network *Client::network(NetworkId networkid)
 {
     if (instance()->_networks.contains(networkid)) return instance()->_networks[networkid];
-    else return 0;
+    else return nullptr;
 }
 
 
@@ -247,7 +222,7 @@ void Client::coreNetworkCreated(NetworkId id)
         qWarning() << "Creation of already existing network requested!";
         return;
     }
-    Network *net = new Network(id, this);
+    auto *net = new Network(id, this);
     addNetwork(net);
 }
 
@@ -273,7 +248,7 @@ QList<IdentityId> Client::identityIds()
 const Identity *Client::identity(IdentityId id)
 {
     if (instance()->_identities.contains(id)) return instance()->_identities[id];
-    else return 0;
+    else return nullptr;
 }
 
 
@@ -308,7 +283,7 @@ void Client::removeIdentity(IdentityId id)
 void Client::coreIdentityCreated(const Identity &other)
 {
     if (!_identities.contains(other.id())) {
-        Identity *identity = new Identity(other, this);
+        auto *identity = new Identity(other, this);
         _identities[other.id()] = identity;
         identity->setInitialized();
         signalProxy()->synchronize(identity);
@@ -380,56 +355,111 @@ void Client::setSyncedToCore()
     connect(bufferSyncer(), SIGNAL(buffersPermanentlyMerged(BufferId, BufferId)), this, SLOT(buffersPermanentlyMerged(BufferId, BufferId)));
     connect(bufferSyncer(), SIGNAL(buffersPermanentlyMerged(BufferId, BufferId)), _messageModel, SLOT(buffersPermanentlyMerged(BufferId, BufferId)));
     connect(bufferSyncer(), SIGNAL(bufferMarkedAsRead(BufferId)), SIGNAL(bufferMarkedAsRead(BufferId)));
+    connect(bufferSyncer(), SIGNAL(bufferActivityChanged(BufferId, const Message::Types)), _networkModel, SLOT(bufferActivityChanged(BufferId, const Message::Types)));
+    connect(bufferSyncer(), SIGNAL(highlightCountChanged(BufferId, int)), _networkModel, SLOT(highlightCountChanged(BufferId, int)));
     connect(networkModel(), SIGNAL(requestSetLastSeenMsg(BufferId, MsgId)), bufferSyncer(), SLOT(requestSetLastSeenMsg(BufferId, const MsgId &)));
-    signalProxy()->synchronize(bufferSyncer());
+
+    SignalProxy *p = signalProxy();
+    p->synchronize(bufferSyncer());
 
     // create a new BufferViewManager
     Q_ASSERT(!_bufferViewManager);
-    _bufferViewManager = new ClientBufferViewManager(signalProxy(), this);
+    _bufferViewManager = new ClientBufferViewManager(p, this);
     connect(_bufferViewManager, SIGNAL(initDone()), _bufferViewOverlay, SLOT(restore()));
 
     // create AliasManager
     Q_ASSERT(!_aliasManager);
     _aliasManager = new ClientAliasManager(this);
     connect(aliasManager(), SIGNAL(initDone()), SLOT(sendBufferedUserInput()));
-    signalProxy()->synchronize(aliasManager());
+    p->synchronize(aliasManager());
 
     // create NetworkConfig
     Q_ASSERT(!_networkConfig);
     _networkConfig = new NetworkConfig("GlobalNetworkConfig", this);
-    signalProxy()->synchronize(networkConfig());
+    p->synchronize(networkConfig());
 
     // create IgnoreListManager
     Q_ASSERT(!_ignoreListManager);
     _ignoreListManager = new ClientIgnoreListManager(this);
-    signalProxy()->synchronize(ignoreListManager());
+    p->synchronize(ignoreListManager());
+
+    // create Core-Side HighlightRuleManager
+    Q_ASSERT(!_highlightRuleManager);
+    _highlightRuleManager = new HighlightRuleManager(this);
+    p->synchronize(highlightRuleManager());
+    // Listen to network removed events
+    connect(this, SIGNAL(networkRemoved(NetworkId)),
+        _highlightRuleManager, SLOT(networkRemoved(NetworkId)));
+
+/*  not ready yet
+    // create TransferManager and DccConfig if core supports them
+    Q_ASSERT(!_dccConfig);
+    Q_ASSERT(!_transferManager);
+    if (isCoreFeatureEnabled(Quassel::Feature::DccFileTransfer)) {
+        _dccConfig = new DccConfig(this);
+        p->synchronize(dccConfig());
+        _transferManager = new ClientTransferManager(this);
+        _transferModel->setManager(_transferManager);
+        p->synchronize(transferManager());
+    }
+*/
 
     // trigger backlog request once all active bufferviews are initialized
-    connect(bufferViewOverlay(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
+    connect(bufferViewOverlay(), SIGNAL(initDone()), this, SLOT(finishConnectionInitialization()));
 
     _connected = true;
     emit connected();
     emit coreConnectionStateChanged(true);
 }
 
-
-void Client::requestInitialBacklog()
+void Client::finishConnectionInitialization()
 {
     // usually it _should_ take longer until the bufferViews are initialized, so that's what
     // triggers this slot. But we have to make sure that we know all buffers yet.
     // so we check the BufferSyncer and in case it wasn't initialized we wait for that instead
     if (!bufferSyncer()->isInitialized()) {
-        disconnect(bufferViewOverlay(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
-        connect(bufferSyncer(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
+        disconnect(bufferViewOverlay(), SIGNAL(initDone()), this, SLOT(finishConnectionInitialization()));
+        connect(bufferSyncer(), SIGNAL(initDone()), this, SLOT(finishConnectionInitialization()));
         return;
     }
-    disconnect(bufferViewOverlay(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
-    disconnect(bufferSyncer(), SIGNAL(initDone()), this, SLOT(requestInitialBacklog()));
+    disconnect(bufferViewOverlay(), SIGNAL(initDone()), this, SLOT(finishConnectionInitialization()));
+    disconnect(bufferSyncer(), SIGNAL(initDone()), this, SLOT(finishConnectionInitialization()));
+
+    requestInitialBacklog();
+    if (isCoreFeatureEnabled(Quassel::Feature::BufferActivitySync)) {
+        bufferSyncer()->markActivitiesChanged();
+        bufferSyncer()->markHighlightCountsChanged();
+    }
+}
 
+
+void Client::requestInitialBacklog()
+{
     _backlogManager->requestInitialBacklog();
 }
 
 
+void Client::requestLegacyCoreInfo()
+{
+    // On older cores, the CoreInfo object was only synchronized on demand.  Synchronize now if
+    // needed.
+    if (isConnected() && !isCoreFeatureEnabled(Quassel::Feature::SyncedCoreInfo)) {
+        // Delete the existing core info object (it will always exist as client is single-threaded)
+        _coreInfo->deleteLater();
+        // No need to set to null when creating new one immediately after
+
+        // Create a fresh, unsynchronized CoreInfo object, emulating legacy behavior of CoreInfo not
+        // persisting
+        _coreInfo = new CoreInfo(this);
+        // Synchronize the new object
+        signalProxy()->synchronize(_coreInfo);
+
+        // Let others know signal handlers have been reset
+        emit coreInfoResynchronized();
+    }
+}
+
+
 void Client::disconnectFromCore()
 {
     if (!coreConnection()->isConnected())
@@ -442,7 +472,6 @@ void Client::disconnectFromCore()
 void Client::setDisconnectedFromCore()
 {
     _connected = false;
-    _coreFeatures = 0;
 
     emit disconnected();
     emit coreConnectionStateChanged(false);
@@ -454,25 +483,44 @@ void Client::setDisconnectedFromCore()
 
     if (_bufferSyncer) {
         _bufferSyncer->deleteLater();
-        _bufferSyncer = 0;
+        _bufferSyncer = nullptr;
     }
 
+    _coreInfo->reset();
+
     if (_bufferViewManager) {
         _bufferViewManager->deleteLater();
-        _bufferViewManager = 0;
+        _bufferViewManager = nullptr;
     }
 
     _bufferViewOverlay->reset();
 
     if (_aliasManager) {
         _aliasManager->deleteLater();
-        _aliasManager = 0;
+        _aliasManager = nullptr;
     }
 
     if (_ignoreListManager) {
         _ignoreListManager->deleteLater();
-        _ignoreListManager = 0;
+        _ignoreListManager = nullptr;
+    }
+
+    if (_highlightRuleManager) {
+        _highlightRuleManager->deleteLater();
+        _highlightRuleManager = nullptr;
+    }
+
+    if (_transferManager) {
+        _transferModel->setManager(nullptr);
+        _transferManager->deleteLater();
+        _transferManager = nullptr;
     }
+
+    if (_dccConfig) {
+        _dccConfig->deleteLater();
+        _dccConfig = nullptr;
+    }
+
     // we probably don't want to save pending input for reconnect
     _userInputBuffer.clear();
 
@@ -483,7 +531,7 @@ void Client::setDisconnectedFromCore()
     while (netIter != _networks.end()) {
         Network *net = netIter.value();
         emit networkRemoved(net->networkId());
-        disconnect(net, SIGNAL(destroyed()), this, 0);
+        disconnect(net, SIGNAL(destroyed()), this, nullptr);
         netIter = _networks.erase(netIter);
         net->deleteLater();
     }
@@ -500,7 +548,7 @@ void Client::setDisconnectedFromCore()
 
     if (_networkConfig) {
         _networkConfig->deleteLater();
-        _networkConfig = 0;
+        _networkConfig = nullptr;
     }
 }
 
@@ -509,7 +557,7 @@ void Client::setDisconnectedFromCore()
 
 void Client::networkDestroyed()
 {
-    Network *net = static_cast<Network *>(sender());
+    auto *net = static_cast<Network *>(sender());
     QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
     while (netIter != _networks.end()) {
         if (*netIter == net) {
@@ -517,7 +565,7 @@ void Client::networkDestroyed()
             break;
         }
         else {
-            netIter++;
+            ++netIter;
         }
     }
 }
@@ -555,7 +603,7 @@ MsgId Client::markerLine(BufferId id)
 {
     if (id.isValid() && networkModel())
         return networkModel()->markerLineMsgId(id);
-    return MsgId();
+    return {};
 }
 
 
@@ -634,22 +682,29 @@ void Client::markBufferAsRead(BufferId id)
 }
 
 
-void Client::logMessage(QtMsgType type, const char *msg)
+void Client::refreshLegacyCoreInfo()
 {
-    fprintf(stderr, "%s\n", msg);
-    fflush(stderr);
-    if (type == QtFatalMsg) {
-        Quassel::logFatalMessage(msg);
-    }
-    else {
-        QString msgString = QString("%1\n").arg(msg);
+    instance()->requestLegacyCoreInfo();
+}
 
-        //Check to see if there is an instance around, else we risk recursions
-        //when calling instance() and creating new ones.
-        if (!instanceExists())
-            return;
 
-        instance()->_debugLog << msgString;
-        emit instance()->logUpdated(msgString);
-    }
+void Client::changePassword(const QString &oldPassword, const QString &newPassword) {
+    CoreAccount account = currentCoreAccount();
+    account.setPassword(newPassword);
+    coreAccountModel()->createOrUpdateAccount(account);
+    emit instance()->requestPasswordChange(nullptr, account.user(), oldPassword, newPassword);
+}
+
+
+void Client::kickClient(int peerId)
+{
+    emit instance()->requestKickClient(peerId);
+}
+
+
+void Client::corePasswordChanged(PeerPtr, bool success)
+{
+    if (success)
+        coreAccountModel()->save();
+    emit passwordChanged(success);
 }