summaryrefslogtreecommitdiff
path: root/noncore/settings/usermanager/userdialog.cpp
Unidiff
Diffstat (limited to 'noncore/settings/usermanager/userdialog.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/usermanager/userdialog.cpp319
1 files changed, 319 insertions, 0 deletions
diff --git a/noncore/settings/usermanager/userdialog.cpp b/noncore/settings/usermanager/userdialog.cpp
new file mode 100644
index 0000000..892fc8a
--- a/dev/null
+++ b/noncore/settings/usermanager/userdialog.cpp
@@ -0,0 +1,319 @@
1/***************************************************************************
2 * *
3 * This program is free software; you can redistribute it and/or modify *
4 * it under the terms of the GNU General Public License as published by *
5 * the Free Software Foundation; either version 2 of the License, or *
6 * (at your option) any later version. *
7 * *
8 ***************************************************************************/
9
10#include "userdialog.h"
11
12#include <qlayout.h>
13#include <qlabel.h>
14#include <qmessagebox.h>
15
16#include <stdlib.h>
17
18#include <stdio.h>
19#include <sys/types.h>
20#include <sys/wait.h>
21#include <unistd.h>
22#include <signal.h>
23
24#include "passwd.h"
25
26/**
27 * UserDialog constructor. Setup the dialog, fill the groupComboBox & groupsListView with all groups.
28 *
29 */
30UserDialog::UserDialog(QWidget* parent, const char* name, bool modal, WFlags fl) : QDialog(parent, name, modal, fl) {
31 QVBoxLayout *layout = new QVBoxLayout(this);
32 myTabWidget=new QTabWidget(this,"User Tab Widget");
33 layout->addWidget(myTabWidget);
34 setupTab1();
35 setupTab2();
36
37 // And also fill the listview & the combobox with all available groups.
38 for( QStringList::Iterator it = accounts->groupStringList.begin(); it!=accounts->groupStringList.end(); ++it) {
39 accounts->splitGroupEntry(*it);
40 new QListViewItem(groupsListView,accounts->gr_name);
41 groupComboBox->insertItem(accounts->gr_name);
42 }
43
44 showMaximized();
45}
46
47/**
48 * Empty destructor.
49 *
50 */
51UserDialog::~UserDialog() {
52}
53
54/**
55 * Creates the first tab, all userinfo is here.
56 *
57 */
58void UserDialog::setupTab1() {
59 QPixmap mypixmap;
60 QWidget *tabpage = new QWidget(myTabWidget,"page1");
61 QVBoxLayout *layout = new QVBoxLayout(tabpage);
62 layout->setMargin(5);
63
64 // Picture
65 picturePushButton = new QPushButton(tabpage,"Label");
66 picturePushButton->setMinimumSize(48,48);
67 picturePushButton->setMaximumSize(48,48);
68 picturePushButton->setPixmap(Resource::loadPixmap("userconfig/usericon"));// Load default usericon.
69 connect(picturePushButton,SIGNAL(clicked()),this,SLOT(clickedPicture()));// Clicking the picture should invoke pictureselector.
70
71 // Login
72 QLabel *loginLabel=new QLabel(tabpage,"Login: ");
73 loginLabel->setText("Login: ");
74 loginLineEdit=new QLineEdit(tabpage,"Login: ");
75
76 // UID
77 QLabel *uidLabel=new QLabel(tabpage,"uid: ");
78 uidLabel->setText("UserID: ");
79 uidLineEdit=new QLineEdit(tabpage,"uid: ");
80 uidLineEdit->setEnabled(false);
81
82 // Username (gecos)
83 QLabel *gecosLabel=new QLabel(tabpage,"gecos");
84 gecosLabel->setText("Username: ");
85 gecosLineEdit=new QLineEdit(tabpage,"gecos");
86
87 // Password
88 QLabel *passwordLabel=new QLabel(tabpage,"password");
89 passwordLabel->setText("Password: ");
90 passwordLineEdit=new QLineEdit(tabpage,"password");
91 passwordLineEdit->setEchoMode(QLineEdit::Password);
92
93 // Shell
94 QLabel *shellLabel=new QLabel(tabpage,"shell");
95 shellLabel->setText("Shell: ");
96 shellComboBox=new QComboBox(tabpage,"shell");
97 shellComboBox->setEditable(true);
98 shellComboBox->insertItem("/bin/sh");
99 shellComboBox->insertItem("/bin/ash");
100 shellComboBox->insertItem("/bin/false");
101
102 // Primary Group
103 QLabel *groupLabel=new QLabel(tabpage,"group");
104 groupLabel->setText("Primary group: ");
105 groupComboBox=new QComboBox(tabpage,"PrimaryGroup");
106
107 // Widget layout
108 QHBoxLayout *hlayout=new QHBoxLayout(-1,"hlayout");
109 layout->addWidget(picturePushButton);
110 layout->addSpacing(5);
111 layout->addLayout(hlayout);
112 QVBoxLayout *vlayout1=new QVBoxLayout(-1,"vlayout1");
113 QVBoxLayout *vlayout2=new QVBoxLayout(-1,"vlayout2");
114 // First column, labels
115 vlayout1->addWidget(loginLabel);
116 vlayout1->addSpacing(5);
117 vlayout1->addWidget(uidLabel);
118 vlayout1->addSpacing(5);
119 vlayout1->addWidget(gecosLabel);
120 vlayout1->addSpacing(5);
121 vlayout1->addWidget(passwordLabel);
122 vlayout1->addSpacing(5);
123 vlayout1->addWidget(shellLabel);
124 vlayout1->addSpacing(5);
125 vlayout1->addWidget(groupLabel);
126 // Second column, data
127 vlayout2->addWidget(loginLineEdit);
128 vlayout2->addSpacing(5);
129 vlayout2->addWidget(uidLineEdit);
130 vlayout2->addSpacing(5);
131 vlayout2->addWidget(gecosLineEdit);
132 vlayout2->addSpacing(5);
133 vlayout2->addWidget(passwordLineEdit);
134 vlayout2->addSpacing(5);
135 vlayout2->addWidget(shellComboBox);
136 vlayout2->addSpacing(5);
137 vlayout2->addWidget(groupComboBox);
138 hlayout->addLayout(vlayout1);
139 hlayout->addLayout(vlayout2);
140
141 myTabWidget->addTab(tabpage,"User Info");
142}
143
144/**
145 * Creates the second tab containing additional groups for the user.
146 *
147 */
148void UserDialog::setupTab2() {
149 QWidget *tabpage = new QWidget(myTabWidget,"page2");
150 QVBoxLayout *layout = new QVBoxLayout(tabpage);
151 layout->setMargin(5);
152
153 // Additional groups
154 groupsListView=new QListView(tabpage,"groups");
155 groupsListView->addColumn("Additional groups");
156 groupsListView->setColumnWidthMode(0,QListView::Maximum);
157 groupsListView->setMultiSelection(true);
158
159 layout->addSpacing(5);
160 // Grouplist
161 layout->addWidget(groupsListView);
162
163 myTabWidget->addTab(tabpage,"User Groups");
164}
165
166/**
167 * Static function that creates the userinfo dialog.
168 * The user will be prompted to add a user.
169 *
170 * @param uid This is a suggested available UID.
171 * @param gid This is a suggested available GID.
172 *
173 * @return <code>true</code> if the user was successfully added, otherwise <code>false</code>.
174 *
175 */
176bool UserDialog::addUser(int uid, int gid) {
177 UserDialog *adduserDialog=new UserDialog();
178 adduserDialog->setCaption(tr("Add User"));
179 adduserDialog->userID=uid;// Set next available UID as default uid.
180 adduserDialog->groupID=gid;// Set next available GID as default gid.
181 // Insert default group into groupComboBox
182 adduserDialog->groupComboBox->insertItem("<create new group>",0);
183 adduserDialog->uidLineEdit->setText(QString::number(uid));
184 // Show the dialog!
185 if(!(adduserDialog->exec())) return false;
186 if((adduserDialog->groupComboBox->currentItem()!=0)) {
187 accounts->findGroup(adduserDialog->groupComboBox->currentText());
188 adduserDialog->groupID=accounts->gr_gid;
189 qWarning(QString::number(accounts->gr_gid));
190 }
191 if(!(accounts->addUser(adduserDialog->loginLineEdit->text(), adduserDialog->passwordLineEdit->text(),
192 adduserDialog->uidLineEdit->text().toInt(), adduserDialog->groupID, adduserDialog->gecosLineEdit->text(),
193 QString("/home/")+adduserDialog->loginLineEdit->text() , adduserDialog->shellComboBox->currentText()))) {
194 QMessageBox::information(0,"Ooops!","Something went wrong!\nUnable to add user.");
195 return false;
196 }
197
198 // Add User to additional groups.
199 QListViewItemIterator it( adduserDialog->groupsListView );
200 for ( ; it.current(); ++it ) {
201 if ( it.current()->isSelected() )
202 accounts->addGroupMember(it.current()->text(0),adduserDialog->loginLineEdit->text());
203 }
204 return true;
205}
206
207/**
208 * Deletes the user account.
209 *
210 * @param username User to be deleted.
211 *
212 * @return <code>true</code> if the user was successfully deleted, otherwise <code>false</code>.
213 *
214 */
215bool UserDialog::delUser(const char *username) {
216 if((accounts->findUser(username))) {// Does that user exist?
217 if(!(accounts->delUser(username))) {// Delete the user.
218 QMessageBox::information(0,"Ooops!","Something went wrong\nUnable to delete user: "+QString(username)+".");
219 }
220 } else {
221 QMessageBox::information(0,"Invalid Username","That username ("+QString(username)+")does not exist.");
222 return false;
223 }
224 return true;
225}
226
227/**
228 * This displays a confirmation dialog wether a user should be deleted or not.
229 * (And also deletes the account)
230 *
231 * @param username User to be deleted.
232 *
233 * @return <code>true</code> if the user was successfully deleted, otherwise <code>false</code>.
234 *
235 */
236bool UserDialog::editUser(const char *username) {
237 UserDialog *edituserDialog=new UserDialog();// Create Dialog
238 edituserDialog->setCaption(tr("Edit User"));
239 accounts->findUser(username);// Locate user in database and fill variables in 'accounts' object.
240 accounts->findGroup(accounts->pw_gid);// Locate the user's primary group, and fill group variables in 'accounts' object.
241 // Fill widgets with userinfo.
242 edituserDialog->loginLineEdit->setText(accounts->pw_name);
243 edituserDialog->uidLineEdit->setText(QString::number(accounts->pw_uid));
244 edituserDialog->gecosLineEdit->setText(accounts->pw_gecos);
245 // Set password to '........', we will later check if this still is the contents, if not, the password has been changed.
246 edituserDialog->passwordLineEdit->setText("........");
247 // If this user is not using /bin/sh,/bin/ash or /bin/false as shell, add that entry to the shell-combobox.
248 if(accounts->pw_shell!="/bin/sh" && accounts->pw_shell!="/bin/ash" && accounts->pw_shell!="/bin/false") {
249 edituserDialog->shellComboBox->insertItem(accounts->pw_shell,0);
250 edituserDialog->shellComboBox->setCurrentItem(0);
251 }
252 // Select the primary group for this user.
253 for(int i=0;i<edituserDialog->groupComboBox->count();++i) {
254 if(accounts->gr_name==edituserDialog->groupComboBox->text(i)) {
255 edituserDialog->groupComboBox->setCurrentItem(i);
256 }
257 }
258 // Select the groups in the listview, to which the user belongs.
259 QRegExp userRegExp(QString("[:\\s]%1\\s").arg(username));
260 QStringList tempList=accounts->groupStringList.grep(userRegExp);// Find all entries in the group database, that the user is a member of.
261 for(QStringList::Iterator it=tempList.begin(); it!=tempList.end(); ++it) {// Iterate over all of them.
262 QListViewItemIterator lvit( edituserDialog->groupsListView );// Compare to all groups.
263 for ( ; lvit.current(); ++lvit ) {
264 if(lvit.current()->text(0)==(*it).left((*it).find(":"))) {
265 lvit.current()->setSelected(true);// If we find a line with that groupname, select it.;
266 }
267 }
268 }
269
270 if(!(edituserDialog->exec())) return false;// SHOW THE DIALOG!
271
272 accounts->findUser(username);// Fill user variables in 'acccounts' object.
273 accounts->pw_name=edituserDialog->loginLineEdit->text();
274 // Has the password been changed ? Make a new "crypt":ed password.
275 if(edituserDialog->passwordLineEdit->text()!="........") accounts->pw_passwd=crypt(edituserDialog->passwordLineEdit->text(), accounts->crypt_make_salt());
276
277 // Set all variables in accounts object, that will be used when calling 'updateUser()'
278 accounts->pw_uid=edituserDialog->uidLineEdit->text().toInt();
279 accounts->findGroup(edituserDialog->groupComboBox->currentText());// Fill all group variables in 'accounts' object.
280 accounts->pw_gid=accounts->gr_gid;
281 accounts->pw_gecos=edituserDialog->gecosLineEdit->text();
282 accounts->pw_shell=edituserDialog->shellComboBox->currentText();
283 // Update userinfo, using the information stored in the user variables stored in the accounts object.
284 accounts->updateUser(username);
285
286 // Remove user from all groups he/she is a member of. (could be done in a better way I guess, this was simple though.)
287 for(QStringList::Iterator it=tempList.begin(); it!=tempList.end(); ++it) {
288 accounts->delGroupMember((*it).left((*it).find(":")),username);
289 }
290
291 // Add User to additional groups that he/she is a member of.
292 QListViewItemIterator it( edituserDialog->groupsListView );
293 for ( ; it.current(); ++it ) {
294 if ( it.current()->isSelected() )
295 accounts->addGroupMember(it.current()->text(0),edituserDialog->loginLineEdit->text());
296 }
297 return true;
298}
299
300/**
301 * "OK" has been clicked. Verify some information before closing the dialog.
302 *
303 */
304void UserDialog::accept() {
305 // Add checking... valid username? username taken?
306 if(loginLineEdit->text().isEmpty()) {
307 QMessageBox::information(0,"Empty Login","Please enter a login.");
308 return;
309 }
310 QDialog::accept();
311}
312
313/**
314 * This slot is called when the usericon is clicked, this loads (should) the iconselector.
315 *
316 */
317void UserDialog::clickedPicture() {
318 QMessageBox::information(0,"Sorry!","Icon selection not yet implemented.\nComming real soon now! (tm)");
319}