diff --git a/singleapplication.cpp b/singleapplication.cpp index 0791096..a4f842e 100644 --- a/singleapplication.cpp +++ b/singleapplication.cpp @@ -57,7 +57,7 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda // To mitigate QSharedMemory issues with large amount of processes // attempting to attach at the same time - d->randomSleep(); + SingleApplicationPrivate::randomSleep(); #ifdef Q_OS_UNIX // By explicitly attaching it and then deleting it we make sure that the @@ -117,7 +117,7 @@ SingleApplication::SingleApplication( int &argc, char *argv[], bool allowSeconda qDebug() << "SingleApplication: Unable to unlock memory for random wait."; qDebug() << d->memory->errorString(); } - d->randomSleep(); + SingleApplicationPrivate::randomSleep(); if( ! d->memory->lock() ){ qCritical() << "SingleApplication: Unable to lock memory after random wait."; abortSafely(); @@ -224,8 +224,7 @@ QString SingleApplication::primaryUser() */ QString SingleApplication::currentUser() { - Q_D( SingleApplication ); - return d->getUsername(); + return SingleApplicationPrivate::getUsername(); } /** diff --git a/singleapplication_p.cpp b/singleapplication_p.cpp index 4a8061f..65859c7 100644 --- a/singleapplication_p.cpp +++ b/singleapplication_p.cpp @@ -158,7 +158,7 @@ void SingleApplicationPrivate::genBlockServerName() blockServerName = appData.result().toBase64().replace("/", "_"); } -void SingleApplicationPrivate::initializeMemoryBlock() +void SingleApplicationPrivate::initializeMemoryBlock() const { auto *inst = static_cast( memory->data() ); inst->primary = false; @@ -170,13 +170,11 @@ void SingleApplicationPrivate::initializeMemoryBlock() void SingleApplicationPrivate::startPrimary() { - Q_Q(SingleApplication); - // Reset the number of connections auto *inst = static_cast ( memory->data() ); inst->primary = true; - inst->primaryPid = q->applicationPid(); + inst->primaryPid = QCoreApplication::applicationPid(); qstrncpy( inst->primaryUser, getUsername().toUtf8().data(), sizeof(inst->primaryUser) ); inst->checksum = blockChecksum(); instanceNumber = 0; @@ -211,7 +209,7 @@ void SingleApplicationPrivate::startSecondary() instanceNumber = inst->secondary; } -bool SingleApplicationPrivate::connectToPrimary( int timeout, ConnectionType connectionType ) +bool SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType connectionType ) { QElapsedTimer time; time.start(); @@ -233,14 +231,14 @@ bool SingleApplicationPrivate::connectToPrimary( int timeout, ConnectionType con socket->connectToServer( blockServerName ); if( socket->state() == QLocalSocket::ConnectingState ){ - socket->waitForConnected( static_cast(timeout - time.elapsed()) ); + socket->waitForConnected( static_cast(msecs - time.elapsed()) ); } // If connected break out of the loop if( socket->state() == QLocalSocket::ConnectedState ) break; // If elapsed time since start is longer than the method timeout return - if( time.elapsed() >= timeout ) return false; + if( time.elapsed() >= msecs ) return false; } } @@ -273,12 +271,12 @@ bool SingleApplicationPrivate::connectToPrimary( int timeout, ConnectionType con socket->write( header ); socket->write( initMsg ); - bool result = socket->waitForBytesWritten( static_cast(timeout - time.elapsed()) ); + bool result = socket->waitForBytesWritten( static_cast(msecs - time.elapsed()) ); socket->flush(); return result; } -quint16 SingleApplicationPrivate::blockChecksum() +quint16 SingleApplicationPrivate::blockChecksum() const { #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) quint16 checksum = qChecksum(QByteArray(static_cast(memory->constData()), offsetof(InstancesInfo, checksum))); @@ -288,7 +286,7 @@ quint16 SingleApplicationPrivate::blockChecksum() return checksum; } -qint64 SingleApplicationPrivate::primaryPid() +qint64 SingleApplicationPrivate::primaryPid() const { qint64 pid; @@ -300,7 +298,7 @@ qint64 SingleApplicationPrivate::primaryPid() return pid; } -QString SingleApplicationPrivate::primaryUser() +QString SingleApplicationPrivate::primaryUser() const { QByteArray username; diff --git a/singleapplication_p.h b/singleapplication_p.h index 2233707..d0703e3 100644 --- a/singleapplication_p.h +++ b/singleapplication_p.h @@ -70,18 +70,18 @@ public: SingleApplicationPrivate( SingleApplication *q_ptr ); ~SingleApplicationPrivate() override; - QString getUsername(); + static QString getUsername(); void genBlockServerName(); - void initializeMemoryBlock(); + void initializeMemoryBlock() const; void startPrimary(); void startSecondary(); - bool connectToPrimary(int msecs, ConnectionType connectionType ); - quint16 blockChecksum(); - qint64 primaryPid(); - QString primaryUser(); + bool connectToPrimary( int msecs, ConnectionType connectionType ); + quint16 blockChecksum() const; + qint64 primaryPid() const; + QString primaryUser() const; void readInitMessageHeader(QLocalSocket *socket); void readInitMessageBody(QLocalSocket *socket); - void randomSleep(); + static void randomSleep(); SingleApplication *q_ptr; QSharedMemory *memory;