aboutsummaryrefslogtreecommitdiff
path: root/singleapplication.cpp
diff options
context:
space:
mode:
authorItay Grudev <itay-grudev@users.noreply.github.com>2017-10-02 12:17:41 +0100
committerGitHub <noreply@github.com>2017-10-02 12:17:41 +0100
commit4f0365107283ce321da713baeab4d2aad0456baf (patch)
tree09caa4a6b735885355b47d1312375fccdd168d3d /singleapplication.cpp
parentFixed typo in the CHANGELOG.md (diff)
downloadsingleapplication-4f0365107283ce321da713baeab4d2aad0456baf.tar.xz
Primary PID support (#36)v3.0.9
* Added the ability to bring the primary application window to the foreground on Windows systems by adding an option flag. THis option can only be used in Windows development and in applications derived from QApplication with a QMainWindow object. Because the primary application needs to be instructed to go to the foreground, the option SecondaryNotification must also be set to use this functionality * Changed the ability to bring the primary application window to the front as discussed in itay-grudev/SingleApplication#31. Now the process ID of the primary application get stored and is accessible for other instances of the application. It is to the developer to bring the applications windows to the front. For convenience the accompanying readme now contains a paragraph with example of how to do this on Windows systems. * v3.0.9 Added SingleApplicationPrivate::primaryPid()
Diffstat (limited to 'singleapplication.cpp')
-rw-r--r--singleapplication.cpp32
1 files changed, 28 insertions, 4 deletions
diff --git a/singleapplication.cpp b/singleapplication.cpp
index 1ab69fa..8711cb1 100644
--- a/singleapplication.cpp
+++ b/singleapplication.cpp
@@ -45,6 +45,7 @@
#include "singleapplication.h"
#include "singleapplication_p.h"
+
static const char NewInstance = 'N';
static const char SecondaryInstance = 'S';
static const char Reconnect = 'R';
@@ -61,14 +62,17 @@ SingleApplicationPrivate::~SingleApplicationPrivate()
socket->close();
delete socket;
}
+
memory->lock();
InstancesInfo* inst = (InstancesInfo*)memory->data();
if( server != nullptr ) {
server->close();
delete server;
inst->primary = false;
+ inst->primaryPid = -1;
}
memory->unlock();
+
delete memory;
}
@@ -128,6 +132,8 @@ void SingleApplicationPrivate::genBlockServerName( int timeout )
void SingleApplicationPrivate::startPrimary( bool resetMemory )
{
+ Q_Q(SingleApplication);
+
#ifdef Q_OS_UNIX
// Handle any further termination signals to ensure the
// QSharedMemory block is deleted even if the process crashes
@@ -158,13 +164,13 @@ void SingleApplicationPrivate::startPrimary( bool resetMemory )
memory->lock();
InstancesInfo* inst = (InstancesInfo*)memory->data();
- if( resetMemory ){
- inst->primary = true;
+ if( resetMemory ) {
inst->secondary = 0;
- } else {
- inst->primary = true;
}
+ inst->primary = true;
+ inst->primaryPid = q->applicationPid();
+
memory->unlock();
instanceNumber = 0;
@@ -217,6 +223,18 @@ void SingleApplicationPrivate::connectToPrimary( int msecs, char connectionType
}
}
+qint64 SingleApplicationPrivate::primaryPid()
+{
+ qint64 pid;
+
+ memory->lock();
+ InstancesInfo* inst = (InstancesInfo*)memory->data();
+ pid = inst->primaryPid;
+ memory->unlock();
+
+ return pid;
+}
+
#ifdef Q_OS_UNIX
void SingleApplicationPrivate::crashHandler()
{
@@ -433,6 +451,12 @@ quint32 SingleApplication::instanceId()
return d->instanceNumber;
}
+qint64 SingleApplication::primaryPid()
+{
+ Q_D(SingleApplication);
+ return d->primaryPid();
+}
+
bool SingleApplication::sendMessage( QByteArray message, int timeout )
{
Q_D(SingleApplication);