PDA

View Full Version : User recognition



eu.x
28th February 2007, 21:33
Hi,

I need to recognize the user by it's login name. I was thinking about use something like "net name" DOS command, but I'll make my application not very portable. There is in Qt some way to know what is the logged user name?

wysota
28th February 2007, 21:42
You can use the USER environment variable under Unix (which is not the best way to do it) or some system call like getuid() or geteuid(). Of course it surely won't work on Windows :)

maxpower
28th February 2007, 21:48
Another not so great choice would be parsing QDir::homePath().
There is also the USERNAME env variable in Windows and the aforementioned USER env variable in Unix.

mAx

wysota
28th February 2007, 22:12
The problem with USER (at least under Linux) is that one can change its contents, so you can't rely on it:

$ echo $USER
wysota
$ export USER=nonexistent
$ echo $USER
nonexistent

Practically the same goes with homePath - you can't assume the path ends up with the user's login name. It's a convention, but not a rule.

stevey
13th March 2007, 03:16
.NET has a namespace called System.Environment, you could download the Mono project source and see how they locate the details, and adapt it for Qt.

This of course would only cover the windows world, but that's half your battle won ;)
Would be nice if Trolltech could include this feature in a future release.
I'm afraid I have no idea how you'd do it on *nix.