summaryrefslogtreecommitdiff
path: root/UCoreClasses.pas
blob: 7a4a43a9789085e6ac9bc31c37afb1f74c36f08c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
(*
    Tux Commander - UCoreClasses - Some useful core classes
    Copyright (C) 2007 Tomas Bzatek <tbzatek@users.sourceforge.net>
    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.