qtsingleapplication.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of Qt Creator.
  7. **
  8. ** Commercial License Usage
  9. ** Licensees holding valid commercial Qt licenses may use this file in
  10. ** accordance with the commercial license agreement provided with the
  11. ** Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and The Qt Company. For licensing terms
  13. ** and conditions see https://www.qt.io/terms-conditions. For further
  14. ** information use the contact form at https://www.qt.io/contact-us.
  15. **
  16. ** GNU General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU
  18. ** General Public License version 3 as published by the Free Software
  19. ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
  20. ** included in the packaging of this file. Please review the following
  21. ** information to ensure the GNU General Public License requirements will
  22. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  23. **
  24. ****************************************************************************/
  25. #include "qtsingleapplication.h"
  26. #include "qtlocalpeer.h"
  27. #include <qtlockedfile.h>
  28. #include <QDir>
  29. #include <QFileOpenEvent>
  30. #include <QSharedMemory>
  31. #include <QWidget>
  32. namespace SharedTools {
  33. static const int instancesSize = 1024;
  34. static QString instancesLockFilename(const QString &appSessionId)
  35. {
  36. const QChar slash(QLatin1Char('/'));
  37. QString res = QDir::tempPath();
  38. if (!res.endsWith(slash))
  39. res += slash;
  40. return res + appSessionId + QLatin1String("-instances");
  41. }
  42. QtSingleApplication::QtSingleApplication(const QString &appId, int &argc, char **argv)
  43. : QApplication(argc, argv),
  44. firstPeer(-1),
  45. pidPeer(0)
  46. {
  47. this->appId = appId;
  48. const QString appSessionId = QtLocalPeer::appSessionId(appId);
  49. // This shared memory holds a zero-terminated array of active (or crashed) instances
  50. instances = new QSharedMemory(appSessionId, this);
  51. actWin = 0;
  52. block = false;
  53. // First instance creates the shared memory, later instances attach to it
  54. const bool created = instances->create(instancesSize);
  55. if (!created) {
  56. if (!instances->attach()) {
  57. qWarning() << "Failed to initialize instances shared memory: "
  58. << instances->errorString();
  59. delete instances;
  60. instances = 0;
  61. return;
  62. }
  63. }
  64. // QtLockedFile is used to workaround QTBUG-10364
  65. QtLockedFile lockfile(instancesLockFilename(appSessionId));
  66. lockfile.open(QtLockedFile::ReadWrite);
  67. lockfile.lock(QtLockedFile::WriteLock);
  68. qint64 *pids = static_cast<qint64 *>(instances->data());
  69. if (!created) {
  70. // Find the first instance that it still running
  71. // The whole list needs to be iterated in order to append to it
  72. for (; *pids; ++pids) {
  73. if (firstPeer == -1 && isRunning(*pids))
  74. firstPeer = *pids;
  75. }
  76. }
  77. // Add current pid to list and terminate it
  78. *pids++ = QCoreApplication::applicationPid();
  79. *pids = 0;
  80. pidPeer = new QtLocalPeer(this, appId + QLatin1Char('-') +
  81. QString::number(QCoreApplication::applicationPid()));
  82. connect(pidPeer, &QtLocalPeer::messageReceived, this, &QtSingleApplication::messageReceived);
  83. pidPeer->isClient();
  84. lockfile.unlock();
  85. }
  86. QtSingleApplication::~QtSingleApplication()
  87. {
  88. if (!instances)
  89. return;
  90. const qint64 appPid = QCoreApplication::applicationPid();
  91. QtLockedFile lockfile(instancesLockFilename(QtLocalPeer::appSessionId(appId)));
  92. lockfile.open(QtLockedFile::ReadWrite);
  93. lockfile.lock(QtLockedFile::WriteLock);
  94. // Rewrite array, removing current pid and previously crashed ones
  95. qint64 *pids = static_cast<qint64 *>(instances->data());
  96. qint64 *newpids = pids;
  97. for (; *pids; ++pids) {
  98. if (*pids != appPid && isRunning(*pids))
  99. *newpids++ = *pids;
  100. }
  101. *newpids = 0;
  102. lockfile.unlock();
  103. }
  104. bool QtSingleApplication::event(QEvent *event)
  105. {
  106. if (event->type() == QEvent::FileOpen) {
  107. QFileOpenEvent *foe = static_cast<QFileOpenEvent*>(event);
  108. emit fileOpenRequest(foe->file());
  109. return true;
  110. }
  111. return QApplication::event(event);
  112. }
  113. bool QtSingleApplication::isRunning(qint64 pid)
  114. {
  115. if (pid == -1) {
  116. pid = firstPeer;
  117. if (pid == -1)
  118. return false;
  119. }
  120. QtLocalPeer peer(this, appId + QLatin1Char('-') + QString::number(pid, 10));
  121. return peer.isClient();
  122. }
  123. bool QtSingleApplication::sendMessage(const QString &message, int timeout, qint64 pid)
  124. {
  125. if (pid == -1) {
  126. pid = firstPeer;
  127. if (pid == -1)
  128. return false;
  129. }
  130. QtLocalPeer peer(this, appId + QLatin1Char('-') + QString::number(pid, 10));
  131. return peer.sendMessage(message, timeout, block);
  132. }
  133. QString QtSingleApplication::applicationId() const
  134. {
  135. return appId;
  136. }
  137. void QtSingleApplication::setBlock(bool value)
  138. {
  139. block = value;
  140. }
  141. void QtSingleApplication::setActivationWindow(QWidget *aw, bool activateOnMessage)
  142. {
  143. actWin = aw;
  144. if (!pidPeer)
  145. return;
  146. if (activateOnMessage)
  147. connect(pidPeer, &QtLocalPeer::messageReceived, this, &QtSingleApplication::activateWindow);
  148. else
  149. disconnect(pidPeer, &QtLocalPeer::messageReceived, this, &QtSingleApplication::activateWindow);
  150. }
  151. QWidget* QtSingleApplication::activationWindow() const
  152. {
  153. return actWin;
  154. }
  155. void QtSingleApplication::activateWindow()
  156. {
  157. if (actWin) {
  158. actWin->setWindowState(actWin->windowState() & ~Qt::WindowMinimized);
  159. actWin->raise();
  160. actWin->activateWindow();
  161. }
  162. }
  163. } // namespace SharedTools