• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • kdelibs
  • KDE Home
  • 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 #ifndef KDE_NO_DEPRECATED
00149 QString KUser::fullName() const {
00150     return d->properties[FullName].toString();
00151 }
00152 #endif
00153 
00154 QString KUser::homeDir() const {
00155     return d->homeDir;
00156 }
00157 
00158 QString KUser::faceIconPath() const
00159 {
00160     QString pathToFaceIcon(homeDir() + QDir::separator() + QLatin1String(".face.icon"));
00161 
00162     if (QFile::exists(pathToFaceIcon)) {
00163         return pathToFaceIcon;
00164     }
00165 
00166     return QString();
00167 }
00168 
00169 QString KUser::shell() const {
00170     return d->shell;
00171 }
00172 
00173 QList<KUserGroup> KUser::groups() const {
00174   QList<KUserGroup> result;
00175   const QList<KUserGroup> allGroups = KUserGroup::allGroups();
00176   QList<KUserGroup>::const_iterator it;
00177   for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
00178     const QList<KUser> users = (*it).users();
00179     if ( users.contains(*this) ) {
00180        result.append(*it);
00181     }
00182   }
00183   return result;
00184 }
00185 
00186 QStringList KUser::groupNames() const {
00187   QStringList result;
00188   const QList<KUserGroup> allGroups = KUserGroup::allGroups();
00189   QList<KUserGroup>::const_iterator it;
00190   for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
00191     const QList<KUser> users = (*it).users();
00192     if ( users.contains(*this) ) {
00193        result.append((*it).name());
00194     }
00195   }
00196   return result;
00197 }
00198 
00199 QVariant KUser::property(UserProperty which) const
00200 {
00201     return d->properties.value(which);
00202 }
00203 
00204 QList<KUser> KUser::allUsers() {
00205   QList<KUser> result;
00206 
00207   passwd* p;
00208 
00209   while ((p = getpwent()))  {
00210     result.append(KUser(p));
00211   }
00212 
00213   endpwent();
00214 
00215   return result;
00216 }
00217 
00218 QStringList KUser::allUserNames() {
00219   QStringList result;
00220 
00221   passwd* p;
00222 
00223   while ((p = getpwent()))  {
00224     result.append(QString::fromLocal8Bit(p->pw_name));
00225   }
00226 
00227   endpwent();
00228   return result;
00229 }
00230 
00231 KUser::~KUser() {
00232 }
00233 
00234 class KUserGroup::Private : public KShared
00235 {
00236 public:
00237     gid_t gid;
00238     QString name;
00239     QList<KUser> users;
00240 
00241     Private() : gid(gid_t(-1)) {}
00242     Private(const char *_name) : gid(gid_t(-1))
00243     {
00244         fillGroup(_name ? ::getgrnam( _name ) : 0);
00245     }
00246     Private(const ::group *p) : gid(gid_t(-1))
00247     {
00248         fillGroup(p);
00249     }
00250 
00251     void fillGroup(const ::group *p) {
00252         if (p) {
00253             gid = p->gr_gid;
00254             name = QString::fromLocal8Bit(p->gr_name);
00255             for (char **user = p->gr_mem; *user; user++)
00256                 users.append(KUser(*user));
00257         }
00258     }
00259 };
00260 
00261 KUserGroup::KUserGroup(KUser::UIDMode mode)
00262 {
00263     d = new Private(getgrgid(KUser(mode).gid()));
00264 }
00265 
00266 KUserGroup::KUserGroup(K_GID _gid)
00267     : d(new Private(getgrgid(_gid)))
00268 {
00269 }
00270 
00271 KUserGroup::KUserGroup(const QString& _name)
00272     : d(new Private(_name.toLocal8Bit().data()))
00273 {
00274 }
00275 
00276 KUserGroup::KUserGroup(const char *_name)
00277     : d(new Private(_name))
00278 {
00279 }
00280 
00281 KUserGroup::KUserGroup(const ::group *g)
00282     : d(new Private(g))
00283 {
00284 }
00285 
00286 KUserGroup::KUserGroup(const KUserGroup & group)
00287   : d(group.d)
00288 {
00289 }
00290 
00291 KUserGroup& KUserGroup::operator =(const KUserGroup& group) {
00292   d = group.d;
00293   return *this;
00294 }
00295 
00296 bool KUserGroup::operator ==(const KUserGroup& group) const {
00297     return (gid() == group.gid()) && (gid() != gid_t(-1));
00298 }
00299 
00300 bool KUserGroup::operator !=(const KUserGroup& user) const {
00301     return (gid() != user.gid()) || (gid() == gid_t(-1));
00302 }
00303 
00304 bool KUserGroup::isValid() const {
00305     return gid() != gid_t(-1);
00306 }
00307 
00308 K_GID KUserGroup::gid() const {
00309     return d->gid;
00310 }
00311 
00312 QString KUserGroup::name() const {
00313     return d->name;
00314 }
00315 
00316 QList<KUser> KUserGroup::users() const {
00317     return d->users;
00318 }
00319 
00320 QStringList KUserGroup::userNames() const {
00321   QStringList result;
00322   QList<KUser>::const_iterator it;
00323   for ( it = d->users.begin(); it != d->users.end(); ++it ) {
00324     result.append((*it).loginName());
00325   }
00326   return result;
00327 }
00328 
00329 QList<KUserGroup> KUserGroup::allGroups() {
00330   QList<KUserGroup> result;
00331 
00332   ::group* g;
00333   while ((g = getgrent()))  {
00334      result.append(KUserGroup(g));
00335   }
00336 
00337   endgrent();
00338 
00339   return result;
00340 }
00341 
00342 QStringList KUserGroup::allGroupNames() {
00343   QStringList result;
00344 
00345   ::group* g;
00346   while ((g = getgrent()))  {
00347      result.append(QString::fromLocal8Bit(g->gr_name));
00348   }
00349 
00350   endgrent();
00351 
00352   return result;
00353 }
00354 
00355 KUserGroup::~KUserGroup() {
00356 }

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
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.7.3
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