Explicitly unlocking current instance to allow restarting on all machines.
This commit is contained in:
parent
233f8b146e
commit
19e910c932
@ -34,6 +34,7 @@
|
|||||||
#include "gui/formmain.h"
|
#include "gui/formmain.h"
|
||||||
#include "gui/webbrowser.h"
|
#include "gui/webbrowser.h"
|
||||||
#include "gui/messagebox.h"
|
#include "gui/messagebox.h"
|
||||||
|
#include "qtsingleapplication/qtsingleapplication.h"
|
||||||
|
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
#include <QNetworkProxy>
|
#include <QNetworkProxy>
|
||||||
@ -274,6 +275,7 @@ void FormSettings::promptForRestart() {
|
|||||||
tr("Application couldn't be restarted. Please, restart it manually for changes to take effect."));
|
tr("Application couldn't be restarted. Please, restart it manually for changes to take effect."));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
QtSingleApplication::instance()->unlock();
|
||||||
qApp->quit();
|
qApp->quit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,137 +67,129 @@ namespace QtLP_Private {
|
|||||||
const char* QtLocalPeer::ack = "ack";
|
const char* QtLocalPeer::ack = "ack";
|
||||||
|
|
||||||
QtLocalPeer::QtLocalPeer(QObject* parent, const QString &appId)
|
QtLocalPeer::QtLocalPeer(QObject* parent, const QString &appId)
|
||||||
: QObject(parent), id(appId)
|
: QObject(parent), id(appId) {
|
||||||
{
|
QString prefix = id;
|
||||||
QString prefix = id;
|
if (id.isEmpty()) {
|
||||||
if (id.isEmpty()) {
|
id = QCoreApplication::applicationFilePath();
|
||||||
id = QCoreApplication::applicationFilePath();
|
|
||||||
#if defined(Q_OS_WIN)
|
#if defined(Q_OS_WIN)
|
||||||
id = id.toLower();
|
id = id.toLower();
|
||||||
#endif
|
#endif
|
||||||
prefix = id.section(QLatin1Char('/'), -1);
|
prefix = id.section(QLatin1Char('/'), -1);
|
||||||
}
|
}
|
||||||
prefix.remove(QRegExp("[^a-zA-Z]"));
|
prefix.remove(QRegExp("[^a-zA-Z]"));
|
||||||
prefix.truncate(6);
|
prefix.truncate(6);
|
||||||
|
|
||||||
QByteArray idc = id.toUtf8();
|
QByteArray idc = id.toUtf8();
|
||||||
quint16 idNum = qChecksum(idc.constData(), idc.size());
|
quint16 idNum = qChecksum(idc.constData(), idc.size());
|
||||||
socketName = QLatin1String("qtsingleapp-") + prefix
|
socketName = QLatin1String("qtsingleapp-") + prefix
|
||||||
+ QLatin1Char('-') + QString::number(idNum, 16);
|
+ QLatin1Char('-') + QString::number(idNum, 16);
|
||||||
|
|
||||||
#if defined(Q_OS_WIN)
|
#if defined(Q_OS_WIN)
|
||||||
if (!pProcessIdToSessionId) {
|
if (!pProcessIdToSessionId) {
|
||||||
QLibrary lib("kernel32");
|
QLibrary lib("kernel32");
|
||||||
pProcessIdToSessionId = (PProcessIdToSessionId)lib.resolve("ProcessIdToSessionId");
|
pProcessIdToSessionId = (PProcessIdToSessionId)lib.resolve("ProcessIdToSessionId");
|
||||||
}
|
}
|
||||||
if (pProcessIdToSessionId) {
|
if (pProcessIdToSessionId) {
|
||||||
DWORD sessionId = 0;
|
DWORD sessionId = 0;
|
||||||
pProcessIdToSessionId(GetCurrentProcessId(), &sessionId);
|
pProcessIdToSessionId(GetCurrentProcessId(), &sessionId);
|
||||||
socketName += QLatin1Char('-') + QString::number(sessionId, 16);
|
socketName += QLatin1Char('-') + QString::number(sessionId, 16);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
socketName += QLatin1Char('-') + QString::number(::getuid(), 16);
|
socketName += QLatin1Char('-') + QString::number(::getuid(), 16);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
server = new QLocalServer(this);
|
server = new QLocalServer(this);
|
||||||
QString lockName = QDir(QDir::tempPath()).absolutePath()
|
QString lockName = QDir(QDir::tempPath()).absolutePath()
|
||||||
+ QLatin1Char('/') + socketName
|
+ QLatin1Char('/') + socketName
|
||||||
+ QLatin1String("-lockfile");
|
+ QLatin1String("-lockfile");
|
||||||
lockFile.setFileName(lockName);
|
lockFile.setFileName(lockName);
|
||||||
lockFile.open(QIODevice::ReadWrite);
|
lockFile.open(QIODevice::ReadWrite);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool QtLocalPeer::isClient() {
|
||||||
|
if (lockFile.isLocked())
|
||||||
bool QtLocalPeer::isClient()
|
|
||||||
{
|
|
||||||
if (lockFile.isLocked())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!lockFile.lock(QtLP_Private::QtLockedFile::WriteLock, false))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
bool res = server->listen(socketName);
|
|
||||||
#if defined(Q_OS_UNIX) && (QT_VERSION >= QT_VERSION_CHECK(4,5,0))
|
|
||||||
// ### Workaround
|
|
||||||
if (!res && server->serverError() == QAbstractSocket::AddressInUseError) {
|
|
||||||
QFile::remove(QDir::cleanPath(QDir::tempPath())+QLatin1Char('/')+socketName);
|
|
||||||
res = server->listen(socketName);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (!res)
|
|
||||||
qWarning("QtSingleCoreApplication: listen on local socket failed, %s", qPrintable(server->errorString()));
|
|
||||||
QObject::connect(server, SIGNAL(newConnection()), SLOT(receiveConnection()));
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
|
if (!lockFile.lock(QtLP_Private::QtLockedFile::WriteLock, false))
|
||||||
|
return true;
|
||||||
|
|
||||||
bool QtLocalPeer::sendMessage(const QString &message, int timeout)
|
bool res = server->listen(socketName);
|
||||||
{
|
#if defined(Q_OS_UNIX) && (QT_VERSION >= QT_VERSION_CHECK(4,5,0))
|
||||||
if (!isClient())
|
// ### Workaround
|
||||||
return false;
|
if (!res && server->serverError() == QAbstractSocket::AddressInUseError) {
|
||||||
|
QFile::remove(QDir::cleanPath(QDir::tempPath())+QLatin1Char('/')+socketName);
|
||||||
QLocalSocket socket;
|
res = server->listen(socketName);
|
||||||
bool connOk = false;
|
}
|
||||||
for(int i = 0; i < 2; i++) {
|
|
||||||
// Try twice, in case the other instance is just starting up
|
|
||||||
socket.connectToServer(socketName);
|
|
||||||
connOk = socket.waitForConnected(timeout/2);
|
|
||||||
if (connOk || i)
|
|
||||||
break;
|
|
||||||
int ms = 250;
|
|
||||||
#if defined(Q_OS_WIN)
|
|
||||||
Sleep(DWORD(ms));
|
|
||||||
#else
|
|
||||||
struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
|
|
||||||
nanosleep(&ts, NULL);
|
|
||||||
#endif
|
#endif
|
||||||
}
|
if (!res)
|
||||||
if (!connOk)
|
qWarning("QtSingleCoreApplication: listen on local socket failed, %s", qPrintable(server->errorString()));
|
||||||
return false;
|
QObject::connect(server, SIGNAL(newConnection()), SLOT(receiveConnection()));
|
||||||
|
return false;
|
||||||
QByteArray uMsg(message.toUtf8());
|
|
||||||
QDataStream ds(&socket);
|
|
||||||
ds.writeBytes(uMsg.constData(), uMsg.size());
|
|
||||||
bool res = socket.waitForBytesWritten(timeout);
|
|
||||||
if (res) {
|
|
||||||
res &= socket.waitForReadyRead(timeout); // wait for ack
|
|
||||||
if (res)
|
|
||||||
res &= (socket.read(qstrlen(ack)) == ack);
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool QtLocalPeer::sendMessage(const QString &message, int timeout) {
|
||||||
|
if (!isClient())
|
||||||
|
return false;
|
||||||
|
|
||||||
void QtLocalPeer::receiveConnection()
|
QLocalSocket socket;
|
||||||
{
|
bool connOk = false;
|
||||||
QLocalSocket* socket = server->nextPendingConnection();
|
for(int i = 0; i < 2; i++) {
|
||||||
if (!socket)
|
// Try twice, in case the other instance is just starting up
|
||||||
return;
|
socket.connectToServer(socketName);
|
||||||
|
connOk = socket.waitForConnected(timeout/2);
|
||||||
|
if (connOk || i)
|
||||||
|
break;
|
||||||
|
int ms = 250;
|
||||||
|
#if defined(Q_OS_WIN)
|
||||||
|
Sleep(DWORD(ms));
|
||||||
|
#else
|
||||||
|
struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
|
||||||
|
nanosleep(&ts, NULL);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
if (!connOk)
|
||||||
|
return false;
|
||||||
|
|
||||||
while (socket->bytesAvailable() < (int)sizeof(quint32))
|
QByteArray uMsg(message.toUtf8());
|
||||||
socket->waitForReadyRead();
|
QDataStream ds(&socket);
|
||||||
QDataStream ds(socket);
|
ds.writeBytes(uMsg.constData(), uMsg.size());
|
||||||
QByteArray uMsg;
|
bool res = socket.waitForBytesWritten(timeout);
|
||||||
quint32 remaining;
|
if (res) {
|
||||||
ds >> remaining;
|
res &= socket.waitForReadyRead(timeout); // wait for ack
|
||||||
uMsg.resize(remaining);
|
if (res)
|
||||||
int got = 0;
|
res &= (socket.read(qstrlen(ack)) == ack);
|
||||||
char* uMsgBuf = uMsg.data();
|
}
|
||||||
do {
|
return res;
|
||||||
got = ds.readRawData(uMsgBuf, remaining);
|
}
|
||||||
remaining -= got;
|
|
||||||
uMsgBuf += got;
|
void QtLocalPeer::receiveConnection() {
|
||||||
} while (remaining && got >= 0 && socket->waitForReadyRead(2000));
|
QLocalSocket* socket = server->nextPendingConnection();
|
||||||
if (got < 0) {
|
if (!socket)
|
||||||
qWarning("QtLocalPeer: Message reception failed %s", socket->errorString().toLatin1().constData());
|
return;
|
||||||
delete socket;
|
|
||||||
return;
|
while (socket->bytesAvailable() < (int)sizeof(quint32))
|
||||||
}
|
socket->waitForReadyRead();
|
||||||
QString message(QString::fromUtf8(uMsg));
|
QDataStream ds(socket);
|
||||||
socket->write(ack, qstrlen(ack));
|
QByteArray uMsg;
|
||||||
socket->waitForBytesWritten(1000);
|
quint32 remaining;
|
||||||
socket->waitForDisconnected(1000); // make sure client reads ack
|
ds >> remaining;
|
||||||
|
uMsg.resize(remaining);
|
||||||
|
int got = 0;
|
||||||
|
char* uMsgBuf = uMsg.data();
|
||||||
|
do {
|
||||||
|
got = ds.readRawData(uMsgBuf, remaining);
|
||||||
|
remaining -= got;
|
||||||
|
uMsgBuf += got;
|
||||||
|
} while (remaining && got >= 0 && socket->waitForReadyRead(2000));
|
||||||
|
if (got < 0) {
|
||||||
|
qWarning("QtLocalPeer: Message reception failed %s", socket->errorString().toLatin1().constData());
|
||||||
delete socket;
|
delete socket;
|
||||||
emit messageReceived(message); //### (might take a long time to return)
|
return;
|
||||||
|
}
|
||||||
|
QString message(QString::fromUtf8(uMsg));
|
||||||
|
socket->write(ack, qstrlen(ack));
|
||||||
|
socket->waitForBytesWritten(1000);
|
||||||
|
socket->waitForDisconnected(1000); // make sure client reads ack
|
||||||
|
delete socket;
|
||||||
|
emit messageReceived(message); //### (might take a long time to return)
|
||||||
}
|
}
|
||||||
|
@ -47,21 +47,28 @@
|
|||||||
|
|
||||||
#include "qtlockedfile.h"
|
#include "qtlockedfile.h"
|
||||||
|
|
||||||
class QtLocalPeer : public QObject
|
class QtLocalPeer : public QObject {
|
||||||
{
|
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit QtLocalPeer(QObject *parent = 0, const QString &appId = QString());
|
explicit QtLocalPeer(QObject *parent = 0, const QString &appId = QString());
|
||||||
|
|
||||||
bool isClient();
|
bool isClient();
|
||||||
bool sendMessage(const QString &message, int timeout);
|
bool sendMessage(const QString &message, int timeout);
|
||||||
QString applicationId() const
|
|
||||||
{ return id; }
|
|
||||||
|
|
||||||
Q_SIGNALS:
|
QString applicationId() const {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unlocks the file.
|
||||||
|
bool unlock() {
|
||||||
|
return lockFile.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
Q_SIGNALS:
|
||||||
void messageReceived(const QString &message);
|
void messageReceived(const QString &message);
|
||||||
|
|
||||||
protected Q_SLOTS:
|
protected Q_SLOTS:
|
||||||
void receiveConnection();
|
void receiveConnection();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -38,310 +38,83 @@
|
|||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
#include "qtsingleapplication.h"
|
#include "qtsingleapplication.h"
|
||||||
#include "qtlocalpeer.h"
|
#include "qtlocalpeer.h"
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
|
|
||||||
/*!
|
void QtSingleApplication::sysInit(const QString &appId) {
|
||||||
\class QtSingleApplication qtsingleapplication.h
|
actWin = 0;
|
||||||
\brief The QtSingleApplication class provides an API to detect and
|
peer = new QtLocalPeer(this, appId);
|
||||||
communicate with running instances of an application.
|
connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&)));
|
||||||
|
|
||||||
This class allows you to create applications where only one
|
|
||||||
instance should be running at a time. I.e., if the user tries to
|
|
||||||
launch another instance, the already running instance will be
|
|
||||||
activated instead. Another usecase is a client-server system,
|
|
||||||
where the first started instance will assume the role of server,
|
|
||||||
and the later instances will act as clients of that server.
|
|
||||||
|
|
||||||
By default, the full path of the executable file is used to
|
|
||||||
determine whether two processes are instances of the same
|
|
||||||
application. You can also provide an explicit identifier string
|
|
||||||
that will be compared instead.
|
|
||||||
|
|
||||||
The application should create the QtSingleApplication object early
|
|
||||||
in the startup phase, and call isRunning() to find out if another
|
|
||||||
instance of this application is already running. If isRunning()
|
|
||||||
returns false, it means that no other instance is running, and
|
|
||||||
this instance has assumed the role as the running instance. In
|
|
||||||
this case, the application should continue with the initialization
|
|
||||||
of the application user interface before entering the event loop
|
|
||||||
with exec(), as normal.
|
|
||||||
|
|
||||||
The messageReceived() signal will be emitted when the running
|
|
||||||
application receives messages from another instance of the same
|
|
||||||
application. When a message is received it might be helpful to the
|
|
||||||
user to raise the application so that it becomes visible. To
|
|
||||||
facilitate this, QtSingleApplication provides the
|
|
||||||
setActivationWindow() function and the activateWindow() slot.
|
|
||||||
|
|
||||||
If isRunning() returns true, another instance is already
|
|
||||||
running. It may be alerted to the fact that another instance has
|
|
||||||
started by using the sendMessage() function. Also data such as
|
|
||||||
startup parameters (e.g. the name of the file the user wanted this
|
|
||||||
new instance to open) can be passed to the running instance with
|
|
||||||
this function. Then, the application should terminate (or enter
|
|
||||||
client mode).
|
|
||||||
|
|
||||||
If isRunning() returns true, but sendMessage() fails, that is an
|
|
||||||
indication that the running instance is frozen.
|
|
||||||
|
|
||||||
Here's an example that shows how to convert an existing
|
|
||||||
application to use QtSingleApplication. It is very simple and does
|
|
||||||
not make use of all QtSingleApplication's functionality (see the
|
|
||||||
examples for that).
|
|
||||||
|
|
||||||
\code
|
|
||||||
// Original
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
QApplication app(argc, argv);
|
|
||||||
|
|
||||||
MyMainWidget mmw;
|
|
||||||
mmw.show();
|
|
||||||
return app.exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Single instance
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
QtSingleApplication app(argc, argv);
|
|
||||||
|
|
||||||
if (app.isRunning())
|
|
||||||
return !app.sendMessage(someDataString);
|
|
||||||
|
|
||||||
MyMainWidget mmw;
|
|
||||||
app.setActivationWindow(&mmw);
|
|
||||||
mmw.show();
|
|
||||||
return app.exec();
|
|
||||||
}
|
|
||||||
\endcode
|
|
||||||
|
|
||||||
Once this QtSingleApplication instance is destroyed (normally when
|
|
||||||
the process exits or crashes), when the user next attempts to run the
|
|
||||||
application this instance will not, of course, be encountered. The
|
|
||||||
next instance to call isRunning() or sendMessage() will assume the
|
|
||||||
role as the new running instance.
|
|
||||||
|
|
||||||
For console (non-GUI) applications, QtSingleCoreApplication may be
|
|
||||||
used instead of this class, to avoid the dependency on the QtGui
|
|
||||||
library.
|
|
||||||
|
|
||||||
\sa QtSingleCoreApplication
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
void QtSingleApplication::sysInit(const QString &appId)
|
|
||||||
{
|
|
||||||
actWin = 0;
|
|
||||||
peer = new QtLocalPeer(this, appId);
|
|
||||||
connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Creates a QtSingleApplication object. The application identifier
|
|
||||||
will be QCoreApplication::applicationFilePath(). \a argc, \a
|
|
||||||
argv, and \a GUIenabled are passed on to the QAppliation constructor.
|
|
||||||
|
|
||||||
If you are creating a console application (i.e. setting \a
|
|
||||||
GUIenabled to false), you may consider using
|
|
||||||
QtSingleCoreApplication instead.
|
|
||||||
*/
|
|
||||||
|
|
||||||
QtSingleApplication::QtSingleApplication(int &argc, char **argv, bool GUIenabled)
|
QtSingleApplication::QtSingleApplication(int &argc, char **argv, bool GUIenabled)
|
||||||
: QApplication(argc, argv, GUIenabled)
|
: QApplication(argc, argv, GUIenabled) {
|
||||||
{
|
sysInit();
|
||||||
sysInit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*!
|
|
||||||
Creates a QtSingleApplication object with the application
|
|
||||||
identifier \a appId. \a argc and \a argv are passed on to the
|
|
||||||
QAppliation constructor.
|
|
||||||
*/
|
|
||||||
|
|
||||||
QtSingleApplication::QtSingleApplication(const QString &appId, int &argc, char **argv)
|
QtSingleApplication::QtSingleApplication(const QString &appId, int &argc, char **argv)
|
||||||
: QApplication(argc, argv)
|
: QApplication(argc, argv) {
|
||||||
{
|
sysInit(appId);
|
||||||
sysInit(appId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if QT_VERSION < 0x050000
|
#if QT_VERSION < 0x050000
|
||||||
|
|
||||||
/*!
|
|
||||||
Creates a QtSingleApplication object. The application identifier
|
|
||||||
will be QCoreApplication::applicationFilePath(). \a argc, \a
|
|
||||||
argv, and \a type are passed on to the QAppliation constructor.
|
|
||||||
*/
|
|
||||||
QtSingleApplication::QtSingleApplication(int &argc, char **argv, Type type)
|
QtSingleApplication::QtSingleApplication(int &argc, char **argv, Type type)
|
||||||
: QApplication(argc, argv, type)
|
: QApplication(argc, argv, type) {
|
||||||
{
|
sysInit();
|
||||||
sysInit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(Q_WS_X11)
|
||||||
# if defined(Q_WS_X11)
|
|
||||||
/*!
|
|
||||||
Special constructor for X11, ref. the documentation of
|
|
||||||
QApplication's corresponding constructor. The application identifier
|
|
||||||
will be QCoreApplication::applicationFilePath(). \a dpy, \a visual,
|
|
||||||
and \a cmap are passed on to the QApplication constructor.
|
|
||||||
*/
|
|
||||||
QtSingleApplication::QtSingleApplication(Display* dpy, Qt::HANDLE visual, Qt::HANDLE cmap)
|
QtSingleApplication::QtSingleApplication(Display* dpy, Qt::HANDLE visual, Qt::HANDLE cmap)
|
||||||
: QApplication(dpy, visual, cmap)
|
: QApplication(dpy, visual, cmap) {
|
||||||
{
|
sysInit();
|
||||||
sysInit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
Special constructor for X11, ref. the documentation of
|
|
||||||
QApplication's corresponding constructor. The application identifier
|
|
||||||
will be QCoreApplication::applicationFilePath(). \a dpy, \a argc, \a
|
|
||||||
argv, \a visual, and \a cmap are passed on to the QApplication
|
|
||||||
constructor.
|
|
||||||
*/
|
|
||||||
QtSingleApplication::QtSingleApplication(Display *dpy, int &argc, char **argv, Qt::HANDLE visual, Qt::HANDLE cmap)
|
QtSingleApplication::QtSingleApplication(Display *dpy, int &argc, char **argv, Qt::HANDLE visual, Qt::HANDLE cmap)
|
||||||
: QApplication(dpy, argc, argv, visual, cmap)
|
: QApplication(dpy, argc, argv, visual, cmap) {
|
||||||
{
|
sysInit();
|
||||||
sysInit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
Special constructor for X11, ref. the documentation of
|
|
||||||
QApplication's corresponding constructor. The application identifier
|
|
||||||
will be \a appId. \a dpy, \a argc, \a
|
|
||||||
argv, \a visual, and \a cmap are passed on to the QApplication
|
|
||||||
constructor.
|
|
||||||
*/
|
|
||||||
QtSingleApplication::QtSingleApplication(Display* dpy, const QString &appId, int argc, char **argv, Qt::HANDLE visual, Qt::HANDLE cmap)
|
QtSingleApplication::QtSingleApplication(Display* dpy, const QString &appId, int argc, char **argv, Qt::HANDLE visual, Qt::HANDLE cmap)
|
||||||
: QApplication(dpy, argc, argv, visual, cmap)
|
: QApplication(dpy, argc, argv, visual, cmap) {
|
||||||
{
|
sysInit(appId);
|
||||||
sysInit(appId);
|
|
||||||
}
|
}
|
||||||
# endif // Q_WS_X11
|
# endif // Q_WS_X11
|
||||||
#endif // QT_VERSION < 0x050000
|
#endif // QT_VERSION < 0x050000
|
||||||
|
|
||||||
|
bool QtSingleApplication::isRunning() {
|
||||||
/*!
|
return peer->isClient();
|
||||||
Returns true if another instance of this application is running;
|
|
||||||
otherwise false.
|
|
||||||
|
|
||||||
This function does not find instances of this application that are
|
|
||||||
being run by a different user (on Windows: that are running in
|
|
||||||
another session).
|
|
||||||
|
|
||||||
\sa sendMessage()
|
|
||||||
*/
|
|
||||||
|
|
||||||
bool QtSingleApplication::isRunning()
|
|
||||||
{
|
|
||||||
return peer->isClient();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool QtSingleApplication::sendMessage(const QString &message, int timeout) {
|
||||||
/*!
|
return peer->sendMessage(message, timeout);
|
||||||
Tries to send the text \a message to the currently running
|
|
||||||
instance. The QtSingleApplication object in the running instance
|
|
||||||
will emit the messageReceived() signal when it receives the
|
|
||||||
message.
|
|
||||||
|
|
||||||
This function returns true if the message has been sent to, and
|
|
||||||
processed by, the current instance. If there is no instance
|
|
||||||
currently running, or if the running instance fails to process the
|
|
||||||
message within \a timeout milliseconds, this function return false.
|
|
||||||
|
|
||||||
\sa isRunning(), messageReceived()
|
|
||||||
*/
|
|
||||||
bool QtSingleApplication::sendMessage(const QString &message, int timeout)
|
|
||||||
{
|
|
||||||
return peer->sendMessage(message, timeout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString QtSingleApplication::id() const {
|
||||||
/*!
|
return peer->applicationId();
|
||||||
Returns the application identifier. Two processes with the same
|
|
||||||
identifier will be regarded as instances of the same application.
|
|
||||||
*/
|
|
||||||
QString QtSingleApplication::id() const
|
|
||||||
{
|
|
||||||
return peer->applicationId();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool QtSingleApplication::unlock() {
|
||||||
/*!
|
return peer->unlock();
|
||||||
Sets the activation window of this application to \a aw. The
|
|
||||||
activation window is the widget that will be activated by
|
|
||||||
activateWindow(). This is typically the application's main window.
|
|
||||||
|
|
||||||
If \a activateOnMessage is true (the default), the window will be
|
|
||||||
activated automatically every time a message is received, just prior
|
|
||||||
to the messageReceived() signal being emitted.
|
|
||||||
|
|
||||||
\sa activateWindow(), messageReceived()
|
|
||||||
*/
|
|
||||||
|
|
||||||
void QtSingleApplication::setActivationWindow(QWidget* aw, bool activateOnMessage)
|
|
||||||
{
|
|
||||||
actWin = aw;
|
|
||||||
if (activateOnMessage)
|
|
||||||
connect(peer, SIGNAL(messageReceived(const QString&)), this, SLOT(activateWindow()));
|
|
||||||
else
|
|
||||||
disconnect(peer, SIGNAL(messageReceived(const QString&)), this, SLOT(activateWindow()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QtSingleApplication::setActivationWindow(QWidget* aw, bool activateOnMessage) {
|
||||||
/*!
|
actWin = aw;
|
||||||
Returns the applications activation window if one has been set by
|
if (activateOnMessage)
|
||||||
calling setActivationWindow(), otherwise returns 0.
|
connect(peer, SIGNAL(messageReceived(const QString&)), this, SLOT(activateWindow()));
|
||||||
|
else
|
||||||
\sa setActivationWindow()
|
disconnect(peer, SIGNAL(messageReceived(const QString&)), this, SLOT(activateWindow()));
|
||||||
*/
|
|
||||||
QWidget* QtSingleApplication::activationWindow() const
|
|
||||||
{
|
|
||||||
return actWin;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QWidget* QtSingleApplication::activationWindow() const {
|
||||||
/*!
|
return actWin;
|
||||||
De-minimizes, raises, and activates this application's activation window.
|
|
||||||
This function does nothing if no activation window has been set.
|
|
||||||
|
|
||||||
This is a convenience function to show the user that this
|
|
||||||
application instance has been activated when he has tried to start
|
|
||||||
another instance.
|
|
||||||
|
|
||||||
This function should typically be called in response to the
|
|
||||||
messageReceived() signal. By default, that will happen
|
|
||||||
automatically, if an activation window has been set.
|
|
||||||
|
|
||||||
\sa setActivationWindow(), messageReceived(), initialize()
|
|
||||||
*/
|
|
||||||
void QtSingleApplication::activateWindow()
|
|
||||||
{
|
|
||||||
if (actWin) {
|
|
||||||
actWin->setWindowState(actWin->windowState() & ~Qt::WindowMinimized);
|
|
||||||
actWin->raise();
|
|
||||||
actWin->activateWindow();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QtSingleApplication::activateWindow() {
|
||||||
/*!
|
if (actWin) {
|
||||||
\fn void QtSingleApplication::messageReceived(const QString& message)
|
actWin->setWindowState(actWin->windowState() & ~Qt::WindowMinimized);
|
||||||
|
actWin->raise();
|
||||||
This signal is emitted when the current instance receives a \a
|
actWin->activateWindow();
|
||||||
message from another instance of this application.
|
}
|
||||||
|
}
|
||||||
\sa sendMessage(), setActivationWindow(), activateWindow()
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
/*!
|
|
||||||
\fn void QtSingleApplication::initialize(bool dummy = true)
|
|
||||||
|
|
||||||
\obsolete
|
|
||||||
*/
|
|
||||||
|
@ -61,11 +61,10 @@ class QtLocalPeer;
|
|||||||
# define QT_QTSINGLEAPPLICATION_EXPORT
|
# define QT_QTSINGLEAPPLICATION_EXPORT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class QT_QTSINGLEAPPLICATION_EXPORT QtSingleApplication : public QApplication
|
class QT_QTSINGLEAPPLICATION_EXPORT QtSingleApplication : public QApplication {
|
||||||
{
|
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit QtSingleApplication(int &argc, char **argv, bool GUIenabled = true);
|
explicit QtSingleApplication(int &argc, char **argv, bool GUIenabled = true);
|
||||||
explicit QtSingleApplication(const QString &id, int &argc, char **argv);
|
explicit QtSingleApplication(const QString &id, int &argc, char **argv);
|
||||||
#if QT_VERSION < 0x050000
|
#if QT_VERSION < 0x050000
|
||||||
@ -80,23 +79,32 @@ public:
|
|||||||
bool isRunning();
|
bool isRunning();
|
||||||
QString id() const;
|
QString id() const;
|
||||||
|
|
||||||
|
// Unlocks locked file, thus allows second instance to start
|
||||||
|
// when application is restarted programatically.
|
||||||
|
// Returns true if unlocked.
|
||||||
|
bool unlock();
|
||||||
|
|
||||||
void setActivationWindow(QWidget* aw, bool activateOnMessage = true);
|
void setActivationWindow(QWidget* aw, bool activateOnMessage = true);
|
||||||
QWidget* activationWindow() const;
|
QWidget* activationWindow() const;
|
||||||
|
|
||||||
// Obsolete:
|
// Obsolete:
|
||||||
void initialize(bool dummy = true)
|
void initialize(bool dummy = true) {
|
||||||
{ isRunning(); Q_UNUSED(dummy) }
|
isRunning();
|
||||||
|
Q_UNUSED(dummy)
|
||||||
|
}
|
||||||
|
|
||||||
public Q_SLOTS:
|
static QtSingleApplication *instance() {
|
||||||
|
return static_cast<QtSingleApplication*>(qApp);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Q_SLOTS:
|
||||||
bool sendMessage(const QString &message, int timeout = 5000);
|
bool sendMessage(const QString &message, int timeout = 5000);
|
||||||
void activateWindow();
|
void activateWindow();
|
||||||
|
|
||||||
|
Q_SIGNALS:
|
||||||
Q_SIGNALS:
|
|
||||||
void messageReceived(const QString &message);
|
void messageReceived(const QString &message);
|
||||||
|
|
||||||
|
private:
|
||||||
private:
|
|
||||||
void sysInit(const QString &appId = QString());
|
void sysInit(const QString &appId = QString());
|
||||||
QtLocalPeer *peer;
|
QtLocalPeer *peer;
|
||||||
QWidget *actWin;
|
QWidget *actWin;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user