Port away from deprecated qsrand() & qrand()

GIT_SILENT
parent 91112821
......@@ -30,6 +30,9 @@
#include <QStandardPaths>
#include <QMutex>
#include <QTimer>
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
#include <QRandomGenerator>
#endif
#include <interfaces/idocumentcontroller.h>
#include <interfaces/icore.h>
......@@ -1064,7 +1067,11 @@ unloadContexts:
checkContextsCount = percentageOfContexts;
if (visitor.checkContexts.size() > ( int )checkContextsCount)
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
startPos = QRandomGenerator::global()->bounded(visitor.checkContexts.size() - checkContextsCount);
#else
startPos = qrand() % (visitor.checkContexts.size() - checkContextsCount);
#endif
int endPos = startPos + maxFinalCleanupCheckContexts;
if (endPos > visitor.checkContexts.size())
......
......@@ -123,7 +123,9 @@ public:
void newSession()
{
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
qsrand(QDateTime::currentDateTimeUtc().toTime_t());
#endif
Session* session = new Session( QUuid::createUuid().toString() );
KProcess::startDetached(ShellExtension::getInstance()->executableFilePath(), QStringList() << QStringLiteral("-s") << session->id().toString() << standardArguments());
......@@ -415,7 +417,9 @@ Session* SessionController::createSession( const QString& name )
if(name.startsWith(QLatin1Char('{'))) {
s = new Session( QUuid(name).toString(), this );
}else{
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
qsrand(QDateTime::currentDateTimeUtc().toTime_t());
#endif
s = new Session( QUuid::createUuid().toString(), this );
s->setName( name );
}
......@@ -502,7 +506,9 @@ QString SessionController::cloneSession( const QString& nameOrid )
Q_D(SessionController);
Session* origSession = session( nameOrid );
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
qsrand(QDateTime::currentDateTimeUtc().toTime_t());
#endif
QUuid id = QUuid::createUuid();
auto copyJob = KIO::copy(QUrl::fromLocalFile(sessionDirectory(origSession->id().toString())),
QUrl::fromLocalFile(sessionDirectory( id.toString())));
......@@ -665,7 +671,9 @@ QString SessionController::showSessionChooserDialog(const QString& headerText, b
const QString selectedSessionId = selected.sibling(selected.row(), 0).data().toString();
if (selectedSessionId.isEmpty()) {
// "Create New Session" item selected, return a fresh UUID
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
qsrand(QDateTime::currentDateTimeUtc().toTime_t());
#endif
return QUuid::createUuid().toString();
}
return selectedSessionId;
......
......@@ -20,6 +20,9 @@
#include <QTimer>
#include <QVBoxLayout>
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
#include <QRandomGenerator>
#endif
#include "mainwindow.h"
#include "partdocument.h"
......@@ -105,8 +108,16 @@ const QString WorkingSetController::makeSetId(const QString& prefix) const
{
QString newId;
const uint maxRetries = 10;
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
auto* randomGenerator = QRandomGenerator::global();
#endif
for(uint retry = 2; retry <= maxRetries; retry++) {
newId = QStringLiteral("%1_%2").arg(prefix).arg(qrand() % 10000000);
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
const auto random = randomGenerator->bounded(10000000);
#else
const auto random = qrand() % 10000000;
#endif
newId = QStringLiteral("%1_%2").arg(prefix).arg(random);
WorkingSetIconParameters params(newId);
for (WorkingSet* set : m_workingSets) {
if(set->isEmpty()) {
......
......@@ -21,6 +21,9 @@
#include <QTest>
#include <QThread>
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
#include <QRandomGenerator>
#endif
#include "../foregroundlock.h"
......@@ -38,11 +41,18 @@ public:
void run() override
{
ForegroundLock lock(false);
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
auto* randomGenerator = QRandomGenerator::global();
#endif
for (int i = 0; i < 1000; ++i) {
if (lock.tryLock()) {
lock.unlock();
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
QThread::usleep(randomGenerator->bounded(20));
#else
QThread::usleep(qrand() % 20);
#endif
}
}
};
......
......@@ -24,6 +24,9 @@
#include <QProcess>
#include <QTemporaryDir>
#include <QDebug>
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
#include <QRandomGenerator>
#endif
#include <tests/autotestshell.h>
#include <tests/testcore.h>
......@@ -99,10 +102,16 @@ bool createFile(const QString& path)
return false;
}
f.write(QByteArray::number(qrand()));
f.write(QByteArray::number(qrand()));
f.write(QByteArray::number(qrand()));
f.write(QByteArray::number(qrand()));
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
auto* randomGenerator = QRandomGenerator::global();
#endif
for (int i = 0; i < 4; ++i) {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
f.write(QByteArray::number(randomGenerator->generate()));
#else
f.write(QByteArray::number(qrand()));
#endif
}
if (!f.flush()) {
qWarning() << f.errorString() << path;
......@@ -116,8 +125,13 @@ bool createFile(const QString& path)
bool writeRandomStructure(QString path, int files)
{
QDir p(path);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
const QString name = QString::number(QRandomGenerator::global()->generate());
if (QRandomGenerator::global()->bounded(5) < 1) {
#else
QString name = QString::number(qrand());
if (qrand() < RAND_MAX / 5) {
#endif
if (!p.mkdir(name)) {
return false;
}
......
......@@ -30,6 +30,9 @@
#include <tests/testhelpers.h>
#include <QTest>
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
#include <QRandomGenerator>
#endif
QTEST_GUILESS_MAIN(TestContexts)
......@@ -54,7 +57,12 @@ void TestContexts::testFunctionContext()
QFETCH(RangeInRevision, argCtxRange);
QFETCH(RangeInRevision, bodyCtxRange);
const IndexedString file(QUrl(QStringLiteral("file:///internal/%1-functionContext.js").arg(qrand())));
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
const auto random = QRandomGenerator::global()->generate();
#else
const auto random = qrand();
#endif
const IndexedString file(QUrl(QStringLiteral("file:///internal/%1-functionContext.js").arg(random)));
ParseSession session(file, code, 0);
QVERIFY(session.ast());
QCOMPARE(session.language().dialect(), QmlJS::Dialect::JavaScript);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment