Add a getCurrentUser function for further reference

This commit is contained in:
ArsenArsen 2017-12-20 11:42:11 +01:00
parent f09bfe37d4
commit 24952bba05
No known key found for this signature in database
GPG Key ID: 683D2F43B0CA4BD2
6 changed files with 43 additions and 1 deletions

View File

@ -1,6 +1,7 @@
#include "macbackend.hpp"
#include <unistd.h>
#include <pwd.h>
std::tuple<QPoint, QPixmap> PlatformBackend::getCursor() {
#warning "TODO: Mac backend"
@ -17,3 +18,16 @@ pid_t PlatformBackend::pid() {
bool PlatformBackend::filenameValid(QString name) {
return !name.contains('/');
}
QString PlatformBackend::getCurrentUser() {
auto pwent = getpwent();
if(!pwent) {
if (qEnvironmentVariableIsSet("USER"))
return QString::fromLocal8Bit(qgetenv("USER"));
else return QString();
}
QString ret = QString::fromLocal8Bit(pwent->pw_name);
endpwent();
return ret;
}

View File

@ -4,6 +4,7 @@
#include <QPixmap>
#define PLATFORM_CAPABILITY_PID
#define PLATFORM_CAPABILITY_CURRENT_USER
class PlatformBackend {
public:
@ -14,6 +15,7 @@ public:
return inst;
}
bool filenameValid(QString name);
QString getCurrentUser();
};
#endif // MACBACKEND_HPP

View File

@ -1,5 +1,6 @@
#include "u32backend.hpp"
#include <Lmcons.h>
#include <QCursor>
#include <QtWin>
#include <windows.h>
@ -43,3 +44,12 @@ bool PlatformBackend::filenameValid(QString name) {
if (periods == name.length()) return false;
return !illegalNames.contains(name);
}
QString PlatformBackend::getCurrentUser() {
char username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
GetUserName(username, &username_len);
QString userName = QString::fromLocal8Bit(username, username_len);
delete[] username;
return userName;
}

View File

@ -7,6 +7,7 @@
#define PLATFORM_CAPABILITY_PID
#define PLATFORM_CAPABILITY_ACTIVEWINDOW
#define PLATFORM_CAPABILITY_CURSOR
#define PLATFORM_CAPABILITY_CURRENT_USER
class PlatformBackend {
public:
@ -18,6 +19,7 @@ public:
}
WId getActiveWID();
bool filenameValid(QString name);
QString getCurrentUser();
};
#endif // U32BACKEND_HPP

View File

@ -3,6 +3,7 @@
#include <QPixmap>
#include <QX11Info>
#include <unistd.h>
#include <pwd.h>
#include <xcb/xcb_cursor.h>
#include <xcb/xcb_util.h>
#include <xcb/xfixes.h>
@ -28,7 +29,6 @@ pid_t PlatformBackend::pid() {
return getpid();
}
WId PlatformBackend::getActiveWID() {
xcb_connection_t *connection = QX11Info::connection();
xcb_get_input_focus_reply_t *focusReply;
@ -58,3 +58,15 @@ WId PlatformBackend::getActiveWID() {
bool PlatformBackend::filenameValid(QString name) {
return !name.contains('/');
}
QString PlatformBackend::getCurrentUser() {
auto pwent = getpwent();
if(!pwent) {
if (qEnvironmentVariableIsSet("USER"))
return QString::fromLocal8Bit(qgetenv("USER"));
else return QString();
}
QString ret = QString::fromLocal8Bit(pwent->pw_name);
endpwent();
return ret;
}

View File

@ -6,6 +6,7 @@
#define PLATFORM_CAPABILITY_PID
#define PLATFORM_CAPABILITY_ACTIVEWINDOW
#define PLATFORM_CAPABILITY_CURSOR
#define PLATFORM_CAPABILITY_CURRENT_USER
class PlatformBackend {
public:
@ -17,6 +18,7 @@ public:
}
WId getActiveWID();
bool filenameValid(QString name);
QString getCurrentUser();
};
#endif // X11BACKEND_HPP