#include "coreapplication.h"
#include "client.h"
#include "core.h"
+#include "internalpeer.h"
+#include "logmessage.h"
#include "qtui.h"
class InternalPeer;
MonolithicApplication::MonolithicApplication(int &argc, char **argv)
: QtUiApplication(argc, argv)
{
-#if defined(HAVE_KDE4) || defined(Q_OS_MAC)
- Quassel::disableCrashHandler();
-#endif /* HAVE_KDE4 || Q_OS_MAC */
+}
+
+
+void MonolithicApplication::init()
+{
+ QtUiApplication::init();
+
+ connect(Client::coreConnection(), SIGNAL(connectToInternalCore(QPointer<InternalPeer>)), this, SLOT(onConnectionRequest(QPointer<InternalPeer>)));
- Quassel::setRunMode(Quassel::Monolithic);
+ // 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();
+ }
}
-bool MonolithicApplication::init()
+Quassel::QuitHandler MonolithicApplication::quitHandler()
{
- if (!QtUiApplication::init())
- return false;
+ return [this]() {
+ quInfo() << "Client shutting down...";
+ connect(_client.get(), SIGNAL(destroyed()), this, SLOT(onClientDestroyed()));
+ _client.release()->deleteLater();
+ };
+}
- connect(Client::coreConnection(), SIGNAL(startInternalCore()), SLOT(startInternalCore()));
- return true;
+void MonolithicApplication::onClientDestroyed()
+{
+ if (_core) {
+ connect(_core, SIGNAL(shutdownComplete()), this, SLOT(onCoreShutdown()));
+ _core->shutdown();
+ }
+ else {
+ QCoreApplication::quit();
+ }
}
-MonolithicApplication::~MonolithicApplication()
+void MonolithicApplication::onCoreShutdown()
{
- // Client needs to be destroyed first
- Client::destroy();
- _core.reset();
- Quassel::destroy();
+ if (_core) {
+ connect(_core, SIGNAL(destroyed()), QCoreApplication::instance(), SLOT(quit()));
+ _coreThread.quit();
+ _coreThread.wait();
+ }
+ else {
+ QCoreApplication::quit();
+ }
}
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<InternalPeer>)), _core, SLOT(connectInternalPeer(QPointer<InternalPeer>)));
+ 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::onConnectionRequest(QPointer<InternalPeer> peer)
{
if (!_core) {
- _core.reset(new Core{}); // FIXME C++14: std::make_unique
- Core::instance()->init();
+ startInternalCore();
}
- connect(Client::coreConnection(), SIGNAL(connectToInternalCore(InternalPeer*)), Core::instance(), SLOT(setupInternalClientSession(InternalPeer*)));
- connect(Core::instance(), SIGNAL(sessionState(Protocol::SessionState)), Client::coreConnection(), SLOT(internalSessionStateReceived(Protocol::SessionState)));
+
+ // 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);
}