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.
24 #include <QtCore/QMutableStringListIterator>
25 #include <QtCore/QDir>
32 class KUser::Private
: public KShared
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))
51 void fillPasswd(const passwd
*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();
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
) );
80 d
= new Private( qgetenv( "LOGNAME" ) );
82 d
= new Private( qgetenv( "USER" ) );
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
)
114 KUser
& KUser::operator =(const KUser
& user
)
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 {
136 K_GID
KUser::gid() const {
140 bool KUser::isSuperUser() const {
144 QString
KUser::loginName() const {
148 QString
KUser::fullName() const {
149 return d
->properties
[FullName
].toString();
152 QString
KUser::homeDir() const {
156 QString
KUser::faceIconPath() const
158 QString
pathToFaceIcon(homeDir() + QDir::separator() + ".face.icon");
160 if (QFile::exists(pathToFaceIcon
)) {
161 return pathToFaceIcon
;
167 QString
KUser::shell() const {
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) ) {
184 QStringList
KUser::groupNames() const {
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());
197 QVariant
KUser::property(UserProperty which
) const
199 return d
->properties
.value(which
);
202 QList
<KUser
> KUser::allUsers() {
207 while ((p
= getpwent())) {
208 result
.append(KUser(p
));
216 QStringList
KUser::allUserNames() {
221 while ((p
= getpwent())) {
222 result
.append(QString::fromLocal8Bit(p
->pw_name
));
232 class KUserGroup::Private
: public KShared
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))
249 void fillGroup(const ::group
*p
) {
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
)
284 KUserGroup::KUserGroup(const KUserGroup
& group
)
289 KUserGroup
& KUserGroup::operator =(const KUserGroup
& group
) {
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 {
310 QString
KUserGroup::name() const {
314 QList
<KUser
> KUserGroup::users() const {
318 QStringList
KUserGroup::userNames() const {
320 QList
<KUser
>::const_iterator it
;
321 for ( it
= d
->users
.begin(); it
!= d
->users
.end(); ++it
) {
322 result
.append((*it
).loginName());
327 QList
<KUserGroup
> KUserGroup::allGroups() {
328 QList
<KUserGroup
> result
;
331 while ((g
= getgrent())) {
332 result
.append(KUserGroup(g
));
340 QStringList
KUserGroup::allGroupNames() {
344 while ((g
= getgrent())) {
345 result
.append(QString::fromLocal8Bit(g
->gr_name
));
353 KUserGroup::~KUserGroup() {