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 +++++++++-------- singleapplication_p.h | 4 ++-- 2 files changed, 11 insertions(+), 10 deletions(-) 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(); diff --git a/singleapplication_p.h b/singleapplication_p.h index 5161411..2682b87 100644 --- a/singleapplication_p.h +++ b/singleapplication_p.h @@ -70,9 +70,9 @@ public: Q_DECLARE_PUBLIC(SingleApplication) SingleApplicationPrivate( SingleApplication *q_ptr ); - ~SingleApplicationPrivate(); + ~SingleApplicationPrivate(); - QByteArray getUsername(); + QString getUsername(); void genBlockServerName(); void initializeMemoryBlock(); void startPrimary(); -- cgit v1.2.1 From 5fff2202b513f00e553e1ec955909db5815a189f Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Fri, 27 Mar 2020 08:00:14 +0100 Subject: Add SingleApplication::currentUser() --- CHANGELOG.md | 5 +++++ README.md | 8 ++++++++ singleapplication.cpp | 6 ++++++ singleapplication.h | 6 ++++++ 4 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2eea70c..103fa88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ Changelog ========= +__3.1.1a__ +---------- + +* Added currentUser() method that returns the user the current instance is running as. + __3.1.0a__ ---------- diff --git a/README.md b/README.md index d3071eb..1dbbb84 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,14 @@ QString SingleApplication::primaryUser() Returns the username the primary instance is running as. +--- + +```cpp +QString SingleApplication::currentUser() +``` + +Returns the username the current instance is running as. + ### Signals ```cpp diff --git a/singleapplication.cpp b/singleapplication.cpp index 26e4ca2..7c955aa 100644 --- a/singleapplication.cpp +++ b/singleapplication.cpp @@ -178,6 +178,12 @@ QString SingleApplication::primaryUser() return d->primaryUser(); } +QString SingleApplication::currentUser() +{ + Q_D(SingleApplication); + return d->getUsername(); +} + bool SingleApplication::sendMessage( QByteArray message, int timeout ) { Q_D(SingleApplication); diff --git a/singleapplication.h b/singleapplication.h index f91e3a2..14c9615 100644 --- a/singleapplication.h +++ b/singleapplication.h @@ -118,6 +118,12 @@ public: */ QString primaryUser(); + /** + * @brief Returns the username of the current user + * @returns {QString} + */ + QString currentUser(); + /** * @brief Sends a message to the primary instance. Returns true on success. * @param {int} timeout - Timeout for connecting -- 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(-) 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