• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

kuser_unix.cpp

Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor,
00019  *  Boston, MA 02110-1301, USA.
00020  */
00021 
00022 #include <kuser.h>
00023 
00024 #include <QtCore/QMutableStringListIterator>
00025 #include <QtCore/QDir>
00026 
00027 #include <pwd.h>
00028 #include <unistd.h>
00029 #include <stdlib.h>
00030 #include <grp.h>
00031 
00032 class KUser::Private : public KShared
00033 {
00034 public:
00035     uid_t uid;
00036     gid_t gid;
00037     QString loginName;
00038     QString homeDir, shell;
00039     QMap<UserProperty, QVariant> properties;
00040 
00041     Private() : uid(uid_t(-1)), gid(gid_t(-1)) {}
00042     Private(const char *name) : uid(uid_t(-1)), gid(gid_t(-1))
00043     {
00044         fillPasswd(name ? ::getpwnam( name ) : 0);
00045     }
00046     Private(const passwd *p) : uid(uid_t(-1)), gid(gid_t(-1))
00047     {
00048         fillPasswd(p);
00049     }
00050 
00051     void fillPasswd(const passwd *p)
00052     {
00053         if (p) {
00054             QString gecos = QString::fromLocal8Bit(p->pw_gecos);
00055             QStringList gecosList = gecos.split(QLatin1Char(','));
00056             // fill up the list, should be at least 4 entries
00057             while (gecosList.size() < 4)
00058                 gecosList << QString();
00059 
00060             uid = p->pw_uid;
00061             gid = p->pw_gid;
00062             loginName = QString::fromLocal8Bit(p->pw_name);
00063             properties[KUser::FullName] = QVariant(gecosList[0]);
00064             properties[KUser::RoomNumber] = QVariant(gecosList[1]);
00065             properties[KUser::WorkPhone] = QVariant(gecosList[2]);
00066             properties[KUser::HomePhone] = QVariant(gecosList[3]);
00067             homeDir = QString::fromLocal8Bit(p->pw_dir);
00068             shell = QString::fromLocal8Bit(p->pw_shell);
00069         }
00070     }
00071 };
00072 
00073 
00074 KUser::KUser(UIDMode mode)
00075 {
00076     uid_t _uid = ::getuid(), _euid;
00077     if (mode == UseEffectiveUID && (_euid = ::geteuid()) != _uid )
00078         d = new Private( ::getpwuid( _euid ) );
00079     else {
00080         d = new Private( qgetenv( "LOGNAME" ) );
00081         if (uid() != _uid) {
00082             d = new Private( qgetenv( "USER" ) );
00083             if (uid() != _uid)
00084                 d = new Private( ::getpwuid( _uid ) );
00085         }
00086     }
00087 }
00088 
00089 KUser::KUser(K_UID _uid)
00090     : d(new Private( ::getpwuid( _uid ) ))
00091 {
00092 }
00093 
00094 KUser::KUser(const QString& name)
00095     : d(new Private( name.toLocal8Bit().data() ))
00096 {
00097 }
00098 
00099 KUser::KUser(const char *name)
00100     : d(new Private( name ))
00101 {
00102 }
00103 
00104 KUser::KUser(const passwd *p)
00105     : d(new Private( p ))
00106 {
00107 }
00108 
00109 KUser::KUser(const KUser & user)
00110   : d(user.d)
00111 {
00112 }
00113 
00114 KUser& KUser::operator =(const KUser& user)
00115 {
00116   d = user.d;
00117   return *this;
00118 }
00119 
00120 bool KUser::operator ==(const KUser& user) const {
00121     return (uid() == user.uid()) && (uid() != uid_t(-1));
00122 }
00123 
00124 bool KUser::operator !=(const KUser& user) const {
00125     return (uid() != user.uid()) || (uid() == uid_t(-1));
00126 }
00127 
00128 bool KUser::isValid() const {
00129     return uid() != uid_t(-1);
00130 }
00131 
00132 K_UID KUser::uid() const {
00133     return d->uid;
00134 }
00135 
00136 K_GID KUser::gid() const {
00137     return d->gid;
00138 }
00139 
00140 bool KUser::isSuperUser() const {
00141     return uid() == 0;
00142 }
00143 
00144 QString KUser::loginName() const {
00145     return d->loginName;
00146 }
00147 
00148 QString KUser::fullName() const {
00149     return d->properties[FullName].toString();
00150 }
00151 
00152 QString KUser::homeDir() const {
00153     return d->homeDir;
00154 }
00155 
00156 QString KUser::faceIconPath() const
00157 {
00158     QString pathToFaceIcon(homeDir() + QDir::separator() + ".face.icon");
00159 
00160     if (QFile::exists(pathToFaceIcon)) {
00161         return pathToFaceIcon;
00162     }
00163 
00164     return QString();
00165 }
00166 
00167 QString KUser::shell() const {
00168     return d->shell;
00169 }
00170 
00171 QList<KUserGroup> KUser::groups() const {
00172   QList<KUserGroup> result;
00173   const QList<KUserGroup> allGroups = KUserGroup::allGroups();
00174   QList<KUserGroup>::const_iterator it;
00175   for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
00176     QList<KUser> users = (*it).users();
00177     if ( users.contains(*this) ) {
00178        result.append(*it);
00179     }
00180   }
00181   return result;
00182 }
00183 
00184 QStringList KUser::groupNames() const {
00185   QStringList result;
00186   const QList<KUserGroup> allGroups = KUserGroup::allGroups();
00187   QList<KUserGroup>::const_iterator it;
00188   for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
00189     QList<KUser> users = (*it).users();
00190     if ( users.contains(*this) ) {
00191        result.append((*it).name());
00192     }
00193   }
00194   return result;
00195 }
00196 
00197 QVariant KUser::property(UserProperty which) const
00198 {
00199     return d->properties.value(which);
00200 }
00201 
00202 QList<KUser> KUser::allUsers() {
00203   QList<KUser> result;
00204 
00205   passwd* p;
00206 
00207   while ((p = getpwent()))  {
00208     result.append(KUser(p));
00209   }
00210 
00211   endpwent();
00212 
00213   return result;
00214 }
00215 
00216 QStringList KUser::allUserNames() {
00217   QStringList result;
00218 
00219   passwd* p;
00220 
00221   while ((p = getpwent()))  {
00222     result.append(QString::fromLocal8Bit(p->pw_name));
00223   }
00224 
00225   endpwent();
00226   return result;
00227 }
00228 
00229 KUser::~KUser() {
00230 }
00231 
00232 class KUserGroup::Private : public KShared
00233 {
00234 public:
00235     gid_t gid;
00236     QString name;
00237     QList<KUser> users;
00238 
00239     Private() : gid(gid_t(-1)) {}
00240     Private(const char *_name) : gid(gid_t(-1))
00241     {
00242         fillGroup(_name ? ::getgrnam( _name ) : 0);
00243     }
00244     Private(const ::group *p) : gid(gid_t(-1))
00245     {
00246         fillGroup(p);
00247     }
00248 
00249     void fillGroup(const ::group *p) {
00250         if (p) {
00251             gid = p->gr_gid;
00252             name = QString::fromLocal8Bit(p->gr_name);
00253             for (char **user = p->gr_mem; *user; user++)
00254                 users.append(KUser(*user));
00255         }
00256     }
00257 };
00258 
00259 KUserGroup::KUserGroup(KUser::UIDMode mode)
00260 {
00261     d = new Private(getgrgid(KUser(mode).gid()));
00262 }
00263 
00264 KUserGroup::KUserGroup(K_GID _gid)
00265     : d(new Private(getgrgid(_gid)))
00266 {
00267 }
00268 
00269 KUserGroup::KUserGroup(const QString& _name)
00270     : d(new Private(_name.toLocal8Bit().data()))
00271 {
00272 }
00273 
00274 KUserGroup::KUserGroup(const char *_name)
00275     : d(new Private(_name))
00276 {
00277 }
00278 
00279 KUserGroup::KUserGroup(const ::group *g)
00280     : d(new Private(g))
00281 {
00282 }
00283 
00284 KUserGroup::KUserGroup(const KUserGroup & group)
00285   : d(group.d)
00286 {
00287 }
00288 
00289 KUserGroup& KUserGroup::operator =(const KUserGroup& group) {
00290   d = group.d;
00291   return *this;
00292 }
00293 
00294 bool KUserGroup::operator ==(const KUserGroup& group) const {
00295     return (gid() == group.gid()) && (gid() != gid_t(-1));
00296 }
00297 
00298 bool KUserGroup::operator !=(const KUserGroup& user) const {
00299     return (gid() != user.gid()) || (gid() == gid_t(-1));
00300 }
00301 
00302 bool KUserGroup::isValid() const {
00303     return gid() != gid_t(-1);
00304 }
00305 
00306 K_GID KUserGroup::gid() const {
00307     return d->gid;
00308 }
00309 
00310 QString KUserGroup::name() const {
00311     return d->name;
00312 }
00313 
00314 QList<KUser> KUserGroup::users() const {
00315     return d->users;
00316 }
00317 
00318 QStringList KUserGroup::userNames() const {
00319   QStringList result;
00320   QList<KUser>::const_iterator it;
00321   for ( it = d->users.begin(); it != d->users.end(); ++it ) {
00322     result.append((*it).loginName());
00323   }
00324   return result;
00325 }
00326 
00327 QList<KUserGroup> KUserGroup::allGroups() {
00328   QList<KUserGroup> result;
00329 
00330   ::group* g;
00331   while ((g = getgrent()))  {
00332      result.append(KUserGroup(g));
00333   }
00334 
00335   endgrent();
00336 
00337   return result;
00338 }
00339 
00340 QStringList KUserGroup::allGroupNames() {
00341   QStringList result;
00342 
00343   ::group* g;
00344   while ((g = getgrent()))  {
00345      result.append(QString::fromLocal8Bit(g->gr_name));
00346   }
00347 
00348   endgrent();
00349 
00350   return result;
00351 }
00352 
00353 KUserGroup::~KUserGroup() {
00354 }

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal