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