-
Notifications
You must be signed in to change notification settings - Fork 5
/
models.cpp
executable file
·458 lines (408 loc) · 15.3 KB
/
models.cpp
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include "models.h"
/**
* Constructor κλάσης
*/
Models::Models()
{}
/**
* Destructor κλάσης
*/
Models::~Models()
{}
/**
* Δημιουργεί το μοντέλο λίστας των χρηστών το οποίο εμφανίζεται στην κύρια φόρμα της εφαρμογής.
*/
QStandardItemModel *Models::createUsersModel( )
{
setpwent();
struct passwd *users = NULL;
struct finfo info;
QStandardItemModel *model = new QStandardItemModel( 0, 6 );
model->setHeaderData( 0, Qt::Horizontal, QObject::tr( "UID" ) );
model->setHeaderData( 1, Qt::Horizontal, QObject::tr( "Login Name" ) );
model->setHeaderData( 2, Qt::Horizontal, QObject::tr( "Real Name" ) );
model->setHeaderData( 3, Qt::Horizontal, QObject::tr( "Home Directory" ) );
model->setHeaderData( 4, Qt::Horizontal, QObject::tr( "Default Shell" ) );
model->setHeaderData( 5, Qt::Horizontal, QObject::tr( "Disk Usage" ) );
Users *us = new Users();
while (( users = getpwent() ) )
{
us->passwd_parse(getpwnam(users->pw_name),&info);
model->insertRow( 0 );
QModelIndex UID = model->index( 0, 0 );
QModelIndex loginName = model->index( 0, 1 );
QModelIndex realName = model->index( 0, 2 );
QModelIndex homeDir = model->index( 0, 3 );
QModelIndex defaultShell = model->index( 0, 4 );
QModelIndex diskUsage = model->index( 0, 5 );
QString userName = QString::fromLatin1( users->pw_name );
if (us->isLocked(userName))
model->setData(UID,QVariant(QImage("/usr/share/apps/UserManager/pics/lock.png")),Qt::DecorationRole);
else
model->setData(UID,QVariant(QImage("/usr/share/apps/UserManager/pics/enabled.jpeg")),Qt::DecorationRole);
model->setData( UID, users->pw_uid ) ;
model->setData( loginName, users->pw_name );
model->setData( realName, info.full_name);
model->setData( homeDir, users->pw_dir );
model->setData( defaultShell, users->pw_shell );
model->setData( diskUsage, QVariant( QString( " " ) ) );
}
endpwent();
if (us != nullptr){ delete us; us = nullptr;}
return model;
}
/**
* Δημιουργεί το μοντέλο λίστας των χρηστών στο οποίο εμφανίζεται και η κατανάλωση δίσκου του κάθε χρήστη.
*/
QStandardItemModel *Models::createUsersModelWithSizes( QStringList sizeList )
{
setpwent();
struct finfo info;
struct passwd *users = NULL;
QStandardItemModel *model = new QStandardItemModel( 0, 6 );
model->setHeaderData( 0, Qt::Horizontal, QObject::tr( "UID" ) );
model->setHeaderData( 1, Qt::Horizontal, QObject::tr( "Login Name" ) );
model->setHeaderData( 2, Qt::Horizontal, QObject::tr( "Real Name" ) );
model->setHeaderData( 3, Qt::Horizontal, QObject::tr( "Home Directory" ) );
model->setHeaderData( 4, Qt::Horizontal, QObject::tr( "Default Shell" ) );
model->setHeaderData( 5, Qt::Horizontal, QObject::tr( "Disk Usage" ) );
int i=0;
Users *us { new Users()} ;
while (( users = getpwent() ) )
{
us->passwd_parse(getpwnam(users->pw_name),&info);
model->insertRow( 0 );
QModelIndex UID = model->index( 0, 0 );
QModelIndex loginName = model->index( 0, 1 );
QModelIndex realName = model->index( 0, 2 );
QModelIndex homeDir = model->index( 0, 3 );
QModelIndex defaultShell = model->index( 0, 4 );
QModelIndex diskUsage = model->index( 0, 5 );
QString userName = QString::fromLatin1( users->pw_name );
if (us->isLocked(userName))
model->setData(UID,QVariant(QImage("/usr/share/apps/UserManager/pics/lock.png")),Qt::DecorationRole);
else
model->setData(UID,QVariant(QImage("/usr/share/apps/UserManager/pics/enabled.jpeg")),Qt::DecorationRole);
model->setData( UID, users->pw_uid ) ;
model->setData( loginName, users->pw_name );
model->setData( realName, info.full_name);
model->setData( homeDir, users->pw_dir );
model->setData( defaultShell, users->pw_shell );
model->setData( diskUsage,sizeList[i]);
i++;
}
endpwent();
delete us;
us=nullptr;
return model;
}
/**
* Δημιουργεί το μοντέλο λίστας των ομάδων του σύστηματος.
*/
QStandardItemModel *Models::createGroupsModel( )
{
struct group *groups = NULL;
setgrent();
QStandardItemModel *model = new QStandardItemModel( 0, 2 );
model->setHeaderData( 0, Qt::Horizontal, QObject::tr( "GID" ) );
model->setHeaderData( 1, Qt::Horizontal, QObject::tr( "Group Name" ) );
while (( groups = getgrent() ) )
{
model->insertRow( 0 );
QModelIndex GID = model->index( 0, 0 );
QModelIndex groupName = model->index( 0, 1 );
model->setData( GID, groups->gr_gid ) ;
model->setData( groupName, groups->gr_name );
}
endgrent();
return model;
}
/**
* Δημιουργεί το μοντέλο του χρήστη που αναζητήθηκε στο σύστημα.
*/
QStandardItemModel *Models::createFoundedGroupModel( struct group *grp )
{
setgrent();
QStandardItemModel *model = new QStandardItemModel( 0, 2 );
model->setHeaderData( 0, Qt::Horizontal, QObject::tr( "GID" ) );
model->setHeaderData( 1, Qt::Horizontal, QObject::tr( "Group Name" ) );
model->insertRow( 0 );
QModelIndex GID = model->index( 0, 0 );
QModelIndex groupName = model->index( 0, 1 );
model->setData( GID, grp->gr_gid ) ;
model->setData( groupName, grp->gr_name );
endgrent();
return model;
}
/**
* Δημιουργεί το μοντέλο της ομάδας που αναζητήθηκε στο σύστημα.
*/
QStandardItemModel *Models::createFoundedUserModel( struct passwd *usr )
{
Users *user = new Users();
QString sizeString;
QStandardItemModel *model = new QStandardItemModel( 0, 6 );
model->setHeaderData( 0, Qt::Horizontal, QObject::tr( "UID" ) );
model->setHeaderData( 1, Qt::Horizontal, QObject::tr( "Login Name" ) );
model->setHeaderData( 2, Qt::Horizontal, QObject::tr( "Real Name" ) );
model->setHeaderData( 3, Qt::Horizontal, QObject::tr( "Home Directory" ) );
model->setHeaderData( 4, Qt::Horizontal, QObject::tr( "Default Shell" ) );
model->setHeaderData( 5, Qt::Horizontal, QObject::tr( "Disk Usage" ) );
{
model->insertRow( 0 );
QModelIndex UID = model->index( 0, 0 );
QModelIndex loginName = model->index( 0, 1 );
QModelIndex realName = model->index( 0, 2 );
QModelIndex homeDir = model->index( 0, 3 );
QModelIndex defaultShell = model->index( 0, 4 );
QModelIndex diskUsage = model->index( 0, 5 );
model->setData( UID, usr->pw_uid ) ;
model->setData( loginName, usr->pw_name );
model->setData( realName, usr->pw_gecos );
model->setData( homeDir, usr->pw_dir );
model->setData( defaultShell, usr->pw_shell );
char *dir = usr->pw_dir;
string dirString = dir;
char path[PATH_MAX] = "\0"; //PATH_MAX == 4096 xarakthres megisth diadromh.arxikopoihsh me \0 giati alliws exei skpoupidia kai mperdeyetai h getSize
for ( size_t i = 0; i < dirString.length(); i++ )//string se char[].to i size_t gia na sygkrinei to size_t pou epistrefei to length().
{
path[i] = dirString[i];
}
if ( path[strlen( path )-1] != '/' )
strncat( path, "/" , strlen(path) );
//totalSize = 0;//mhdenismos giati merikoi home fakeloi den yparxoun,kai an den mhdenistei krataei to mege8os tou teleytaiou fakelou poy yphrxe kai metrh8hke
uint64_t size = user->getSize( path );
if ( size < 1000 && size > 0 ) //if < 1kb
{
sizeString.setNum( size ) ;//bytes
sizeString.append( " B" );
}
else
if ( size < 1000000 && size > 1000 ) //if < 1mb
{
uint64_t finalSize = ( size / 1024 );//bytes->kb
sizeString.setNum( finalSize ) ;
sizeString.append( " KB" );
}
else
if ( size > 1000000 ) //if > 1mb
{
uint64_t finalSize = ( size / 1024 / 1024 );//bytes->mb
sizeString.setNum( finalSize ) ;
sizeString.append( " MB" );
}
else
sizeString = "-"; //an totalsize=0 h den yparxei o fakelos
model->setData( diskUsage, sizeString );
}
if (user != nullptr) { delete user; user = nullptr; }
return model;
}
/**
* Δημιουργεί το μοντέλο των χρηστών που είναι μέλη σε μια ομάδα.
*/
QStandardItemModel *Models::createMembersModel ( const char *groupName )
{
char **members;
setpwent();
struct passwd * user=NULL;
struct finfo info;
struct group *grp = getgrnam ( groupName );
members = grp->gr_mem;
Users *us = new Users();
QStandardItemModel *model = new QStandardItemModel ( 0, 3 );
model->setHeaderData ( 0, Qt::Horizontal, QObject::tr ( "UID" ) );
model->setHeaderData ( 1, Qt::Horizontal, QObject::tr ( "Login Name" ) );
model->setHeaderData ( 2, Qt::Horizontal, QObject::tr ( "Real Name" ) );
while ( *members )
{
user = getpwnam ( *members );
if ( user==NULL ) //an exei meinei akyrh eggrafh enos xrhsth san member sto etc/group enw o xrhsths den yparxei sto etc/passwd
members++;
else
{
model->insertRow ( 0 );
QModelIndex UID = model->index ( 0, 0 );
QModelIndex loginName = model->index ( 0, 1 );
QModelIndex realName = model->index ( 0, 2 );
model->setData ( UID, user->pw_uid ) ;
model->setData ( loginName, * ( members ) );
us->passwd_parse ( getpwnam ( *members ),&info );
model->setData ( realName, info.full_name ) ;
members++;
}
}
user=NULL;
setpwent();
while ( ( user=getpwent() ) )
{
if ( user->pw_gid==grp->gr_gid )
{
model->insertRow ( 0 );
QModelIndex UID = model->index ( 0, 0 );
QModelIndex loginName = model->index ( 0, 1 );
QModelIndex realName = model->index ( 0, 2 );
model->setData ( UID, user->pw_uid ) ;
model->setData ( UID,QVariant ( QImage ( "/usr/share/apps/UserManager/pics/p.jpeg" ) ),Qt::DecorationRole );
model->setData ( loginName, user->pw_name );
us->passwd_parse ( getpwnam ( user->pw_name ),&info );
model->setData ( realName, info.full_name ) ;
}
}
endpwent();
if(us != nullptr){ delete us; us = nullptr; }
return model;
}
/**
* Δημιουργεί το μοντέλο των ομάδων στις οποίες είναι μέλος ο χρήστης
*/
QStandardItemModel *Models::UsersInGroupModel ( const char *groupName )
{
//idia me thn createMembersModel alla xwris na emfanizei ton omonymo xrhsth pou epeidh einai kai o idiokthths tou group den mporoume na ton kanoume remove ap to group tou
char **members=NULL;
struct passwd * user=NULL;
QStandardItemModel *model = new QStandardItemModel ( 0,1 );
model->setHeaderData ( 0, Qt::Horizontal, QObject::tr ( "*Users IN Group*" ) );
//vhma vhma
//1)pairnw thn domh tou group gia to opoio 8elw na vrw ta member.2)an yparxoun melh tote paw kai sygkrinw to onoma tou member me ayto tou group gia to opoio psaxnw kai an einai idio shmainei oti to member einai kai o idiokthths tous group ara den to vazw sthn lista giati oute na ton vgalw apo to group tou ginetai oute na ton valw (afou einai hdh mesa).3)an den einai o idiokthths paw kai vazw ena ena ta member mesa sto montelo
struct group *grp = getgrnam ( groupName );
members = grp->gr_mem;
if ( *members!=NULL )
{
while ( *members )
{
user = getpwnam ( *members );
if ( user!=NULL )//an yparxei akyrh eggrafh sto passwd
{
if ( user->pw_gid==grp->gr_gid ) //elegxos gia na mpei mpei o idiokthths tou group sth lista
members++;
else
{
model->insertRow ( 0 );
QModelIndex userName = model->index ( 0, 0 );
model->setData ( userName, * ( members ) );
members++;
}
}
else
members++;
}
}
return model;
}
/**
* Δημιουργεί το μοντέλο των ομάδων στις οποίες ΔΕΝ είναι μέλος ο χρήστης
*/
QStandardItemModel *Models::UsersNotInGroupModel ( const char *groupName )
{
char **members=NULL;
struct passwd * user=NULL;
QStandardItemModel *model = new QStandardItemModel ( 0,1 );
struct group *grp = NULL;
model->setHeaderData ( 0, Qt::Horizontal, QObject::tr ( "*Users NOT In Group*" ) );
//vhma vhma
//perna ena ena ta member kai meta gia ka8e ena xrhsth tou systhmatos koitaei an einai den einai member me thn [strcmp ( *members, user->pw_name ) != 0]
//an den einai ton vazei sto montelo
//an paei meta to else shmainei oti ta *members==NULL kai ara to group den exei kanena melos mesa.ara me thn getpwent paw kai pairnw enan enan olous tous user apo to systhma kai tous xwnw sto montelo.pali ginetai o elegxos gia na mhn mpei o idiokthths tou group
grp = getgrnam ( groupName );
members = grp->gr_mem;
if ( *members!=NULL )
{
setpwent();
while ( ( user = getpwent() ) )
{
bool found=true;
if ( user->pw_gid==grp->gr_gid ) //elegxos an einai to primary kapoiu xrhsth.an einai den xreiazetai na mpei sth lista me tous nonmembers apla anti na fainetai sto groups fainetai sto 3 pedio ap to passwd.(to pw_gid) pou krataei to primary group tou ka8e user
goto usersPrimary;
members = grp->gr_mem;//xanagemizei ta members gia sygkrish me ton epomeno user
while ( *members )
{
if ( strcmp ( *members, user->pw_name ) != 0 )
found=false;
else
{
found=true;//an vre8hke den einai melos ara break gia na meinei true kai na mhn allaxei se false ap thn epomenh sygkrish
break;
}
members++;
}
usersPrimary:
if ( found==false ) //osoi user den vre8hkan mpainoun sto montelo
{
model->insertRow ( 0 );
QModelIndex userName = model->index ( 0, 0 );
model->setData ( userName, user->pw_name );
}
}
endpwent();
}
else
{
setpwent();
while ( ( user = getpwent() ) )
{
if ( user->pw_uid!=grp->gr_gid ) //elegxos an einai to primary kapoiu xrhsth.an einai den xreiazetai na mpei sth lista me tous nonmembers giati einai member kai apla anti na fainetai sto groups fainetai sto 3 pedio ap to passwd.(to pw_gid) pou krataei to primary group tou ka8e user
{
model->insertRow ( 0 );
QModelIndex userName = model->index ( 0, 0 );
model->setData ( userName, user->pw_name );
}
else
{}
}
endpwent();
}
return model;
}
/**
* Δημιουργεί ένα μοντέλο με όλες τις ομάδες του συστήματος και δίπλα στην κάθε μια τοποθετεί ένα checkbox,ανάλογα με τον αν ο χρήστης του ορίσματος είναι μέλος στην εκάστοτε ομάδα ή όχι.
*/
QStandardItemModel *Models::createUserInGroupsModel ( QString username )
{
struct group *grp=NULL;
struct passwd *user=NULL;
QStandardItemModel *model = new QStandardItemModel ( 0, 2 );
user=getpwnam ( username.toLatin1().data() );
char **members=NULL;
model->setHeaderData ( 1, Qt::Horizontal, QObject::tr ( "Groups" ) );
model->setHeaderData ( 0, Qt::Horizontal, QObject::tr ( "" ) );
setgrent();
while ( ( grp = getgrent() ) )
{
bool found=false;
QModelIndex checkbox = model->index ( 0, 0 );
QModelIndex groupname = model->index ( 0, 1 );
//QModelIndex pic = model->index ( 0, 2 );
if ( ( grp->gr_gid==user->pw_gid ) )
goto resume;//an vrei to primary group tou na mhn to emfanisei sto montelo giati to emfanizei hdh sto label primary group apo katw
members = grp->gr_mem;
if ( *members==NULL )
{
model->setData ( checkbox, Qt::Unchecked, Qt::CheckStateRole );
model->setData ( groupname, grp->gr_name );
found=true;
}
while ( *members )
{
QString mem ( *members );
if ( mem==username )
{
model->setData ( checkbox, Qt::Checked, Qt::CheckStateRole );
model->setData ( groupname, grp->gr_name );
found=true;
break;
}
members++;
}
if ( !found )
{
model->setData ( checkbox, Qt::Unchecked, Qt::CheckStateRole );
model->setData ( groupname, grp->gr_name );
}
model->insertRow ( 0 );
resume:
{}
}
endgrent();
return model;
}