summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2020-02-23 13:40:04 +0100
committerNicolas Werner <nicolas.werner@hotmail.de>2020-02-23 13:44:05 +0100
commite96241265e31264eaa96fda34c7a69c1f1ecf6c0 (patch)
tree7a3147527b52eef15021452ee8696c27061e6131 /src
parentFix Registration (diff)
downloadnheko-e96241265e31264eaa96fda34c7a69c1f1ecf6c0.tar.xz
Use third_party singleapplication helper
This may still not raise the application correctly on some WMs, because
they don't allow disturbing the user with windows popping up. I don't
think we want to work around that.

fixes #84
fixes #113
Diffstat (limited to 'src')
-rw-r--r--src/RunGuard.cpp84
-rw-r--r--src/RunGuard.h31
-rw-r--r--src/main.cpp26
3 files changed, 12 insertions, 129 deletions
diff --git a/src/RunGuard.cpp b/src/RunGuard.cpp
deleted file mode 100644

index 75833eb7..00000000 --- a/src/RunGuard.cpp +++ /dev/null
@@ -1,84 +0,0 @@ -#include "RunGuard.h" - -#include <QCryptographicHash> - -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) -{ - memLock.acquire(); - { - // Fix for *nix: http://habrahabr.ru/post/173281/ - QSharedMemory fix(sharedmemKey); - fix.attach(); - } - - memLock.release(); -} - -RunGuard::~RunGuard() { release(); } - -bool -RunGuard::isAnotherRunning() -{ - if (sharedMem.isAttached()) - return false; - - memLock.acquire(); - const bool isRunning = sharedMem.attach(); - - if (isRunning) - sharedMem.detach(); - - memLock.release(); - - return isRunning; -} - -bool -RunGuard::tryToRun() -{ - // Extra check - if (isAnotherRunning()) - 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/src/RunGuard.h b/src/RunGuard.h deleted file mode 100644
index f9a9641a..00000000 --- a/src/RunGuard.h +++ /dev/null
@@ -1,31 +0,0 @@ -#pragma once - -// -// Taken from -// https://stackoverflow.com/questions/5006547/qt-best-practice-for-a-single-instance-app-protection -// - -#include <QObject> -#include <QSharedMemory> -#include <QSystemSemaphore> - -class RunGuard -{ -public: - RunGuard(const QString &key); - ~RunGuard(); - - bool isAnotherRunning(); - bool tryToRun(); - void release(); - -private: - const QString key; - const QString memLockKey; - const QString sharedmemKey; - - QSharedMemory sharedMem; - QSystemSemaphore memLock; - - Q_DISABLE_COPY(RunGuard) -}; diff --git a/src/main.cpp b/src/main.cpp
index ed012a62..042ef8c0 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -37,9 +37,9 @@ #include "Logging.h" #include "MainWindow.h" #include "MatrixClient.h" -#include "RunGuard.h" #include "Utils.h" #include "config/nheko.h" +#include "singleapplication.h" #if defined(Q_OS_MAC) #include "emoji/MacHelper.h" @@ -104,18 +104,6 @@ createCacheDirectory() int main(int argc, char *argv[]) { - RunGuard guard("run_guard"); - - if (!guard.tryToRun()) { - QApplication a(argc, argv); - - QMessageBox msgBox; - msgBox.setText("Another instance of Nheko is running"); - msgBox.exec(); - - return 0; - } - #if defined(Q_OS_LINUX) || defined(Q_OS_WIN) || defined(Q_OS_FREEBSD) if (qgetenv("QT_SCALE_FACTOR").size() == 0) { float factor = utils::scaleFactor(); @@ -130,7 +118,12 @@ main(int argc, char *argv[]) QCoreApplication::setOrganizationName("nheko"); QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); - QApplication app(argc, argv); + SingleApplication app(argc, + argv, + false, + SingleApplication::Mode::User | + SingleApplication::Mode::ExcludeAppPath | + SingleApplication::Mode::ExcludeAppVersion); QCommandLineParser parser; parser.addHelpOption(); @@ -192,6 +185,11 @@ main(int argc, char *argv[]) nhlog::net()->debug("bye"); } }); + QObject::connect(&app, &SingleApplication::instanceStarted, &w, [&w]() { + w.show(); + w.raise(); + w.activateWindow(); + }); #if defined(Q_OS_MAC) // Temporary solution for the emoji picker until