source: src-qt4/pc-usermanager/maindlgcode.cpp @ 1620346

9.1-release9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since 1620346 was 1620346, checked in by Kris Moore <kris@…>, 15 months ago

Initial import of PC-BSD /current/ SVN repo

  • Property mode set to 100644
File size: 12.0 KB
Line 
1/***************************************************************************
2 *   Copyright (C) 2007, 2012 by Tim McCormick   *
3 *   tim@pcbsd.org   *
4 *                                                                         *
5 *   Permission is hereby granted, free of charge, to any person obtaining *
6 *   a copy of this software and associated documentation files (the       *
7 *   "Software"), to deal in the Software without restriction, including   *
8 *   without limitation the rights to use, copy, modify, merge, publish,   *
9 *   distribute, sublicense, and/or sell copies of the Software, and to    *
10 *   permit persons to whom the Software is furnished to do so, subject to *
11 *   the following conditions:                                             *
12 *                                                                         *
13 *   The above copyright notice and this permission notice shall be        *
14 *   included in all copies or substantial portions of the Software.       *
15 *                                                                         *
16 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
17 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
18 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
20 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
21 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
22 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
23 ***************************************************************************/
24#include "maindlgcode.h"
25#include "user.h"
26#include "group.h"
27#include <QMessageBox>
28#include <QInputDialog>
29#include <QDebug>
30
31#include <qcombobox.h>
32#include <qlabel.h>
33#include <qlineedit.h>
34#include <qpushbutton.h>
35#include <qtooltip.h>
36       
37void mainDlgCode::programInit(UserManagerBackend *back, QString dir)
38{
39    setEnabled(false);
40   
41    chroot = dir;
42
43    this->back = back;
44    availablePointer = 0;
45    memberPointer = 0;
46    white = QColor(255, 255, 255);
47    red = QColor(255, 78, 78);
48    orange = QColor(255, 211, 65);
49    systemChange = false;
50   
51    updateShellList();
52    updateGroupList();
53    updateUserList();
54   
55    setEnabled(true);
56   
57    //Make connections
58    connect(groupList, SIGNAL(currentTextChanged( const QString & )), this, SLOT(getGroupDetails( const QString& )));
59    connect(newButton, SIGNAL(clicked()), this, SLOT(showAddDlg()));
60    connect(back, SIGNAL(usersChanged()), this, SLOT(usersChanged()));
61    connect(deleteButton, SIGNAL(clicked()), this, SLOT(removeUser()));
62    connect(makeMemberBut, SIGNAL(clicked()), this, SLOT(makeMember()));
63    connect(makeAvailableBut, SIGNAL(clicked()), this, SLOT(makeAvailable()));
64    connect(back, SIGNAL(groupsChanged()), this, SLOT(groupsChanged()));
65    connect(removeGroupBut, SIGNAL(clicked()), this, SLOT(removeGroup()));
66    connect(addGroupBut, SIGNAL(clicked()), this, SLOT(addGroup()));
67    connect(passwordButton, SIGNAL(clicked()), this, SLOT(changePassword()));
68    connect(fullnameBox, SIGNAL(textChanged(const QString&)), this, SLOT(fullnameChanged()));
69    connect(homeBox, SIGNAL(textChanged(const QString&)), this, SLOT(homeChanged()));
70    connect(simpleBut, SIGNAL(clicked()), this, SLOT(simplePressed()));
71    connect(shellBox, SIGNAL(activated(const QString&)), this, SLOT(shellChanged()));
72    connect(groupBox, SIGNAL(activated(const QString&)), this, SLOT(groupChanged()));
73}
74
75void mainDlgCode::updateUserList()
76{
77    qDebug() << "MainDlg: Updating user list";
78    disconnect(userList, SIGNAL(currentTextChanged( const QString & )), 0, 0);
79   
80    QStringList users = back->getAllUsers(-1, -1);
81    users.sort();
82    userList->clear();
83    userList->addItems(users);
84    userList->setCurrentRow(0);
85   
86    getUserDetails(userList->currentItem()->text());
87   
88    connect(userList, SIGNAL(currentTextChanged( const QString & )), this, SLOT(getUserDetails( const QString& )));
89   
90    //These won't always invoke first time without these lines
91    fullnameChanged();
92    homeChanged();
93}
94
95void mainDlgCode::updateShellList()
96{
97    qDebug() << "MainDlg: Updating shell list";
98    QStringList shells = back->getShells();
99    shells.sort();
100    shellBox->clear();
101    shellBox->addItems(shells);
102}
103
104void mainDlgCode::updateGroupList()
105{
106    qDebug() << "MainDlg: Updating group list";
107    disconnect(groupList, SIGNAL(currentTextChanged( const QString & )), 0, 0);
108
109    QStringList groups = back->getAllGroups(-1, -1);
110    groups.sort();
111    groupBox->clear();
112    groupBox->addItems(groups);
113    groupList->clear();
114    groupList->addItems(groups);
115    groupList->setCurrentRow(0);
116
117    getGroupDetails(groupList->currentItem()->text());
118
119    qDebug() << "MainDlg: Done with updateGroupList()";
120    connect(groupList, SIGNAL(currentTextChanged( const QString & )), this, SLOT(getGroupDetails( const QString& )));
121}
122
123void mainDlgCode::enableEdits(bool enable)
124{
125    deleteButton->setEnabled(enable);
126    fullnameBox->setEnabled(enable);
127    homeBox->setEnabled(enable);
128    groupBox->setEnabled(enable);
129}
130
131void mainDlgCode::getUserDetails(const QString &username)
132{
133    systemChange = true;
134   
135    qDebug() << "Fetching details for user: " << username;
136    User *user = back->getUser(username);
137   
138    //Disable certain form fields if the user is root, to stop changing of important fields
139    enableEdits(user->getUid() != 0);
140    //Do not allow the removal of the currently logged in user
141    if (getenv("USER") == username) deleteButton->setEnabled(false);
142   
143    QString uid = QString::number(user->getUid());
144    if (uid == "-1") { uid = tr("New User"); }
145    uidBox->setText(uid);
146    fullnameBox->setText(user->getFullname());
147    homeBox->setText(user->getHome());
148   
149    shellBox->setItemText(shellBox->currentIndex(), user->getShell());
150   
151    QString groupname;
152    if ( user->getGid() != -1 ) {
153        Group *group = back->getGroup(user->getGid());
154        groupname = group->getGroupname();
155        if (groupname == "Not Set") { groupname = username; }
156    } else {
157        groupname = username;
158    }
159    groupBox->setItemText(groupBox->currentIndex(), groupname);
160   
161    systemChange = false;
162}
163
164void mainDlgCode::getGroupDetails(const QString &groupname)
165{
166    qDebug() << "Fetching details for group: " << groupname;
167    QStringList users = back->getAllUsers(-1, -1);
168    users.sort();
169    Group *group = back->getGroup(groupname);
170
171    //Disable remove button if we're wheel
172    removeGroupBut->setEnabled(group->getGid() != 0);
173    memberPointer = memberList->currentItem();
174    availablePointer = availableList->currentItem();
175    memberList->clear();
176    availableList->clear();
177   
178    //Traverse the user list, if the user is a member place in the 'members' box, otherwise place them in the 'available' box.
179    for (QStringList::Iterator it = users.begin(); it != users.end(); it++)
180    {
181        if (group->isMember(*it) == true) { memberList->addItem(*it); }
182        else { availableList->addItem(*it); }
183    }
184    //memberList->setCurrentItem(memberPointer);
185    //availableList->setCurrentItem(availablePointer);
186}
187
188void mainDlgCode::showAddDlg()
189{
190    addui = new AddDlgCode();
191    addui->programInit(back);
192    addui->show();
193}
194
195UserManagerBackend* mainDlgCode::getBackend()
196{
197    return back;
198}
199
200void mainDlgCode::usersChanged()
201{
202    updateGroupList();
203    updateUserList();
204    emit changed(true);
205}
206
207void mainDlgCode::removeUser()
208{
209    QString username = userList->currentItem()->text();
210    User *user = back->getUser(username);
211    QString home = user->getHome();
212    int answer = QMessageBox::question(this,
213                                       tr("Delete Home Directory?"),
214                                       tr("Delete the home directory for ") + username + " (" + home + ")?" ,
215                                       QMessageBox::Yes | QMessageBox::No,
216                                       QMessageBox::No);
217    if(answer == QMessageBox::Yes) user->setDeleteHome(true);
218    back->deleteUser(username);
219}
220
221void mainDlgCode::makeAvailable()
222{
223    back->removeUserFromGroup(memberList->currentItem()->text(), groupList->currentItem()->text());
224}
225
226void mainDlgCode::makeMember()
227{
228    back->addUserToGroup(availableList->currentItem()->text(), groupList->currentItem()->text());
229}
230
231void mainDlgCode::groupsChanged()
232{
233    updateGroupList();
234    emit changed(true);
235}
236
237void mainDlgCode::removeGroup()
238{
239    back->deleteGroup(groupList->currentItem()->text());
240}
241
242void mainDlgCode::addGroup()
243{
244    int valid = -1;
245    QString group = "";
246    while (valid != 0)
247    {
248        switch(valid)
249        {
250            case 1:
251                QMessageBox::critical(this, tr("Error adding group..."), tr("You must specify a name for your group"));
252                break;
253            case 2:
254                QMessageBox::critical(this, tr("Error adding group..."), tr("Invalid group name specified. Group names may contain only letters and number. Spaces are NOT permitted"));
255                break;
256            case 3:
257                QMessageBox::critical(this, tr("Error adding group..."), tr("The group '") + group + tr("' already exists, please choose a different name"));
258                break;
259        }
260       
261        group = QInputDialog::getText(this, tr("New Group"), tr("Please enter a name for your new group"));
262        if (group == QString::null) { return; }
263        valid = back->validateGroupname(group);
264    }
265    back->addGroup(group);
266}
267
268void mainDlgCode::changePassword()
269{
270    changePassUi = new ChangePassCode();
271    changePassUi->programInit(userList->currentItem()->text(), back);
272    changePassUi->show();
273}
274
275void mainDlgCode::homeChanged()
276{
277    User *user = back->getUser(userList->currentItem()->text());
278   
279    int homeError = back->validateHome(homeBox->text());
280   
281    QColor homeBoxColour = white;
282    homeBox->setToolTip(tr("Directory will be used"));
283   
284    switch (homeError)
285    {
286    case 1:
287        homeBox->setToolTip(tr("The field is empty"));
288        break;
289    case 2:
290        homeBoxColour = red;
291        homeBox->setToolTip(tr("Invalid Path!"));
292        break;
293    case 3:
294        homeBox->setToolTip(tr("Directory does not exist, but will be made"));
295        break;
296    case 4:
297        homeBoxColour = orange;
298        homeBox->setToolTip(tr("Placing home directory outside '/home/' can cause compatibility problems with some programs. NOT RECOMMENDED"));
299        break;
300    case 5:
301        homeBoxColour = red;
302        homeBox->setToolTip(tr("Requested path is a file (not a directory!)"));
303        break;
304    }
305   
306    if ((! systemChange) && (homeError != 5) && (homeError != 2))
307    {
308        user->setHome(homeBox->text());
309        user->setChanges();
310        emit changed(true);
311    }
312   
313    QPalette bgPal(homeBoxColour);
314    bgPal.setColor(QPalette::Window,homeBoxColour);
315    homeBox->setPalette(bgPal);
316}
317
318void mainDlgCode::fullnameChanged()
319{
320    User *user = back->getUser(userList->currentItem()->text());
321   
322    int fullnameError = back->validateFullname(fullnameBox->text());
323   
324    QColor fullnameBoxColour = white;
325    fullnameBox->setToolTip(tr("Good Input"));
326   
327    switch(fullnameError)
328    {
329    case 1:
330        fullnameBox->setToolTip(tr("The field is empty"));
331        break;
332    case 2:
333        fullnameBoxColour = red;
334        fullnameBox->setToolTip(tr("This field may not contain ! @ or :"));
335        break;
336    }
337   
338    if ((! systemChange) && (fullnameError != 2))
339    {
340        user->setFullname(fullnameBox->text());
341        user->setChanges();
342        emit changed(true);
343    }
344   
345    QPalette bgPal(fullnameBoxColour);
346    bgPal.setColor(QPalette::Window,fullnameBoxColour);
347    fullnameBox->setPalette(bgPal);
348}
349
350void mainDlgCode::simplePressed()
351{
352    emit simpleView();
353}
354
355void mainDlgCode::shellChanged()
356{
357    User *user = back->getUser(userList->currentItem()->text());
358   
359    if (! systemChange)
360    {
361        user->setShell(shellBox->currentText());
362        user->setChanges();
363        emit changed(true);
364    }
365}
366
367void mainDlgCode::groupChanged()
368{
369    User *user = back->getUser(userList->currentItem()->text());
370   
371    if (! systemChange)
372    {
373        user->setGid(back->getGroup(groupBox->currentText())->getGid());
374        user->setChanges();
375        emit changed(true);
376    }
377}
378
Note: See TracBrowser for help on using the repository browser.