diff options
| author | Linus Jahn <lnj@kaidan.im> | 2022-04-06 13:02:29 +0200 |
|---|---|---|
| committer | Linus Jahn <lnj@kaidan.im> | 2022-04-06 15:02:47 +0200 |
| commit | a8fd01e50a3088df8bc6b367d1c14fe1da93be91 (patch) | |
| tree | 415b66cdd850fdb0491e0d2e5fd698ae61f1c1d8 /src | |
| parent | a3e110d6a219f2dc3bfd6d8fc238f5d022f30aea (diff) | |
| download | qxmpp-a8fd01e50a3088df8bc6b367d1c14fe1da93be91.tar.gz | |
Message: Move encryption namespace parsing into Global private
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/base/QXmppGlobal.cpp | 49 | ||||
| -rw-r--r-- | src/base/QXmppGlobal.h | 3 | ||||
| -rw-r--r-- | src/base/QXmppGlobal_p.h | 20 | ||||
| -rw-r--r-- | src/base/QXmppMessage.cpp | 36 |
5 files changed, 79 insertions, 30 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 88afe50e..5da1f458 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -153,6 +153,7 @@ set(SOURCE_FILES base/QXmppElement.cpp base/QXmppEntityTimeIq.cpp base/QXmppGeolocItem.cpp + base/QXmppGlobal.cpp base/QXmppHttpUploadIq.cpp base/QXmppIbbIq.cpp base/QXmppIq.cpp 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 <lnj@kaidan.im> +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#include "QXmppConstants_p.h" +#include "QXmppGlobal_p.h" + +#include <QList> + +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::Encryption> 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 <lnj@kaidan.im> +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#ifndef QXMPPGLOBAL_P_H +#define QXMPPGLOBAL_P_H + +#include "QXmppGlobal.h" + +#include <optional> + +namespace QXmpp::Private { + +std::optional<Encryption> 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<QXmpp::Encryption>(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()); } /// |
