Debian Codegraph @

enterpassworddialog/enterpassworddialog.cpp

raw symbols SCIP
1 #include "./enterpassworddialog.h"
2 #include "../misc/dialogutils.h"
3
4 #include "ui_enterpassworddialog.h"
5
6 #include <QApplication>
7 #include <QEvent>
8 #include <QGraphicsPixmapItem>
9 #include <QGraphicsScene>
10 #include <QIcon>
11 #include <QKeyEvent>
12 #include <QMessageBox>
13 #include <QStyle>
14
15 #if defined(QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION) && defined(Q_OS_WIN32)
16 #include <windows.h>
17 #elif defined(X_AVAILABLE)
18 #include <X11/XKBlib.h>
19 #undef KeyPress
20 #undef KeyRelease
21 #undef FocusIn
22 #undef FocusOut
23 #endif
24
25 namespace QtUtilities {
26
27 /*!
28 * \class EnterPasswordDialog
29 * \brief The EnterPasswordDialog class provides a simple dialog to ask the user
30 * for a password.
31 */
32
33 /*!
34 * \brief Constructs a password dialog.
35 * \param parent Specifies the parent widget.
36 */
37 EnterPasswordDialog::EnterPasswordDialog(QWidget *parent)
38 : QDialog(parent)
39 , m_ui(new Ui::EnterPasswordDialog)
40 {
41 // setup ui
42 m_ui->setupUi(this);
43 makeHeading(m_ui->instructionLabel);
44 setStyleSheet(dialogStyleForPalette(palette()));
45 setDescription();
46 setPromptForUserName(false);
47 setVerificationRequired(false);
48 setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
49 installEventFilter(this);
50 m_ui->userNameLineEdit->installEventFilter(this);
51 m_ui->password1LineEdit->installEventFilter(this);
52 m_ui->password2LineEdit->installEventFilter(this);
53 m_capslockPressed = isCapslockPressed();
54 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
55 // draw icon to capslock warning graphics view
56 QIcon icon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning, nullptr, this);
57 QGraphicsScene *scene = new QGraphicsScene();
58 QGraphicsPixmapItem *item = new QGraphicsPixmapItem(icon.pixmap(16, 16));
59 scene->addItem(item);
60 m_ui->capslockWarningGraphicsView->setScene(scene);
61 // connect signals and slots
62 connect(m_ui->showPasswordCheckBox, &QCheckBox::clicked, this, &EnterPasswordDialog::updateShowPassword);
63 connect(m_ui->noPwCheckBox, &QCheckBox::clicked, this, &EnterPasswordDialog::updateShowPassword);
64 connect(m_ui->confirmPushButton, &QPushButton::clicked, this, &EnterPasswordDialog::confirm);
65 connect(m_ui->abortPushButton, &QPushButton::clicked, this, &EnterPasswordDialog::abort);
66 // grab the keyboard
67 grabKeyboard();
68 }
69
70 /*!
71 * \brief Destroys the password dialog.
72 */
73 EnterPasswordDialog::~EnterPasswordDialog()
74 {
75 }
76
77 /*!
78 * \brief Returns the description. The description is shown under the
79 * instruction text.
80 * \sa setDescription()
81 */
82 QString EnterPasswordDialog::description() const
83 {
84 return m_ui->descLabel->text();
85 }
86
87 /*!
88 * \brief Sets the description.
89 * \sa description()
90 */
91 void EnterPasswordDialog::setDescription(const QString &description)
92 {
93 m_ui->descLabel->setText(description);
94 m_ui->descLabel->setHidden(description.isEmpty());
95 adjustSize();
96 }
97
98 /*!
99 * \brief Returns whether the dialogs prompts for a user name as well.
100 *
101 * The dialog does not prompt for a user name by default.
102 *
103 * \sa setPromptForUserName()
104 */
105 bool EnterPasswordDialog::promtForUserName() const
106 {
107 return !m_ui->userNameLineEdit->isHidden();
108 }
109
110 /*!
111 * \brief Sets whethere the dialog prompts for a user name as well.
112 * \sa promptForUserName()
113 */
114 void EnterPasswordDialog::setPromptForUserName(bool prompt)
115 {
116 m_ui->userNameLineEdit->setHidden(!prompt);
117 adjustSize();
118 }
119
120 /*!
121 * \brief Returns an indication whether a verification (password has to be
122 * entered twice) is required.
123 *
124 * \sa EnterPasswordDialog::setVerificationRequired()
125 */
126 bool EnterPasswordDialog::isVerificationRequired() const
127 {
128 return !m_ui->password2LineEdit->isHidden();
129 }
130
131 /*!
132 * \brief Returns an indication whether the user is force to enter a password.
133 *
134 * If no password is required, the user is allowed to skip the dialog without
135 * entering
136 * a password.
137 *
138 * \sa EnterPasswordDialog::setPasswordRequired()
139 */
140 bool EnterPasswordDialog::isPasswordRequired() const
141 {
142 return m_ui->noPwCheckBox->isHidden();
143 }
144
145 /*!
146 * \brief Sets whether the user is force to enter a password.
147 *
148 * If no password is required, the user is allowed to skip the dialog without
149 * entering
150 * a password.
151 *
152 * \sa EnterPasswordDialog::isPasswordRequired()
153 */
154 void EnterPasswordDialog::setPasswordRequired(bool value)
155 {
156 m_ui->noPwCheckBox->setHidden(value);
157 m_ui->noPwCheckBox->setChecked(false);
158 adjustSize();
159 }
160
161 /*!
162 * \brief Updates the relevant controls to show entered characters or to mask
163 * them them.
164 *
165 * This private slot is called when m_ui->showPasswordCheckBox is clicked.
166 */
167 void EnterPasswordDialog::updateShowPassword()
168 {
169 m_ui->password1LineEdit->setEchoMode(m_ui->showPasswordCheckBox->isChecked() ? QLineEdit::Normal : QLineEdit::Password);
170 m_ui->password1LineEdit->setEnabled(!m_ui->noPwCheckBox->isChecked());
171 m_ui->password2LineEdit->setEnabled(!(m_ui->showPasswordCheckBox->isChecked() || m_ui->noPwCheckBox->isChecked()));
172 }
173
174 /*!
175 * \brief Sets whether a verification (password has to be entered twice) is
176 * required.
177 *
178 * \sa EnterPasswordDialog::isVerificationRequired()
179 */
180 void EnterPasswordDialog::setVerificationRequired(bool value)
181 {
182 if (m_instruction.isEmpty()) {
183 m_ui->instructionLabel->setText(value ? tr("Enter the new password") : tr("Enter the password"));
184 }
185 m_ui->password2LineEdit->setHidden(!value);
186 adjustSize();
187 }
188
189 /*!
190 * \brief Sets the instruction text.
191 *
192 * \sa EnterPasswordDialog::instruction()
193 */
194 void EnterPasswordDialog::setInstruction(const QString &value)
195 {
196 m_instruction = value;
197 if (m_instruction.isEmpty()) {
198 m_ui->instructionLabel->setText(isVerificationRequired() ? tr("Enter the new password") : tr("Enter the password"));
199 } else {
200 m_ui->instructionLabel->setText(value);
201 }
202 adjustSize();
203 }
204
205 bool EnterPasswordDialog::event(QEvent *event)
206 {
207 switch (event->type()) {
208 case QEvent::PaletteChange:
209 setStyleSheet(dialogStyleForPalette(palette()));
210 break;
211 case QEvent::KeyPress: {
212 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
213 if (keyEvent->key() == Qt::Key_CapsLock) {
214 m_capslockPressed = !m_capslockPressed;
215 }
216 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
217 break;
218 }
219 case QEvent::LanguageChange:
220 m_ui->retranslateUi(this);
221 break;
222 default:;
223 }
224 return QDialog::event(event);
225 }
226
227 /*!
228 * \brief Internal method to notice when the capslock key is pressed by the
229 * user.
230 *
231 * Invocation of this method is done by installing the event filter in the
232 * constructor.
233 */
234 bool EnterPasswordDialog::eventFilter(QObject *sender, QEvent *event)
235 {
236 switch (event->type()) {
237 case QEvent::KeyPress: {
238 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
239 if (keyEvent->key() == Qt::Key_CapsLock) {
240 m_capslockPressed = !m_capslockPressed;
241 } else {
242 QString text = keyEvent->text();
243 if (text.length()) {
244 QChar firstChar = text.at(0);
245 bool shiftPressed = (keyEvent->modifiers() & Qt::ShiftModifier) != 0;
246 if ((shiftPressed && firstChar.isLower()) || (!shiftPressed && firstChar.isUpper())) {
247 m_capslockPressed = true;
248 } else if (firstChar.isLetter()) {
249 m_capslockPressed = false;
250 }
251 }
252 }
253 m_ui->capslockWarningWidget->setVisible(m_capslockPressed);
254 } break;
255 case QEvent::FocusIn:
256 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
257 releaseKeyboard();
258 qobject_cast<QWidget *>(sender)->grabKeyboard();
259 }
260 break;
261 case QEvent::FocusOut:
262 if (sender == m_ui->userNameLineEdit || sender == m_ui->password1LineEdit || sender == m_ui->password2LineEdit) {
263 qobject_cast<QWidget *>(sender)->releaseKeyboard();
264 grabKeyboard();
265 }
266 break;
267 default:;
268 }
269 return false;
270 }
271
272 /*!
273 * \brief Sets the dialog status to QDialog::Accepted if a valid password has
274 * been enterd.
275 * Displays an error message otherwise.
276 *
277 * This private slot is called when m_ui->confirmPushButton is clicked.
278 */
279 void EnterPasswordDialog::confirm()
280 {
281 if (!isPasswordRequired() && m_ui->noPwCheckBox->isChecked()) {
282 m_password.clear();
283 done(QDialog::Accepted);
284 } else {
285 QString userName = m_ui->userNameLineEdit->text();
286 QString password = m_ui->password1LineEdit->text();
287 QString repeatedPassword = m_ui->password2LineEdit->text();
288 if (promtForUserName() && userName.isEmpty()) {
289 QMessageBox::warning(this, windowTitle(), tr("You didn't enter a user name."));
290 } else if (password.isEmpty()) {
291 QMessageBox::warning(this, windowTitle(), tr("You didn't enter a password."));
292 } else {
293 if (isVerificationRequired() && (password != repeatedPassword) && !m_ui->showPasswordCheckBox->isChecked()) {
294 if (repeatedPassword.isEmpty()) {
295 QMessageBox::warning(this, windowTitle(),
296 tr("You have to enter the new password twice to "
297 "ensure you enterd it correct."));
298 } else {
299 QMessageBox::warning(this, windowTitle(), tr("You mistyped the password."));
300 }
301 } else {
302 m_userName = userName;
303 m_password = password;
304 done(QDialog::Accepted);
305 }
306 }
307 }
308 }
309
310 /*!
311 * \brief Returns an indication whether the capslock key is pressed using
312 * platform specific functions.
313 *
314 * \remarks
315 * - Returns always false for unsupported platforms.
316 * - This method always returns false when the detection is not
317 * supported. It is supported under X11
318 * and Windows.
319 * - The function requires the application to be linked against X11 on
320 * Linux/Unix.
321 * - This static function will be used internally to detect whether the
322 * capslock key is pressed when initializing the dialog.
323 */
324 bool EnterPasswordDialog::isCapslockPressed()
325 {
326 #if defined(QT_UTILITIES_PLATFORM_SPECIFIC_CAPSLOCK_DETECTION) && defined(Q_OS_WIN32)
327 return GetKeyState(VK_CAPITAL) == 1;
328 #elif defined(X_AVAILABLE)
329 auto *const d = XOpenDisplay(nullptr);
330 auto capsState = false;
331 if (d) {
332 unsigned n;
333 XkbGetIndicatorState(d, XkbUseCoreKbd, &n);
334 capsState = (n & 0x01) == 1;
335 }
336 return capsState;
337 #else
338 return false;
339 #endif
340 }
341 } // namespace QtUtilities