X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fqtui%2Fmonoapplication.cpp;h=809f11472cf54d9f4a8698d25516c15ea00fced5;hp=a63ec55811cf7011f1158fb5b78c998a286ad11f;hb=a65f42197839da536975b3e2858eedcef420035f;hpb=1eb21546673535f5707aa6346e8c749b928cd772 diff --git a/src/qtui/monoapplication.cpp b/src/qtui/monoapplication.cpp index a63ec558..809f1147 100644 --- a/src/qtui/monoapplication.cpp +++ b/src/qtui/monoapplication.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-08 by the Quassel IRC Team * + * Copyright (C) 2005-2019 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -15,54 +15,107 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ #include "monoapplication.h" #include "coreapplication.h" #include "client.h" -#include "clientsyncer.h" #include "core.h" +#include "internalpeer.h" +#include "logmessage.h" #include "qtui.h" +class InternalPeer; + MonolithicApplication::MonolithicApplication(int &argc, char **argv) - : QtUiApplication(argc, argv), - _internalInitDone(false) + : QtUiApplication(argc, argv) { - _internal = new CoreApplicationInternal(); // needed for parser options - setRunMode(Monolithic); + Quassel::setRunMode(Quassel::Monolithic); } -bool MonolithicApplication::init() { - if(!Quassel::init()) // parse args - return false; - if(isOptionSet("port")) { - _internal->init(); - _internalInitDone = true; - } +void MonolithicApplication::init() +{ + QtUiApplication::init(); + + connect(Client::coreConnection(), SIGNAL(connectToInternalCore(QPointer)), this, SLOT(onConnectionRequest(QPointer))); + + // If port is set, start internal core directly so external clients can connect + // This is useful in case the mono client re-gains remote connection capability, + // in which case the internal core would not have to be started by default. + if (Quassel::isOptionSet("port")) { + startInternalCore(); + } +} + - connect(Client::instance(), SIGNAL(newClientSyncer(ClientSyncer *)), this, SLOT(newClientSyncer(ClientSyncer *))); - return QtUiApplication::init(); +Quassel::QuitHandler MonolithicApplication::quitHandler() +{ + return [this]() { + quInfo() << "Client shutting down..."; + connect(_client.get(), SIGNAL(destroyed()), this, SLOT(onClientDestroyed())); + _client.release()->deleteLater(); + }; +} + + +void MonolithicApplication::onClientDestroyed() +{ + if (_core) { + connect(_core, SIGNAL(shutdownComplete()), this, SLOT(onCoreShutdown())); + _core->shutdown(); + } + else { + QCoreApplication::quit(); + } } -MonolithicApplication::~MonolithicApplication() { - // Client needs to be destroyed first - Client::destroy(); - delete _internal; + +void MonolithicApplication::onCoreShutdown() +{ + if (_core) { + connect(_core, SIGNAL(destroyed()), QCoreApplication::instance(), SLOT(quit())); + _coreThread.quit(); + _coreThread.wait(); + } + else { + QCoreApplication::quit(); + } } -void MonolithicApplication::newClientSyncer(ClientSyncer *syncer) { - connect(syncer, SIGNAL(startInternalCore()), this, SLOT(startInternalCore())); + +void MonolithicApplication::startInternalCore() +{ + if (_core) { + // Already started + return; + } + + // Start internal core in a separate thread, so it doesn't block the UI + _core = new Core{}; + _core->moveToThread(&_coreThread); + connect(&_coreThread, SIGNAL(started()), _core, SLOT(initAsync())); + connect(&_coreThread, SIGNAL(finished()), _core, SLOT(deleteLater())); + + connect(this, SIGNAL(connectInternalPeer(QPointer)), _core, SLOT(connectInternalPeer(QPointer))); + connect(_core, SIGNAL(sessionState(Protocol::SessionState)), Client::coreConnection(), SLOT(internalSessionStateReceived(Protocol::SessionState))); + + connect(_core, SIGNAL(dbUpgradeInProgress(bool)), Client::instance(), SLOT(onDbUpgradeInProgress(bool))); + connect(_core, SIGNAL(exitRequested(int,QString)), Client::instance(), SLOT(onExitRequested(int,QString))); + + _coreThread.start(); } -void MonolithicApplication::startInternalCore() { - if(!_internalInitDone) { - _internal->init(); - _internalInitDone = true; - } - Core *core = Core::instance(); - ClientSyncer *syncer = static_cast(sender()); - connect(syncer, SIGNAL(connectToInternalCore(SignalProxy *)), core, SLOT(setupInternalClientSession(SignalProxy *))); - connect(core, SIGNAL(sessionState(const QVariant &)), syncer, SLOT(internalSessionStateReceived(const QVariant &))); + +void MonolithicApplication::onConnectionRequest(QPointer peer) +{ + if (!_core) { + startInternalCore(); + } + + // While starting the core may take a while, the object itself is instantiated synchronously and the connections + // established, so it's safe to emit this immediately. The core will take care of queueing the request until + // initialization is complete. + emit connectInternalPeer(peer); }