beee2cd8d729ea51c5a712691225d9b1173ee172
[quassel.git] / src / main / monoapplication.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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 "monoapplication.h"
22
23 #include "client.h"
24 #include "core.h"
25 #include "coreapplication.h"
26 #include "internalpeer.h"
27 #include "logmessage.h"
28 #include "qtui.h"
29
30 class InternalPeer;
31
32 MonolithicApplication::MonolithicApplication(int& argc, char** argv)
33     : QtUiApplication(argc, argv)
34 {}
35
36 void MonolithicApplication::init()
37 {
38     QtUiApplication::init();
39
40     connect(Client::coreConnection(), &CoreConnection::connectToInternalCore, this, &MonolithicApplication::onConnectionRequest);
41
42     // If port is set, start internal core directly so external clients can connect
43     // This is useful in case the mono client re-gains remote connection capability,
44     // in which case the internal core would not have to be started by default.
45     if (Quassel::isOptionSet("port")) {
46         startInternalCore();
47     }
48 }
49
50 Quassel::QuitHandler MonolithicApplication::quitHandler()
51 {
52     return [this]() {
53         quInfo() << "Client shutting down...";
54         connect(_client.get(), &QObject::destroyed, this, &MonolithicApplication::onClientDestroyed);
55         _client.release()->deleteLater();
56     };
57 }
58
59 void MonolithicApplication::onClientDestroyed()
60 {
61     if (_core) {
62         connect(_core, &Core::shutdownComplete, this, &MonolithicApplication::onCoreShutdown);
63         _core->shutdown();
64     }
65     else {
66         QCoreApplication::quit();
67     }
68 }
69
70 void MonolithicApplication::onCoreShutdown()
71 {
72     if (_core) {
73         connect(_core, &QObject::destroyed, QCoreApplication::instance(), &QCoreApplication::quit);
74         _coreThread.quit();
75         _coreThread.wait();
76     }
77     else {
78         QCoreApplication::quit();
79     }
80 }
81
82 void MonolithicApplication::startInternalCore()
83 {
84     if (_core) {
85         // Already started
86         return;
87     }
88
89     // Start internal core in a separate thread, so it doesn't block the UI
90     _core = new Core{};
91     _core->moveToThread(&_coreThread);
92     connect(&_coreThread, &QThread::started, _core.data(), &Core::initAsync);
93     connect(&_coreThread, &QThread::finished, _core.data(), &QObject::deleteLater);
94
95     connect(this, &MonolithicApplication::connectInternalPeer, _core, &Core::connectInternalPeer);
96     connect(_core, &Core::sessionStateReceived, Client::coreConnection(), &CoreConnection::internalSessionStateReceived);
97
98     connect(_core.data(), &Core::dbUpgradeInProgress, Client::instance(), &Client::onDbUpgradeInProgress);
99     connect(_core.data(), &Core::exitRequested, Client::instance(), &Client::onExitRequested);
100
101     _coreThread.start();
102 }
103
104 void MonolithicApplication::onConnectionRequest(QPointer<InternalPeer> peer)
105 {
106     if (!_core) {
107         startInternalCore();
108     }
109
110     // While starting the core may take a while, the object itself is instantiated synchronously and the connections
111     // established, so it's safe to emit this immediately. The core will take care of queueing the request until
112     // initialization is complete.
113     emit connectInternalPeer(peer);
114 }