source: src-qt4/pc-usermanager/simpleaddcode.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@…>, 16 months ago

Initial import of PC-BSD /current/ SVN repo

  • Property mode set to 100644
File size: 7.3 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 "simpleaddcode.h"
25       
26#include <qlineedit.h>
27#include <qtooltip.h>
28#include <qpushbutton.h>
29#include <QMessageBox>
30#include <QDebug>
31       
32void SimpleAddCode::programInit(UserManagerBackend *back)
33{
34    this->back = back;
35    white = QColor(255, 255, 255);
36    red = QColor(255, 78, 78);
37   
38    connect(usernameBox, SIGNAL(textChanged(const QString&)), this, SLOT(usernameChanged()));
39    connect(passBox, SIGNAL(textChanged(const QString&)), this, SLOT(passwordChanged()));
40    connect(confirmPassBox, SIGNAL(textChanged(const QString&)), this, SLOT(passwordChanged()));
41    connect(fullnameBox, SIGNAL(textChanged(const QString&)), this, SLOT(fullnameChanged()));
42    connect(okButton, SIGNAL(clicked()), this, SLOT(submit()));
43}
44
45void SimpleAddCode::usernameChanged()
46{
47    QString username = usernameBox->text().toLower();
48    usernameBox->setText(username);
49   
50    //Validate
51    int usernameError = back->validateUsername(username);
52   
53    QColor usernameBoxColour = white;
54    usernameBox->setToolTip( tr("Good Input"));
55   
56    switch (usernameError) 
57    {
58    case 1:
59        usernameBox->setToolTip( tr("The field is empty"));
60        break;
61    case 2:
62        usernameBoxColour = red;
63        usernameBox->setToolTip( tr("This field can only accept letters and numbers"));
64        break;
65    case 3:
66        usernameBoxColour = red;
67        usernameBox->setToolTip(tr("This username is already in use, please choose another."));
68    }
69   
70    QPalette bgPal(usernameBoxColour);
71    bgPal.setColor(QPalette::Window,usernameBoxColour);
72    usernameBox->setPalette(bgPal);
73
74}
75
76void SimpleAddCode::passwordChanged()
77{
78    int passError = back->validatePassword(passBox->text(), confirmPassBox->text());
79   
80    QColor passBoxColour = white;
81    passBox->setToolTip(tr("Passwords match"));
82   
83    switch (passError)
84    {
85    case 1:
86        passBox->setToolTip("");
87        break;
88    case 2:
89        passBoxColour = red;
90        passBox->setToolTip(tr("Passwords do not match"));
91        break;
92    case 3:
93        passBoxColour = red;
94        passBox->setToolTip(tr("Invalid input"));
95    }
96   
97    QPalette bgPal(passBoxColour);
98    bgPal.setColor(QPalette::Window,passBoxColour);
99    passBox->setPalette(bgPal);
100
101}
102
103void SimpleAddCode::fullnameChanged()
104{
105    int fullnameError = back->validateFullname(fullnameBox->text());
106   
107    QColor fullnameBoxColour = white;
108    fullnameBox->setToolTip(tr("Good Input"));
109   
110    switch(fullnameError)
111    {
112    case 1:
113        fullnameBox->setToolTip(tr("The field is empty"));
114        break;
115    case 2:
116        fullnameBoxColour = red;
117        fullnameBox->setToolTip(tr("This field may not contain ! @ or :"));
118        break;
119    }
120   
121    QPalette bgPal(fullnameBoxColour);
122    bgPal.setColor(QPalette::Window,fullnameBoxColour);
123    fullnameBox->setPalette(bgPal);
124}
125
126void SimpleAddCode::submit()
127{
128    qDebug() << "starting submit()";
129
130    //Validate all data fields, prompt with errors or create the new user as requested
131    bool inputError = false;
132    bool warnError = false;
133    QString inputText = tr("The following errors occurred while trying to add a new user:\n");
134    QString warnText = tr("The following non-critical errors occurred while trying to add a new user, do you wish to continue?\n");
135   
136    //Username validation
137    qDebug() << "before username validation.........";
138    QString username = usernameBox->text();
139    int usernameError = back->validateUsername(username);
140   
141    switch (usernameError)
142    {
143    case 1:
144        inputText += tr("\n- The 'Username' field has not been filled in.");
145        break;
146    case 2:
147        inputError = true;
148        inputText += tr("\n- The 'Username' field may only contain letters and numbers (not spaces).");
149        break;
150    case 3:
151        inputError = true;
152        inputText += tr("\n- The username you have chosen is already in use, please choose another.");
153        break;
154    }
155   
156
157    //Full name validation
158    qDebug() << "before full name validation.........";
159    QString fullname = fullnameBox->text();
160    int fullnameError = back->validateFullname(fullname);
161    switch (fullnameError)
162    {
163    case 1:
164        inputError = true;
165        inputText += tr("\n- The 'Full Name' field has not been filled in.");
166        break;
167    case 2:
168        inputError = true;
169        inputText += tr("\n- The 'Full Name' field may not contain ! @ or :");
170        break;
171    }
172   
173    //Password validation
174    qDebug() << "before password validation.........";
175    QString password = passBox->text();
176    QString confirmPassword = confirmPassBox->text();
177   
178    int passError = back->validatePassword(password, confirmPassword);
179    qDebug() << "after password validation.........";
180   
181    switch (passError)
182    {
183    case 0:
184        qDebug() << "Case 0 was found!!!........." + username + " pass: " + password;
185        //back->changePassword(username, password);
186        close();
187        break;
188    case 1:
189        inputError = true;
190        inputText += tr("\n- Both password fields must be filled in.");
191        break;
192    case 2:
193        inputError = true;
194        inputText += tr("\n- Passwords don't match, please try again.");
195        break;
196    case 3:
197        inputError = true;
198        inputText += tr("\n- Invalid password");
199        break;
200    }
201   
202   //Throw up error or continue
203
204   if (inputError)
205   {
206       //Throw up the error box!
207       QMessageBox::critical(this, tr("Error!"), inputText);
208   }
209   else
210   {
211       //If we need to issue a warning, do it, only continue if the user wishes it to be so.
212       int abortAdd = 0;
213       if (warnError)
214       {
215           abortAdd = QMessageBox::warning(this, tr("Warning!"), warnText, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
216       }
217       //Check we didn't abort the operation
218       if (abortAdd == 0)
219       {
220           qDebug() << "Before back->addUser";
221           back->addUser(username, fullname, "/usr/home/" + username, "/bin/csh", QString::null, password);
222           back->changePassword(username, password);
223           qDebug() << "After back->addUser";
224       }
225   }
226}
227
Note: See TracBrowser for help on using the repository browser.