From 49c282a64c2e357185cce851f7ce02f426a58afb Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Fri, 27 Mar 2020 07:52:48 +0100 Subject: The codec for string from qgetenv and pw->pw_name is not necessarily utf8. So use QString::fromLocal8Bit and QStrings. --- singleapplication_p.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'singleapplication_p.cpp') diff --git a/singleapplication_p.cpp b/singleapplication_p.cpp index 8eb49ab..705a980 100644 --- a/singleapplication_p.cpp +++ b/singleapplication_p.cpp @@ -84,23 +84,24 @@ SingleApplicationPrivate::~SingleApplicationPrivate() delete memory; } -QByteArray SingleApplicationPrivate::getUsername(){ +QString SingleApplicationPrivate::getUsername() +{ #ifdef Q_OS_WIN wchar_t username[UNLEN + 1]; // Specifies size of the buffer on input DWORD usernameLength = UNLEN + 1; if( GetUserNameW( username, &usernameLength ) ) - return QString::fromWCharArray( username ).toUtf8(); - return qgetenv( "USERNAME" ); + return QString::fromWCharArray( username ); + return qEnvironmentVariable( "USERNAME" ); #endif #ifdef Q_OS_UNIX - QByteArray username; + QString username; uid_t uid = geteuid(); struct passwd *pw = getpwuid( uid ); if( pw ) - username = pw->pw_name; + username = QString::fromLocal8Bit( pw->pw_name ); if( username.isEmpty() ) - username = qgetenv( "USER" ); + username = qEnvironmentVariable( "USER" ); return username; #endif } @@ -127,7 +128,7 @@ void SingleApplicationPrivate::genBlockServerName() // User level block requires a user specific data in the hash if( options & SingleApplication::Mode::User ) { - appData.addData( getUsername() ); + appData.addData( getUsername().toUtf8() ); } // Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with @@ -175,7 +176,7 @@ void SingleApplicationPrivate::startPrimary() inst->primary = true; inst->primaryPid = q->applicationPid(); - strncpy( inst->primaryUser, getUsername().data(), 127 ); + strncpy( inst->primaryUser, getUsername().toUtf8().data(), 127 ); inst->primaryUser[127] = '\0'; inst->checksum = blockChecksum(); -- cgit v1.2.1 From 00a2530465d9089b314d3f6fddaaae383db6c22d Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Fri, 27 Mar 2020 08:19:41 +0100 Subject: Add support for Qt < 5.10 --- singleapplication_p.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'singleapplication_p.cpp') diff --git a/singleapplication_p.cpp b/singleapplication_p.cpp index 705a980..ce50383 100644 --- a/singleapplication_p.cpp +++ b/singleapplication_p.cpp @@ -92,16 +92,25 @@ QString SingleApplicationPrivate::getUsername() DWORD usernameLength = UNLEN + 1; if( GetUserNameW( username, &usernameLength ) ) return QString::fromWCharArray( username ); +#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) + return QString::fromLocal8Bit( qgetenv( "USERNAME" ) ); +#else return qEnvironmentVariable( "USERNAME" ); #endif +#endif #ifdef Q_OS_UNIX QString username; uid_t uid = geteuid(); struct passwd *pw = getpwuid( uid ); if( pw ) username = QString::fromLocal8Bit( pw->pw_name ); - if( username.isEmpty() ) + if ( username.isEmpty() ) { +#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0) + username = QString::fromLocal8Bit( qgetenv( "USER" ) ); +#else username = qEnvironmentVariable( "USER" ); +#endif + } return username; #endif } -- cgit v1.2.1