fix logic
[personal-kdelibs.git] / kdecore / util / kuser_unix.cpp
blob2e515de10e532417663310cce6606221bd323f97
1 /*
2 * KUser - represent a user/account
3 * Copyright (C) 2002 Tim Jansen <tim@tjansen.de>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #include <kuser.h>
24 #include <QtCore/QMutableStringListIterator>
25 #include <QtCore/QDir>
27 #include <pwd.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <grp.h>
32 class KUser::Private : public KShared
34 public:
35 uid_t uid;
36 gid_t gid;
37 QString loginName;
38 QString homeDir, shell;
39 QMap<UserProperty, QVariant> properties;
41 Private() : uid(uid_t(-1)), gid(gid_t(-1)) {}
42 Private(const char *name) : uid(uid_t(-1)), gid(gid_t(-1))
44 fillPasswd(name ? ::getpwnam( name ) : 0);
46 Private(const passwd *p) : uid(uid_t(-1)), gid(gid_t(-1))
48 fillPasswd(p);
51 void fillPasswd(const passwd *p)
53 if (p) {
54 QString gecos = QString::fromLocal8Bit(p->pw_gecos);
55 QStringList gecosList = gecos.split(QLatin1Char(','));
56 // fill up the list, should be at least 4 entries
57 while (gecosList.size() < 4)
58 gecosList << QString();
60 uid = p->pw_uid;
61 gid = p->pw_gid;
62 loginName = QString::fromLocal8Bit(p->pw_name);
63 properties[KUser::FullName] = QVariant(gecosList[0]);
64 properties[KUser::RoomNumber] = QVariant(gecosList[1]);
65 properties[KUser::WorkPhone] = QVariant(gecosList[2]);
66 properties[KUser::HomePhone] = QVariant(gecosList[3]);
67 homeDir = QString::fromLocal8Bit(p->pw_dir);
68 shell = QString::fromLocal8Bit(p->pw_shell);
74 KUser::KUser(UIDMode mode)
76 uid_t _uid = ::getuid(), _euid;
77 if (mode == UseEffectiveUID && (_euid = ::geteuid()) != _uid )
78 d = new Private( ::getpwuid( _euid ) );
79 else {
80 d = new Private( qgetenv( "LOGNAME" ) );
81 if (uid() != _uid) {
82 d = new Private( qgetenv( "USER" ) );
83 if (uid() != _uid)
84 d = new Private( ::getpwuid( _uid ) );
89 KUser::KUser(K_UID _uid)
90 : d(new Private( ::getpwuid( _uid ) ))
94 KUser::KUser(const QString& name)
95 : d(new Private( name.toLocal8Bit().data() ))
99 KUser::KUser(const char *name)
100 : d(new Private( name ))
104 KUser::KUser(const passwd *p)
105 : d(new Private( p ))
109 KUser::KUser(const KUser & user)
110 : d(user.d)
114 KUser& KUser::operator =(const KUser& user)
116 d = user.d;
117 return *this;
120 bool KUser::operator ==(const KUser& user) const {
121 return (uid() == user.uid()) && (uid() != uid_t(-1));
124 bool KUser::operator !=(const KUser& user) const {
125 return (uid() != user.uid()) || (uid() == uid_t(-1));
128 bool KUser::isValid() const {
129 return uid() != uid_t(-1);
132 K_UID KUser::uid() const {
133 return d->uid;
136 K_GID KUser::gid() const {
137 return d->gid;
140 bool KUser::isSuperUser() const {
141 return uid() == 0;
144 QString KUser::loginName() const {
145 return d->loginName;
148 QString KUser::fullName() const {
149 return d->properties[FullName].toString();
152 QString KUser::homeDir() const {
153 return d->homeDir;
156 QString KUser::faceIconPath() const
158 QString pathToFaceIcon(homeDir() + QDir::separator() + ".face.icon");
160 if (QFile::exists(pathToFaceIcon)) {
161 return pathToFaceIcon;
164 return QString();
167 QString KUser::shell() const {
168 return d->shell;
171 QList<KUserGroup> KUser::groups() const {
172 QList<KUserGroup> result;
173 const QList<KUserGroup> allGroups = KUserGroup::allGroups();
174 QList<KUserGroup>::const_iterator it;
175 for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
176 QList<KUser> users = (*it).users();
177 if ( users.contains(*this) ) {
178 result.append(*it);
181 return result;
184 QStringList KUser::groupNames() const {
185 QStringList result;
186 const QList<KUserGroup> allGroups = KUserGroup::allGroups();
187 QList<KUserGroup>::const_iterator it;
188 for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
189 QList<KUser> users = (*it).users();
190 if ( users.contains(*this) ) {
191 result.append((*it).name());
194 return result;
197 QVariant KUser::property(UserProperty which) const
199 return d->properties.value(which);
202 QList<KUser> KUser::allUsers() {
203 QList<KUser> result;
205 passwd* p;
207 while ((p = getpwent())) {
208 result.append(KUser(p));
211 endpwent();
213 return result;
216 QStringList KUser::allUserNames() {
217 QStringList result;
219 passwd* p;
221 while ((p = getpwent())) {
222 result.append(QString::fromLocal8Bit(p->pw_name));
225 endpwent();
226 return result;
229 KUser::~KUser() {
232 class KUserGroup::Private : public KShared
234 public:
235 gid_t gid;
236 QString name;
237 QList<KUser> users;
239 Private() : gid(gid_t(-1)) {}
240 Private(const char *_name) : gid(gid_t(-1))
242 fillGroup(_name ? ::getgrnam( _name ) : 0);
244 Private(const ::group *p) : gid(gid_t(-1))
246 fillGroup(p);
249 void fillGroup(const ::group *p) {
250 if (p) {
251 gid = p->gr_gid;
252 name = QString::fromLocal8Bit(p->gr_name);
253 for (char **user = p->gr_mem; *user; user++)
254 users.append(KUser(*user));
259 KUserGroup::KUserGroup(KUser::UIDMode mode)
261 d = new Private(getgrgid(KUser(mode).gid()));
264 KUserGroup::KUserGroup(K_GID _gid)
265 : d(new Private(getgrgid(_gid)))
269 KUserGroup::KUserGroup(const QString& _name)
270 : d(new Private(_name.toLocal8Bit().data()))
274 KUserGroup::KUserGroup(const char *_name)
275 : d(new Private(_name))
279 KUserGroup::KUserGroup(const ::group *g)
280 : d(new Private(g))
284 KUserGroup::KUserGroup(const KUserGroup & group)
285 : d(group.d)
289 KUserGroup& KUserGroup::operator =(const KUserGroup& group) {
290 d = group.d;
291 return *this;
294 bool KUserGroup::operator ==(const KUserGroup& group) const {
295 return (gid() == group.gid()) && (gid() != gid_t(-1));
298 bool KUserGroup::operator !=(const KUserGroup& user) const {
299 return (gid() != user.gid()) || (gid() == gid_t(-1));
302 bool KUserGroup::isValid() const {
303 return gid() != gid_t(-1);
306 K_GID KUserGroup::gid() const {
307 return d->gid;
310 QString KUserGroup::name() const {
311 return d->name;
314 QList<KUser> KUserGroup::users() const {
315 return d->users;
318 QStringList KUserGroup::userNames() const {
319 QStringList result;
320 QList<KUser>::const_iterator it;
321 for ( it = d->users.begin(); it != d->users.end(); ++it ) {
322 result.append((*it).loginName());
324 return result;
327 QList<KUserGroup> KUserGroup::allGroups() {
328 QList<KUserGroup> result;
330 ::group* g;
331 while ((g = getgrent())) {
332 result.append(KUserGroup(g));
335 endgrent();
337 return result;
340 QStringList KUserGroup::allGroupNames() {
341 QStringList result;
343 ::group* g;
344 while ((g = getgrent())) {
345 result.append(QString::fromLocal8Bit(g->gr_name));
348 endgrent();
350 return result;
353 KUserGroup::~KUserGroup() {