From a8fd01e50a3088df8bc6b367d1c14fe1da93be91 Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Wed, 6 Apr 2022 13:02:29 +0200 Subject: Message: Move encryption namespace parsing into Global private --- src/base/QXmppGlobal.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++ src/base/QXmppGlobal.h | 3 +++ src/base/QXmppGlobal_p.h | 20 +++++++++++++++++++ src/base/QXmppMessage.cpp | 36 ++++++---------------------------- 4 files changed, 78 insertions(+), 30 deletions(-) create mode 100644 src/base/QXmppGlobal.cpp create mode 100644 src/base/QXmppGlobal_p.h (limited to 'src/base') diff --git a/src/base/QXmppGlobal.cpp b/src/base/QXmppGlobal.cpp new file mode 100644 index 00000000..1d0d9afb --- /dev/null +++ b/src/base/QXmppGlobal.cpp @@ -0,0 +1,49 @@ +// SPDX-FileCopyrightText: 2022 Linus Jahn +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#include "QXmppConstants_p.h" +#include "QXmppGlobal_p.h" + +#include + +static const QStringList ENCRYPTION_NAMESPACES = { + QString(), + QString(), + ns_otr, + ns_legacy_openpgp, + ns_ox, + ns_omemo, + ns_omemo_1, + ns_omemo_2 +}; + +static const QStringList ENCRYPTION_NAMES = { + QString(), + QString(), + QStringLiteral("OTR"), + QStringLiteral("Legacy OpenPGP"), + QStringLiteral("OpenPGP for XMPP (OX)"), + QStringLiteral("OMEMO"), + QStringLiteral("OMEMO 1"), + QStringLiteral("OMEMO 2") +}; + +std::optional QXmpp::Private::encryptionFromString(const QString &str) +{ + int index = ENCRYPTION_NAMESPACES.indexOf(str); + if (index < 0) { + return {}; + } + return QXmpp::Encryption(index); +} + +QString QXmpp::Private::encryptionToString(Encryption encryption) +{ + return ENCRYPTION_NAMESPACES.at(int(encryption)); +} + +QString QXmpp::Private::encryptionToName(Encryption encryption) +{ + return ENCRYPTION_NAMES.at(int(encryption)); +} diff --git a/src/base/QXmppGlobal.h b/src/base/QXmppGlobal.h index 40f88801..9d93e4ac 100644 --- a/src/base/QXmppGlobal.h +++ b/src/base/QXmppGlobal.h @@ -73,6 +73,9 @@ enum Encryption { Omemo1, /// \xep{0384, OMEMO Encryption} since version 0.8 Omemo2, + +// Keep in sync with namespaces and names in Global.cpp! + #if QXMPP_DEPRECATED_SINCE(1, 5) /// \xep{0364, Current Off-the-Record Messaging Usage} /// \deprecated This enum is deprecated since QXmpp 1.5. Use diff --git a/src/base/QXmppGlobal_p.h b/src/base/QXmppGlobal_p.h new file mode 100644 index 00000000..0fb562fb --- /dev/null +++ b/src/base/QXmppGlobal_p.h @@ -0,0 +1,20 @@ +// SPDX-FileCopyrightText: 2022 Linus Jahn +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#ifndef QXMPPGLOBAL_P_H +#define QXMPPGLOBAL_P_H + +#include "QXmppGlobal.h" + +#include + +namespace QXmpp::Private { + +std::optional encryptionFromString(const QString &str); +QString encryptionToString(Encryption); +QString encryptionToName(Encryption); + +} + +#endif // QXMPPGLOBAL_P_H diff --git a/src/base/QXmppMessage.cpp b/src/base/QXmppMessage.cpp index dfb60240..83d05b2e 100644 --- a/src/base/QXmppMessage.cpp +++ b/src/base/QXmppMessage.cpp @@ -9,6 +9,7 @@ #include "QXmppBitsOfBinaryDataList.h" #include "QXmppConstants_p.h" +#include "QXmppGlobal_p.h" #include "QXmppMixInvitation.h" #include "QXmppOmemoElement.h" #include "QXmppTrustMessageElement.h" @@ -43,17 +44,6 @@ static const QStringList MARKER_TYPES = { QStringLiteral("acknowledged") }; -static const QStringList ENCRYPTION_NAMESPACES = { - QString(), - QString(), - ns_otr, - ns_legacy_openpgp, - ns_ox, - ns_omemo, - ns_omemo_1, - ns_omemo_2 -}; - static const QStringList HINT_TYPES = { QStringLiteral("no-permanent-store"), QStringLiteral("no-store"), @@ -61,17 +51,6 @@ static const QStringList HINT_TYPES = { QStringLiteral("store") }; -static const QStringList ENCRYPTION_NAMES = { - QString(), - QString(), - QStringLiteral("OTR"), - QStringLiteral("Legacy OpenPGP"), - QStringLiteral("OpenPGP for XMPP (OX)"), - QStringLiteral("OMEMO"), - QStringLiteral("OMEMO 1"), - QStringLiteral("OMEMO 2") -}; - static bool checkElement(const QDomElement &element, const QString &tagName, const QString &xmlns) { return element.tagName() == tagName && element.namespaceURI() == xmlns; @@ -974,13 +953,10 @@ void QXmppMessage::setMixUserNick(const QString &mixUserNick) /// QXmpp::Encryption QXmppMessage::encryptionMethod() const { - if (d->encryptionMethod.isEmpty()) + if (d->encryptionMethod.isEmpty()) { return QXmpp::NoEncryption; - - int index = ENCRYPTION_NAMESPACES.indexOf(d->encryptionMethod); - if (index < 0) - return QXmpp::UnknownEncryption; - return static_cast(index); + } + return QXmpp::Private::encryptionFromString(d->encryptionMethod).value_or(QXmpp::UnknownEncryption); } /// @@ -991,7 +967,7 @@ QXmpp::Encryption QXmppMessage::encryptionMethod() const /// void QXmppMessage::setEncryptionMethod(QXmpp::Encryption method) { - d->encryptionMethod = ENCRYPTION_NAMESPACES.at(int(method)); + d->encryptionMethod = QXmpp::Private::encryptionToString(method); } /// @@ -1027,7 +1003,7 @@ QString QXmppMessage::encryptionName() const { if (!d->encryptionName.isEmpty()) return d->encryptionName; - return ENCRYPTION_NAMES.at(int(encryptionMethod())); + return QXmpp::Private::encryptionToName(encryptionMethod()); } /// -- cgit v1.2.3