qt: Disable echoing if backspace is pressed first.
authorDamien Goutte-Gattat via Gnupg-devel <gnupg-devel@gnupg.org>
Sat, 4 Aug 2018 21:24:10 +0000 (22:24 +0100)
committerDamien Goutte-Gattat <dgouttegattat@incenp.org>
Wed, 15 Aug 2018 18:33:40 +0000 (19:33 +0100)
* qt/pinlineedit.h: New file.
* qt/pinlineedit.cpp: New file.
* qt/Makefile.am: Add new source files.
* qt/pinentrydialog.cpp (PinEntryDialog): New member _got_input;
Type of _edit member changed to PinLineEdit.
(PinEntryDialog::onBackspace): New slot.

GnuPG-bug-id: 3428
Signed-off-by: Damien Goutte-Gattat <dgouttegattat@incenp.org>
qt/Makefile.am
qt/pinentrydialog.cpp
qt/pinentrydialog.h
qt/pinlineedit.cpp [new file with mode: 0644]
qt/pinlineedit.h [new file with mode: 0644]

index 698005e..840d0c0 100644 (file)
@@ -43,16 +43,17 @@ pinentry_qt_LDADD = \
        $(COMMON_LIBS) $(PINENTRY_QT_LIBS) $(libcurses) $(LIBCAP)
 
 BUILT_SOURCES = \
-       pinentryconfirm.moc pinentrydialog.moc
+       pinentryconfirm.moc pinentrydialog.moc pinlineedit.moc
 
 CLEANFILES = \
-       pinentryconfirm.moc pinentrydialog.moc
+       pinentryconfirm.moc pinentrydialog.moc pinlineedit.moc
 
 pinentry_qt_SOURCES = pinentrydialog.h pinentrydialog.cpp \
-       main.cpp qrc_pinentry.cpp pinentryconfirm.cpp pinentryconfirm.h
+       main.cpp qrc_pinentry.cpp pinentryconfirm.cpp pinentryconfirm.h \
+       pinlineedit.h pinlineedit.cpp
 
 nodist_pinentry_qt_SOURCES = \
-       pinentryconfirm.moc pinentrydialog.moc
+       pinentryconfirm.moc pinentrydialog.moc pinlineedit.moc
 
 .h.moc:
        $(MOC) `test -f '$<' || echo '$(srcdir)/'`$< -o $@
index d42ae4a..b7f2e53 100644 (file)
@@ -36,6 +36,7 @@
 #include <QLineEdit>
 #include <QAction>
 #include <QCheckBox>
+#include "pinlineedit.h"
 
 #ifdef Q_OS_WIN
 #include <windows.h>
@@ -137,6 +138,7 @@ PinEntryDialog::PinEntryDialog(QWidget *parent, const char *name,
     : QDialog(parent),
       mRepeat(NULL),
       _grabbed(false),
+      _got_input(false),
       mVisibilityTT(visibilityTT),
       mHideTT(hideTT),
       mVisiActionEdit(NULL),
@@ -163,7 +165,7 @@ PinEntryDialog::PinEntryDialog(QWidget *parent, const char *name,
     _prompt = new QLabel(this);
     _prompt->hide();
 
-    _edit = new QLineEdit(this);
+    _edit = new PinLineEdit(this);
     _edit->setMaxLength(256);
     _edit->setEchoMode(QLineEdit::Password);
 
@@ -205,6 +207,8 @@ PinEntryDialog::PinEntryDialog(QWidget *parent, const char *name,
             this, SLOT(updateQuality(QString)));
     connect(_edit, SIGNAL(textChanged(QString)),
             this, SLOT(textChanged(QString)));
+    connect(_edit, SIGNAL(backspacePressed()),
+            this, SLOT(onBackspace()));
 
     QTimer::singleShot(0, _edit, SLOT(setFocus()));
 
@@ -356,6 +360,16 @@ void PinEntryDialog::setQualityBarTT(const QString &txt)
     }
 }
 
+void PinEntryDialog::onBackspace()
+{
+    if (!_got_input) {
+        _edit->setEchoMode(QLineEdit::NoEcho);
+        if (mRepeat) {
+            mRepeat->setEchoMode(QLineEdit::NoEcho);
+        }
+    }
+}
+
 void PinEntryDialog::updateQuality(const QString &txt)
 {
     int length;
@@ -366,6 +380,8 @@ void PinEntryDialog::updateQuality(const QString &txt)
         _timer->stop();
     }
 
+    _got_input = true;
+
     if (!_have_quality_bar || !_pinentry_info) {
         return;
     }
index 52b7343..396f03b 100644 (file)
@@ -33,6 +33,7 @@
 class QLabel;
 class QPushButton;
 class QLineEdit;
+class PinLineEdit;
 class QString;
 class QProgressBar;
 class QCheckBox;
@@ -89,6 +90,7 @@ protected slots:
     void textChanged(const QString &);
     void focusChanged(QWidget *old, QWidget *now);
     void toggleVisibility();
+    void onBackspace();
 
 protected:
     /* reimp */ void showEvent(QShowEvent *event);
@@ -100,13 +102,14 @@ private:
     QLabel    *_prompt;
     QLabel    *_quality_bar_label;
     QProgressBar *_quality_bar;
-    QLineEdit *_edit;
-    QLineEdit *mRepeat;
+    PinLineEdit *_edit;
+    QLineEdit   *mRepeat;
     QPushButton *_ok;
     QPushButton *_cancel;
     bool       _grabbed;
     bool       _have_quality_bar;
     bool       _timed_out;
+    bool       _got_input;
     pinentry_t _pinentry_info;
     QTimer    *_timer;
     QString    mRepeatError,
diff --git a/qt/pinlineedit.cpp b/qt/pinlineedit.cpp
new file mode 100644 (file)
index 0000000..c7c7f36
--- /dev/null
@@ -0,0 +1,36 @@
+/* pinlineedit.cpp - Modified QLineEdit widget.
+ * Copyright (C) 2018 Damien Goutte-Gattat
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include "pinlineedit.h"
+
+#include <QKeyEvent>
+
+PinLineEdit::PinLineEdit(QWidget *parent) : QLineEdit(parent)
+{
+}
+
+void
+PinLineEdit::keyPressEvent(QKeyEvent *e)
+{
+    QLineEdit::keyPressEvent(e);
+
+    if ( e->key() == Qt::Key::Key_Backspace )
+       emit backspacePressed();
+}
+
+#include "pinlineedit.moc"
diff --git a/qt/pinlineedit.h b/qt/pinlineedit.h
new file mode 100644 (file)
index 0000000..b1b48df
--- /dev/null
@@ -0,0 +1,38 @@
+/* pinlineedit.h - Modified QLineEdit widget.
+ * Copyright (C) 2018 Damien Goutte-Gattat
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#ifndef _PINLINEEDIT_H_
+#define _PINLINEEDIT_H_
+
+#include <QLineEdit>
+
+class PinLineEdit : public QLineEdit
+{
+    Q_OBJECT
+
+public:
+    PinLineEdit(QWidget *);
+
+signals:
+    void backspacePressed();
+
+protected:
+    void keyPressEvent(QKeyEvent *);
+};
+
+#endif // _PINLINEEDIT_H_