Skip to content

Commit

Permalink
Add setCodec(). Fix MacPasteboard.
Browse files Browse the repository at this point in the history
  • Loading branch information
varjolintu committed Mar 31, 2024
1 parent 43f80ce commit a636daa
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 102 deletions.
7 changes: 4 additions & 3 deletions src/browser/PasskeyUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,19 @@ QJsonArray PasskeyUtils::parseCredentialTypes(const QJsonArray& credentialTypes)
}));
} else {
for (const auto current : credentialTypes) {
if (current["type"] != BrowserPasskeys::PUBLIC_KEY || current["alg"].isUndefined()) {
const auto currentObject = current.toObject();
if (currentObject["type"] != BrowserPasskeys::PUBLIC_KEY || currentObject["alg"].isUndefined()) {
continue;
}

const auto currentAlg = current["alg"].toInt();
const auto currentAlg = currentObject["alg"].toInt();
if (currentAlg != WebAuthnAlgorithms::ES256 && currentAlg != WebAuthnAlgorithms::RS256
&& currentAlg != WebAuthnAlgorithms::EDDSA) {
continue;
}

credTypesAndPubKeyAlgs.push_back(QJsonObject({
{"type", current["type"]},
{"type", currentObject["type"]},
{"alg", currentAlg},
}));
}
Expand Down
42 changes: 36 additions & 6 deletions src/cli/TextStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "TextStream.h"

#include <QProcessEnvironment>
#include <QTextCodec>
#include <QStringConverter>
#ifdef Q_OS_WIN
#include <windows.h>
#endif
Expand Down Expand Up @@ -87,14 +87,44 @@ void TextStream::detectCodec()
// Only override codec if LANG is set, otherwise Qt will assume
// US-ASCII, which is almost always wrong and results in
// Unicode passwords being displayed as question marks.
codecName = QTextCodec::codecForLocale()->name();
codecName = "System";
}
#endif

codecName = env.value("ENCODING_OVERRIDE", codecName);
auto* codec = QTextCodec::codecForName(codecName.toLatin1());
if (codec) {
// TODO: Solve
// setCodec(codec);

QStringConverter::Encoding codec = QStringConverter::Utf8;
if (codecName.toLatin1().compare(QByteArray("UTF-8"), Qt::CaseInsensitive) == 0
|| codecName.toLatin1().compare(QByteArray("Utf8"), Qt::CaseInsensitive) == 0) {
codec = QStringConverter::Utf8;
} else if (codecName.toLatin1().compare(QByteArray("UTF-16"), Qt::CaseInsensitive) == 0
|| codecName.toLatin1().compare(QByteArray("Utf16"), Qt::CaseInsensitive) == 0) {
codec = QStringConverter::Utf16;
} else if (codecName.toLatin1().compare(QByteArray("UTF-16BE"), Qt::CaseInsensitive) == 0
|| codecName.toLatin1().compare(QByteArray("Utf16BE"), Qt::CaseInsensitive) == 0) {
codec = QStringConverter::Utf16BE;
} else if (codecName.toLatin1().compare(QByteArray("UTF-16LE"), Qt::CaseInsensitive) == 0
|| codecName.toLatin1().compare(QByteArray("Utf16LE"), Qt::CaseInsensitive) == 0) {
codec = QStringConverter::Utf16LE;
} else if (codecName.toLatin1().compare(QByteArray("UTF-32"), Qt::CaseInsensitive) == 0
|| codecName.toLatin1().compare(QByteArray("Utf32"), Qt::CaseInsensitive) == 0) {
codec = QStringConverter::Utf32;
} else if (codecName.toLatin1().compare(QByteArray("UTF-32BE"), Qt::CaseInsensitive) == 0
|| codecName.toLatin1().compare(QByteArray("Utf32BE"), Qt::CaseInsensitive) == 0) {
codec = QStringConverter::Utf32BE;
} else if (codecName.toLatin1().compare(QByteArray("UTF-32LE"), Qt::CaseInsensitive) == 0
|| codecName.toLatin1().compare(QByteArray("Utf32LE"), Qt::CaseInsensitive) == 0) {
codec = QStringConverter::Utf32LE;
} else if (codecName.toLatin1().compare(QByteArray("ISO 8859-1"), Qt::CaseInsensitive) == 0
|| codecName.toLatin1().compare(QByteArray("Latin1"), Qt::CaseInsensitive) == 0) {
codec = QStringConverter::Latin1;
} else if (codecName.toLatin1().compare(QByteArray("Windows-850"), Qt::CaseInsensitive) == 0
|| codecName.toLatin1().compare(QByteArray("Windows-1252"), Qt::CaseInsensitive) == 0
|| codecName.toLatin1().compare(QByteArray("System"), Qt::CaseInsensitive) == 0) {
codec = QStringConverter::System;
} else {
return;
}

setEncoding(codec);
}
75 changes: 38 additions & 37 deletions src/core/MacPasteboard.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2024 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
Expand All @@ -14,27 +14,22 @@
* 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 <QTextCodec>
#include "MacPasteboard.h"

QString MacPasteboard::convertorName()
{
return QLatin1String("MacPasteboard");
}
#include "MacPasteboard.h"
#include <QStringConverter>

QString MacPasteboard::flavorFor(const QString& mimetype)
QString MacPasteboard::utiForMime(const QString& mime) const
{
if (mimetype == QLatin1String("text/plain")) {
if (mime == QLatin1String("text/plain")) {
return QLatin1String("public.utf8-plain-text");
} else if (mimetype == QLatin1String("application/x-nspasteboard-concealed-type")) {
} else if (mime == QLatin1String("application/x-nspasteboard-concealed-type")) {
return QLatin1String("org.nspasteboard.ConcealedType");
}

int i = mimetype.indexOf(QLatin1String("charset="));
int i = mime.indexOf(QLatin1String("charset="));

if (i >= 0) {
QString cs(mimetype.mid(i + 8).toLower());
QString cs(mime.mid(i + 8).toLower());
i = cs.indexOf(QLatin1Char(';'));

if (i >= 0) {
Expand All @@ -47,55 +42,61 @@ QString MacPasteboard::flavorFor(const QString& mimetype)
return QLatin1String("public.utf16-plain-text");
}
}
return QString();
return {};
}

QString MacPasteboard::mimeFor(QString flavor)
QString MacPasteboard::mimeForUti(const QString& uti) const
{
if (flavor == QLatin1String("public.utf8-plain-text"))
if (uti == QLatin1String("public.utf8-plain-text"))
return QLatin1String("text/plain");
if (flavor == QLatin1String("org.nspasteboard.ConcealedType"))
if (uti == QLatin1String("org.nspasteboard.ConcealedType"))
return QLatin1String("application/x-nspasteboard-concealed-type");
if (flavor == QLatin1String("public.utf16-plain-text"))
if (uti == QLatin1String("public.utf16-plain-text"))
return QLatin1String("text/plain;charset=utf16");
return QString();
return {};
}

bool MacPasteboard::canConvert(const QString& mimetype, QString flavor)
bool MacPasteboard::canConvert(const QString& mime, const QString& uti) const
{
Q_UNUSED(mimetype);
Q_UNUSED(flavor);
Q_UNUSED(mime);
Q_UNUSED(uti);
return true;
}

QVariant MacPasteboard::convertToMime(const QString& mimetype, QList<QByteArray> data, QString flavor)
QVariant MacPasteboard::convertToMime(const QString& mime, const QList<QByteArray>& data, const QString& uti) const
{
if (data.count() > 1)
qWarning("QMime::convertToMime: Cannot handle multiple member data");
const QByteArray& firstData = data.first();
QVariant ret;
if (flavor == QLatin1String("public.utf8-plain-text")) {
if (uti == QLatin1String("public.utf8-plain-text")) {
ret = QString::fromUtf8(firstData);
} else if (flavor == QLatin1String("org.nspasteboard.ConcealedType")) {
} else if (uti == QLatin1String("org.nspasteboard.ConcealedType")) {
ret = QString::fromUtf8(firstData);
} else if (flavor == QLatin1String("public.utf16-plain-text")) {
ret = QTextCodec::codecForName("UTF-16")->toUnicode(firstData);
} else if (uti == QLatin1String("public.utf16-plain-text")) {
auto toUtf16 = QStringDecoder(QStringDecoder::Utf16);
QVariant var{toUtf16(firstData)};
return var;
} else {
qWarning("QMime::convertToMime: unhandled mimetype: %s", qPrintable(mimetype));
qWarning("QMime::convertToMime: unhandled mimetype: %s", qPrintable(mime));
}
return ret;
}

QList<QByteArray> MacPasteboard::convertFromMime(const QString&, QVariant data, QString flavor)
QList<QByteArray> MacPasteboard::convertFromMime(const QString& mime, const QVariant& data, const QString& uti) const
{
Q_UNUSED(mime);

QList<QByteArray> ret;
QString string = data.toString();
if (flavor == QLatin1String("public.utf8-plain-text"))
ret.append(string.toUtf8());
else if (flavor == QLatin1String("org.nspasteboard.ConcealedType"))
ret.append(string.toUtf8());
else if (flavor == QLatin1String("public.utf16-plain-text"))
ret.append(QTextCodec::codecForName("UTF-16")->fromUnicode(string));
QString dataString = data.toString();
if (uti == QLatin1String("public.utf8-plain-text")) {
ret.append(dataString.toUtf8());
} else if (uti == QLatin1String("org.nspasteboard.ConcealedType")) {
ret.append(dataString.toUtf8());
} else if (uti == QLatin1String("public.utf16-plain-text")) {
auto toUtf16 = QStringEncoder(QStringDecoder::Utf16);
QByteArray baUtf16 = toUtf16(dataString);
ret.append(baUtf16);
}
return ret;
}
*/
23 changes: 12 additions & 11 deletions src/core/MacPasteboard.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2024 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
Expand All @@ -14,27 +14,28 @@
* 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 KEEPASSXC_MACPASTEBOARD_H
#define KEEPASSXC_MACPASTEBOARD_H

#include <QObject>
//#include <QTextCodec>
#include <QStringConverter>
#include <QUtiMimeConverter>
#include <QVariant>

class MacPasteboard : public QObject, public QUtiMimeConverter
{
public:
explicit MacPasteboard() : QUtiMimeConverter()
explicit MacPasteboard()
: QUtiMimeConverter()
{
}

//QString convertorName() override;
virtual bool canConvert(const QString &mime, const QString &uti) override;
QString mimeForUti(const QString &uti) override;
QString utiForMime(const QString &mime) override;
QVariant convertToMime(const QString &mime, const QList<QByteArray> &data, const QString &uti) override;
QList<QByteArray> convertFromMime(const QString &mime, const QVariant &data, const QString &uti) override;
bool canConvert(const QString& mime, const QString& uti) const;
QString mimeForUti(const QString& uti) const override;
QString utiForMime(const QString& mime) const override;
QVariant convertToMime(const QString& mime, const QList<QByteArray>& data, const QString& uti) const override;
QList<QByteArray> convertFromMime(const QString& mime, const QVariant& data, const QString& uti) const override;
};

#endif // KEEPASSXC_MACPASTEBOARD_H*/
#endif // KEEPASSXC_MACPASTEBOARD_H
12 changes: 9 additions & 3 deletions src/format/CsvParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,15 @@ void CsvParser::setComment(const QChar& c)

void CsvParser::setCodec(const QString& s)
{
Q_UNUSED(s)
// TODO: Solve
//m_ts.setCodec(QTextCodec::codecForName(s.toLocal8Bit()));
if (s.toLocal8Bit().compare(QByteArray("UTF-8"), Qt::CaseInsensitive) == 0) {
m_ts.setEncoding(QStringConverter::Utf8);
} else if (s.toLocal8Bit().compare(QByteArray("Windows-1252"), Qt::CaseInsensitive) == 0) {
m_ts.setEncoding(QStringConverter::System);
} else if (s.toLocal8Bit().compare(QByteArray("UTF-16"), Qt::CaseInsensitive) == 0) {
m_ts.setEncoding(QStringConverter::Utf16);
} else if (s.toLocal8Bit().compare(QByteArray("UTF-16LE"), Qt::CaseInsensitive) == 0) {
m_ts.setEncoding(QStringConverter::Utf16LE);
}
}

void CsvParser::setFieldSeparator(const QChar& c)
Expand Down
1 change: 0 additions & 1 deletion src/format/Kdbx4Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,6 @@ QVariantMap Kdbx4Reader::readVariantMap(QIODevice* device)
break;

case KeePass2::VariantMapFieldType::String:
//vm.insert(name, QVariant(QString::fromUtf8(valueBytes)));
vm.insert(name, QVariant(QString::fromUtf8(valueBytes.toStdString().c_str())));
break;

Expand Down
2 changes: 1 addition & 1 deletion src/format/KdbxXmlReader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2024 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
Expand Down
2 changes: 1 addition & 1 deletion src/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ MainWindow::MainWindow()
connect(m_inactivityTimer, SIGNAL(inactivityDetected()), this, SLOT(lockDatabasesAfterInactivity()));
applySettingsChanges();

/ Prevent conflicts with global Mac shortcuts (force Control on all platforms)
// Prevent conflicts with global Mac shortcuts (force Control on all platforms)
#ifdef Q_OS_MAC
auto modifier = Qt::META;
#else
Expand Down
6 changes: 3 additions & 3 deletions src/gui/csvImport/CsvImportWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
/*
* Copyright (C) 2016 Enrico Mariotti <enricomariotti@yahoo.it>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2024 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
Expand Down Expand Up @@ -39,7 +39,7 @@ namespace
return group;
}

auto nameList = groupPath.split("/", QString::SkipEmptyParts);
auto nameList = groupPath.split("/", Qt::SkipEmptyParts);
// Skip over first group name if root
if (nameList.first().compare("root", Qt::CaseInsensitive)) {
nameList.removeFirst();
Expand Down
14 changes: 6 additions & 8 deletions src/keys/drivers/YubiKey.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2024 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2014 Kyle Manna <kyle@kylemanna.com>
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -24,8 +24,6 @@
#include <QSet>
#include <QtConcurrent>

QMutex YubiKey::s_interfaceMutex(QMutex::Recursive);

YubiKey::YubiKey()
{
int num_interfaces = 0;
Expand Down Expand Up @@ -73,7 +71,7 @@ bool YubiKey::isInitialized()

bool YubiKey::findValidKeys()
{
QMutexLocker lock(&s_interfaceMutex);
QMutexLocker lock(&m_interfaces_detect_mutex);

m_usbKeys = YubiKeyInterfaceUSB::instance()->findValidKeys();
m_pcscKeys = YubiKeyInterfacePCSC::instance()->findValidKeys();
Expand All @@ -88,7 +86,7 @@ void YubiKey::findValidKeysAsync()

YubiKey::KeyMap YubiKey::foundKeys()
{
QMutexLocker lock(&s_interfaceMutex);
QMutexLocker lock(&m_interfaces_detect_mutex);
KeyMap foundKeys;

for (auto i = m_usbKeys.cbegin(); i != m_usbKeys.cend(); ++i) {
Expand All @@ -104,7 +102,7 @@ YubiKey::KeyMap YubiKey::foundKeys()

QString YubiKey::errorMessage()
{
QMutexLocker lock(&s_interfaceMutex);
QMutexLocker lock(&m_interfaces_detect_mutex);

QString error;
error.clear();
Expand Down Expand Up @@ -141,7 +139,7 @@ QString YubiKey::errorMessage()
*/
bool YubiKey::testChallenge(YubiKeySlot slot, bool* wouldBlock)
{
QMutexLocker lock(&s_interfaceMutex);
QMutexLocker lock(&m_interfaces_detect_mutex);

if (m_usbKeys.contains(slot)) {
return YubiKeyInterfaceUSB::instance()->testChallenge(slot, wouldBlock);
Expand All @@ -166,7 +164,7 @@ bool YubiKey::testChallenge(YubiKeySlot slot, bool* wouldBlock)
YubiKey::ChallengeResult
YubiKey::challenge(YubiKeySlot slot, const QByteArray& challenge, Botan::secure_vector<char>& response)
{
QMutexLocker lock(&s_interfaceMutex);
QMutexLocker lock(&m_interfaces_detect_mutex);

m_error.clear();

Expand Down

0 comments on commit a636daa

Please sign in to comment.