From 93371b871890d94aa87961d4a18abe388d39ace7 Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Tue, 21 Feb 2017 16:16:25 +0100 Subject: [PATCH] Add some command line options. --- linphone-desktop/assets/languages/en.ts | 23 ++++++ linphone-desktop/assets/languages/fr.ts | 23 ++++++ linphone-desktop/src/app/App.cpp | 76 ++++++++++++++----- linphone-desktop/src/app/App.hpp | 14 +++- linphone-desktop/src/app/Logger.cpp | 2 +- linphone-desktop/src/app/Logger.hpp | 6 +- linphone-desktop/src/app/Paths.cpp | 5 +- linphone-desktop/src/app/Paths.hpp | 3 +- .../src/components/core/CoreManager.cpp | 34 +++++---- .../src/components/core/CoreManager.hpp | 5 +- linphone-desktop/src/main.cpp | 4 +- 11 files changed, 149 insertions(+), 46 deletions(-) diff --git a/linphone-desktop/assets/languages/en.ts b/linphone-desktop/assets/languages/en.ts index 5085e9d97..2327cf44c 100644 --- a/linphone-desktop/assets/languages/en.ts +++ b/linphone-desktop/assets/languages/en.ts @@ -1,6 +1,29 @@ + + App + + Linphone seems to be running correctly + + + + Log to stdout some debug information while running. + + + + Specify the linphone configuration file to use. + + + + Run self test and exit 0 if it succeeded. + + + + A free SIP video-phone + + + CallControls diff --git a/linphone-desktop/assets/languages/fr.ts b/linphone-desktop/assets/languages/fr.ts index bc0b0ee92..cca1442f3 100644 --- a/linphone-desktop/assets/languages/fr.ts +++ b/linphone-desktop/assets/languages/fr.ts @@ -1,6 +1,29 @@ + + App + + Linphone seems to be running correctly + + + + Log to stdout some debug information while running. + + + + Specify the linphone configuration file to use. + + + + Run self test and exit 0 if it succeeded. + + + + A free SIP video-phone + + + CallControls diff --git a/linphone-desktop/src/app/App.cpp b/linphone-desktop/src/app/App.cpp index 4a9ad5fa1..f5a7f2858 100644 --- a/linphone-desktop/src/app/App.cpp +++ b/linphone-desktop/src/app/App.cpp @@ -31,12 +31,14 @@ #include "../components/timeline/TimelineModel.hpp" #include "App.hpp" +#include "Logger.hpp" #include #include #include #include #include +#include #include #define LANGUAGES_PATH ":/languages/" @@ -52,6 +54,8 @@ App *App::m_instance = nullptr; App::App (int &argc, char **argv) : QApplication(argc, argv) { + this->setApplicationVersion("4.0"); + if (m_english_translator.load(QLocale(QLocale::English), LANGUAGES_PATH)) installTranslator(&m_english_translator); else @@ -67,24 +71,6 @@ App::App (int &argc, char **argv) : QApplication(argc, argv) { qWarning() << QStringLiteral("Unable to found translations for locale: %1.") .arg(current_locale.name()); } - - // 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)); - - // Provide `+custom` folders for custom components. - m_file_selector = new QQmlFileSelector(&m_engine); - m_file_selector->setExtraSelectors(QStringList("custom")); - - // Set modules paths. - m_engine.addImportPath(":/ui/modules"); - m_engine.addImportPath(":/ui/scripts"); - m_engine.addImportPath(":/ui/views"); - - // Don't quit if last window is closed!!! - setQuitOnLastWindowClosed(false); } // ----------------------------------------------------------------------------- @@ -111,8 +97,26 @@ bool App::hasFocus () const { // ----------------------------------------------------------------------------- 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)); + + // Provide `+custom` folders for custom components. + m_file_selector = new QQmlFileSelector(&m_engine); + m_file_selector->setExtraSelectors(QStringList("custom")); + + // Set modules paths. + m_engine.addImportPath(":/ui/modules"); + m_engine.addImportPath(":/ui/scripts"); + m_engine.addImportPath(":/ui/views"); + + // Don't quit if last window is closed!!! + setQuitOnLastWindowClosed(false); + // Init core. - CoreManager::init(); + CoreManager::init(m_parser.value("config")); qInfo() << "Core manager initialized."; qInfo() << "Activated selectors:" << QQmlFileSelector::get(&m_engine)->selector()->allSelectors(); @@ -141,6 +145,29 @@ void App::initContentApp () { setTrayIcon(); #endif // ifndef __APPLE__ + + if (m_parser.isSet("selftest")) { + QTimer::singleShot(300, this, &App::quit); + } +} + +// ----------------------------------------------------------------------------- + +void App::parseArgs () { + m_parser.setApplicationDescription(tr("A free SIP video-phone")); + m_parser.addHelpOption(); + m_parser.addVersionOption(); + m_parser.addOptions({ + { "config", tr("Specify the linphone configuration file to use."), "file" }, + { "selftest", tr("Run self test and exit 0 if it succeeded.") }, + { { "V", "verbose" }, tr("Log to stdout some debug information while running.") } + }); + + m_parser.process(*this); + + if (m_parser.isSet("verbose")) { + Logger::instance()->setVerbose(true); + } } // ----------------------------------------------------------------------------- @@ -259,7 +286,7 @@ void App::setTrayIcon () { // trayIcon: Right click actions. QAction *quit_action = new QAction("Quit", root); - root->connect(quit_action, &QAction::triggered, qApp, &QCoreApplication::quit); + root->connect(quit_action, &QAction::triggered, this, &App::quit); QAction *restore_action = new QAction("Restore", root); root->connect(restore_action, &QAction::triggered, root, &QQuickWindow::showNormal); @@ -288,3 +315,12 @@ void App::setTrayIcon () { m_system_tray_icon->setToolTip("Linphone"); m_system_tray_icon->show(); } + +// ----------------------------------------------------------------------------- + +void App::quit () { + if (m_parser.isSet("selftest")) { + cout << tr("Linphone seems to be running correctly").toStdString() << endl; + } + QCoreApplication::quit(); +} diff --git a/linphone-desktop/src/app/App.hpp b/linphone-desktop/src/app/App.hpp index bf1339a89..f8527151b 100644 --- a/linphone-desktop/src/app/App.hpp +++ b/linphone-desktop/src/app/App.hpp @@ -29,6 +29,7 @@ #include "ThumbnailProvider.hpp" #include +#include #include #include #include @@ -55,15 +56,18 @@ public: Q_INVOKABLE QQuickWindow *getSettingsWindow () const; + void initContentApp (); + Q_INVOKABLE QString locale () const { return m_locale; } - static void init (int &argc, char **argv) { + void parseArgs (); + + static void create (int &argc, char **argv) { if (!m_instance) { // Instance must be exists before content. m_instance = new App(argc, argv); - m_instance->initContentApp(); } } @@ -71,16 +75,18 @@ public: return m_instance; } +public slots: + void quit (); + private: App (int &argc, char **argv); ~App () = default; - void initContentApp (); - void registerTypes (); void createSubWindows (); void setTrayIcon (); + QCommandLineParser m_parser; QQmlApplicationEngine m_engine; QQmlFileSelector *m_file_selector = nullptr; QSystemTrayIcon *m_system_tray_icon = nullptr; diff --git a/linphone-desktop/src/app/Logger.cpp b/linphone-desktop/src/app/Logger.cpp index 2d4da4b81..58a0fd4c0 100644 --- a/linphone-desktop/src/app/Logger.cpp +++ b/linphone-desktop/src/app/Logger.cpp @@ -136,7 +136,7 @@ void Logger::init () { linphone_core_set_log_level(ORTP_MESSAGE); linphone_core_set_log_handler( [](const char *domain, OrtpLogLevel type, const char *fmt, va_list args) { - if (m_instance->m_display_core_logs) + if (m_instance->isVerbose()) linphoneLogger(domain, type, fmt, args); } ); diff --git a/linphone-desktop/src/app/Logger.hpp b/linphone-desktop/src/app/Logger.hpp index 6967c0743..b74a9a119 100644 --- a/linphone-desktop/src/app/Logger.hpp +++ b/linphone-desktop/src/app/Logger.hpp @@ -30,11 +30,15 @@ class Logger { public: static void init (); + static Logger * instance () { return m_instance; }; + + bool isVerbose () const { return m_verbose; }; + void setVerbose (bool verbose) { m_verbose = verbose; }; private: Logger () = default; - bool m_display_core_logs = false; + bool m_verbose = false; static Logger *m_instance; }; diff --git a/linphone-desktop/src/app/Paths.cpp b/linphone-desktop/src/app/Paths.cpp index 58844fd98..665a9a27b 100644 --- a/linphone-desktop/src/app/Paths.cpp +++ b/linphone-desktop/src/app/Paths.cpp @@ -96,7 +96,10 @@ string Paths::getCallHistoryFilepath () { return getFilePath(MAIN_PATH + PATH_CALL_HISTORY_LIST); } -string Paths::getConfigFilepath () { +string Paths::getConfigFilepath (const QString &configPath) { + if (!configPath.isEmpty()) { + return getFilePath(QFileInfo(configPath).absoluteFilePath()); + } return getFilePath(MAIN_PATH + PATH_CONFIG); } diff --git a/linphone-desktop/src/app/Paths.hpp b/linphone-desktop/src/app/Paths.hpp index db9098ef5..968418f32 100644 --- a/linphone-desktop/src/app/Paths.hpp +++ b/linphone-desktop/src/app/Paths.hpp @@ -24,13 +24,14 @@ #define PATHS_H_ #include +#include // ============================================================================= namespace Paths { std::string getAvatarsDirpath (); std::string getCallHistoryFilepath (); - std::string getConfigFilepath (); + std::string getConfigFilepath (const QString &configPath); std::string getFriendsListFilepath (); std::string getCapturesDirPath (); std::string getLogsDirpath (); diff --git a/linphone-desktop/src/components/core/CoreManager.cpp b/linphone-desktop/src/components/core/CoreManager.cpp index 9b9429f4b..d1c4820e5 100644 --- a/linphone-desktop/src/components/core/CoreManager.cpp +++ b/linphone-desktop/src/components/core/CoreManager.cpp @@ -35,20 +35,10 @@ using namespace std; CoreManager *CoreManager::m_instance = nullptr; -CoreManager::CoreManager (QObject *parent) : QObject(parent), m_handlers(make_shared()) { - QDir dir(QCoreApplication::applicationDirPath()); - if (dir.dirName() == "MacOS") { - dir.cdUp(); - dir.cd("Resources"); - QDir mspluginsdir(dir); - mspluginsdir.cd("lib/mediastreamer/plugins"); - QDir datadir(dir); - datadir.cd("share"); - linphone::Factory::get()->setMspluginsDir(::Utils::qStringToLinphoneString(mspluginsdir.absolutePath())); - linphone::Factory::get()->setTopResourcesDir(::Utils::qStringToLinphoneString(datadir.absolutePath())); - } +CoreManager::CoreManager (const QString &configPath, QObject *parent) : QObject(parent), m_handlers(make_shared()) { + setResourcesPaths(); - m_core = linphone::Factory::get()->createCore(m_handlers, Paths::getConfigFilepath(), ""); + m_core = linphone::Factory::get()->createCore(m_handlers, Paths::getConfigFilepath(configPath), ""); m_core->setVideoDisplayFilter("MSOGL"); m_core->usePreviewWindow(true); @@ -60,9 +50,9 @@ void CoreManager::enableHandlers () { m_cbs_timer->start(); } -void CoreManager::init () { +void CoreManager::init (const QString &configPath) { if (!m_instance) { - m_instance = new CoreManager(); + m_instance = new CoreManager(configPath); m_instance->m_calls_list_model = new CallsListModel(m_instance); m_instance->m_contacts_list_model = new ContactsListModel(m_instance); @@ -88,3 +78,17 @@ void CoreManager::setDatabasesPaths () { m_core->setCallLogsDatabasePath(Paths::getCallHistoryFilepath()); m_core->setChatDatabasePath(Paths::getMessageHistoryFilepath()); } + +void CoreManager::setResourcesPaths () { + QDir dir(QCoreApplication::applicationDirPath()); + if (dir.dirName() == "MacOS") { + dir.cdUp(); + dir.cd("Resources"); + QDir mspluginsdir(dir); + mspluginsdir.cd("lib/mediastreamer/plugins"); + QDir datadir(dir); + datadir.cd("share"); + linphone::Factory::get()->setMspluginsDir(::Utils::qStringToLinphoneString(mspluginsdir.absolutePath())); + linphone::Factory::get()->setTopResourcesDir(::Utils::qStringToLinphoneString(datadir.absolutePath())); + } +} diff --git a/linphone-desktop/src/components/core/CoreManager.hpp b/linphone-desktop/src/components/core/CoreManager.hpp index fd0990332..8063caf1a 100644 --- a/linphone-desktop/src/components/core/CoreManager.hpp +++ b/linphone-desktop/src/components/core/CoreManager.hpp @@ -68,7 +68,7 @@ public: // Initialization. // --------------------------------------------------------------------------- - static void init (); + static void init (const QString &configPath); static CoreManager *getInstance () { return m_instance; @@ -81,9 +81,10 @@ public: Q_INVOKABLE VcardModel *createDetachedVcardModel (); private: - CoreManager (QObject *parent = Q_NULLPTR); + CoreManager (const QString &configPath, QObject *parent = Q_NULLPTR); void setDatabasesPaths (); + void setResourcesPaths (); std::shared_ptr m_core; std::shared_ptr m_handlers; diff --git a/linphone-desktop/src/main.cpp b/linphone-desktop/src/main.cpp index 259e6f2f2..11175a1f2 100644 --- a/linphone-desktop/src/main.cpp +++ b/linphone-desktop/src/main.cpp @@ -42,7 +42,9 @@ int main (int argc, char *argv[]) { * QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); */ - App::init(argc, argv); + App::create(argc, argv); + App::getInstance()->parseArgs(); + App::getInstance()->initContentApp(); // Run! return App::getInstance()->exec();