Add a new option to main.cpp to specify a specific pid.

Add a new option to main.cpp which allows the user to specify a specific pid to
use to open a new file and forwards all the arguments to that pid.  Required some
changes to QtSingleApplication, but I did not alter any existing API.  Just added
some new methods for querying an instance of a specific pid.

Together with some changes to the terminal plugin this will allow the user to open
any file with Qt Creator from within the terminal plugin by simply invoking the
qtcreator binary.  All that is necessary is the addition of an alias like so:

if [ -n "$QTCREATOR_PID" ] ; then alias qtcreator="/path/to/qtcreator/binary/qtcreator -pid $QTCREATOR_PID" ; fi

This is entirely analagous to what kate allows where you can open any file with
kate from within the embedded terminal.

Change-Id: I476d78d673ee60052a02eb974eefc5368d24193b
Reviewed-by: Eike Ziller <eike.ziller@nokia.com>
Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
Adam Treat
2012-01-22 11:02:41 -05:00
committed by hjk
parent 6ade2e327f
commit b5940e0fd6
3 changed files with 76 additions and 31 deletions

View File

@@ -70,8 +70,13 @@ static const char fixedOptionsC[] =
"Options:\n" "Options:\n"
" -help Display this help\n" " -help Display this help\n"
" -version Display program version\n" " -version Display program version\n"
" -client Attempt to connect to already running instance\n" " -client Attempt to connect to already running first instance\n"
" -settingspath <path> Override the default path where user settings are stored\n"; " -settingspath <path> Override the default path where user settings are stored\n"
#if !defined(Q_OS_WIN)
" -pid <pid> Attempt to connect to instance given by pid\n";
#else
;
#endif
static const char HELP_OPTION1[] = "-h"; static const char HELP_OPTION1[] = "-h";
static const char HELP_OPTION2[] = "-help"; static const char HELP_OPTION2[] = "-help";
@@ -80,6 +85,9 @@ static const char HELP_OPTION4[] = "--help";
static const char VERSION_OPTION[] = "-version"; static const char VERSION_OPTION[] = "-version";
static const char CLIENT_OPTION[] = "-client"; static const char CLIENT_OPTION[] = "-client";
static const char SETTINGS_OPTION[] = "-settingspath"; static const char SETTINGS_OPTION[] = "-settingspath";
#if !defined(Q_OS_WIN)
static const char PID_OPTION[] = "-pid";
#endif
typedef QList<ExtensionSystem::PluginSpec *> PluginSpecSet; typedef QList<ExtensionSystem::PluginSpec *> PluginSpecSet;
@@ -310,6 +318,9 @@ int main(int argc, char **argv)
appOptions.insert(QLatin1String(HELP_OPTION4), false); appOptions.insert(QLatin1String(HELP_OPTION4), false);
appOptions.insert(QLatin1String(VERSION_OPTION), false); appOptions.insert(QLatin1String(VERSION_OPTION), false);
appOptions.insert(QLatin1String(CLIENT_OPTION), false); appOptions.insert(QLatin1String(CLIENT_OPTION), false);
#if !defined(Q_OS_WIN)
appOptions.insert(QLatin1String(PID_OPTION), true);
#endif
QString errorMessage; QString errorMessage;
if (!pluginManager.parseOptions(arguments, appOptions, &foundAppOptions, &errorMessage)) { if (!pluginManager.parseOptions(arguments, appOptions, &foundAppOptions, &errorMessage)) {
displayError(errorMessage); displayError(errorMessage);
@@ -348,19 +359,29 @@ int main(int argc, char **argv)
return 0; return 0;
} }
const bool isFirstInstance = !app.isRunning(); qint64 pid = -1;
if (!isFirstInstance && foundAppOptions.contains(QLatin1String(CLIENT_OPTION))) { #if !defined(Q_OS_WIN)
if (app.sendMessage(pluginManager.serializedArguments())) if (foundAppOptions.contains(QLatin1String(PID_OPTION))) {
QString pidString = foundAppOptions.value(QLatin1String(PID_OPTION));
bool pidOk;
qint64 tmpPid = pidString.toInt(&pidOk);
if (pidOk)
pid = tmpPid;
}
#endif
if (app.isRunning() && (pid != -1 || foundAppOptions.contains(QLatin1String(CLIENT_OPTION)))) {
if (app.sendMessage(pluginManager.serializedArguments(), 5000 /*timeout*/, pid))
return 0; return 0;
// Message could not be send, maybe it was in the process of quitting // Message could not be send, maybe it was in the process of quitting
if (app.isRunning()) { if (app.isRunning(pid)) {
// Nah app is still running, ask the user // Nah app is still running, ask the user
int button = askMsgSendFailed(); int button = askMsgSendFailed();
while(button == QMessageBox::Retry) { while(button == QMessageBox::Retry) {
if (app.sendMessage(pluginManager.serializedArguments())) if (app.sendMessage(pluginManager.serializedArguments(), 5000 /*timeout*/, pid))
return 0; return 0;
if (!app.isRunning()) // App quit while we were trying so start a new creator if (!app.isRunning(pid)) // App quit while we were trying so start a new creator
button = QMessageBox::Yes; button = QMessageBox::Yes;
else else
button = askMsgSendFailed(); button = askMsgSendFailed();
@@ -380,13 +401,10 @@ int main(int argc, char **argv)
errorOverview.exec(); errorOverview.exec();
} }
if (isFirstInstance) { // Set up lock and remote arguments.
// Set up lock and remote arguments for the first instance only. app.initialize();
// Silently fallback to unconnected instances for any subsequent instances. QObject::connect(&app, SIGNAL(messageReceived(QString)),
app.initialize(); &pluginManager, SLOT(remoteArguments(QString)));
QObject::connect(&app, SIGNAL(messageReceived(QString)),
&pluginManager, SLOT(remoteArguments(QString)));
}
QObject::connect(&app, SIGNAL(fileOpenRequest(QString)), coreplugin->plugin(), QObject::connect(&app, SIGNAL(fileOpenRequest(QString)), coreplugin->plugin(),
SLOT(fileOpenRequest(QString))); SLOT(fileOpenRequest(QString)));

View File

@@ -41,8 +41,10 @@ namespace SharedTools {
void QtSingleApplication::sysInit(const QString &appId) void QtSingleApplication::sysInit(const QString &appId)
{ {
actWin = 0; actWin = 0;
peer = new QtLocalPeer(this, appId); firstPeer = new QtLocalPeer(this, appId);
connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&))); connect(firstPeer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&)));
pidPeer = new QtLocalPeer(this, appId + QLatin1Char('-') + QString::number(::getpid(), 10));
connect(pidPeer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&)));
} }
@@ -56,6 +58,7 @@ QtSingleApplication::QtSingleApplication(int &argc, char **argv, bool GUIenabled
QtSingleApplication::QtSingleApplication(const QString &appId, int &argc, char **argv) QtSingleApplication::QtSingleApplication(const QString &appId, int &argc, char **argv)
: QApplication(argc, argv) : QApplication(argc, argv)
{ {
this->appId = appId;
sysInit(appId); sysInit(appId);
} }
@@ -84,6 +87,7 @@ QtSingleApplication::QtSingleApplication(Display* dpy, const QString &appId,
int argc, char **argv, Qt::HANDLE visual, Qt::HANDLE colormap) int argc, char **argv, Qt::HANDLE visual, Qt::HANDLE colormap)
: QApplication(dpy, argc, argv, visual, colormap) : QApplication(dpy, argc, argv, visual, colormap)
{ {
this->appId = appId;
sysInit(appId); sysInit(appId);
} }
#endif #endif
@@ -98,30 +102,50 @@ bool QtSingleApplication::event(QEvent *event)
return QApplication::event(event); return QApplication::event(event);
} }
bool QtSingleApplication::isRunning() bool QtSingleApplication::isRunning(qint64 pid)
{ {
return peer->isClient(); if (pid == -1)
return firstPeer->isClient();
QtLocalPeer peer(this, appId + QLatin1Char('-') + QString::number(pid, 10));
return peer.isClient();
} }
bool QtSingleApplication::sendMessage(const QString &message, int timeout) void QtSingleApplication::initialize(bool)
{ {
return peer->sendMessage(message, timeout); firstPeer->isClient();
pidPeer->isClient();
} }
bool QtSingleApplication::sendMessage(const QString &message, int timeout, qint64 pid)
{
if (pid == -1)
return firstPeer->sendMessage(message, timeout);
QtLocalPeer peer(this, appId + QLatin1Char('-') + QString::number(pid, 10));
return peer.sendMessage(message, timeout);
}
QString QtSingleApplication::id() const QString QtSingleApplication::id() const
{ {
return peer->applicationId(); return firstPeer->applicationId();
} }
QString QtSingleApplication::applicationId() const
{
return appId;
}
void QtSingleApplication::setActivationWindow(QWidget *aw, bool activateOnMessage) void QtSingleApplication::setActivationWindow(QWidget *aw, bool activateOnMessage)
{ {
actWin = aw; actWin = aw;
if (activateOnMessage) if (activateOnMessage) {
connect(peer, SIGNAL(messageReceived(QString)), this, SLOT(activateWindow())); connect(firstPeer, SIGNAL(messageReceived(QString)), this, SLOT(activateWindow()));
else connect(pidPeer, SIGNAL(messageReceived(QString)), this, SLOT(activateWindow()));
disconnect(peer, SIGNAL(messageReceived(QString)), this, SLOT(activateWindow())); } else {
disconnect(firstPeer, SIGNAL(messageReceived(QString)), this, SLOT(activateWindow()));
disconnect(pidPeer, SIGNAL(messageReceived(QString)), this, SLOT(activateWindow()));
}
} }

View File

@@ -49,22 +49,23 @@ public:
QtSingleApplication(Display *dpy, int &argc, char **argv, Qt::HANDLE visual = 0, Qt::HANDLE cmap = 0); QtSingleApplication(Display *dpy, int &argc, char **argv, Qt::HANDLE visual = 0, Qt::HANDLE cmap = 0);
#endif #endif
bool isRunning(); bool isRunning(qint64 pid = -1);
QString id() const; QString id() const;
void setActivationWindow(QWidget* aw, bool activateOnMessage = true); void setActivationWindow(QWidget* aw, bool activateOnMessage = true);
QWidget* activationWindow() const; QWidget* activationWindow() const;
bool event(QEvent *event); bool event(QEvent *event);
QString applicationId() const;
public Q_SLOTS: public Q_SLOTS:
bool sendMessage(const QString &message, int timeout = 5000); bool sendMessage(const QString &message, int timeout = 5000, qint64 pid = -1);
void activateWindow(); void activateWindow();
//Obsolete methods: //Obsolete methods:
public: public:
void initialize(bool = true) void initialize(bool = true);
{ isRunning(); }
#if defined(Q_WS_X11) #if defined(Q_WS_X11)
QtSingleApplication(Display* dpy, const QString &id, int argc, char **argv, Qt::HANDLE visual = 0, Qt::HANDLE colormap = 0); QtSingleApplication(Display* dpy, const QString &id, int argc, char **argv, Qt::HANDLE visual = 0, Qt::HANDLE colormap = 0);
@@ -77,8 +78,10 @@ Q_SIGNALS:
private: private:
void sysInit(const QString &appId = QString()); void sysInit(const QString &appId = QString());
QtLocalPeer *peer; QtLocalPeer *firstPeer;
QtLocalPeer *pidPeer;
QWidget *actWin; QWidget *actWin;
QString appId;
}; };
} // namespace SharedTools } // namespace SharedTools