(* Tux Commander - UCoreClasses - Some useful core classes Copyright (C) 2007 Tomas Bzatek Check for updates on tuxcmd.sourceforge.net This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *) unit UCoreClasses; interface uses lazglib2, lazgobject2, lazgtk3, SysUtils, Classes, ULibc, IniFiles, GTKStdCtrls, GTKPixbuf; type TSystemUser = class public UserName, FullName, HomeDir, LoginShell: string; UID, GID: Cardinal; end; TSystemGroup = class public GroupName: string; GID: Cardinal; Users: TStrings; end; TUserManager = class private public UserList, GroupList: TList; constructor Create; destructor Destroy; override; function GetUserName(UID: Cardinal; const ShowNA: boolean = True): string; function GetGroupName(GID: Cardinal; const ShowNA: boolean = True): string; end; TMyIniFile = class(TIniFile) private FReadOnly: boolean; public constructor Create(const AFileName: string; ReadOnly: boolean); override; procedure UpdateFile; override; end; implementation uses GTKForms, ULocale, UCoreUtils; (********************************************************************************************************************************) (********************************************************************************************************************************) constructor TUserManager.Create; var pwd: PPasswd; User: TSystemUser; grp: PGroup; Group: TSystemGroup; i: integer; begin inherited Create; UserList := TList.Create; GroupList := TList.Create; // Load and process /etc/passwd try setpwent; pwd := getpwent; while pwd <> nil do begin User := TSystemUser.Create; User.UserName := String(StrToUTF8(pwd^.pw_name)); User.FullName := String(StrToUTF8(pwd^.pw_gecos)); User.HomeDir := String(StrToUTF8(pwd^.pw_dir)); User.LoginShell := String(StrToUTF8(pwd^.pw_shell)); User.UID := pwd^.pw_uid; User.GID := pwd^.pw_gid; UserList.Add(User); pwd := getpwent; end; endpwent; except end; // Load and process /etc/group try setgrent; grp := getgrent; while grp <> nil do begin Group := TSystemGroup.Create; Group.GroupName := String(StrToUTF8(grp^.gr_name)); Group.GID := grp^.gr_gid; Group.Users := TStringList.Create; {$R-} if grp^.gr_mem^ <> nil then begin i := 0; repeat Group.Users.Add(String(StrToUTF8(PCharArray(grp^.gr_mem^)[i]))); Inc(i); until PCharArray(grp^.gr_mem^)[i] = nil; end; {$R+} GroupList.Add(Group); grp := getgrent; end; endgrent; except end; end; destructor TUserManager.Destroy; var i: integer; begin if UserList.Count > 0 then for i := UserList.Count - 1 downto 0 do begin TSystemUser(UserList[i]).Free; UserList.Delete(i); end; UserList.Clear; UserList.Free; if GroupList.Count > 0 then for i := GroupList.Count - 1 downto 0 do begin TSystemGroup(GroupList[i]).Users.Clear; TSystemGroup(GroupList[i]).Users.Free; TSystemGroup(GroupList[i]).Free; GroupList.Delete(i); end; GroupList.Clear; GroupList.Free; inherited Destroy; end; function TUserManager.GetUserName(UID: Cardinal; const ShowNA: boolean = True): string; var i: integer; begin if ShowNA then Result := 'N/A' else Result := IntToStr(UID); if UserList.Count > 0 then for i := 0 to UserList.Count - 1 do if TSystemUser(UserList[i]).UID = UID then begin Result := TSystemUser(UserList[i]).UserName; Break; end; end; function TUserManager.GetGroupName(GID: Cardinal; const ShowNA: boolean = True): string; var i: integer; begin if ShowNA then Result := 'N/A' else Result := IntToStr(GID); if GroupList.Count > 0 then for i := 0 to GroupList.Count - 1 do if TSystemGroup(GroupList[i]).GID = GID then begin Result := TSystemGroup(GroupList[i]).GroupName; Break; end; end; (********************************************************************************************************************************) (********************************************************************************************************************************) constructor TMyIniFile.Create(const AFileName: string; ReadOnly: boolean); begin FReadOnly := ReadOnly; inherited Create(AFileName); end; procedure TMyIniFile.UpdateFile; begin if not FReadOnly then inherited UpdateFile; end; (********************************************************************************************************************************) end.