qtsingleapplication.cpp 6.01 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
con's avatar
con committed
29
30
31

#include "qtsingleapplication.h"
#include "qtlocalpeer.h"
hjk's avatar
hjk committed
32

33
34
35
#include <qtlockedfile.h>

#include <QDir>
36
#include <QFileOpenEvent>
37
38
#include <QSharedMemory>
#include <QWidget>
con's avatar
con committed
39
40
41

namespace SharedTools {

42
43
44
45
46
47
48
49
50
51
52
53
54
static const int instancesSize = 1024;

static QString instancesLockFilename(const QString &appSessionId)
{
    const QChar slash(QLatin1Char('/'));
    QString res = QDir::tempPath();
    if (!res.endsWith(slash))
        res += slash;
    return res + appSessionId + QLatin1String("-instances");
}

QtSingleApplication::QtSingleApplication(const QString &appId, int &argc, char **argv)
    : QApplication(argc, argv),
55
56
      firstPeer(-1),
      pidPeer(0)
con's avatar
con committed
57
{
58
59
60
61
62
63
    this->appId = appId;

    const QString appSessionId = QtLocalPeer::appSessionId(appId);

    // This shared memory holds a zero-terminated array of active (or crashed) instances
    instances = new QSharedMemory(appSessionId, this);
con's avatar
con committed
64
    actWin = 0;
65
    block = false;
con's avatar
con committed
66

67
68
69
70
71
72
73
74
75
76
77
    // First instance creates the shared memory, later instances attach to it
    const bool created = instances->create(instancesSize);
    if (!created) {
        if (!instances->attach()) {
            qWarning() << "Failed to initialize instances shared memory: "
                       << instances->errorString();
            delete instances;
            instances = 0;
            return;
        }
    }
78

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    // QtLockedFile is used to workaround QTBUG-10364
    QtLockedFile lockfile(instancesLockFilename(appSessionId));

    lockfile.open(QtLockedFile::ReadWrite);
    lockfile.lock(QtLockedFile::WriteLock);
    qint64 *pids = static_cast<qint64 *>(instances->data());
    if (!created) {
        // Find the first instance that it still running
        // The whole list needs to be iterated in order to append to it
        for (; *pids; ++pids) {
            if (firstPeer == -1 && isRunning(*pids))
                firstPeer = *pids;
        }
    }
    // Add current pid to list and terminate it
94
    *pids++ = QCoreApplication::applicationPid();
95
96
    *pids = 0;
    lockfile.unlock();
con's avatar
con committed
97
98
}

99
QtSingleApplication::~QtSingleApplication()
con's avatar
con committed
100
{
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    if (!instances)
        return;
    const qint64 appPid = QCoreApplication::applicationPid();
    QtLockedFile lockfile(instancesLockFilename(QtLocalPeer::appSessionId(appId)));
    lockfile.open(QtLockedFile::ReadWrite);
    lockfile.lock(QtLockedFile::WriteLock);
    // Rewrite array, removing current pid and previously crashed ones
    qint64 *pids = static_cast<qint64 *>(instances->data());
    qint64 *newpids = pids;
    for (; *pids; ++pids) {
        if (*pids != appPid && isRunning(*pids))
            *newpids++ = *pids;
    }
    *newpids = 0;
    lockfile.unlock();
con's avatar
con committed
116
117
118
119
120
121
122
123
124
125
126
127
}

bool QtSingleApplication::event(QEvent *event)
{
    if (event->type() == QEvent::FileOpen) {
        QFileOpenEvent *foe = static_cast<QFileOpenEvent*>(event);
        emit fileOpenRequest(foe->file());
        return true;
    }
    return QApplication::event(event);
}

128
bool QtSingleApplication::isRunning(qint64 pid)
con's avatar
con committed
129
{
130
131
132
133
134
    if (pid == -1) {
        pid = firstPeer;
        if (pid == -1)
            return false;
    }
135
136
137

    QtLocalPeer peer(this, appId + QLatin1Char('-') + QString::number(pid, 10));
    return peer.isClient();
con's avatar
con committed
138
139
}

140
void QtSingleApplication::initialize(bool)
con's avatar
con committed
141
{
142
143
144
    pidPeer = new QtLocalPeer(this, appId + QLatin1Char('-') +
                              QString::number(QCoreApplication::applicationPid()));
    connect(pidPeer, SIGNAL(messageReceived(QString,QObject*)), SIGNAL(messageReceived(QString,QObject*)));
145
    pidPeer->isClient();
con's avatar
con committed
146
147
}

148
149
bool QtSingleApplication::sendMessage(const QString &message, int timeout, qint64 pid)
{
150
151
152
153
154
    if (pid == -1) {
        pid = firstPeer;
        if (pid == -1)
            return false;
    }
155
156

    QtLocalPeer peer(this, appId + QLatin1Char('-') + QString::number(pid, 10));
157
    return peer.sendMessage(message, timeout, block);
158
}
con's avatar
con committed
159

160
161
162
163
QString QtSingleApplication::applicationId() const
{
    return appId;
}
con's avatar
con committed
164

165
166
167
168
169
void QtSingleApplication::setBlock(bool value)
{
    block = value;
}

hjk's avatar
hjk committed
170
void QtSingleApplication::setActivationWindow(QWidget *aw, bool activateOnMessage)
con's avatar
con committed
171
172
{
    actWin = aw;
173
174
    if (!pidPeer)
        return;
175
    if (activateOnMessage)
176
        connect(pidPeer, SIGNAL(messageReceived(QString,QObject*)), this, SLOT(activateWindow()));
177
    else
178
        disconnect(pidPeer, SIGNAL(messageReceived(QString,QObject*)), this, SLOT(activateWindow()));
con's avatar
con committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
}


QWidget* QtSingleApplication::activationWindow() const
{
    return actWin;
}


void QtSingleApplication::activateWindow()
{
    if (actWin) {
        actWin->setWindowState(actWin->windowState() & ~Qt::WindowMinimized);
        actWin->raise();
        actWin->activateWindow();
    }
}

hjk's avatar
hjk committed
197
} // namespace SharedTools