aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md5
-rw-r--r--README.md8
-rw-r--r--meson.build2
-rw-r--r--singleapplication.cpp6
-rw-r--r--singleapplication.h6
-rw-r--r--singleapplication_p.cpp28
-rw-r--r--singleapplication_p.h4
7 files changed, 47 insertions, 12 deletions
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/meson.build b/meson.build
index c85b439..633c39f 100644
--- a/meson.build
+++ b/meson.build
@@ -1,5 +1,5 @@
project('SingleApplication', ['cpp'],
- version: '3.1.0b',
+ version: '3.1.1a',
default_options: ['cpp_std=c++2a', 'warning_level=3'],
license: 'MIT',
)
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
@@ -119,6 +119,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
* @returns {bool}
diff --git a/singleapplication_p.cpp b/singleapplication_p.cpp
index 0086279..26fa56f 100644
--- a/singleapplication_p.cpp
+++ b/singleapplication_p.cpp
@@ -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
}
@@ -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
@@ -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();
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();