Commit 0106e497 authored by Ronan Abhamon's avatar Ronan Abhamon

feat(src/app/App): provide a way to restart core and app

parent b19b0156
...@@ -89,6 +89,7 @@ App::App (int &argc, char *argv[]) : SingleApplication(argc, argv, true) { ...@@ -89,6 +89,7 @@ App::App (int &argc, char *argv[]) : SingleApplication(argc, argv, true) {
App::~App () { App::~App () {
qInfo() << QStringLiteral("Destroying app..."); qInfo() << QStringLiteral("Destroying app...");
delete mEngine;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -120,35 +121,56 @@ inline void activeSplashScreen (App *app) { ...@@ -120,35 +121,56 @@ inline void activeSplashScreen (App *app) {
} }
void App::initContentApp () { void App::initContentApp () {
// Destroy qml components and linphone core if necessary.
if (mEngine) {
qInfo() << QStringLiteral("Restarting app...");
delete mEngine;
mCallsWindow = nullptr;
mSettingsWindow = nullptr;
CoreManager::uninit();
} else {
// Don't quit if last window is closed!!!
setQuitOnLastWindowClosed(false);
QObject::connect(
this, &App::receivedMessage, this, [this](int, QByteArray message) {
if (message == "show")
App::smartShowWindow(getMainWindow());
}
);
}
// Init core. // Init core.
CoreManager::init(this, mParser.value("config")); CoreManager::init(this, mParser.value("config"));
qInfo() << "Activated selectors:" << QQmlFileSelector::get(&mEngine)->selector()->allSelectors();
// Init engine content.
mEngine = new QQmlApplicationEngine();
qInfo() << QStringLiteral("Activated selectors:") << QQmlFileSelector::get(mEngine)->selector()->allSelectors();
// Provide `+custom` folders for custom components. // Provide `+custom` folders for custom components.
(new QQmlFileSelector(&mEngine, this))->setExtraSelectors(QStringList("custom")); (new QQmlFileSelector(mEngine, mEngine))->setExtraSelectors(QStringList("custom"));
// Set modules paths. // Set modules paths.
mEngine.addImportPath(":/ui/modules"); mEngine->addImportPath(":/ui/modules");
mEngine.addImportPath(":/ui/scripts"); mEngine->addImportPath(":/ui/scripts");
mEngine.addImportPath(":/ui/views"); mEngine->addImportPath(":/ui/views");
// Provide avatars/thumbnails providers. // Provide avatars/thumbnails providers.
mEngine.addImageProvider(AvatarProvider::PROVIDER_ID, new AvatarProvider()); mEngine->addImageProvider(AvatarProvider::PROVIDER_ID, new AvatarProvider());
mEngine.addImageProvider(ThumbnailProvider::PROVIDER_ID, new ThumbnailProvider()); mEngine->addImageProvider(ThumbnailProvider::PROVIDER_ID, new ThumbnailProvider());
// Don't quit if last window is closed!!!
setQuitOnLastWindowClosed(false);
// Register types.
registerTypes(); registerTypes();
registerSharedTypes();
// Enable notifications. // Enable notifications.
mNotifier = new Notifier(this); createNotifier();
// Load main view. // Load main view.
qInfo() << "Loading main view..."; qInfo() << QStringLiteral("Loading main view...");
mEngine.load(QUrl(QML_VIEW_MAIN_WINDOW)); mEngine->load(QUrl(QML_VIEW_MAIN_WINDOW));
if (mEngine.rootObjects().isEmpty()) if (mEngine->rootObjects().isEmpty())
qFatal("Unable to open main window."); qFatal("Unable to open main window.");
// Load splashscreen. // Load splashscreen.
...@@ -159,13 +181,6 @@ void App::initContentApp () { ...@@ -159,13 +181,6 @@ void App::initContentApp () {
&CoreManager::linphoneCoreCreated, &CoreManager::linphoneCoreCreated,
this, mParser.isSet("selftest") ? &App::quit : &App::openAppAfterInit this, mParser.isSet("selftest") ? &App::quit : &App::openAppAfterInit
); );
QObject::connect(
this, &App::receivedMessage, this, [this](int, QByteArray message) {
if (message == "show")
App::smartShowWindow(getMainWindow());
}
);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -228,7 +243,7 @@ QQuickWindow *App::getCallsWindow () { ...@@ -228,7 +243,7 @@ QQuickWindow *App::getCallsWindow () {
QQuickWindow *App::getMainWindow () const { QQuickWindow *App::getMainWindow () const {
return qobject_cast<QQuickWindow *>( return qobject_cast<QQuickWindow *>(
const_cast<QQmlApplicationEngine *>(&mEngine)->rootObjects().at(0) const_cast<QQmlApplicationEngine *>(mEngine)->rootObjects().at(0)
); );
} }
...@@ -238,7 +253,7 @@ QQuickWindow *App::getSettingsWindow () { ...@@ -238,7 +253,7 @@ QQuickWindow *App::getSettingsWindow () {
QObject::connect( QObject::connect(
mSettingsWindow, &QWindow::visibilityChanged, this, [](QWindow::Visibility visibility) { mSettingsWindow, &QWindow::visibilityChanged, this, [](QWindow::Visibility visibility) {
if (visibility == QWindow::Hidden) { if (visibility == QWindow::Hidden) {
qInfo() << "Update nat policy."; qInfo() << QStringLiteral("Update nat policy.");
shared_ptr<linphone::Core> core = CoreManager::getInstance()->getCore(); shared_ptr<linphone::Core> core = CoreManager::getInstance()->getCore();
core->setNatPolicy(core->getNatPolicy()); core->setNatPolicy(core->getNatPolicy());
} }
...@@ -331,6 +346,10 @@ void App::registerTypes () { ...@@ -331,6 +346,10 @@ void App::registerTypes () {
registerUncreatableType(ContactModel, "ContactModel"); registerUncreatableType(ContactModel, "ContactModel");
registerUncreatableType(SipAddressObserver, "SipAddressObserver"); registerUncreatableType(SipAddressObserver, "SipAddressObserver");
registerUncreatableType(VcardModel, "VcardModel"); registerUncreatableType(VcardModel, "VcardModel");
}
void App::registerSharedTypes () {
qInfo() << QStringLiteral("Registering shared types...");
registerSharedSingletonType(App, "App", App::getInstance); registerSharedSingletonType(App, "App", App::getInstance);
registerSharedSingletonType(CoreManager, "CoreManager", CoreManager::getInstance); registerSharedSingletonType(CoreManager, "CoreManager", CoreManager::getInstance);
...@@ -348,7 +367,7 @@ void App::registerTypes () { ...@@ -348,7 +367,7 @@ void App::registerTypes () {
void App::setTrayIcon () { void App::setTrayIcon () {
QQuickWindow *root = getMainWindow(); QQuickWindow *root = getMainWindow();
QSystemTrayIcon *systemTrayIcon = new QSystemTrayIcon(root); QSystemTrayIcon *systemTrayIcon = new QSystemTrayIcon(mEngine);
// trayIcon: Right click actions. // trayIcon: Right click actions.
QAction *quitAction = new QAction("Quit", root); QAction *quitAction = new QAction("Quit", root);
...@@ -387,6 +406,13 @@ void App::setTrayIcon () { ...@@ -387,6 +406,13 @@ void App::setTrayIcon () {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void App::createNotifier () {
if (!mNotifier)
mNotifier = new Notifier(this);
}
// -----------------------------------------------------------------------------
QString App::getConfigLocale () const { QString App::getConfigLocale () const {
return ::Utils::linphoneStringToQString( return ::Utils::linphoneStringToQString(
CoreManager::getInstance()->getCore()->getConfig()->getString( CoreManager::getInstance()->getCore()->getConfig()->getString(
......
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "../components/notifier/Notifier.hpp" #include "../components/notifier/Notifier.hpp"
#include "../externals/single-application/SingleApplication.hpp" #include "../externals/single-application/SingleApplication.hpp"
#define APP_CODE_RESTART 1000
// ============================================================================= // =============================================================================
class DefaultTranslator; class DefaultTranslator;
...@@ -52,7 +54,7 @@ public: ...@@ -52,7 +54,7 @@ public:
void tryToUsePreferredLocale (); void tryToUsePreferredLocale ();
QQmlEngine *getEngine () { QQmlEngine *getEngine () {
return &mEngine; return mEngine;
} }
Notifier *getNotifier () const { Notifier *getNotifier () const {
...@@ -64,12 +66,16 @@ public: ...@@ -64,12 +66,16 @@ public:
bool hasFocus () const; bool hasFocus () const;
Q_INVOKABLE QQuickWindow *getSettingsWindow ();
static App *getInstance () { static App *getInstance () {
return static_cast<App *>(QApplication::instance()); return static_cast<App *>(QApplication::instance());
} }
Q_INVOKABLE void restart () {
exit(APP_CODE_RESTART);
}
Q_INVOKABLE QQuickWindow *getSettingsWindow ();
Q_INVOKABLE static void smartShowWindow (QQuickWindow *window); Q_INVOKABLE static void smartShowWindow (QQuickWindow *window);
Q_INVOKABLE static QString convertUrlToLocalPath (const QUrl &url); Q_INVOKABLE static QString convertUrlToLocalPath (const QUrl &url);
...@@ -81,7 +87,9 @@ signals: ...@@ -81,7 +87,9 @@ signals:
private: private:
void registerTypes (); void registerTypes ();
void registerSharedTypes ();
void setTrayIcon (); void setTrayIcon ();
void createNotifier ();
QString getConfigLocale () const; QString getConfigLocale () const;
void setConfigLocale (const QString &locale); void setConfigLocale (const QString &locale);
...@@ -103,7 +111,7 @@ private: ...@@ -103,7 +111,7 @@ private:
QVariantList mAvailableLocales; QVariantList mAvailableLocales;
QString mLocale; QString mLocale;
QQmlApplicationEngine mEngine; QQmlApplicationEngine *mEngine = nullptr;
DefaultTranslator *mTranslator = nullptr; DefaultTranslator *mTranslator = nullptr;
Notifier *mNotifier = nullptr; Notifier *mNotifier = nullptr;
......
...@@ -73,6 +73,13 @@ void CoreManager::init (QObject *parent, const QString &configPath) { ...@@ -73,6 +73,13 @@ void CoreManager::init (QObject *parent, const QString &configPath) {
QObject::connect(timer, &QTimer::timeout, mInstance, &CoreManager::iterate); QObject::connect(timer, &QTimer::timeout, mInstance, &CoreManager::iterate);
} }
void CoreManager::uninit () {
if (mInstance) {
delete mInstance;
mInstance = nullptr;
}
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
VcardModel *CoreManager::createDetachedVcardModel () { VcardModel *CoreManager::createDetachedVcardModel () {
......
...@@ -98,6 +98,7 @@ public: ...@@ -98,6 +98,7 @@ public:
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static void init (QObject *parent, const QString &configPath); static void init (QObject *parent, const QString &configPath);
static void uninit ();
static CoreManager *getInstance () { static CoreManager *getInstance () {
return mInstance; return mInstance;
......
...@@ -97,7 +97,12 @@ int main (int argc, char *argv[]) { ...@@ -97,7 +97,12 @@ int main (int argc, char *argv[]) {
// Init and run! // Init and run!
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
app.initContentApp();
qInfo() << QStringLiteral("Running app..."); qInfo() << QStringLiteral("Running app...");
return app.exec();
int ret;
do {
app.initContentApp();
ret = app.exec();
} while (ret == APP_CODE_RESTART);
return ret;
} }
import Common 1.0 import Common 1.0
import Linphone 1.0
// ============================================================================= // =============================================================================
AssistantAbstractView { AssistantAbstractView {
mainAction: (function () { mainAction: (function () {
console.log('TODO') App.restart()
}) })
mainActionEnabled: url.text.length > 0 mainActionEnabled: url.text.length > 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment