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

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

parent b11088db
......@@ -31,14 +31,18 @@
#include "../utils.hpp"
#include "App.hpp"
#include "AvatarProvider.hpp"
#include "DefaultTranslator.hpp"
#include "Logger.hpp"
#include "ThumbnailProvider.hpp"
#include <QDir>
#include <QFileSelector>
#include <QMenu>
#include <QTimer>
#include <QQmlFileSelector>
#include <QSystemTrayIcon>
#include <QtDebug>
#include <QTimer>
#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<QQuickWindow *>(component.create());
QQuickWindow *window = qobject_cast<QQuickWindow *>(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();
}
......@@ -24,15 +24,11 @@
#define APP_H_
#include "../components/notifier/Notifier.hpp"
#include "AvatarProvider.hpp"
#include "ThumbnailProvider.hpp"
#include <QApplication>
#include <QCommandLineParser>
#include <QQmlApplicationEngine>
#include <QQmlFileSelector>
#include <QQuickWindow>
#include <QSystemTrayIcon>
// =============================================================================
......@@ -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<App *>(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_
......@@ -27,6 +27,7 @@
#include <QUuid>
#include "../../app/App.hpp"
#include "../../app/AvatarProvider.hpp"
#include "../../app/Paths.hpp"
#include "../../utils.hpp"
#include "../core/CoreManager.hpp"
......
......@@ -62,3 +62,12 @@ void CoreHandlers::onMessageReceived (
if (!app->hasFocus())
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:
const std::shared_ptr<linphone::ChatRoom> &room,
const std::shared_ptr<linphone::ChatMessage> &message
) 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_
......@@ -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<CoreHandlers>()) {
CoreManager::CoreManager (QObject *parent, const QString &config_path) : QObject(parent), m_handlers(make_shared<CoreHandlers>()) {
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);
......
......@@ -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 ();
......
......@@ -20,9 +20,12 @@
* Author: Ronan Abhamon
*/
#include <iostream>
#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();
}
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