aboutsummaryrefslogtreecommitdiff
path: root/src/base/QXmppUtils.cpp
diff options
context:
space:
mode:
authorLinus Jahn <lnj@kaidan.im>2020-02-10 22:16:12 +0100
committerLNJ <lnj@kaidan.im>2020-02-11 16:19:40 +0100
commitf583f1a71459f413f9f869b8f1616722054dbea8 (patch)
treeb63ff44750dba3ffb92cf1b73c37ff44f0596eac /src/base/QXmppUtils.cpp
parentbfd26369d1ef032837fbd2b52ea0ed4cc04abe91 (diff)
downloadqxmpp-f583f1a71459f413f9f869b8f1616722054dbea8.tar.gz
utils: Generate UUIDs for stanza hashes by default
The QXmppUtils::generateStanzaHash() generates UUIDs by default now. UUIDs are not generated, if the default parameter is changed to a different value (!= 36). The behaviour is not changed for other values than 36. This way all users of QXmpp will automatically start to use UUIDs, if they use the generateStanzaHash() method.
Diffstat (limited to 'src/base/QXmppUtils.cpp')
-rw-r--r--src/base/QXmppUtils.cpp31
1 files changed, 30 insertions, 1 deletions
diff --git a/src/base/QXmppUtils.cpp b/src/base/QXmppUtils.cpp
index 7c5f631e..5d09c1c7 100644
--- a/src/base/QXmppUtils.cpp
+++ b/src/base/QXmppUtils.cpp
@@ -35,6 +35,7 @@
#include <QRegExp>
#include <QString>
#include <QStringList>
+#include <QUuid>
#include <QXmlStreamWriter>
// adapted from public domain source by Ross Williams and Eric Durbin
@@ -291,12 +292,40 @@ QByteArray QXmppUtils::generateRandomBytes(int length)
return bytes;
}
+///
+/// Creates a new stanza id in the UUID format.
+///
+/// \since QXmpp 1.3
+///
+QString QXmppUtils::generateStanzaUuid()
+{
+#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
+ return QUuid::createUuid().toString(QUuid::WithoutBraces);
+#else
+ return QUuid::createUuid().toString().mid(1, 36);
+#endif
+}
+
+///
/// Returns a random alphanumerical string of the specified size.
///
+/// Since QXmpp 1.3 this will generate a UUID, if the specified \p length is 36
+/// which is also the new default value. The returned string is still 36
+/// characters long, but will contain dashes (as specified in the UUID format).
+///
+/// \note It is recommended to use UUIDs for cases where IDs must be unique and
+/// are possibly stored permanently. This can be done using
+/// QXmppUtils::generateStanzaUuid(). However, since that function is only
+/// available since QXmpp 1.3, you may also want to continue to use this
+/// function because of compatibility reasons.
+///
/// \param length
-
+///
QString QXmppUtils::generateStanzaHash(int length)
{
+ if (length == 36)
+ return QXmppUtils::generateStanzaUuid();
+
const QString somechars = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int N = somechars.size();
QString hashResult;