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