From 39224c7bf72a6e371356e1e23cc4574d3a18116b Mon Sep 17 00:00:00 2001 From: pokamest Date: Wed, 24 Feb 2021 13:38:23 -0800 Subject: [PATCH] SingleApplication --- .../3rd/SingleApplication/singleapplication.h | 2 +- client/client.pro | 2 - client/main.cpp | 12 +-- client/runguard.cpp | 88 ------------------- client/runguard.h | 37 -------- 5 files changed, 2 insertions(+), 139 deletions(-) delete mode 100644 client/runguard.cpp delete mode 100644 client/runguard.h diff --git a/client/3rd/SingleApplication/singleapplication.h b/client/3rd/SingleApplication/singleapplication.h index 91cabf93..400c88ac 100644 --- a/client/3rd/SingleApplication/singleapplication.h +++ b/client/3rd/SingleApplication/singleapplication.h @@ -27,7 +27,7 @@ #include #ifndef QAPPLICATION_CLASS - #define QAPPLICATION_CLASS QCoreApplication + #define QAPPLICATION_CLASS QApplication #endif #include QT_STRINGIFY(QAPPLICATION_CLASS) diff --git a/client/client.pro b/client/client.pro index c6096816..3f913186 100644 --- a/client/client.pro +++ b/client/client.pro @@ -21,7 +21,6 @@ HEADERS += \ defines.h \ managementserver.h \ protocols/shadowsocksvpnprotocol.h \ - runguard.h \ settings.h \ ui/Controls/SlidingStackedWidget.h \ ui/mainwindow.h \ @@ -39,7 +38,6 @@ SOURCES += \ main.cpp \ managementserver.cpp \ protocols/shadowsocksvpnprotocol.cpp \ - runguard.cpp \ settings.cpp \ ui/Controls/SlidingStackedWidget.cpp \ ui/mainwindow.cpp \ diff --git a/client/main.cpp b/client/main.cpp index e3d28845..f08e66d4 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -6,13 +6,10 @@ #include "debug.h" #include "defines.h" -#include "runguard.h" +#include "singleapplication.h" #include "ui/mainwindow.h" -#define QAPPLICATION_CLASS QApplication -#include "singleapplication.h" - #ifdef Q_OS_WIN #include "Windows.h" #endif @@ -28,7 +25,6 @@ static void loadTranslator() int main(int argc, char *argv[]) { QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true); - //RunGuard::instance(APPLICATION_NAME).activate(); #ifdef Q_OS_WIN AllowSetForegroundWindow(ASFW_ANY); @@ -42,12 +38,6 @@ int main(int argc, char *argv[]) loadTranslator(); -// if (!RunGuard::instance().tryToRun()) { -// qDebug() << "Tried to run second instance. Exiting..."; -// QMessageBox::information(NULL, QObject::tr("Notification"), QObject::tr("AmneziaVPN is already running.")); -// return 0; -// } - QFontDatabase::addApplicationFont(":/fonts/Lato-Black.ttf"); QFontDatabase::addApplicationFont(":/fonts/Lato-BlackItalic.ttf"); QFontDatabase::addApplicationFont(":/fonts/Lato-Bold.ttf"); diff --git a/client/runguard.cpp b/client/runguard.cpp deleted file mode 100644 index 729c7ece..00000000 --- a/client/runguard.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "runguard.h" -#include - -namespace -{ - -QString generateKeyHash( const QString& key, const QString& salt ) -{ - QByteArray data; - - data.append( key.toUtf8() ); - data.append( salt.toUtf8() ); - data = QCryptographicHash::hash( data, QCryptographicHash::Sha1 ).toHex(); - - return data; -} - -} - -RunGuard::RunGuard(const QString& key) - : key( key ) - , memLockKey( generateKeyHash( key, "_memLockKey" ) ) - , sharedmemKey( generateKeyHash( key, "_sharedmemKey" ) ) - , sharedMem( sharedmemKey ) - , memLock( memLockKey, 1 ) -{ - qDebug() << "RunGuard::RunGuard key" << key; -} - -RunGuard &RunGuard::instance(const QString& key) -{ - static RunGuard s(key); - return s; -} - -void RunGuard::activate() -{ - memLock.acquire(); - { - QSharedMemory fix(sharedmemKey); // Fix for *nix: http://habrahabr.ru/post/173281/ - fix.attach(); - } - memLock.release(); -} - -RunGuard::~RunGuard() -{ - release(); -} - -bool RunGuard::isAnotherRunning() const -{ - if ( sharedMem.isAttached() ) - return false; - - memLock.acquire(); - const bool isRunning = sharedMem.attach(); - if ( isRunning ) - sharedMem.detach(); - memLock.release(); - - return isRunning; -} - -bool RunGuard::tryToRun() -{ - if ( isAnotherRunning() ) // Extra check - return false; - - memLock.acquire(); - const bool result = sharedMem.create( sizeof( quint64 ) ); - memLock.release(); - if ( !result ) - { - release(); - return false; - } - - return true; -} - -void RunGuard::release() -{ - memLock.acquire(); - if ( sharedMem.isAttached() ) - sharedMem.detach(); - memLock.release(); -} diff --git a/client/runguard.h b/client/runguard.h deleted file mode 100644 index 96df44a8..00000000 --- a/client/runguard.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef RUNGUARD_H -#define RUNGUARD_H - -#include -#include -#include -#include - -/** - * @brief The RunGuard class - The application single instance (via shared memory) - */ -class RunGuard -{ - -public: - static RunGuard &instance(const QString& key = QString()); - - ~RunGuard(); - - void activate(); - bool isAnotherRunning() const; - bool tryToRun(); - void release(); - -private: - RunGuard(const QString& key); - Q_DISABLE_COPY( RunGuard ) - - const QString key; - const QString memLockKey; - const QString sharedmemKey; - - mutable QSharedMemory sharedMem; - mutable QSystemSemaphore memLock; - -}; -#endif // RUNGUARD_H