File: UCoreClasses.pas

package info (click to toggle)
tuxcmd 0.6.70%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 3,612 kB
  • ctags: 2,757
  • sloc: pascal: 49,754; ansic: 2,272; makefile: 106
file content (349 lines) | stat: -rw-r--r-- 11,500 bytes parent folder | download | duplicates (3)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
(*
    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 gdk2pixbuf, gtk2, gdk2, glib2, SysUtils, Classes, ULibc, IniFiles, GTKClasses, GTKStdCtrls, GTKDialogs, GTKPixbuf, UGnome,
     UEngines;

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 FileName: string; const ReadOnly: boolean);
       procedure UpdateFile; override;
     end;

     TGTKImageButton = class(TGTKButton)
     private
       FHBox: PGtkWidget;
       FLabel: PGtkWidget;
       FImage: PGtkWidget;
       FEventBoxLeft, FEventBoxRight: PGtkWidget;
       function GetCaption: string;
       procedure SetCaption(Value: string);
       procedure SetIcon(Value: TGDKPixbuf);
       procedure SetSpacing(Value: integer);
     public
       procedure SetFromStock(Stock_ID: string; IconSize: TGTKIconSize);
     published
       constructor Create(AOwner: TComponent); override;
       constructor CreateWithoutLabel(AOwner: TComponent);
       property Caption: string read GetCaption write SetCaption;
       property Icon: TGDKPixbuf write SetIcon;
       property Spacing: integer write SetSpacing;
     end;

     TGTKImageToggleButton = class(TGTKToggleButton)
     private
       FHBox: PGtkWidget;
       FLabel: PGtkWidget;
       FImage: PGtkWidget;
       function GetCaption: string;
       procedure SetCaption(Value: string);
       procedure SetIcon(Value: TGDKPixbuf);
     public
       procedure SetFromStock(Stock_ID: string; IconSize: TGTKIconSize);
     published
       constructor Create(AOwner: TComponent); override;
       constructor CreateWithoutLabel(AOwner: TComponent);
       property Caption: string read GetCaption write SetCaption;
       property Icon: TGDKPixbuf write SetIcon;
     end;


implementation

uses GTKForms, GTKUtils, ULocale, UConfig, UCore, 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 FileName: string; const ReadOnly: boolean);
begin
  FReadOnly := ReadOnly;
  inherited Create(FileName);
end;

procedure TMyIniFile.UpdateFile;
begin
  if not FReadOnly then inherited UpdateFile;
end;

(********************************************************************************************************************************)
(********************************************************************************************************************************)
constructor TGTKImageButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FWidget := gtk_button_new;
  FLabel := gtk_label_new('');
  FImage := gtk_image_new;
  FHBox := gtk_hbox_new(False, 2);
  FEventBoxLeft := gtk_vbox_new(false, 0);
  FEventBoxRight := gtk_vbox_new(false, 0);
  gtk_box_pack_start(PGtkBox(FHBox), FEventBoxLeft, False, False, 0);
  gtk_box_pack_start(PGtkBox(FHBox), FImage, False, False, 0);
  gtk_box_pack_start(PGtkBox(FHBox), FLabel, True, True, 0);
  gtk_box_pack_start(PGtkBox(FHBox), FEventBoxRight, False, False, 0);
  gtk_container_add(PGtkContainer(FWidget), FHBox);
  g_signal_connect(PGtkObject(FWidget), 'clicked', G_CALLBACK(@TGTKButton_OnClick), Self);
  gtk_widget_show(FLabel);
  gtk_widget_show(FImage);
  gtk_widget_show(FHBox);
  gtk_widget_show(FEventBoxLeft);
  gtk_widget_show(FEventBoxRight);
  Show;
end;

constructor TGTKImageButton.CreateWithoutLabel(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FWidget := gtk_button_new;
  FLabel := gtk_label_new('');
  FImage := gtk_image_new;
  FHBox := gtk_hbox_new(False, 2);
  FEventBoxLeft := gtk_vbox_new(false, 0);
  FEventBoxRight := gtk_vbox_new(false, 0);
  gtk_box_pack_start(PGtkBox(FHBox), FEventBoxLeft, False, False, 0);
  gtk_box_pack_start(PGtkBox(FHBox), FImage, False, False, 0);
  gtk_box_pack_start(PGtkBox(FHBox), FEventBoxRight, False, False, 0);
  gtk_container_add(PGtkContainer(FWidget), FHBox);
  g_signal_connect(PGtkObject(FWidget), 'clicked', G_CALLBACK(@TGTKButton_OnClick), Self);
  gtk_widget_show(FLabel);
  gtk_widget_show(FImage);
  gtk_widget_show(FHBox);
  gtk_widget_show(FEventBoxLeft);
  gtk_widget_show(FEventBoxRight);
  Show;
end;

function TGTKImageButton.GetCaption: string;
begin
  Result := gtk_label_get_text(PGtkLabel(FLabel));
end;

procedure TGTKImageButton.SetCaption(Value: string);
begin
  gtk_label_set_text_with_mnemonic(PGtkLabel(FLabel), PChar(Value));
end;

procedure TGTKImageButton.SetIcon(Value: TGDKPixbuf);
begin
  if Assigned(Value) and Assigned(Value.FPixbuf) then
    gtk_image_set_from_pixbuf(PGtkImage(FImage), Value.FPixbuf);
end;

procedure TGTKImageButton.SetFromStock(Stock_ID: string; IconSize: TGTKIconSize);
begin
  gtk_image_set_from_stock(PGtkImage(FImage), PChar(Stock_ID), Ord(IconSize));
end;

procedure TGTKImageButton.SetSpacing(Value: integer);
begin
  gtk_widget_set_size_request(FEventBoxLeft, Value, -1);
  gtk_widget_set_size_request(FEventBoxRight, Value, -1);
end;


(********************************************************************************************************************************)
(********************************************************************************************************************************)
constructor TGTKImageToggleButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FWidget := gtk_toggle_button_new;
  FLabel := gtk_label_new('');
  FImage := gtk_image_new;
  FHBox := gtk_hbox_new(False, 2);
  gtk_box_pack_start(PGtkBox(FHBox), FImage, False, False, 0);
  gtk_box_pack_start(PGtkBox(FHBox), FLabel, True, True, 0);
  gtk_container_add(PGtkContainer(FWidget), FHBox);
  g_signal_connect(PGtkObject(FWidget), 'clicked', G_CALLBACK(@TGTKButton_OnClick), Self);
  gtk_widget_show(FLabel);
  gtk_widget_show(FImage);
  gtk_widget_show(FHBox);
  Show;
end;

constructor TGTKImageToggleButton.CreateWithoutLabel(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FWidget := gtk_button_new;
  FLabel := gtk_label_new('');
  FImage := gtk_image_new;
  gtk_container_add(PGtkContainer(FWidget), FImage);
  g_signal_connect(PGtkObject(FWidget), 'clicked', G_CALLBACK(@TGTKButton_OnClick), Self);
  gtk_widget_show(FLabel);
  gtk_widget_show(FImage);
  Show;
end;

function TGTKImageToggleButton.GetCaption: string;
begin
  Result := gtk_label_get_text(PGtkLabel(FLabel));
end;

procedure TGTKImageToggleButton.SetCaption(Value: string);
begin
  gtk_label_set_text(PGtkLabel(FLabel), PChar(Value));
end;

procedure TGTKImageToggleButton.SetIcon(Value: TGDKPixbuf);
begin
  if Assigned(Value) and Assigned(Value.FPixbuf) then
    gtk_image_set_from_pixbuf(PGtkImage(FImage), Value.FPixbuf);
end;

procedure TGTKImageToggleButton.SetFromStock(Stock_ID: string; IconSize: TGTKIconSize);
begin
  gtk_image_set_from_stock(PGtkImage(FImage), PChar(Stock_ID), Ord(IconSize));
end;

(********************************************************************************************************************************)


end.