Add QR code generator for TOTP export (#1167)

* Resolves #764
* Add libqrencode and qtsvg dependencies 
* Ensure QR code remains square
* Auto-close QR code dialog when database is locked
* Add databaseLocked() Signal to databaseWidget
* Correct otpauth URI output in Totp::writeSettings(...)
This commit is contained in:
Adolfo E. García
2018-10-19 12:42:49 -06:00
committed by Jonathan White
parent 80749958b7
commit bb16dc6d01
21 changed files with 584 additions and 16 deletions

21
src/qrcode/CMakeLists.txt Normal file
View File

@@ -0,0 +1,21 @@
# Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
#
# 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 or (at your option)
# version 3 of the License.
#
# 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/>.
set(qrcode_SOURCES
QrCode.cpp
)
add_library(qrcode STATIC ${qrcode_SOURCES})
target_link_libraries(qrcode Qt5::Core Qt5::Widgets Qt5::Svg ${QRENCODE_LIBRARY})

132
src/qrcode/QrCode.cpp Normal file
View File

@@ -0,0 +1,132 @@
/*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* 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 or (at your option)
* version 3 of the License.
*
* 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/>.
*/
#include "QrCode.h"
#include "QrCode_p.h"
#include <QBrush>
#include <QByteArray>
#include <QIODevice>
#include <QImage>
#include <QPainter>
#include <QPen>
#include <QString>
#include <QSvgGenerator>
#include <QVariant>
QrCodePrivate::QrCodePrivate()
: m_qrcode(nullptr)
{
}
QrCodePrivate::~QrCodePrivate()
{
if (m_qrcode) {
QRcode_free(m_qrcode);
}
}
QrCode::QrCode()
: d_ptr(new QrCodePrivate())
{
}
QrCode::QrCode(const QString& data, const Version version, const ErrorCorrectionLevel ecl, const bool caseSensitive)
: d_ptr(new QrCodePrivate())
{
init(data, version, ecl, caseSensitive);
}
QrCode::QrCode(const QByteArray& data, const Version version, const ErrorCorrectionLevel ecl)
: d_ptr(new QrCodePrivate())
{
init(data, version, ecl);
}
QrCode::~QrCode() = default;
void QrCode::init(const QString& data, const Version version, const ErrorCorrectionLevel ecl, bool caseSensitive)
{
if (data.isEmpty()) {
return;
}
d_ptr->m_qrcode = QRcode_encodeString(data.toLocal8Bit().data(),
static_cast<int>(version),
static_cast<QRecLevel>(ecl),
QR_MODE_8,
caseSensitive ? 1 : 0);
}
void QrCode::init(const QByteArray& data, const Version version, const ErrorCorrectionLevel ecl)
{
if (data.isEmpty()) {
return;
}
d_ptr->m_qrcode = QRcode_encodeData(data.size(),
reinterpret_cast<const unsigned char*>(data.data()),
static_cast<int>(version),
static_cast<QRecLevel>(ecl));
}
bool QrCode::isValid() const
{
return d_ptr->m_qrcode != nullptr;
}
void QrCode::writeSvg(QIODevice* outputDevice, const int dpi, const int margin) const
{
if (margin < 0 || d_ptr->m_qrcode == nullptr || outputDevice == nullptr) {
return;
}
const int width = d_ptr->m_qrcode->width + margin * 2;
QSvgGenerator generator;
generator.setSize(QSize(width, width));
generator.setViewBox(QRect(0, 0, width, width));
generator.setResolution(dpi);
generator.setOutputDevice(outputDevice);
QPainter painter;
painter.begin(&generator);
// Background
painter.setClipRect(QRect(0, 0, width, width));
painter.fillRect(QRect(0, 0, width, width), Qt::white);
// Foreground
// "Dots" are stored in a quint8 x quint8 array using row-major order.
// A dot is black if the LSB of its corresponding quint8 is 1.
const QPen pen(Qt::black, 0, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
const QBrush brush(Qt::black);
painter.setPen(pen);
painter.setBrush(brush);
const int rowSize = d_ptr->m_qrcode->width;
unsigned char* dot = d_ptr->m_qrcode->data;
for (int y = 0; y < rowSize; ++y) {
for (int x = 0; x < rowSize; ++x) {
if (quint8(0x01) == (static_cast<quint8>(*dot++) & quint8(0x01))) {
painter.drawRect(margin + x, margin + y, 1, 1);
}
}
}
painter.end();
}

78
src/qrcode/QrCode.h Normal file
View File

@@ -0,0 +1,78 @@
/*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* 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 or (at your option)
* version 3 of the License.
*
* 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/>.
*/
#ifndef KEEPASSX_QRCODE_H
#define KEEPASSX_QRCODE_H
#include <QScopedPointer>
#include <QtCore/qglobal.h>
class QImage;
class QIODevice;
class QString;
class QByteArray;
struct QrCodePrivate;
class QrCode
{
public:
enum class ErrorCorrectionLevel : int
{
LOW = 0,
MEDIUM,
QUARTILE,
HIGH
};
// See: http://www.qrcode.com/en/about/version.html
// clang-format off
enum class Version : int
{
AUTO = 0,
V1, V2, V3, V4, V5, V6, V7, V8, V9, V10, V11, V12, V13, V14, V15, V16, V17, V18, V19, V20,
V21, V22, V23, V24, V25, V26, V27, V28, V29, V30, V31, V32, V33, V34, V35, V36, V37, V38, V39, V40
};
// clang-format on
// Uses QRcode_encodeString (can't contain NUL characters)
explicit QrCode(const QString& data,
const Version version = Version::AUTO,
const ErrorCorrectionLevel ecl = ErrorCorrectionLevel::HIGH,
const bool caseSensitive = true);
// Uses QRcode_encodeData (can contain NUL characters)
explicit QrCode(const QByteArray& data,
const Version version = Version::AUTO,
const ErrorCorrectionLevel ecl = ErrorCorrectionLevel::HIGH);
QrCode();
~QrCode();
bool isValid() const;
void writeSvg(QIODevice* outputDevice, const int dpi, const int margin = 4) const;
private:
void init(const QString& data, const Version version, const ErrorCorrectionLevel ecl, const bool caseSensitive);
void init(const QByteArray& data, const Version version, const ErrorCorrectionLevel ecl);
QScopedPointer<QrCodePrivate> d_ptr;
};
#endif // KEEPASSX_QRCODE_H

33
src/qrcode/QrCode_p.h Normal file
View File

@@ -0,0 +1,33 @@
/*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
*
* 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 or (at your option)
* version 3 of the License.
*
* 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/>.
*/
/* This class exists to isolate <qrencode.h> from the rest of the code base. */
#ifndef KEEPASSX_QRCODEPRIVATE_H
#define KEEPASSX_QRCODEPRIVATE_H
#include <qrencode.h>
struct QrCodePrivate
{
QRcode* m_qrcode;
QrCodePrivate();
~QrCodePrivate();
};
#endif // KEEPASSX_QRCODEPRIVATE_H