Commit 52cba384 authored by Ronan Abhamon's avatar Ronan Abhamon

feat(src/app/App): supports single instance

parent 15b5093b
...@@ -62,7 +62,7 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DQT_QML_DEBUG -DQT_DECLARAT ...@@ -62,7 +62,7 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DQT_QML_DEBUG -DQT_DECLARAT
# Define packages, libs, sources, headers, resources and languages. # Define packages, libs, sources, headers, resources and languages.
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
set(QT5_PACKAGES Core Gui Quick Widgets QuickControls2 Svg LinguistTools) set(QT5_PACKAGES Core Gui Quick Widgets QuickControls2 Svg LinguistTools Network)
find_package(Linphone REQUIRED) find_package(Linphone REQUIRED)
find_package(LinphoneCxx REQUIRED) find_package(LinphoneCxx REQUIRED)
......
...@@ -60,7 +60,7 @@ inline bool installLocale (App &app, QTranslator &translator, const QLocale &loc ...@@ -60,7 +60,7 @@ inline bool installLocale (App &app, QTranslator &translator, const QLocale &loc
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[]) : SingleApplication(argc, argv) {
setApplicationVersion("4.0"); setApplicationVersion("4.0");
setWindowIcon(QIcon(WINDOW_ICON_PATH)); setWindowIcon(QIcon(WINDOW_ICON_PATH));
......
...@@ -25,16 +25,17 @@ ...@@ -25,16 +25,17 @@
#include "../components/notifier/Notifier.hpp" #include "../components/notifier/Notifier.hpp"
#include <QApplication>
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QQuickWindow> #include <QQuickWindow>
#include "../externals/single-application/SingleApplication.hpp"
// ============================================================================= // =============================================================================
class DefaultTranslator; class DefaultTranslator;
class App : public QApplication { class App : public SingleApplication {
Q_OBJECT; Q_OBJECT;
Q_PROPERTY(QString configLocale READ getConfigLocale WRITE setConfigLocale NOTIFY configLocaleChanged); Q_PROPERTY(QString configLocale READ getConfigLocale WRITE setConfigLocale NOTIFY configLocaleChanged);
...@@ -42,7 +43,7 @@ class App : public QApplication { ...@@ -42,7 +43,7 @@ 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 (int &argc, char *argv[]);
~App (); ~App ();
void initContentApp (); void initContentApp ();
......
...@@ -65,7 +65,7 @@ SingleApplicationPrivate::~SingleApplicationPrivate () { ...@@ -65,7 +65,7 @@ SingleApplicationPrivate::~SingleApplicationPrivate () {
delete socket; delete socket;
} }
memory->lock(); memory->lock();
InstancesInfo *inst = (InstancesInfo *)memory->data(); InstancesInfo *inst = static_cast<InstancesInfo *>(memory->data());
if (server != nullptr) { if (server != nullptr) {
server->close(); server->close();
delete server; delete server;
...@@ -78,19 +78,19 @@ SingleApplicationPrivate::~SingleApplicationPrivate () { ...@@ -78,19 +78,19 @@ SingleApplicationPrivate::~SingleApplicationPrivate () {
void SingleApplicationPrivate::genBlockServerName (int timeout) { void SingleApplicationPrivate::genBlockServerName (int timeout) {
QCryptographicHash appData(QCryptographicHash::Sha256); QCryptographicHash appData(QCryptographicHash::Sha256);
appData.addData("SingleApplication", 17); appData.addData("SingleApplication", 17);
appData.addData(SingleApplication::app_t::applicationName().toUtf8()); appData.addData(QApplication::applicationName().toUtf8());
appData.addData(SingleApplication::app_t::organizationName().toUtf8()); appData.addData(QApplication::organizationName().toUtf8());
appData.addData(SingleApplication::app_t::organizationDomain().toUtf8()); appData.addData(QApplication::organizationDomain().toUtf8());
if (!(options & SingleApplication::Mode::ExcludeAppVersion)) { if (!(options & SingleApplication::Mode::ExcludeAppVersion)) {
appData.addData(SingleApplication::app_t::applicationVersion().toUtf8()); appData.addData(QApplication::applicationVersion().toUtf8());
} }
if (!(options & SingleApplication::Mode::ExcludeAppPath)) { if (!(options & SingleApplication::Mode::ExcludeAppPath)) {
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
appData.addData(SingleApplication::app_t::applicationFilePath().toLower().toUtf8()); appData.addData(QApplication::applicationFilePath().toLower().toUtf8());
#else #else
appData.addData(SingleApplication::app_t::applicationFilePath().toUtf8()); appData.addData(QApplication::applicationFilePath().toUtf8());
#endif // ifdef Q_OS_WIN #endif // ifdef Q_OS_WIN
} }
...@@ -157,7 +157,7 @@ void SingleApplicationPrivate::startPrimary (bool resetMemory) { ...@@ -157,7 +157,7 @@ void SingleApplicationPrivate::startPrimary (bool resetMemory) {
// Reset the number of connections // Reset the number of connections
memory->lock(); memory->lock();
InstancesInfo *inst = (InstancesInfo *)memory->data(); InstancesInfo *inst = static_cast<InstancesInfo *>(memory->data());
if (resetMemory) { if (resetMemory) {
inst->primary = true; inst->primary = true;
...@@ -207,7 +207,7 @@ void SingleApplicationPrivate::connectToPrimary (int msecs, char connectionType) ...@@ -207,7 +207,7 @@ void SingleApplicationPrivate::connectToPrimary (int msecs, char connectionType)
QByteArray initMsg = blockServerName.toLatin1(); QByteArray initMsg = blockServerName.toLatin1();
initMsg.append(connectionType); initMsg.append(connectionType);
initMsg.append((const char *)&instanceNumber, sizeof(quint32)); initMsg.append(reinterpret_cast<const char *>(&instanceNumber), sizeof(quint32));
initMsg.append(QByteArray::number(qChecksum(initMsg.constData(), initMsg.length()), 256)); initMsg.append(QByteArray::number(qChecksum(initMsg.constData(), initMsg.length()), 256));
socket->write(initMsg); socket->write(initMsg);
...@@ -283,7 +283,7 @@ void SingleApplicationPrivate::slotConnectionEstablished () { ...@@ -283,7 +283,7 @@ void SingleApplicationPrivate::slotConnectionEstablished () {
initMsg += connectionType; initMsg += connectionType;
tmp = nextConnSocket->read(sizeof(quint32)); tmp = nextConnSocket->read(sizeof(quint32));
const char *data = tmp.constData(); const char *data = tmp.constData();
instanceId = (quint32) * data; instanceId = static_cast<quint32>(*data);
initMsg += tmp; initMsg += tmp;
// Verify the checksum of the initMsg // Verify the checksum of the initMsg
QByteArray checksum = QByteArray::number( QByteArray checksum = QByteArray::number(
...@@ -356,7 +356,7 @@ void SingleApplicationPrivate::slotClientConnectionClosed (QLocalSocket *closedS ...@@ -356,7 +356,7 @@ void SingleApplicationPrivate::slotClientConnectionClosed (QLocalSocket *closedS
* @param {bool} allowSecondaryInstances * @param {bool} allowSecondaryInstances
*/ */
SingleApplication::SingleApplication (int &argc, char *argv[], bool allowSecondary, Options options, int timeout) SingleApplication::SingleApplication (int &argc, char *argv[], bool allowSecondary, Options options, int timeout)
: app_t(argc, argv), d_ptr(new SingleApplicationPrivate(this)) { : QApplication(argc, argv), d_ptr(new SingleApplicationPrivate(this)) {
Q_D(SingleApplication); Q_D(SingleApplication);
// Store the current mode of the program // Store the current mode of the program
...@@ -384,7 +384,7 @@ SingleApplication::SingleApplication (int &argc, char *argv[], bool allowSeconda ...@@ -384,7 +384,7 @@ SingleApplication::SingleApplication (int &argc, char *argv[], bool allowSeconda
// Attempt to attach to the memory segment // Attempt to attach to the memory segment
if (d->memory->attach()) { if (d->memory->attach()) {
d->memory->lock(); d->memory->lock();
InstancesInfo *inst = (InstancesInfo *)d->memory->data(); InstancesInfo *inst = static_cast<InstancesInfo *>(d->memory->data());
if (!inst->primary) { if (!inst->primary) {
d->startPrimary(false); d->startPrimary(false);
......
...@@ -25,15 +25,10 @@ ...@@ -25,15 +25,10 @@
#ifndef SINGLE_APPLICATION_H #ifndef SINGLE_APPLICATION_H
#define SINGLE_APPLICATION_H #define SINGLE_APPLICATION_H
#include <QApplication>
#include <QtCore/QtGlobal> #include <QtCore/QtGlobal>
#include <QtNetwork/QLocalSocket> #include <QtNetwork/QLocalSocket>
#ifndef QAPPLICATION_CLASS
#define QAPPLICATION_CLASS QCoreApplication
#endif // ifndef QAPPLICATION_CLASS
#include QT_STRINGIFY(QAPPLICATION_CLASS)
// ============================================================================= // =============================================================================
class SingleApplicationPrivate; class SingleApplicationPrivate;
...@@ -43,11 +38,9 @@ class SingleApplicationPrivate; ...@@ -43,11 +38,9 @@ class SingleApplicationPrivate;
* Application * Application
* @see QCoreApplication * @see QCoreApplication
*/ */
class SingleApplication : public QAPPLICATION_CLASS { class SingleApplication : public QApplication {
Q_OBJECT Q_OBJECT
typedef QAPPLICATION_CLASS app_t;
public: public:
/** /**
* @brief Mode of operation of SingleApplication. * @brief Mode of operation of SingleApplication.
...@@ -90,7 +83,7 @@ public: ...@@ -90,7 +83,7 @@ public:
* @see See the corresponding QAPPLICATION_CLASS constructor for reference * @see See the corresponding QAPPLICATION_CLASS constructor for reference
*/ */
explicit SingleApplication (int &argc, char *argv[], bool allowSecondary = false, Options options = Mode::User, int timeout = 100); explicit SingleApplication (int &argc, char *argv[], bool allowSecondary = false, Options options = Mode::User, int timeout = 100);
~SingleApplication (); virtual ~SingleApplication ();
/** /**
* @brief Returns if the instance is the primary instance * @brief Returns if the instance is the primary instance
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
*/ */
#include <iostream> #include <iostream>
#include "app/App.hpp" #include "app/App.hpp"
#include "app/Logger.hpp" #include "app/Logger.hpp"
......
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