Skip to content

Commit

Permalink
QtSingleApplication: Replace custom lockfile.
Browse files Browse the repository at this point in the history
  • Loading branch information
gottcode committed Dec 5, 2018
1 parent 2809a9c commit 6504e14
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
20 changes: 7 additions & 13 deletions src/qtsingleapplication/qtlocalpeer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@ static PProcessIdToSessionId pProcessIdToSessionId = 0;
#include <unistd.h>
#endif

namespace QtLP_Private {
#include "qtlockedfile.cpp"
#if defined(Q_OS_WIN)
#include "qtlockedfile_win.cpp"
#else
#include "qtlockedfile_unix.cpp"
#endif
}

const char* QtLocalPeer::ack = "ack";

QtLocalPeer::QtLocalPeer(QObject* parent, const QString &appId)
Expand Down Expand Up @@ -104,18 +95,21 @@ QtLocalPeer::QtLocalPeer(QObject* parent, const QString &appId)
QString lockName = QDir(QDir::tempPath()).absolutePath()
+ QLatin1Char('/') + socketName
+ QLatin1String("-lockfile");
lockFile.setFileName(lockName);
lockFile.open(QIODevice::ReadWrite);
lockFile = new QLockFile(lockName);
lockFile->setStaleLockTime(0);
#if defined(Q_OS_WIN)
lockFile->removeStaleLockFile();
#endif
}



bool QtLocalPeer::isClient()
{
if (lockFile.isLocked())
if (lockFile->isLocked())
return false;

if (!lockFile.lock(QtLP_Private::QtLockedFile::WriteLock, false))
if (!lockFile->tryLock(50))
return true;

bool res = server->listen(socketName);
Expand Down
7 changes: 4 additions & 3 deletions src/qtsingleapplication/qtlocalpeer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@

#include <QLocalServer>
#include <QLocalSocket>
#include <QLockFile>
#include <QDir>

#include "qtlockedfile.h"

class QtLocalPeer : public QObject
{
Q_OBJECT

public:
QtLocalPeer(QObject *parent = 0, const QString &appId = QString());
~QtLocalPeer()
{ delete lockFile; }
bool isClient();
bool sendMessage(const QString &message, int timeout);
QString applicationId() const
Expand All @@ -68,7 +69,7 @@ protected Q_SLOTS:
QString id;
QString socketName;
QLocalServer* server;
QtLP_Private::QtLockedFile lockFile;
QLockFile* lockFile;

private:
static const char* ack;
Expand Down

0 comments on commit 6504e14

Please sign in to comment.