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
|