Commit 2faee0fc authored by Martin Straka's avatar Martin Straka
Browse files

Merge branch 'otp-paste' into 'develop'

OTP paste

The text dialogue has been modified for Android devices to contain paste button.

Addresses issue #44.

See merge request !33
parents 4353b319 0743c55d
......@@ -108,7 +108,8 @@ SOURCES += \
src/net/isds_wrapper.cpp \
src/net/isds_session.cpp \
src/net/db_wrapper.cpp \
src/setwrapper.cpp
src/setwrapper.cpp \
src/widgets/paste_input_dialogue.cpp
HEADERS += \
src/accounts.h \
......@@ -136,7 +137,8 @@ HEADERS += \
src/net/isds_wrapper.h \
src/net/isds_session.h \
src/net/db_wrapper.h \
src/setwrapper.h
src/setwrapper.h \
src/widgets/paste_input_dialogue.h
RESOURCES += \
res/qml.qrc \
......
......@@ -25,7 +25,6 @@
#include <QDebug>
#include <QFileInfo>
#include <QMessageBox>
#include <QInputDialog>
#include "src/common.h"
#include "src/net/isds_const.h"
......@@ -33,7 +32,7 @@
#include "src/net/xml_layer.h"
#include "src/models/accountmodel.h"
#include "src/settings.h"
#include "src/widgets/paste_input_dialogue.h"
IsdsSession::IsdsSession(QObject *parent) : QObject(parent),
isdsCtxMap()
......@@ -77,7 +76,7 @@ bool IsdsSession::createIsdsCtx(const QString &userName, QString &errText)
if (!acntData.password().isEmpty()) {
isdsCtx.password = acntData.password();
} else {
QString text = QInputDialog::getText(0,
QString text = PasteInputDialogue::getText(0,
tr("Password: %1").arg(userName),
tr("Enter password for account\n'%1'").arg(acntData.accountName()),
QLineEdit::Password, NULL, &ok, Qt::Dialog);
......@@ -94,7 +93,7 @@ bool IsdsSession::createIsdsCtx(const QString &userName, QString &errText)
return false;
}
} else if (acntData.loginMethod() == USERNAME_PWD_HOTP) {
QString text = QInputDialog::getText(0,
QString text = PasteInputDialogue::getText(0,
tr("Security code: %1").arg(userName),
tr("Enter security code for account\n'%1'").arg(acntData.accountName()),
QLineEdit::Normal, NULL, &ok, Qt::Dialog,
......@@ -157,7 +156,7 @@ bool IsdsSession::setCertificateData(const QString &userName,
return false;
}
const QString passPhrase = QInputDialog::getText(0,
const QString passPhrase = PasteInputDialogue::getText(0,
tr("Certificate password: %1").arg(userName),
tr("Enter certificate password for account\n'%1'").arg(accountName),
QLineEdit::Password, NULL, &ok, Qt::Dialog);
......
......@@ -22,12 +22,12 @@
*/
#include <QDebug>
#include <QInputDialog>
#include <QMessageBox>
#include "src/net/isds_wrapper.h"
#include "src/models/accountmodel.h"
#include "src/settings.h"
#include "src/widgets/paste_input_dialogue.h"
IsdsWrapper::IsdsWrapper(QObject *parent) : QObject(parent)
{
......@@ -332,7 +332,7 @@ void IsdsWrapper::changePassword(const QString &userName,
}
bool ok;
QString newPwd = QInputDialog::getText(0,
QString newPwd = PasteInputDialogue::getText(0,
tr("New password: %1").arg(userName),
tr("Enter a new password for account\n'%1'").arg(accountName),
QLineEdit::Normal, NULL, &ok, Qt::Dialog);
......@@ -371,7 +371,7 @@ void IsdsWrapper::changePassword(const QString &userName,
return;
}
QString text = QInputDialog::getText(0,
QString text = PasteInputDialogue::getText(0,
tr("SMS code: %1").arg(userName),
tr("Enter SMS code for account\n'%1'").arg(accountName),
QLineEdit::Normal, NULL, &ok, Qt::Dialog,
......@@ -383,7 +383,7 @@ void IsdsWrapper::changePassword(const QString &userName,
}
} else if (m_isdsSession.isdsCtxMap[userName].login_method == USERNAME_PWD_HOTP) {
QString text = QInputDialog::getText(0,
QString text = PasteInputDialogue::getText(0,
tr("Security code: %1").arg(userName),
tr("Enter security code for account\n'%1'").arg(accountName),
QLineEdit::Normal, NULL, &ok, Qt::Dialog,
......
......@@ -24,7 +24,6 @@
#include <QDebug>
#include <QEventLoop>
#include <QInputDialog>
#include <QNetworkAccessManager>
#include <QNetworkCookie>
#include <QNetworkCookieJar>
......@@ -36,6 +35,7 @@
#include "src/net/net_layer.h"
#include "src/net/xml_layer.h"
#include "src/net/isds_const.h"
#include "src/widgets/paste_input_dialogue.h"
void printRequest(QNetworkRequest request, QByteArray requestContent)
......@@ -512,7 +512,7 @@ bool NetLayer::doTotpLoginRequest(IsdsContext &ctx, const QByteArray &xmlDataIn,
/* Set authorization header */
bool ok;
const QString code = QInputDialog::getText(0,
const QString code = PasteInputDialogue::getText(0,
tr("SMS code: %1").arg(ctx.username),
tr("Enter SMS code for user '%1'").arg(ctx.username),
QLineEdit::Normal, NULL, &ok, Qt::Dialog, Qt::ImhDigitsOnly);
......
/*
* Copyright (C) 2014-2016 CZ.NIC
*
* 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 3 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 <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#include <QClipboard>
#include <QDebug>
#include <QDialogButtonBox>
#include <QGuiApplication>
#include <QPlainTextEdit>
#include <QPushButton>
#include "src/widgets/paste_input_dialogue.h"
PasteInputDialogue::PasteInputDialogue(QWidget *parent, Qt::WindowFlags flags)
: QInputDialog(parent, flags),
m_timer(this),
m_pasteButton(Q_NULLPTR),
m_textField(Q_NULLPTR)
{
}
PasteInputDialogue::~PasteInputDialogue(void)
{
m_timer.stop();
if ((m_pasteButton != NULL) && (m_textField != NULL)) {
m_pasteButton->disconnect(SIGNAL(clicked()), m_textField,
SLOT(paste()));
m_timer.disconnect(SIGNAL(timeout()), this,
SLOT(activatePasteButton()));
}
}
QString PasteInputDialogue::getText(QWidget *parent, const QString &title,
const QString &label, QLineEdit::EchoMode mode, const QString &text,
bool *ok, Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
{
#if defined Q_OS_ANDROID
//#if 1
PasteInputDialogue dialogue(parent, flags);
dialogue.setWindowTitle(title);
dialogue.setLabelText(label);
dialogue.setTextValue(text);
dialogue.setTextEchoMode(mode);
dialogue.setInputMethodHints(inputMethodHints);
dialogue.setVisible(true); /* Constructs the dialogue. */
dialogue.setVisible(false);
dialogue.extendDialoguePaste();
int ret = dialogue.exec();
if (ok) {
*ok = !!ret;
}
if (ret) {
return dialogue.textValue();
} else {
return QString();
}
#else /* !defined Q_OS_ANDROID */
return QInputDialog::getText(parent, title, label, mode, text, ok,
flags, inputMethodHints);
#endif /* defined Q_OS_ANDROID */
}
void PasteInputDialogue::activatePasteButton(void)
{
if (m_pasteButton == Q_NULLPTR) {
return;
}
QClipboard *clipboard = QGuiApplication::clipboard();
if (clipboard == Q_NULLPTR) {
return;
}
/* Activate button only when clipboard contains text. */
//m_pasteButton->setVisible(!clipboard->text().isEmpty());
m_pasteButton->setEnabled(!clipboard->text().isEmpty());
}
void PasteInputDialogue::extendDialoguePaste(void)
{
QList<QDialogButtonBox *> buttonBoxes(
findChildren<QDialogButtonBox *>());
QDialogButtonBox *buttonBox = Q_NULLPTR;
m_textField = findEditArea();
if (m_textField == Q_NULLPTR) {
/* No suitable text field found. */
qWarning("Could not find editable area.\n");
return;
}
/* There should be only one button box. */
if (buttonBoxes.size() == 1) {
buttonBox = buttonBoxes.at(0);
}
if (buttonBox == Q_NULLPTR) {
qWarning("Could not locate button box.\n");
m_textField = Q_NULLPTR;
return;
}
m_pasteButton = buttonBox->addButton(
tr("Paste"), QDialogButtonBox::ActionRole);
if (m_pasteButton == Q_NULLPTR) {
qWarning("Could not create paste button.\n");
m_textField = Q_NULLPTR;
return;
}
m_pasteButton->setEnabled(false);
//m_pasteButton->setVisible(false);
connect(m_pasteButton, SIGNAL(clicked()), m_textField, SLOT(paste()));
connect(&m_timer, SIGNAL(timeout()), this, SLOT(activatePasteButton()));
m_timer.setInterval(500);
m_timer.start();
qInfo("Created and connected paste button.\n");
}
QObject *PasteInputDialogue::findEditArea(void) const
{
QLineEdit *lineEdit = Q_NULLPTR;
QPlainTextEdit *plainTextEdit = Q_NULLPTR;
{
QList<QLineEdit *> lineEdits(findChildren<QLineEdit *>());
if (lineEdits.size() == 1) {
lineEdit = lineEdits.at(0);
}
QList<QPlainTextEdit *> plainTextEdits(
findChildren<QPlainTextEdit *>());
if (plainTextEdits.size() == 1) {
plainTextEdit = plainTextEdits.at(0);
}
}
if ((lineEdit != Q_NULLPTR) && (plainTextEdit == Q_NULLPTR)) {
return lineEdit;
} else if ((lineEdit == Q_NULLPTR) && (plainTextEdit != Q_NULLPTR)) {
return plainTextEdit;
}
return Q_NULLPTR;
}
/*
* Copyright (C) 2014-2016 CZ.NIC
*
* 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 3 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 <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#ifndef _PASTE_INPUT_DIALOGUE_H_
#define _PASTE_INPUT_DIALOGUE_H_
#include <QInputDialog>
#include <QLineEdit>
#include <QString>
#include <QTimer>
/*!
* @brief Text input dialogue with paste button.
*
* @note This class is a nasty hack to explicitly allow pasting text from
* clipboard on Android devices.
*/
class PasteInputDialogue : public QInputDialog {
Q_OBJECT
private:
/*!
* @brief Private constructor.
*
* @param[in] parent Parent widget.
* @param[in] flags Flags s used in QInputDialog.
*/
PasteInputDialogue(QWidget *parent = Q_NULLPTR,
Qt::WindowFlags flags = Qt::WindowFlags());
public:
/*!
* @brief Destructor.
*/
~PasteInputDialogue(void);
/*!
* @brief Generates a text edit dialogue. Paste button is added on
* Android devices.
*/
static QString getText(QWidget *parent, const QString &title,
const QString &label, QLineEdit::EchoMode mode = QLineEdit::Normal,
const QString &text = QString(), bool *ok = Q_NULLPTR,
Qt::WindowFlags flags = Qt::WindowFlags(),
Qt::InputMethodHints inputMethodHints = Qt::ImhNone);
private slots:
/*!
* @brief Activates pasting button according to clipboard content.
*/
void activatePasteButton(void);
private:
/*!
* @brief This method adds the paste button.
*
* @note Must be called after the whole dialogue is constructed.
*/
void extendDialoguePaste(void);
/*!
* @brief Find text field in the dialogue.
*
* @return Pointer to text field object if found.
*/
QObject *findEditArea(void) const;
QTimer m_timer; /*!< Timer used to check clipboard and activate button. */
QPushButton *m_pasteButton; /*!< Convenience pointer to paste button. */
QObject *m_textField; /*!< Text field receiving the paste command. */
};
#endif /* _PASTE_INPUT_DIALOGUE_H_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment