From 16ae7c20437acad1b37fd632cfb0ea06309a8c63 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Mon, 27 Feb 2017 11:18:59 +0100 Subject: [PATCH] fix(src/app/App): destroy correctly app --- linphone-desktop/src/app/App.cpp | 82 ++++++++++++------- linphone-desktop/src/app/App.hpp | 26 +----- .../src/components/contact/VcardModel.cpp | 1 + .../src/components/core/CoreHandlers.cpp | 9 ++ .../src/components/core/CoreHandlers.hpp | 7 ++ .../src/components/core/CoreManager.cpp | 8 +- .../src/components/core/CoreManager.hpp | 4 +- linphone-desktop/src/main.cpp | 12 +-- 8 files changed, 86 insertions(+), 63 deletions(-) diff --git a/linphone-desktop/src/app/App.cpp b/linphone-desktop/src/app/App.cpp index 78b3dd40b..d4a448950 100644 --- a/linphone-desktop/src/app/App.cpp +++ b/linphone-desktop/src/app/App.cpp @@ -31,14 +31,18 @@ #include "../utils.hpp" #include "App.hpp" +#include "AvatarProvider.hpp" #include "DefaultTranslator.hpp" #include "Logger.hpp" +#include "ThumbnailProvider.hpp" #include #include #include -#include +#include +#include #include +#include #define DEFAULT_LOCALE "en" @@ -52,14 +56,13 @@ // ============================================================================= -App *App::m_instance = nullptr; - inline bool installLocale (App &app, QTranslator &translator, const QLocale &locale) { return translator.load(locale, LANGUAGES_PATH) && app.installTranslator(&translator); } App::App (int &argc, char **argv) : QApplication(argc, argv) { setApplicationVersion("4.0"); + setWindowIcon(QIcon(WINDOW_ICON_PATH)); // List available locales. for (const auto &locale : QDir(LANGUAGES_PATH).entryList()) @@ -82,29 +85,37 @@ App::App (int &argc, char **argv) : QApplication(argc, argv) { qInfo() << QStringLiteral("Use default locale: %1").arg(m_locale); } +App::~App () { + qInfo() << "Destroying app..."; +} + // ----------------------------------------------------------------------------- void App::initContentApp () { - // Provide avatars/thumbnails providers. - m_engine.addImageProvider(AvatarProvider::PROVIDER_ID, &m_avatar_provider); - m_engine.addImageProvider(ThumbnailProvider::PROVIDER_ID, &m_thumbnail_provider); - - setWindowIcon(QIcon(WINDOW_ICON_PATH)); + // Avoid double free. + m_engine.setObjectOwnership(this, QQmlEngine::CppOwnership); // 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. m_engine.addImportPath(":/ui/modules"); m_engine.addImportPath(":/ui/scripts"); 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!!! setQuitOnLastWindowClosed(false); // Init core. - CoreManager::init(m_parser.value("config")); + CoreManager::init(nullptr, m_parser.value("config")); qInfo() << "Core manager initialized."; qInfo() << "Activated selectors:" << QQmlFileSelector::get(&m_engine)->selector()->allSelectors(); @@ -130,14 +141,17 @@ void App::initContentApp () { } } - // Register types ans make sub windows. + // Register types and create sub-windows. registerTypes(); - createSubWindows(); // 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. qInfo() << "Loading main view..."; @@ -145,6 +159,8 @@ void App::initContentApp () { if (m_engine.rootObjects().isEmpty()) qFatal("Unable to open main window."); + createSubWindows(); + #ifndef __APPLE__ // Enable TrayIconSystem. if (!QSystemTrayIcon::isSystemTrayAvailable()) @@ -293,22 +309,28 @@ void App::registerTypes () { // ----------------------------------------------------------------------------- -inline QQuickWindow *createSubWindow (QQmlApplicationEngine &engine, const char *path) { - QQmlComponent component(&engine, QUrl(path)); +inline QQuickWindow *createSubWindow (App *app, const char *path) { + QQmlEngine *engine = app->getEngine(); + + QQmlComponent component(engine, QUrl(path)); if (component.isError()) { qWarning() << component.errors(); abort(); } - // Default Ownership is Cpp: http://doc.qt.io/qt-5/qqmlengine.html#ObjectOwnership-enum - return qobject_cast(component.create()); + QQuickWindow *window = qobject_cast(component.create()); + + QQmlEngine::setObjectOwnership(window, QQmlEngine::CppOwnership); + window->setParent(app->getMainWindow()); + + return window; } void App::createSubWindows () { qInfo() << "Create sub windows..."; - m_calls_window = createSubWindow(m_engine, QML_VIEW_CALLS_WINDOW); - m_settings_window = createSubWindow(m_engine, QML_VIEW_SETTINGS_WINDOW); + m_calls_window = createSubWindow(this, QML_VIEW_CALLS_WINDOW); + m_settings_window = createSubWindow(this, QML_VIEW_SETTINGS_WINDOW); } // ----------------------------------------------------------------------------- @@ -317,7 +339,7 @@ void App::setTrayIcon () { QQuickWindow *root = getMainWindow(); QMenu *menu = new QMenu(); - m_system_tray_icon = new QSystemTrayIcon(root); + QSystemTrayIcon *system_tray_icon = new QSystemTrayIcon(root); // trayIcon: Right click actions. QAction *quit_action = new QAction("Quit", root); @@ -328,7 +350,7 @@ void App::setTrayIcon () { // trayIcon: Left click actions. root->connect( - m_system_tray_icon, &QSystemTrayIcon::activated, [root]( + system_tray_icon, &QSystemTrayIcon::activated, [root]( QSystemTrayIcon::ActivationReason reason ) { if (reason == QSystemTrayIcon::Trigger) { @@ -345,10 +367,10 @@ void App::setTrayIcon () { menu->addSeparator(); menu->addAction(quit_action); - m_system_tray_icon->setContextMenu(menu); - m_system_tray_icon->setIcon(QIcon(WINDOW_ICON_PATH)); - m_system_tray_icon->setToolTip("Linphone"); - m_system_tray_icon->show(); + system_tray_icon->setContextMenu(menu); + system_tray_icon->setIcon(QIcon(WINDOW_ICON_PATH)); + system_tray_icon->setToolTip("Linphone"); + system_tray_icon->show(); } // ----------------------------------------------------------------------------- @@ -376,8 +398,8 @@ QString App::getLocale () const { // ----------------------------------------------------------------------------- void App::quit () { - if (m_parser.isSet("selftest")) { + if (m_parser.isSet("selftest")) cout << tr("selftestResult").toStdString() << endl; - } - QCoreApplication::quit(); + + QApplication::quit(); } diff --git a/linphone-desktop/src/app/App.hpp b/linphone-desktop/src/app/App.hpp index 92ee4ee83..d103740de 100644 --- a/linphone-desktop/src/app/App.hpp +++ b/linphone-desktop/src/app/App.hpp @@ -24,15 +24,11 @@ #define APP_H_ #include "../components/notifier/Notifier.hpp" -#include "AvatarProvider.hpp" -#include "ThumbnailProvider.hpp" #include #include #include -#include #include -#include // ============================================================================= @@ -46,6 +42,9 @@ class App : public QApplication { Q_PROPERTY(QVariantList availableLocales READ getAvailableLocales CONSTANT); public: + App (int &argc, char **argv); + ~App (); + void initContentApp (); void parseArgs (); @@ -64,15 +63,8 @@ public: 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 () { - return m_instance; + return static_cast(QApplication::instance()); } public slots: @@ -82,9 +74,6 @@ signals: void configLocaleChanged (const QString &locale); private: - App (int &argc, char **argv); - ~App () = default; - void registerTypes (); void createSubWindows (); void setTrayIcon (); @@ -100,11 +89,6 @@ private: QCommandLineParser m_parser; 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; @@ -115,8 +99,6 @@ private: QQuickWindow *m_calls_window = nullptr; QQuickWindow *m_settings_window = nullptr; - - static App *m_instance; }; #endif // APP_H_ diff --git a/linphone-desktop/src/components/contact/VcardModel.cpp b/linphone-desktop/src/components/contact/VcardModel.cpp index beafdf7b3..e64cfc6af 100644 --- a/linphone-desktop/src/components/contact/VcardModel.cpp +++ b/linphone-desktop/src/components/contact/VcardModel.cpp @@ -27,6 +27,7 @@ #include #include "../../app/App.hpp" +#include "../../app/AvatarProvider.hpp" #include "../../app/Paths.hpp" #include "../../utils.hpp" #include "../core/CoreManager.hpp" diff --git a/linphone-desktop/src/components/core/CoreHandlers.cpp b/linphone-desktop/src/components/core/CoreHandlers.cpp index 63594c44c..97b9e18b6 100644 --- a/linphone-desktop/src/components/core/CoreHandlers.cpp +++ b/linphone-desktop/src/components/core/CoreHandlers.cpp @@ -62,3 +62,12 @@ void CoreHandlers::onMessageReceived ( if (!app->hasFocus()) app->getNotifier()->notifyReceivedMessage(message); } + +void CoreHandlers::onRegistrationStateChanged ( + const shared_ptr &core, + const shared_ptr &config, + linphone::RegistrationState state, + const string &message +) { + // TODO. +} diff --git a/linphone-desktop/src/components/core/CoreHandlers.hpp b/linphone-desktop/src/components/core/CoreHandlers.hpp index d8102dcd0..2953ed964 100644 --- a/linphone-desktop/src/components/core/CoreHandlers.hpp +++ b/linphone-desktop/src/components/core/CoreHandlers.hpp @@ -56,6 +56,13 @@ private: const std::shared_ptr &room, const std::shared_ptr &message ) override; + + void onRegistrationStateChanged ( + const std::shared_ptr &core, + const std::shared_ptr &config, + linphone::RegistrationState state, + const std::string &message + ) override; }; #endif // CORE_HANDLERS_H_ diff --git a/linphone-desktop/src/components/core/CoreManager.cpp b/linphone-desktop/src/components/core/CoreManager.cpp index 4183c8e3d..13a27a11a 100644 --- a/linphone-desktop/src/components/core/CoreManager.cpp +++ b/linphone-desktop/src/components/core/CoreManager.cpp @@ -35,10 +35,10 @@ using namespace std; CoreManager *CoreManager::m_instance = nullptr; -CoreManager::CoreManager (const QString &configPath, QObject *parent) : QObject(parent), m_handlers(make_shared()) { +CoreManager::CoreManager (QObject *parent, const QString &config_path) : QObject(parent), m_handlers(make_shared()) { 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->usePreviewWindow(true); @@ -50,9 +50,9 @@ void CoreManager::enableHandlers () { m_cbs_timer->start(); } -void CoreManager::init (const QString &configPath) { +void CoreManager::init (QObject *parent, const QString &config_path) { 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_contacts_list_model = new ContactsListModel(m_instance); diff --git a/linphone-desktop/src/components/core/CoreManager.hpp b/linphone-desktop/src/components/core/CoreManager.hpp index ae90c040c..ca1129b5b 100644 --- a/linphone-desktop/src/components/core/CoreManager.hpp +++ b/linphone-desktop/src/components/core/CoreManager.hpp @@ -73,7 +73,7 @@ public: // Initialization. // --------------------------------------------------------------------------- - static void init (const QString &configPath); + static void init (QObject *parent, const QString &config_path); static CoreManager *getInstance () { return m_instance; @@ -86,7 +86,7 @@ public: Q_INVOKABLE VcardModel *createDetachedVcardModel (); private: - CoreManager (const QString &configPath, QObject *parent = Q_NULLPTR); + CoreManager (QObject *parent, const QString &config_path); void setDatabasesPaths (); void setResourcesPaths (); diff --git a/linphone-desktop/src/main.cpp b/linphone-desktop/src/main.cpp index 2bfbce435..0888684f2 100644 --- a/linphone-desktop/src/main.cpp +++ b/linphone-desktop/src/main.cpp @@ -20,9 +20,12 @@ * Author: Ronan Abhamon */ +#include #include "app/App.hpp" #include "app/Logger.hpp" +using namespace std; + // ============================================================================= int main (int argc, char *argv[]) { @@ -42,12 +45,11 @@ int main (int argc, char *argv[]) { * QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); */ - App::create(argc, argv); + App app(argc, argv); - App *app = App::getInstance(); - app->parseArgs(); - app->initContentApp(); + app.parseArgs(); + app.initContentApp(); // Run! - return app->exec(); + return app.exec(); }