Commit 16ae7c20 authored by Ronan Abhamon's avatar Ronan Abhamon

fix(src/app/App): destroy correctly app

parent b11088db
...@@ -31,14 +31,18 @@ ...@@ -31,14 +31,18 @@
#include "../utils.hpp" #include "../utils.hpp"
#include "App.hpp" #include "App.hpp"
#include "AvatarProvider.hpp"
#include "DefaultTranslator.hpp" #include "DefaultTranslator.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include "ThumbnailProvider.hpp"
#include <QDir> #include <QDir>
#include <QFileSelector> #include <QFileSelector>
#include <QMenu> #include <QMenu>
#include <QTimer> #include <QQmlFileSelector>
#include <QSystemTrayIcon>
#include <QtDebug> #include <QtDebug>
#include <QTimer>
#define DEFAULT_LOCALE "en" #define DEFAULT_LOCALE "en"
...@@ -52,14 +56,13 @@ ...@@ -52,14 +56,13 @@
// ============================================================================= // =============================================================================
App *App::m_instance = nullptr;
inline bool installLocale (App &app, QTranslator &translator, const QLocale &locale) { inline bool installLocale (App &app, QTranslator &translator, const QLocale &locale) {
return translator.load(locale, LANGUAGES_PATH) && app.installTranslator(&translator); return translator.load(locale, LANGUAGES_PATH) && app.installTranslator(&translator);
} }
App::App (int &argc, char **argv) : QApplication(argc, argv) { App::App (int &argc, char **argv) : QApplication(argc, argv) {
setApplicationVersion("4.0"); setApplicationVersion("4.0");
setWindowIcon(QIcon(WINDOW_ICON_PATH));
// List available locales. // List available locales.
for (const auto &locale : QDir(LANGUAGES_PATH).entryList()) for (const auto &locale : QDir(LANGUAGES_PATH).entryList())
...@@ -82,29 +85,37 @@ App::App (int &argc, char **argv) : QApplication(argc, argv) { ...@@ -82,29 +85,37 @@ App::App (int &argc, char **argv) : QApplication(argc, argv) {
qInfo() << QStringLiteral("Use default locale: %1").arg(m_locale); qInfo() << QStringLiteral("Use default locale: %1").arg(m_locale);
} }
App::~App () {
qInfo() << "Destroying app...";
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void App::initContentApp () { void App::initContentApp () {
// Provide avatars/thumbnails providers. // Avoid double free.
m_engine.addImageProvider(AvatarProvider::PROVIDER_ID, &m_avatar_provider); m_engine.setObjectOwnership(this, QQmlEngine::CppOwnership);
m_engine.addImageProvider(ThumbnailProvider::PROVIDER_ID, &m_thumbnail_provider);
setWindowIcon(QIcon(WINDOW_ICON_PATH));
// Provide `+custom` folders for custom components. // Provide `+custom` folders for custom components.
m_file_selector = new QQmlFileSelector(&m_engine); {
m_file_selector->setExtraSelectors(QStringList("custom")); QQmlFileSelector *file_selector = new QQmlFileSelector(&m_engine);
file_selector = new QQmlFileSelector(&m_engine);
file_selector->setExtraSelectors(QStringList("custom"));
}
// Set modules paths. // Set modules paths.
m_engine.addImportPath(":/ui/modules"); m_engine.addImportPath(":/ui/modules");
m_engine.addImportPath(":/ui/scripts"); m_engine.addImportPath(":/ui/scripts");
m_engine.addImportPath(":/ui/views"); m_engine.addImportPath(":/ui/views");
// Provide avatars/thumbnails providers.
m_engine.addImageProvider(AvatarProvider::PROVIDER_ID, new AvatarProvider());
m_engine.addImageProvider(ThumbnailProvider::PROVIDER_ID, new ThumbnailProvider());
// Don't quit if last window is closed!!! // Don't quit if last window is closed!!!
setQuitOnLastWindowClosed(false); setQuitOnLastWindowClosed(false);
// Init core. // Init core.
CoreManager::init(m_parser.value("config")); CoreManager::init(nullptr, m_parser.value("config"));
qInfo() << "Core manager initialized."; qInfo() << "Core manager initialized.";
qInfo() << "Activated selectors:" << QQmlFileSelector::get(&m_engine)->selector()->allSelectors(); qInfo() << "Activated selectors:" << QQmlFileSelector::get(&m_engine)->selector()->allSelectors();
...@@ -130,14 +141,17 @@ void App::initContentApp () { ...@@ -130,14 +141,17 @@ void App::initContentApp () {
} }
} }
// Register types ans make sub windows. // Register types and create sub-windows.
registerTypes(); registerTypes();
createSubWindows();
// Enable notifications. // Enable notifications.
m_notifier = new Notifier(); m_notifier = new Notifier(this);
CoreManager::getInstance()->enableHandlers(); {
CoreManager *core = CoreManager::getInstance();
core->enableHandlers();
core->setParent(this);
}
// Load main view. // Load main view.
qInfo() << "Loading main view..."; qInfo() << "Loading main view...";
...@@ -145,6 +159,8 @@ void App::initContentApp () { ...@@ -145,6 +159,8 @@ void App::initContentApp () {
if (m_engine.rootObjects().isEmpty()) if (m_engine.rootObjects().isEmpty())
qFatal("Unable to open main window."); qFatal("Unable to open main window.");
createSubWindows();
#ifndef __APPLE__ #ifndef __APPLE__
// Enable TrayIconSystem. // Enable TrayIconSystem.
if (!QSystemTrayIcon::isSystemTrayAvailable()) if (!QSystemTrayIcon::isSystemTrayAvailable())
...@@ -293,22 +309,28 @@ void App::registerTypes () { ...@@ -293,22 +309,28 @@ void App::registerTypes () {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
inline QQuickWindow *createSubWindow (QQmlApplicationEngine &engine, const char *path) { inline QQuickWindow *createSubWindow (App *app, const char *path) {
QQmlComponent component(&engine, QUrl(path)); QQmlEngine *engine = app->getEngine();
QQmlComponent component(engine, QUrl(path));
if (component.isError()) { if (component.isError()) {
qWarning() << component.errors(); qWarning() << component.errors();
abort(); abort();
} }
// Default Ownership is Cpp: http://doc.qt.io/qt-5/qqmlengine.html#ObjectOwnership-enum QQuickWindow *window = qobject_cast<QQuickWindow *>(component.create());
return qobject_cast<QQuickWindow *>(component.create());
QQmlEngine::setObjectOwnership(window, QQmlEngine::CppOwnership);
window->setParent(app->getMainWindow());
return window;
} }
void App::createSubWindows () { void App::createSubWindows () {
qInfo() << "Create sub windows..."; qInfo() << "Create sub windows...";
m_calls_window = createSubWindow(m_engine, QML_VIEW_CALLS_WINDOW); m_calls_window = createSubWindow(this, QML_VIEW_CALLS_WINDOW);
m_settings_window = createSubWindow(m_engine, QML_VIEW_SETTINGS_WINDOW); m_settings_window = createSubWindow(this, QML_VIEW_SETTINGS_WINDOW);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -317,7 +339,7 @@ void App::setTrayIcon () { ...@@ -317,7 +339,7 @@ void App::setTrayIcon () {
QQuickWindow *root = getMainWindow(); QQuickWindow *root = getMainWindow();
QMenu *menu = new QMenu(); QMenu *menu = new QMenu();
m_system_tray_icon = new QSystemTrayIcon(root); QSystemTrayIcon *system_tray_icon = new QSystemTrayIcon(root);
// trayIcon: Right click actions. // trayIcon: Right click actions.
QAction *quit_action = new QAction("Quit", root); QAction *quit_action = new QAction("Quit", root);
...@@ -328,7 +350,7 @@ void App::setTrayIcon () { ...@@ -328,7 +350,7 @@ void App::setTrayIcon () {
// trayIcon: Left click actions. // trayIcon: Left click actions.
root->connect( root->connect(
m_system_tray_icon, &QSystemTrayIcon::activated, [root]( system_tray_icon, &QSystemTrayIcon::activated, [root](
QSystemTrayIcon::ActivationReason reason QSystemTrayIcon::ActivationReason reason
) { ) {
if (reason == QSystemTrayIcon::Trigger) { if (reason == QSystemTrayIcon::Trigger) {
...@@ -345,10 +367,10 @@ void App::setTrayIcon () { ...@@ -345,10 +367,10 @@ void App::setTrayIcon () {
menu->addSeparator(); menu->addSeparator();
menu->addAction(quit_action); menu->addAction(quit_action);
m_system_tray_icon->setContextMenu(menu); system_tray_icon->setContextMenu(menu);
m_system_tray_icon->setIcon(QIcon(WINDOW_ICON_PATH)); system_tray_icon->setIcon(QIcon(WINDOW_ICON_PATH));
m_system_tray_icon->setToolTip("Linphone"); system_tray_icon->setToolTip("Linphone");
m_system_tray_icon->show(); system_tray_icon->show();
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -376,8 +398,8 @@ QString App::getLocale () const { ...@@ -376,8 +398,8 @@ QString App::getLocale () const {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void App::quit () { void App::quit () {
if (m_parser.isSet("selftest")) { if (m_parser.isSet("selftest"))
cout << tr("selftestResult").toStdString() << endl; cout << tr("selftestResult").toStdString() << endl;
}
QCoreApplication::quit(); QApplication::quit();
} }
...@@ -24,15 +24,11 @@ ...@@ -24,15 +24,11 @@
#define APP_H_ #define APP_H_
#include "../components/notifier/Notifier.hpp" #include "../components/notifier/Notifier.hpp"
#include "AvatarProvider.hpp"
#include "ThumbnailProvider.hpp"
#include <QApplication> #include <QApplication>
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QQmlFileSelector>
#include <QQuickWindow> #include <QQuickWindow>
#include <QSystemTrayIcon>
// ============================================================================= // =============================================================================
...@@ -46,6 +42,9 @@ class App : public QApplication { ...@@ -46,6 +42,9 @@ class App : public QApplication {
Q_PROPERTY(QVariantList availableLocales READ getAvailableLocales CONSTANT); Q_PROPERTY(QVariantList availableLocales READ getAvailableLocales CONSTANT);
public: public:
App (int &argc, char **argv);
~App ();
void initContentApp (); void initContentApp ();
void parseArgs (); void parseArgs ();
...@@ -64,15 +63,8 @@ public: ...@@ -64,15 +63,8 @@ public:
Q_INVOKABLE QQuickWindow *getSettingsWindow () const; Q_INVOKABLE QQuickWindow *getSettingsWindow () const;
static void create (int &argc, char **argv) {
if (!m_instance) {
// Instance must be exists before content.
m_instance = new App(argc, argv);
}
}
static App *getInstance () { static App *getInstance () {
return m_instance; return static_cast<App *>(QApplication::instance());
} }
public slots: public slots:
...@@ -82,9 +74,6 @@ signals: ...@@ -82,9 +74,6 @@ signals:
void configLocaleChanged (const QString &locale); void configLocaleChanged (const QString &locale);
private: private:
App (int &argc, char **argv);
~App () = default;
void registerTypes (); void registerTypes ();
void createSubWindows (); void createSubWindows ();
void setTrayIcon (); void setTrayIcon ();
...@@ -100,11 +89,6 @@ private: ...@@ -100,11 +89,6 @@ private:
QCommandLineParser m_parser; QCommandLineParser m_parser;
QQmlApplicationEngine m_engine; QQmlApplicationEngine m_engine;
QQmlFileSelector *m_file_selector = nullptr;
QSystemTrayIcon *m_system_tray_icon = nullptr;
AvatarProvider m_avatar_provider;
ThumbnailProvider m_thumbnail_provider;
DefaultTranslator *m_translator = nullptr; DefaultTranslator *m_translator = nullptr;
...@@ -115,8 +99,6 @@ private: ...@@ -115,8 +99,6 @@ private:
QQuickWindow *m_calls_window = nullptr; QQuickWindow *m_calls_window = nullptr;
QQuickWindow *m_settings_window = nullptr; QQuickWindow *m_settings_window = nullptr;
static App *m_instance;
}; };
#endif // APP_H_ #endif // APP_H_
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <QUuid> #include <QUuid>
#include "../../app/App.hpp" #include "../../app/App.hpp"
#include "../../app/AvatarProvider.hpp"
#include "../../app/Paths.hpp" #include "../../app/Paths.hpp"
#include "../../utils.hpp" #include "../../utils.hpp"
#include "../core/CoreManager.hpp" #include "../core/CoreManager.hpp"
......
...@@ -62,3 +62,12 @@ void CoreHandlers::onMessageReceived ( ...@@ -62,3 +62,12 @@ void CoreHandlers::onMessageReceived (
if (!app->hasFocus()) if (!app->hasFocus())
app->getNotifier()->notifyReceivedMessage(message); app->getNotifier()->notifyReceivedMessage(message);
} }
void CoreHandlers::onRegistrationStateChanged (
const shared_ptr<linphone::Core> &core,
const shared_ptr<linphone::ProxyConfig> &config,
linphone::RegistrationState state,
const string &message
) {
// TODO.
}
...@@ -56,6 +56,13 @@ private: ...@@ -56,6 +56,13 @@ private:
const std::shared_ptr<linphone::ChatRoom> &room, const std::shared_ptr<linphone::ChatRoom> &room,
const std::shared_ptr<linphone::ChatMessage> &message const std::shared_ptr<linphone::ChatMessage> &message
) override; ) override;
void onRegistrationStateChanged (
const std::shared_ptr<linphone::Core> &core,
const std::shared_ptr<linphone::ProxyConfig> &config,
linphone::RegistrationState state,
const std::string &message
) override;
}; };
#endif // CORE_HANDLERS_H_ #endif // CORE_HANDLERS_H_
...@@ -35,10 +35,10 @@ using namespace std; ...@@ -35,10 +35,10 @@ using namespace std;
CoreManager *CoreManager::m_instance = nullptr; CoreManager *CoreManager::m_instance = nullptr;
CoreManager::CoreManager (const QString &configPath, QObject *parent) : QObject(parent), m_handlers(make_shared<CoreHandlers>()) { CoreManager::CoreManager (QObject *parent, const QString &config_path) : QObject(parent), m_handlers(make_shared<CoreHandlers>()) {
setResourcesPaths(); setResourcesPaths();
m_core = linphone::Factory::get()->createCore(m_handlers, Paths::getConfigFilepath(configPath), ""); m_core = linphone::Factory::get()->createCore(m_handlers, Paths::getConfigFilepath(config_path), "");
m_core->setVideoDisplayFilter("MSOGL"); m_core->setVideoDisplayFilter("MSOGL");
m_core->usePreviewWindow(true); m_core->usePreviewWindow(true);
...@@ -50,9 +50,9 @@ void CoreManager::enableHandlers () { ...@@ -50,9 +50,9 @@ void CoreManager::enableHandlers () {
m_cbs_timer->start(); m_cbs_timer->start();
} }
void CoreManager::init (const QString &configPath) { void CoreManager::init (QObject *parent, const QString &config_path) {
if (!m_instance) { if (!m_instance) {
m_instance = new CoreManager(configPath); m_instance = new CoreManager(parent, config_path);
m_instance->m_calls_list_model = new CallsListModel(m_instance); m_instance->m_calls_list_model = new CallsListModel(m_instance);
m_instance->m_contacts_list_model = new ContactsListModel(m_instance); m_instance->m_contacts_list_model = new ContactsListModel(m_instance);
......
...@@ -73,7 +73,7 @@ public: ...@@ -73,7 +73,7 @@ public:
// Initialization. // Initialization.
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
static void init (const QString &configPath); static void init (QObject *parent, const QString &config_path);
static CoreManager *getInstance () { static CoreManager *getInstance () {
return m_instance; return m_instance;
...@@ -86,7 +86,7 @@ public: ...@@ -86,7 +86,7 @@ public:
Q_INVOKABLE VcardModel *createDetachedVcardModel (); Q_INVOKABLE VcardModel *createDetachedVcardModel ();
private: private:
CoreManager (const QString &configPath, QObject *parent = Q_NULLPTR); CoreManager (QObject *parent, const QString &config_path);
void setDatabasesPaths (); void setDatabasesPaths ();
void setResourcesPaths (); void setResourcesPaths ();
......
...@@ -20,9 +20,12 @@ ...@@ -20,9 +20,12 @@
* Author: Ronan Abhamon * Author: Ronan Abhamon
*/ */
#include <iostream>
#include "app/App.hpp" #include "app/App.hpp"
#include "app/Logger.hpp" #include "app/Logger.hpp"
using namespace std;
// ============================================================================= // =============================================================================
int main (int argc, char *argv[]) { int main (int argc, char *argv[]) {
...@@ -42,12 +45,11 @@ int main (int argc, char *argv[]) { ...@@ -42,12 +45,11 @@ int main (int argc, char *argv[]) {
* QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); * QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
*/ */
App::create(argc, argv); App app(argc, argv);
App *app = App::getInstance(); app.parseArgs();
app->parseArgs(); app.initContentApp();
app->initContentApp();
// Run! // Run!
return app->exec(); return app.exec();
} }
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