Skip to content

Commit

Permalink
Merge pull request #97 from autoantwort/master
Browse files Browse the repository at this point in the history
SingleApplication::currentUser and codecs
  • Loading branch information
itay-grudev committed Mar 28, 2020
2 parents 4abe20a + 00a2530 commit 4baf2e7
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

__3.1.1a__
----------

* Added currentUser() method that returns the user the current instance is running as.

__3.1.0a__
----------

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions singleapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions singleapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ class SingleApplication : public QAPPLICATION_CLASS
*/
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
Expand Down
28 changes: 19 additions & 9 deletions singleapplication_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,33 @@ 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 );
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
return QString::fromLocal8Bit( qgetenv( "USERNAME" ) );
#else
return qEnvironmentVariable( "USERNAME" );
#endif
#endif
#ifdef Q_OS_UNIX
QByteArray username;
QString username;
uid_t uid = geteuid();
struct passwd *pw = getpwuid( uid );
if( pw )
username = pw->pw_name;
if( username.isEmpty() )
username = qgetenv( "USER" );
username = QString::fromLocal8Bit( pw->pw_name );
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
}
Expand All @@ -127,7 +137,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
Expand Down Expand Up @@ -175,7 +185,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();

Expand Down
4 changes: 2 additions & 2 deletions singleapplication_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ Q_OBJECT
Q_DECLARE_PUBLIC(SingleApplication)

SingleApplicationPrivate( SingleApplication *q_ptr );
~SingleApplicationPrivate();
~SingleApplicationPrivate();

QByteArray getUsername();
QString getUsername();
void genBlockServerName();
void initializeMemoryBlock();
void startPrimary();
Expand Down

0 comments on commit 4baf2e7

Please sign in to comment.