Port away from deprecated qsrand() & qrand()

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