Merge pull request #97 from autoantwort/master

SingleApplication::currentUser and codecs
This commit is contained in:
Itay Grudev 2020-03-28 01:35:42 +00:00 committed by GitHub
commit 4baf2e74f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 11 deletions

View File

@ -1,6 +1,11 @@
Changelog Changelog
========= =========
__3.1.1a__
----------
* Added currentUser() method that returns the user the current instance is running as.
__3.1.0a__ __3.1.0a__
---------- ----------

View File

@ -214,6 +214,14 @@ QString SingleApplication::primaryUser()
Returns the username the primary instance is running as. Returns the username the primary instance is running as.
---
```cpp
QString SingleApplication::currentUser()
```
Returns the username the current instance is running as.
### Signals ### Signals
```cpp ```cpp

View File

@ -178,6 +178,12 @@ QString SingleApplication::primaryUser()
return d->primaryUser(); return d->primaryUser();
} }
QString SingleApplication::currentUser()
{
Q_D(SingleApplication);
return d->getUsername();
}
bool SingleApplication::sendMessage( QByteArray message, int timeout ) bool SingleApplication::sendMessage( QByteArray message, int timeout )
{ {
Q_D(SingleApplication); Q_D(SingleApplication);

View File

@ -118,6 +118,12 @@ public:
*/ */
QString primaryUser(); 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. * @brief Sends a message to the primary instance. Returns true on success.
* @param {int} timeout - Timeout for connecting * @param {int} timeout - Timeout for connecting

View File

@ -84,23 +84,33 @@ SingleApplicationPrivate::~SingleApplicationPrivate()
delete memory; delete memory;
} }
QByteArray SingleApplicationPrivate::getUsername(){ QString SingleApplicationPrivate::getUsername()
{
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
wchar_t username[UNLEN + 1]; wchar_t username[UNLEN + 1];
// Specifies size of the buffer on input // Specifies size of the buffer on input
DWORD usernameLength = UNLEN + 1; DWORD usernameLength = UNLEN + 1;
if( GetUserNameW( username, &usernameLength ) ) if( GetUserNameW( username, &usernameLength ) )
return QString::fromWCharArray( username ).toUtf8(); return QString::fromWCharArray( username );
return qgetenv( "USERNAME" ); #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
return QString::fromLocal8Bit( qgetenv( "USERNAME" ) );
#else
return qEnvironmentVariable( "USERNAME" );
#endif
#endif #endif
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
QByteArray username; QString username;
uid_t uid = geteuid(); uid_t uid = geteuid();
struct passwd *pw = getpwuid( uid ); struct passwd *pw = getpwuid( uid );
if( pw ) if( pw )
username = pw->pw_name; username = QString::fromLocal8Bit( pw->pw_name );
if( username.isEmpty() ) if ( username.isEmpty() ) {
username = qgetenv( "USER" ); #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
username = QString::fromLocal8Bit( qgetenv( "USER" ) );
#else
username = qEnvironmentVariable( "USER" );
#endif
}
return username; return username;
#endif #endif
} }
@ -127,7 +137,7 @@ void SingleApplicationPrivate::genBlockServerName()
// User level block requires a user specific data in the hash // User level block requires a user specific data in the hash
if( options & SingleApplication::Mode::User ) { 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 // Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with
@ -175,7 +185,7 @@ void SingleApplicationPrivate::startPrimary()
inst->primary = true; inst->primary = true;
inst->primaryPid = q->applicationPid(); inst->primaryPid = q->applicationPid();
strncpy( inst->primaryUser, getUsername().data(), 127 ); strncpy( inst->primaryUser, getUsername().toUtf8().data(), 127 );
inst->primaryUser[127] = '\0'; inst->primaryUser[127] = '\0';
inst->checksum = blockChecksum(); inst->checksum = blockChecksum();

View File

@ -70,9 +70,9 @@ public:
Q_DECLARE_PUBLIC(SingleApplication) Q_DECLARE_PUBLIC(SingleApplication)
SingleApplicationPrivate( SingleApplication *q_ptr ); SingleApplicationPrivate( SingleApplication *q_ptr );
~SingleApplicationPrivate(); ~SingleApplicationPrivate();
QByteArray getUsername(); QString getUsername();
void genBlockServerName(); void genBlockServerName();
void initializeMemoryBlock(); void initializeMemoryBlock();
void startPrimary(); void startPrimary();