2010-01-04 11:36:55 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2010-01-04 11:36:55 +01:00
|
|
|
**
|
2012-07-19 12:26:56 +02:00
|
|
|
** Contact: http://www.qt-project.org/
|
2010-01-04 11:36:55 +01:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** 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.
|
2010-01-04 11:36:55 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-01-04 11:36:55 +01:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
2009-05-19 14:54:52 +02:00
|
|
|
#include "externaleditors.h"
|
|
|
|
|
#include "qt4project.h"
|
|
|
|
|
#include "qt4projectmanagerconstants.h"
|
|
|
|
|
|
|
|
|
|
#include <utils/synchronousprocess.h>
|
|
|
|
|
#include <projectexplorer/projectexplorer.h>
|
2012-04-24 15:49:09 +02:00
|
|
|
#include <projectexplorer/target.h>
|
2009-05-19 14:54:52 +02:00
|
|
|
#include <projectexplorer/session.h>
|
2012-09-03 18:31:44 +02:00
|
|
|
#include <qtsupport/qtkitinformation.h>
|
2011-05-20 21:40:53 +02:00
|
|
|
#include <qtsupport/qtversionmanager.h>
|
2009-05-19 14:54:52 +02:00
|
|
|
#include <designer/designerconstants.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QProcess>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QSignalMapper>
|
2009-05-19 14:54:52 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTcpSocket>
|
|
|
|
|
#include <QTcpServer>
|
2009-05-19 14:54:52 +02:00
|
|
|
|
|
|
|
|
enum { debug = 0 };
|
|
|
|
|
|
|
|
|
|
namespace Qt4ProjectManager {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
// Figure out the Qt4 project used by the file if any
|
|
|
|
|
static Qt4Project *qt4ProjectFor(const QString &fileName)
|
|
|
|
|
{
|
|
|
|
|
ProjectExplorer::ProjectExplorerPlugin *pe = ProjectExplorer::ProjectExplorerPlugin::instance();
|
|
|
|
|
if (ProjectExplorer::Project *baseProject = pe->session()->projectForFile(fileName))
|
|
|
|
|
if (Qt4Project *project = qobject_cast<Qt4Project*>(baseProject))
|
|
|
|
|
return project;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------ Messages
|
|
|
|
|
static inline QString msgStartFailed(const QString &binary, QStringList arguments)
|
|
|
|
|
{
|
|
|
|
|
arguments.push_front(binary);
|
|
|
|
|
return ExternalQtEditor::tr("Unable to start \"%1\"").arg(arguments.join(QString(QLatin1Char(' '))));
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-07 18:17:24 +01:00
|
|
|
static inline QString msgAppNotFound(const QString &id)
|
2009-05-19 14:54:52 +02:00
|
|
|
{
|
2010-01-07 18:17:24 +01:00
|
|
|
return ExternalQtEditor::tr("The application \"%1\" could not be found.").arg(id);
|
2009-05-19 14:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -- Commands and helpers
|
|
|
|
|
#ifdef Q_OS_MAC
|
2011-06-20 12:32:22 +02:00
|
|
|
static const char linguistBinaryC[] = "Linguist";
|
|
|
|
|
static const char designerBinaryC[] = "Designer";
|
2009-05-19 14:54:52 +02:00
|
|
|
|
2010-12-07 16:48:57 +01:00
|
|
|
// Mac: Change the call 'Foo.app/Contents/MacOS/Foo <filelist>' to
|
|
|
|
|
// 'open -a Foo.app <filelist>'. doesn't support generic command line arguments
|
2009-05-19 14:54:52 +02:00
|
|
|
static void createMacOpenCommand(QString *binary, QStringList *arguments)
|
|
|
|
|
{
|
|
|
|
|
const int appFolderIndex = binary->lastIndexOf(QLatin1String("/Contents/MacOS/"));
|
|
|
|
|
if (appFolderIndex != -1) {
|
|
|
|
|
binary->truncate(appFolderIndex);
|
|
|
|
|
arguments->push_front(*binary);
|
2010-12-07 16:48:57 +01:00
|
|
|
arguments->push_front(QLatin1String("-a"));
|
2009-05-19 14:54:52 +02:00
|
|
|
*binary = QLatin1String("open");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#else
|
2011-06-20 12:32:22 +02:00
|
|
|
static const char linguistBinaryC[] = "linguist";
|
|
|
|
|
static const char designerBinaryC[] = "designer";
|
2009-05-19 14:54:52 +02:00
|
|
|
#endif
|
|
|
|
|
|
2011-06-20 12:32:22 +02:00
|
|
|
static const char designerIdC[] = "Qt.Designer";
|
|
|
|
|
static const char linguistIdC[] = "Qt.Linguist";
|
2010-01-07 18:17:24 +01:00
|
|
|
|
2011-06-20 12:32:22 +02:00
|
|
|
static const char designerDisplayName[] = QT_TRANSLATE_NOOP("OpenWith::Editors", "Qt Designer");
|
|
|
|
|
static const char linguistDisplayName[] = QT_TRANSLATE_NOOP("OpenWith::Editors", "Qt Linguist");
|
2009-05-19 14:54:52 +02:00
|
|
|
|
|
|
|
|
// -------------- ExternalQtEditor
|
2010-01-07 18:17:24 +01:00
|
|
|
ExternalQtEditor::ExternalQtEditor(const QString &id,
|
|
|
|
|
const QString &displayName,
|
|
|
|
|
const QString &mimetype,
|
|
|
|
|
QObject *parent) :
|
2009-05-19 14:54:52 +02:00
|
|
|
Core::IExternalEditor(parent),
|
|
|
|
|
m_mimeTypes(mimetype),
|
2010-01-07 18:17:24 +01:00
|
|
|
m_id(id),
|
|
|
|
|
m_displayName(displayName)
|
2009-05-19 14:54:52 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList ExternalQtEditor::mimeTypes() const
|
|
|
|
|
{
|
|
|
|
|
return m_mimeTypes;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-07 09:26:29 +02:00
|
|
|
Core::Id ExternalQtEditor::id() const
|
2010-01-07 18:17:24 +01:00
|
|
|
{
|
|
|
|
|
return m_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ExternalQtEditor::displayName() const
|
2009-05-19 14:54:52 +02:00
|
|
|
{
|
2010-01-07 18:17:24 +01:00
|
|
|
return m_displayName;
|
2009-05-19 14:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ExternalQtEditor::getEditorLaunchData(const QString &fileName,
|
|
|
|
|
QtVersionCommandAccessor commandAccessor,
|
|
|
|
|
const QString &fallbackBinary,
|
|
|
|
|
const QStringList &additionalArguments,
|
|
|
|
|
bool useMacOpenCommand,
|
|
|
|
|
EditorLaunchData *data,
|
|
|
|
|
QString *errorMessage) const
|
|
|
|
|
{
|
|
|
|
|
// Get the binary either from the current Qt version of the project or Path
|
2011-06-20 12:32:22 +02:00
|
|
|
if (const Qt4Project *project = qt4ProjectFor(fileName)) {
|
2012-04-24 15:49:09 +02:00
|
|
|
if (const ProjectExplorer::Target *target = project->activeTarget()) {
|
2012-09-03 18:31:44 +02:00
|
|
|
if (const QtSupport::BaseQtVersion *qtVersion = QtSupport::QtKitInformation::qtVersion(target->kit())) {
|
2012-04-24 15:49:09 +02:00
|
|
|
data->binary = (qtVersion->*commandAccessor)();
|
|
|
|
|
data->workingDirectory = project->projectDirectory();
|
2011-06-20 12:32:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (data->binary.isEmpty()) {
|
2009-05-19 14:54:52 +02:00
|
|
|
data->workingDirectory.clear();
|
2009-10-05 11:06:05 +02:00
|
|
|
data->binary = Utils::SynchronousProcess::locateBinary(fallbackBinary);
|
2009-05-19 14:54:52 +02:00
|
|
|
}
|
|
|
|
|
if (data->binary.isEmpty()) {
|
2011-09-07 09:26:29 +02:00
|
|
|
*errorMessage = msgAppNotFound(id().toString());
|
2009-05-19 14:54:52 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Setup binary + arguments, use Mac Open if appropriate
|
|
|
|
|
data->arguments = additionalArguments;
|
|
|
|
|
data->arguments.push_back(fileName);
|
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
|
if (useMacOpenCommand)
|
2009-05-19 15:17:15 +02:00
|
|
|
createMacOpenCommand(&(data->binary), &(data->arguments));
|
2009-05-19 14:54:52 +02:00
|
|
|
#else
|
|
|
|
|
Q_UNUSED(useMacOpenCommand)
|
|
|
|
|
#endif
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << Q_FUNC_INFO << '\n' << data->binary << data->arguments;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ExternalQtEditor::startEditorProcess(const EditorLaunchData &data, QString *errorMessage)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << Q_FUNC_INFO << '\n' << data.binary << data.arguments << data.workingDirectory;
|
|
|
|
|
qint64 pid = 0;
|
|
|
|
|
if (!QProcess::startDetached(data.binary, data.arguments, data.workingDirectory, &pid)) {
|
|
|
|
|
*errorMessage = msgStartFailed(data.binary, data.arguments);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --------------- LinguistExternalEditor
|
|
|
|
|
LinguistExternalEditor::LinguistExternalEditor(QObject *parent) :
|
2010-01-07 18:17:24 +01:00
|
|
|
ExternalQtEditor(QLatin1String(linguistIdC),
|
|
|
|
|
QLatin1String(linguistDisplayName),
|
2009-05-19 14:54:52 +02:00
|
|
|
QLatin1String(Qt4ProjectManager::Constants::LINGUIST_MIMETYPE),
|
|
|
|
|
parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool LinguistExternalEditor::startEditor(const QString &fileName, QString *errorMessage)
|
|
|
|
|
{
|
|
|
|
|
EditorLaunchData data;
|
2011-05-20 21:40:53 +02:00
|
|
|
return getEditorLaunchData(fileName, &QtSupport::BaseQtVersion::linguistCommand,
|
2009-05-19 14:54:52 +02:00
|
|
|
QLatin1String(linguistBinaryC),
|
|
|
|
|
QStringList(), true, &data, errorMessage)
|
|
|
|
|
&& startEditorProcess(data, errorMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --------------- MacDesignerExternalEditor, using Mac 'open'
|
|
|
|
|
MacDesignerExternalEditor::MacDesignerExternalEditor(QObject *parent) :
|
2010-01-07 18:17:24 +01:00
|
|
|
ExternalQtEditor(QLatin1String(designerIdC),
|
|
|
|
|
QLatin1String(designerDisplayName),
|
2009-05-19 14:54:52 +02:00
|
|
|
QLatin1String(Qt4ProjectManager::Constants::FORM_MIMETYPE),
|
|
|
|
|
parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MacDesignerExternalEditor::startEditor(const QString &fileName, QString *errorMessage)
|
|
|
|
|
{
|
|
|
|
|
EditorLaunchData data;
|
2011-05-20 21:40:53 +02:00
|
|
|
return getEditorLaunchData(fileName, &QtSupport::BaseQtVersion::designerCommand,
|
2009-05-19 14:54:52 +02:00
|
|
|
QLatin1String(designerBinaryC),
|
|
|
|
|
QStringList(), true, &data, errorMessage)
|
|
|
|
|
&& startEditorProcess(data, errorMessage);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --------------- DesignerExternalEditor with Designer Tcp remote control.
|
|
|
|
|
DesignerExternalEditor::DesignerExternalEditor(QObject *parent) :
|
2010-01-07 18:17:24 +01:00
|
|
|
ExternalQtEditor(QLatin1String(designerIdC),
|
|
|
|
|
QLatin1String(designerDisplayName),
|
2009-05-19 14:54:52 +02:00
|
|
|
QLatin1String(Designer::Constants::FORM_MIMETYPE),
|
|
|
|
|
parent),
|
|
|
|
|
m_terminationMapper(0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DesignerExternalEditor::processTerminated(const QString &binary)
|
|
|
|
|
{
|
|
|
|
|
const ProcessCache::iterator it = m_processCache.find(binary);
|
|
|
|
|
if (it == m_processCache.end())
|
|
|
|
|
return;
|
|
|
|
|
// Make sure socket is closed and cleaned, remove from cache
|
|
|
|
|
QTcpSocket *socket = it.value();
|
|
|
|
|
m_processCache.erase(it); // Note that closing will cause the slot to be retriggered
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << Q_FUNC_INFO << '\n' << binary << socket->state();
|
|
|
|
|
if (socket->state() == QAbstractSocket::ConnectedState)
|
|
|
|
|
socket->close();
|
|
|
|
|
socket->deleteLater();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesignerExternalEditor::startEditor(const QString &fileName, QString *errorMessage)
|
|
|
|
|
{
|
|
|
|
|
EditorLaunchData data;
|
|
|
|
|
// Find the editor binary
|
2011-05-20 21:40:53 +02:00
|
|
|
if (!getEditorLaunchData(fileName, &QtSupport::BaseQtVersion::designerCommand,
|
2009-05-19 14:54:52 +02:00
|
|
|
QLatin1String(designerBinaryC),
|
|
|
|
|
QStringList(), false, &data, errorMessage)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Known one?
|
|
|
|
|
const ProcessCache::iterator it = m_processCache.find(data.binary);
|
|
|
|
|
if (it != m_processCache.end()) {
|
|
|
|
|
// Process is known, write to its socket to cause it to open the file
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << Q_FUNC_INFO << "\nWriting to socket:" << data.binary << fileName;
|
|
|
|
|
QTcpSocket *socket = it.value();
|
|
|
|
|
if (!socket->write(fileName.toUtf8() + '\n')) {
|
|
|
|
|
*errorMessage = tr("Qt Designer is not responding (%1).").arg(socket->errorString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// No process yet. Create socket & launch the process
|
|
|
|
|
QTcpServer server;
|
|
|
|
|
if (!server.listen(QHostAddress::LocalHost)) {
|
|
|
|
|
*errorMessage = tr("Unable to create server socket: %1").arg(server.errorString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const quint16 port = server.serverPort();
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << Q_FUNC_INFO << "\nLaunching server:" << port << data.binary << fileName;
|
|
|
|
|
// Start first one with file and socket as '-client port file'
|
|
|
|
|
// Wait for the socket listening
|
|
|
|
|
data.arguments.push_front(QString::number(port));
|
|
|
|
|
data.arguments.push_front(QLatin1String("-client"));
|
|
|
|
|
|
|
|
|
|
if (!startEditorProcess(data, errorMessage))
|
|
|
|
|
return false;
|
|
|
|
|
// Set up signal mapper to track process termination via socket
|
|
|
|
|
if (!m_terminationMapper) {
|
|
|
|
|
m_terminationMapper = new QSignalMapper(this);
|
|
|
|
|
connect(m_terminationMapper, SIGNAL(mapped(QString)), this, SLOT(processTerminated(QString)));
|
|
|
|
|
}
|
|
|
|
|
// Insert into cache if socket is created, else try again next time
|
|
|
|
|
if (server.waitForNewConnection(3000)) {
|
|
|
|
|
QTcpSocket *socket = server.nextPendingConnection();
|
|
|
|
|
socket->setParent(this);
|
|
|
|
|
m_processCache.insert(data.binary, socket);
|
|
|
|
|
m_terminationMapper->setMapping(socket, data.binary);
|
|
|
|
|
connect(socket, SIGNAL(disconnected()), m_terminationMapper, SLOT(map()));
|
|
|
|
|
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), m_terminationMapper, SLOT(map()));
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Qt4ProjectManager
|