kdecore Library API Documentation

kuser.cpp

00001 /*
00002  *  KUser - represent a user/account
00003  *  Copyright (C) 2002 Tim Jansen <tim@tjansen.de>
00004  *
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Library General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Library General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Library General Public License
00017  *  along with this library; see the file COPYING.LIB.  If not, write to
00018  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  *  Boston, MA 02111-1307, USA.
00020  */
00021 
00022 #include <kuser.h>
00023 
00024 #include "kstringhandler.h"
00025 
00026 #include <qstringlist.h>
00027 
00028 #include <sys/types.h>
00029 #include <pwd.h>
00030 #include <unistd.h>
00031 #include <stdlib.h>
00032 
00033 class KUserPrivate : public KShared
00034 {
00035 public:
00036     bool valid;
00037     long uid, gid;
00038     QString loginName, fullName;
00039     QString roomNumber, workPhone, homePhone;
00040     QString homeDir, shell;
00041 
00042     KUserPrivate() : valid(false) {}
00043 
00044     KUserPrivate(long _uid,
00045              long _gid,
00046              const QString &_loginname,
00047              const QString &_fullname,
00048              const QString &_room,
00049              const QString &_workPhone,
00050              const QString &_homePhone,
00051              const QString &_homedir,
00052              const QString &_shell) :
00053         valid(true),
00054         uid(_uid),
00055         gid(_gid),
00056         loginName(_loginname),
00057         fullName(_fullname),
00058         roomNumber(_room),
00059         workPhone(_workPhone),
00060         homePhone(_homePhone),
00061         homeDir(_homedir),
00062         shell(_shell) {}
00063 };
00064 
00065 
00066 KUser::KUser(UIDMode mode) {
00067     long _uid = ::getuid(), _euid;
00068     if (mode == UseEffectiveUID && (_euid = ::geteuid()) != _uid )
00069         fillPasswd( ::getpwuid( _euid ) );
00070     else {
00071         fillName( ::getenv( "LOGNAME" ) );
00072         if (uid() != _uid) {
00073             fillName( ::getenv( "USER" ) );
00074             if (uid() != _uid)
00075                 fillPasswd( ::getpwuid( _uid ) );
00076         }
00077     }
00078 }
00079 
00080 KUser::KUser(long uid) {
00081     fillPasswd( ::getpwuid( uid ) );
00082 }
00083 
00084 KUser::KUser(const QString& name) {
00085     fillName( name.local8Bit().data() );
00086 }
00087 
00088 KUser::KUser(const char *name) {
00089     fillName( name );
00090 }
00091 
00092 bool KUser::operator ==(const KUser& user) const {
00093     if (isValid() != user.isValid())
00094     return false;
00095     if (isValid())
00096     return uid() == user.uid();
00097     else
00098     return true;
00099 }
00100 
00101 bool KUser::operator !=(const KUser& user) const {
00102     return !operator ==(user);
00103 }
00104 
00105 void KUser::fillName(const char *name) {
00106     fillPasswd(name ? ::getpwnam( name ) : 0);
00107 }
00108 
00109 void KUser::fillPasswd(struct passwd *p) {
00110     if (p) {
00111         QString gecos = KStringHandler::from8Bit(p->pw_gecos); 
00112         QStringList gecosList = QStringList::split(',', gecos, true);
00113 
00114         d = new KUserPrivate(p->pw_uid,
00115                      p->pw_gid,
00116                      QString::fromLocal8Bit(p->pw_name),
00117                      (gecosList.size() > 0) ? gecosList[0] : QString::null,
00118                      (gecosList.size() > 1) ? gecosList[1] : QString::null,
00119                      (gecosList.size() > 2) ? gecosList[2] : QString::null,
00120                      (gecosList.size() > 3) ? gecosList[3] : QString::null,
00121                      QString::fromLocal8Bit(p->pw_dir),
00122                      QString::fromLocal8Bit(p->pw_shell));
00123     }
00124     else
00125         d = new KUserPrivate();
00126 }
00127 
00128 bool KUser::isValid() const {
00129     return d->valid;
00130 }
00131 
00132 long KUser::uid() const {
00133     if (d->valid)
00134         return d->uid;
00135     else
00136         return -1;
00137 }
00138 
00139 long KUser::gid() const {
00140     if (d->valid)
00141         return d->gid;
00142     else
00143         return -1;
00144 }
00145 
00146 bool KUser::isSuperUser() const {
00147     return uid() == 0;
00148 }
00149 
00150 QString KUser::loginName() const {
00151     if (d->valid)
00152         return d->loginName;
00153     else
00154         return QString::null;
00155 }
00156 
00157 QString KUser::fullName() const {
00158     if (d->valid)
00159         return d->fullName;
00160     else
00161         return QString::null;
00162 }
00163 
00164 QString KUser::roomNumber() const {
00165     if (d->valid)
00166         return d->roomNumber;
00167     else
00168         return QString::null;
00169 }
00170 
00171 QString KUser::workPhone() const {
00172     if (d->valid)
00173         return d->workPhone;
00174     else
00175         return QString::null;
00176 }
00177 
00178 QString KUser::homePhone() const {
00179     if (d->valid)
00180         return d->homePhone;
00181     else
00182         return QString::null;
00183 }
00184 
00185 QString KUser::homeDir() const {
00186     if (d->valid)
00187         return d->homeDir;
00188     else
00189         return QString::null;
00190 }
00191 
00192 QString KUser::shell() const {
00193     if (d->valid)
00194         return d->shell;
00195     else
00196         return QString::null;
00197 }
00198 
00199 KUser::~KUser() {
00200 }
KDE Logo
This file is part of the documentation for kdecore Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed May 5 07:19:41 2004 by doxygen 1.3.6 written by Dimitri van Heesch, © 1997-2003