fix:use a different socketname

This commit is contained in:
xiaoyifang 2023-05-05 21:38:29 +08:00
parent 62584afee8
commit ce8f5eeb7f
2 changed files with 10 additions and 23 deletions

View file

@ -252,7 +252,7 @@ int main( int argc, char ** argv )
newArgv[ argc ] = ARG_DISABLE_WEB_SECURITY; newArgv[ argc ] = ARG_DISABLE_WEB_SECURITY;
newArgv[ argc + 1 ] = nullptr; newArgv[ argc + 1 ] = nullptr;
QHotkeyApplication app( "GoldenDict", newArgc, newArgv ); QHotkeyApplication app( "GoldenDict-ng", newArgc, newArgv );
QHotkeyApplication::setApplicationName( "GoldenDict-ng" ); QHotkeyApplication::setApplicationName( "GoldenDict-ng" );
QHotkeyApplication::setOrganizationDomain( "https://github.com/xiaoyifang/goldendict-ng" ); QHotkeyApplication::setOrganizationDomain( "https://github.com/xiaoyifang/goldendict-ng" );

View file

@ -62,36 +62,22 @@ const char* QtLocalPeer::ack = "ack";
QtLocalPeer::QtLocalPeer(QObject* parent, const QString &appId) QtLocalPeer::QtLocalPeer(QObject* parent, const QString &appId)
: QObject(parent), id(appId) : QObject(parent), id(appId)
{ {
QString prefix = id;
if (id.isEmpty()) { if (id.isEmpty()) {
id = QCoreApplication::applicationFilePath(); id = QCoreApplication::applicationName();
#if defined(Q_OS_WIN)
id = id.toLower();
#endif
prefix = id.section(QLatin1Char('/'), -1);
} }
QString prefix = id;
#if defined( Q_OS_WIN )
prefix = prefix.toLower();
#endif
prefix.remove(QRegularExpression("[^a-zA-Z]")); prefix.remove(QRegularExpression("[^a-zA-Z]"));
prefix.truncate(6); prefix.truncate(6);
QByteArray idc = id.toUtf8(); QByteArray idc = QDir::home().dirName().toUtf8();
quint16 idNum = qChecksum(idc.constData(), idc.size()); quint16 idNum = qChecksum(idc.constData(), idc.size());
socketName = QLatin1String("qtsingleapp-") + prefix socketName = QLatin1String("single-") + prefix
+ QLatin1Char('-') + QString::number(idNum, 16); + QLatin1Char('-') + QString::number(idNum, 16);
#if defined(Q_OS_WIN)
if (!pProcessIdToSessionId) {
QLibrary lib("kernel32");
pProcessIdToSessionId = (PProcessIdToSessionId)lib.resolve("ProcessIdToSessionId");
}
if (pProcessIdToSessionId) {
DWORD sessionId = 0;
pProcessIdToSessionId(GetCurrentProcessId(), &sessionId);
socketName += QLatin1Char('-') + QString::number(sessionId, 16);
}
#else
socketName += QLatin1Char('-') + QString::number(::getuid(), 16);
#endif
server = new QLocalServer(this); server = new QLocalServer(this);
QString lockName = QDir(QDir::tempPath()).absolutePath() QString lockName = QDir(QDir::tempPath()).absolutePath()
+ QLatin1Char('/') + socketName + QLatin1Char('/') + socketName
@ -194,5 +180,6 @@ void QtLocalPeer::receiveConnection()
} }
QtLocalPeer::~QtLocalPeer() { QtLocalPeer::~QtLocalPeer() {
server->close();
lockFile->unlock(); // Ensure file unlocked lockFile->unlock(); // Ensure file unlocked
} }