• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
kuser.cpp
1 /*
2  * KUser - represent a user/account
3  * Copyright (C) 2002 Tim Jansen <tim@tjansen.de>
4  *
5  *
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.
10  *
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.
15  *
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.
20  */
21 
22 #include <kuser.h>
23 
24 #include "kstringhandler.h"
25 #include <tqvaluelist.h>
26 #include <tqstringlist.h>
27 
28 #include <sys/types.h>
29 #include <pwd.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <grp.h>
33 
34 
35 class KUserPrivate : public TDEShared
36 {
37 public:
38  bool valid;
39  long uid, gid;
40  TQString loginName, fullName;
41  TQString roomNumber, workPhone, homePhone;
42  TQString homeDir, shell;
43 
44  KUserPrivate() : valid(false) {}
45 
46  KUserPrivate(long _uid,
47  long _gid,
48  const TQString &_loginname,
49  const TQString &_fullname,
50  const TQString &_room,
51  const TQString &_workPhone,
52  const TQString &_homePhone,
53  const TQString &_homedir,
54  const TQString &_shell) :
55  valid(true),
56  uid(_uid),
57  gid(_gid),
58  loginName(_loginname),
59  fullName(_fullname),
60  roomNumber(_room),
61  workPhone(_workPhone),
62  homePhone(_homePhone),
63  homeDir(_homedir),
64  shell(_shell) {}
65 };
66 
67 
68 KUser::KUser(UIDMode mode) {
69  long _uid = ::getuid(), _euid;
70  if (mode == UseEffectiveUID && (_euid = ::geteuid()) != _uid )
71  fillPasswd( ::getpwuid( _euid ) );
72  else {
73  fillName( ::getenv( "LOGNAME" ) );
74  if (uid() != _uid) {
75  fillName( ::getenv( "USER" ) );
76  if (uid() != _uid)
77  fillPasswd( ::getpwuid( _uid ) );
78  }
79  }
80 }
81 
82 KUser::KUser(long uid) {
83  fillPasswd( ::getpwuid( uid ) );
84 }
85 
86 KUser::KUser(const TQString& name) {
87  fillName( name.local8Bit().data() );
88 }
89 
90 KUser::KUser(const char *name) {
91  fillName( name );
92 }
93 
94 KUser::KUser(struct passwd *p) {
95  fillPasswd(p);
96 }
97 
98 KUser::KUser(const KUser & user)
99  : d(user.d)
100 {
101 }
102 
103 KUser& KUser::operator =(const KUser& user)
104 {
105  d = user.d;
106  return *this;
107 }
108 
109 bool KUser::operator ==(const KUser& user) const {
110  if (isValid() != user.isValid())
111  return false;
112  if (isValid())
113  return uid() == user.uid();
114  else
115  return true;
116 }
117 
118 bool KUser::operator !=(const KUser& user) const {
119  return !operator ==(user);
120 }
121 
122 void KUser::fillName(const char *name) {
123  fillPasswd(name ? ::getpwnam( name ) : 0);
124 }
125 
126 void KUser::fillPasswd(struct passwd *p) {
127  if (p) {
128  TQString gecos = KStringHandler::from8Bit(p->pw_gecos);
129  TQStringList gecosList = TQStringList::split(',', gecos, true);
130 
131  d = new KUserPrivate(p->pw_uid,
132  p->pw_gid,
133  TQString::fromLocal8Bit(p->pw_name),
134  (gecosList.size() > 0) ? gecosList[0] : TQString::null,
135  (gecosList.size() > 1) ? gecosList[1] : TQString::null,
136  (gecosList.size() > 2) ? gecosList[2] : TQString::null,
137  (gecosList.size() > 3) ? gecosList[3] : TQString::null,
138  TQString::fromLocal8Bit(p->pw_dir),
139  TQString::fromLocal8Bit(p->pw_shell));
140  }
141  else
142  d = new KUserPrivate();
143 }
144 
145 bool KUser::isValid() const {
146  return d->valid;
147 }
148 
149 long KUser::uid() const {
150  if (d->valid)
151  return d->uid;
152  else
153  return -1;
154 }
155 
156 long KUser::gid() const {
157  if (d->valid)
158  return d->gid;
159  else
160  return -1;
161 }
162 
163 bool KUser::isSuperUser() const {
164  return uid() == 0;
165 }
166 
167 TQString KUser::loginName() const {
168  if (d->valid)
169  return d->loginName;
170  else
171  return TQString::null;
172 }
173 
174 TQString KUser::fullName() const {
175  if (d->valid)
176  return d->fullName;
177  else
178  return TQString::null;
179 }
180 
181 TQString KUser::roomNumber() const {
182  if (d->valid)
183  return d->roomNumber;
184  else
185  return TQString::null;
186 }
187 
188 TQString KUser::workPhone() const {
189  if (d->valid)
190  return d->workPhone;
191  else
192  return TQString::null;
193 }
194 
195 TQString KUser::homePhone() const {
196  if (d->valid)
197  return d->homePhone;
198  else
199  return TQString::null;
200 }
201 
202 TQString KUser::homeDir() const {
203  if (d->valid)
204  return d->homeDir;
205  else
206  return TQString::null;
207 }
208 
209 TQString KUser::shell() const {
210  if (d->valid)
211  return d->shell;
212  else
213  return TQString::null;
214 }
215 
216 TQValueList<KUserGroup> KUser::groups() const {
217  TQValueList<KUserGroup> result;
218  TQValueList<KUserGroup> allGroups = KUserGroup::allGroups();
219  TQValueList<KUserGroup>::const_iterator it;
220  for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
221  TQValueList<KUser> users = (*it).users();
222  if ( users.find( *this ) != users.end()) {
223  result.append(*it);
224  }
225  }
226  return result;
227 }
228 
229 TQStringList KUser::groupNames() const {
230  TQStringList result;
231  TQValueList<KUserGroup> allGroups = KUserGroup::allGroups();
232  TQValueList<KUserGroup>::const_iterator it;
233  for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
234  TQValueList<KUser> users = (*it).users();
235  if ( users.find( *this ) != users.end()) {
236  result.append((*it).name());
237  }
238  }
239  return result;
240 }
241 
242 
243 TQValueList<KUser> KUser::allUsers() {
244  TQValueList<KUser> result;
245 
246  struct passwd* p;
247 
248  while ((p = getpwent())) {
249  result.append(KUser(p));
250  }
251 
252  endpwent();
253 
254  return result;
255 }
256 
257 TQStringList KUser::allUserNames() {
258  TQStringList result;
259 
260  struct passwd* p;
261 
262  while ((p = getpwent())) {
263  result.append(TQString::fromLocal8Bit(p->pw_name));
264  }
265 
266  endpwent();
267  return result;
268 }
269 
270 
271 KUser::~KUser() {
272 }
273 
274 class KUserGroupPrivate : public TDEShared
275 {
276 public:
277  bool valid;
278  long gid;
279  TQString name;
280  TQValueList<KUser> users;
281 
282  KUserGroupPrivate() : valid(false) {}
283 
284  KUserGroupPrivate(long _gid,
285  const TQString & _name,
286  const TQValueList<KUser> & _users):
287  valid(true),
288  gid(_gid),
289  name(_name),
290  users(_users) {}
291 };
292 
293 KUserGroup::KUserGroup(KUser::UIDMode mode) {
294  KUser user(mode);
295  fillGroup(getgrgid(user.gid()));
296 }
297 
298 KUserGroup::KUserGroup(long gid) {
299  fillGroup(getgrgid(gid));
300 }
301 
302 KUserGroup::KUserGroup(const TQString& name) {
303  fillName(name.local8Bit().data());
304 }
305 
306 KUserGroup::KUserGroup(const char *name) {
307  fillName(name);
308 }
309 
310 KUserGroup::KUserGroup(struct group *g) {
311  fillGroup(g);
312 }
313 
314 
315 KUserGroup::KUserGroup(const KUserGroup & group)
316  : d(group.d)
317 {
318 }
319 
320 KUserGroup& KUserGroup::operator =(const KUserGroup& group) {
321  d = group.d;
322  return *this;
323 }
324 
325 bool KUserGroup::operator ==(const KUserGroup& group) const {
326  if (isValid() != group.isValid())
327  return false;
328  if (isValid())
329  return gid() == group.gid();
330  else
331  return true;
332 }
333 
334 bool KUserGroup::operator !=(const KUserGroup& user) const {
335  return !operator ==(user);
336 }
337 
338 void KUserGroup::fillName(const char *name) {
339  fillGroup(name ? ::getgrnam( name ) : 0);
340 }
341 
342 void KUserGroup::fillGroup(struct group *p) {
343  if (!p) {
344  d = new KUserGroupPrivate();
345  return;
346  }
347 
348  TQString name = KStringHandler::from8Bit(p->gr_name);
349  TQValueList<KUser> users;
350 
351  char **user = p->gr_mem;
352  for ( ; *user; user++) {
353  KUser kUser(TQString::fromLocal8Bit(*user));
354  users.append(kUser);
355  }
356 
357  d = new KUserGroupPrivate(p->gr_gid,
358  TQString::fromLocal8Bit(p->gr_name),
359  users);
360 
361 }
362 
363 bool KUserGroup::isValid() const {
364  return d->valid;
365 }
366 
367 long KUserGroup::gid() const {
368  if (d->valid)
369  return d->gid;
370  else
371  return -1;
372 }
373 
374 TQString KUserGroup::name() const {
375  if (d->valid)
376  return d->name;
377  else
378  return TQString::null;
379 }
380 
381 const TQValueList<KUser>& KUserGroup::users() const {
382  return d->users;
383 }
384 
385 TQStringList KUserGroup::userNames() const {
386  TQStringList result;
387  TQValueList<KUser>::const_iterator it;
388  for ( it = d->users.begin(); it != d->users.end(); ++it ) {
389  result.append((*it).loginName());
390  }
391  return result;
392 }
393 
394 
395 
396 TQValueList<KUserGroup> KUserGroup::allGroups() {
397  TQValueList<KUserGroup> result;
398 
399  struct group* g;
400  while ((g = getgrent())) {
401  result.append(KUserGroup(g));
402  }
403 
404  endgrent();
405 
406  return result;
407 }
408 
409 TQStringList KUserGroup::allGroupNames() {
410  TQStringList result;
411 
412  struct group* g;
413  while ((g = getgrent())) {
414  result.append(TQString::fromLocal8Bit(g->gr_name));
415  }
416 
417  endgrent();
418 
419  return result;
420 }
421 
422 
423 KUserGroup::~KUserGroup() {
424 }
425 
KStringHandler::from8Bit
static TQString from8Bit(const char *str)
Construct TQString from a c string, guessing whether it is UTF8- or Local8Bit-encoded.
Definition: kstringhandler.cpp:652
KUserGroup
Represents a group on your system.
Definition: kuser.h:256
KUserGroup::KUserGroup
KUserGroup(KUser::UIDMode mode=KUser::UseEffectiveUID)
Create an object from the group of the current user.
Definition: kuser.cpp:293
KUserGroup::users
const TQValueList< KUser > & users() const
Returns a list of all users of the group.
Definition: kuser.cpp:381
KUserGroup::allGroupNames
static TQStringList allGroupNames()
Returns a list of all group names on this system.
Definition: kuser.cpp:409
KUserGroup::userNames
TQStringList userNames() const
Returns a list of all user login names of the group.
Definition: kuser.cpp:385
KUserGroup::name
TQString name() const
The name of the group.
Definition: kuser.cpp:374
KUserGroup::isValid
bool isValid() const
Returns wether the group is valid.
Definition: kuser.cpp:363
KUserGroup::operator!=
bool operator!=(const KUserGroup &group) const
Two KUserGroup objects are not equal if either isValid() is not true or gid() are not identical.
Definition: kuser.cpp:334
KUserGroup::gid
long gid() const
Returns the group id of the group.
Definition: kuser.cpp:367
KUserGroup::operator==
bool operator==(const KUserGroup &group) const
Two KUserGroup objects are equal if isValid() is true and gid() are identical.
Definition: kuser.cpp:325
KUserGroup::allGroups
static TQValueList< KUserGroup > allGroups()
Returns a list of all groups on this system.
Definition: kuser.cpp:396
KUserGroup::~KUserGroup
~KUserGroup()
Destructor.
Definition: kuser.cpp:423
KUserGroup::operator=
KUserGroup & operator=(const KUserGroup &group)
Copies a group.
Definition: kuser.cpp:320
KUser
Represents a user on your system.
Definition: kuser.h:45
KUser::uid
long uid() const
Returns the user id of the user.
Definition: kuser.cpp:149
KUser::KUser
KUser(UIDMode mode=UseEffectiveUID)
Creates an object that contains information about the current user.
Definition: kuser.cpp:68
KUser::roomNumber
TQString roomNumber() const
The user's room number.
Definition: kuser.cpp:181
KUser::shell
TQString shell() const
The path to the user's login shell.
Definition: kuser.cpp:209
KUser::homePhone
TQString homePhone() const
The user's home phone.
Definition: kuser.cpp:195
KUser::workPhone
TQString workPhone() const
The user's work phone.
Definition: kuser.cpp:188
KUser::fullName
TQString fullName() const
The full name of the user.
Definition: kuser.cpp:174
KUser::homeDir
TQString homeDir() const
The path to the user's home directory.
Definition: kuser.cpp:202
KUser::gid
long gid() const
Returns the group id of the user.
Definition: kuser.cpp:156
KUser::groupNames
TQStringList groupNames() const
Returns all group names of the user.
Definition: kuser.cpp:229
KUser::operator!=
bool operator!=(const KUser &user) const
Two KUser objects are not equal if either isValid() is not true or uid() are not identical.
Definition: kuser.cpp:118
KUser::groups
TQValueList< KUserGroup > groups() const
Returns all groups of the user.
Definition: kuser.cpp:216
KUser::~KUser
~KUser()
Destructor.
Definition: kuser.cpp:271
KUser::operator==
bool operator==(const KUser &user) const
Two KUser objects are equal if isValid() is true and the uid() are identical.
Definition: kuser.cpp:109
KUser::isValid
bool isValid() const
Returns true if the user is valid.
Definition: kuser.cpp:145
KUser::loginName
TQString loginName() const
The login name of the user.
Definition: kuser.cpp:167
KUser::isSuperUser
bool isSuperUser() const
Checks whether the user it the super user (root).
Definition: kuser.cpp:163
KUser::allUsers
static TQValueList< KUser > allUsers()
Returns all users of the system.
Definition: kuser.cpp:243
KUser::allUserNames
static TQStringList allUserNames()
Returns all user names of the system.
Definition: kuser.cpp:257
KUser::operator=
KUser & operator=(const KUser &user)
Copies a user.
Definition: kuser.cpp:103
KUser::UIDMode
UIDMode
Definition: kuser.h:49
KUser::UseEffectiveUID
@ UseEffectiveUID
Use the effective user id.
Definition: kuser.h:50
TDEShared
Reference counting for shared objects.
Definition: ksharedptr.h:40
KShell::homeDir
TQString homeDir(const TQString &user)
Obtain a user's home directory.
Definition: kshell.cpp:369
KStdAction::name
const char * name(StdAction id)

tdecore

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

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.1
This website is maintained by Timothy Pearson.