Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a function to check if the instance is the only instance #91

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@ Returns if the instance is a secondary instance.

---

```cpp
bool SingleApplication::isSingleInstance()
```
Returns if the instance is the only running instance.

---

```cpp
quint32 SingleApplication::instanceId()
```
Expand Down
25 changes: 25 additions & 0 deletions singleapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,31 @@ bool SingleApplication::isSecondary()
return d->server == nullptr;
}

bool SingleApplication::isSingleInstance()
{
Q_D(SingleApplication);

InstancesInfo backup = *static_cast<InstancesInfo*>( d->memory->data() );

d->memory->detach();

if ( d->memory->create( sizeof( InstancesInfo ) ) ) {
d->memory->lock();
*static_cast<InstancesInfo*>( d->memory->data() ) = backup;
d->memory->unlock();
return true;
}

if( ! d->memory->attach() ) {
qCritical() << "SingleApplication: Unable to attach to shared memory block.";
qCritical() << d->memory->errorString();
delete d;
::exit( EXIT_FAILURE );
}

return false;
}

quint32 SingleApplication::instanceId()
{
Q_D(SingleApplication);
Expand Down
6 changes: 6 additions & 0 deletions singleapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ class SingleApplication : public QAPPLICATION_CLASS
*/
bool isSecondary();

/**
* @brief Check if the instance is the only running instance.
* @returns {bool}
*/
bool isSingleInstance();

/**
* @brief Returns a unique identifier for the current instance
* @returns {qint32}
Expand Down