Disable multi instances and add mainwindow icon.

This commit is contained in:
Hork 2019-04-18 08:02:45 +08:00
parent ee2c126a6a
commit ab7047a1f8
5 changed files with 119 additions and 3 deletions

View File

@ -34,7 +34,8 @@ SOURCES += \
vinteract.cpp \
db.cpp \
vmess.cpp \
utils.cpp
utils.cpp \
src/runguard.cpp
HEADERS += \
mainwindow.h \
@ -43,7 +44,8 @@ HEADERS += \
vinteract.h \
db.h \
vmess.h \
utils.h
utils.h \
src/runguard.h
FORMS += \
mainwindow.ui \

View File

@ -8,7 +8,8 @@
#include <QSqlQuery>
#include <QSqlError>
#include <QFileInfo>
#include <QMessageBox>
#include "runguard.h"
void init()
{
@ -40,6 +41,11 @@ void init()
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
RunGuard guard("Hv2ray");
if(!guard.tryToRun()) {
QMessageBox::critical(0, "Already running", "Another instance of Hv2ray is already running!", QMessageBox::Ok | QMessageBox::Default);
return 0;
}
init();
MainWindow w;
w.show();

View File

@ -17,6 +17,7 @@ MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
this->setWindowIcon(QIcon("Himeki.ico"));
ui->setupUi(this);
updateConfTable();
ui->configTable->setContextMenuPolicy(Qt::CustomContextMenu);

76
src/runguard.cpp Normal file
View File

@ -0,0 +1,76 @@
#include "runguard.h"
#include <QCryptographicHash>
//from https://stackoverflow.com/a/28172162
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();
{
QSharedMemory fix( sharedmemKey ); // Fix for *nix: http://habrahabr.ru/post/173281/
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()
{
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();
}

31
src/runguard.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef RUNGUARD_H
#define RUNGUARD_H
#include <QObject>
#include <QSharedMemory>
#include <QSystemSemaphore>
//from https://stackoverflow.com/a/28172162
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 )
};
#endif // RUNGUARD_H