2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-01-04 11:36:55 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-01-04 11:36:55 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-01-04 11:36:55 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2010-01-04 11:36:55 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-01-04 11:36:55 +01:00
|
|
|
|
2009-05-19 14:54:52 +02:00
|
|
|
#include "externaleditors.h"
|
|
|
|
|
|
2016-09-28 17:14:53 +02:00
|
|
|
#include <utils/algorithm.h>
|
2013-03-18 17:53:18 +01:00
|
|
|
#include <utils/hostosinfo.h>
|
2009-05-19 14:54:52 +02:00
|
|
|
#include <utils/synchronousprocess.h>
|
2014-02-07 22:56:48 +01:00
|
|
|
#include <projectexplorer/project.h>
|
2009-05-19 14:54:52 +02:00
|
|
|
#include <projectexplorer/projectexplorer.h>
|
2014-02-07 22:39:44 +01:00
|
|
|
#include <projectexplorer/projectexplorerconstants.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>
|
2009-05-19 14:54:52 +02:00
|
|
|
#include <designer/designerconstants.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QProcess>
|
|
|
|
|
#include <QDebug>
|
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
|
|
|
|
2015-02-01 18:44:47 +02:00
|
|
|
using namespace ProjectExplorer;
|
|
|
|
|
|
2009-05-19 14:54:52 +02:00
|
|
|
enum { debug = 0 };
|
|
|
|
|
|
2013-10-16 11:02:37 +02:00
|
|
|
namespace QmakeProjectManager {
|
2009-05-19 14:54:52 +02:00
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
// ------------ Messages
|
|
|
|
|
static inline QString msgStartFailed(const QString &binary, QStringList arguments)
|
|
|
|
|
{
|
|
|
|
|
arguments.push_front(binary);
|
2014-08-23 01:19:53 +02:00
|
|
|
return ExternalQtEditor::tr("Unable to start \"%1\"").arg(arguments.join(QLatin1Char(' ')));
|
2009-05-19 14:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
2016-09-28 15:43:13 +02:00
|
|
|
static QString linguistBinary(const QtSupport::BaseQtVersion *qtVersion)
|
2013-03-18 17:53:18 +01:00
|
|
|
{
|
2016-09-28 15:43:13 +02:00
|
|
|
if (qtVersion)
|
|
|
|
|
return qtVersion->linguistCommand();
|
2016-11-29 16:29:29 +00:00
|
|
|
return QLatin1String(Utils::HostOsInfo::isMacHost() ? "Linguist" : "linguist");
|
2013-03-18 17:53:18 +01:00
|
|
|
}
|
|
|
|
|
|
2016-09-28 15:43:13 +02:00
|
|
|
static QString designerBinary(const QtSupport::BaseQtVersion *qtVersion)
|
2013-03-18 17:53:18 +01:00
|
|
|
{
|
2016-09-28 15:43:13 +02:00
|
|
|
if (qtVersion)
|
|
|
|
|
return qtVersion->designerCommand();
|
2016-11-29 16:29:29 +00:00
|
|
|
return QLatin1String(Utils::HostOsInfo::isMacHost() ? "Designer" : "designer");
|
2013-03-18 17:53:18 +01:00
|
|
|
}
|
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
|
2016-09-28 15:43:13 +02:00
|
|
|
static ExternalQtEditor::LaunchData createMacOpenCommand(const ExternalQtEditor::LaunchData &data)
|
2009-05-19 14:54:52 +02:00
|
|
|
{
|
2016-09-28 15:43:13 +02:00
|
|
|
ExternalQtEditor::LaunchData openData = data;
|
|
|
|
|
const int appFolderIndex = data.binary.lastIndexOf(QLatin1String("/Contents/MacOS/"));
|
2009-05-19 14:54:52 +02:00
|
|
|
if (appFolderIndex != -1) {
|
2016-09-28 15:43:13 +02:00
|
|
|
openData.binary = "open";
|
2017-02-22 15:09:35 +01:00
|
|
|
openData.arguments = QStringList({QString("-a"), data.binary.left(appFolderIndex)})
|
2016-09-28 15:43:13 +02:00
|
|
|
+ data.arguments;
|
2009-05-19 14:54:52 +02:00
|
|
|
}
|
2016-09-28 15:43:13 +02:00
|
|
|
return openData;
|
2009-05-19 14:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
2020-06-26 13:59:38 +02:00
|
|
|
ExternalQtEditor::ExternalQtEditor(Utils::Id id,
|
2010-01-07 18:17:24 +01:00
|
|
|
const QString &displayName,
|
|
|
|
|
const QString &mimetype,
|
2016-09-28 15:43:13 +02:00
|
|
|
const CommandForQtVersion &commandForQtVersion) :
|
2009-05-19 14:54:52 +02:00
|
|
|
m_mimeTypes(mimetype),
|
2013-03-19 13:22:38 +01:00
|
|
|
m_id(id),
|
2016-09-28 15:43:13 +02:00
|
|
|
m_displayName(displayName),
|
|
|
|
|
m_commandForQtVersion(commandForQtVersion)
|
2009-05-19 14:54:52 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-28 15:43:13 +02:00
|
|
|
ExternalQtEditor *ExternalQtEditor::createLinguistEditor()
|
|
|
|
|
{
|
|
|
|
|
return new ExternalQtEditor(linguistIdC,
|
|
|
|
|
QLatin1String(linguistDisplayName),
|
|
|
|
|
QLatin1String(ProjectExplorer::Constants::LINGUIST_MIMETYPE),
|
|
|
|
|
linguistBinary);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExternalQtEditor *ExternalQtEditor::createDesignerEditor()
|
|
|
|
|
{
|
|
|
|
|
if (Utils::HostOsInfo::isMacHost()) {
|
|
|
|
|
return new ExternalQtEditor(designerIdC,
|
|
|
|
|
QLatin1String(designerDisplayName),
|
|
|
|
|
QLatin1String(ProjectExplorer::Constants::FORM_MIMETYPE),
|
|
|
|
|
designerBinary);
|
|
|
|
|
} else {
|
|
|
|
|
return new DesignerExternalEditor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-19 14:54:52 +02:00
|
|
|
QStringList ExternalQtEditor::mimeTypes() const
|
|
|
|
|
{
|
|
|
|
|
return m_mimeTypes;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-26 13:59:38 +02:00
|
|
|
Utils::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
|
|
|
}
|
|
|
|
|
|
2016-09-28 17:14:53 +02:00
|
|
|
static QString findFirstCommand(QVector<QtSupport::BaseQtVersion *> qtVersions,
|
|
|
|
|
ExternalQtEditor::CommandForQtVersion command)
|
|
|
|
|
{
|
|
|
|
|
foreach (QtSupport::BaseQtVersion *qt, qtVersions) {
|
|
|
|
|
if (qt) {
|
|
|
|
|
const QString binary = command(qt);
|
|
|
|
|
if (!binary.isEmpty())
|
|
|
|
|
return binary;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-19 14:54:52 +02:00
|
|
|
bool ExternalQtEditor::getEditorLaunchData(const QString &fileName,
|
2016-09-28 15:43:13 +02:00
|
|
|
LaunchData *data,
|
2009-05-19 14:54:52 +02:00
|
|
|
QString *errorMessage) const
|
|
|
|
|
{
|
2016-09-28 17:14:53 +02:00
|
|
|
// Check in order for Qt version with the binary:
|
|
|
|
|
// - active kit of project
|
|
|
|
|
// - any other of the project
|
|
|
|
|
// - default kit
|
|
|
|
|
// - any other kit
|
|
|
|
|
// As fallback check PATH
|
|
|
|
|
data->workingDirectory.clear();
|
|
|
|
|
QVector<QtSupport::BaseQtVersion *> qtVersionsToCheck; // deduplicated after being filled
|
2019-05-28 13:49:26 +02:00
|
|
|
if (const Project *project = SessionManager::projectForFile(Utils::FilePath::fromString(fileName))) {
|
2016-09-28 17:14:53 +02:00
|
|
|
data->workingDirectory = project->projectDirectory().toString();
|
|
|
|
|
// active kit
|
2015-02-01 18:44:47 +02:00
|
|
|
if (const Target *target = project->activeTarget()) {
|
2019-02-06 12:50:51 +01:00
|
|
|
qtVersionsToCheck << QtSupport::QtKitAspect::qtVersion(target->kit());
|
2011-06-20 12:32:22 +02:00
|
|
|
}
|
2016-09-28 17:14:53 +02:00
|
|
|
// all kits of project
|
|
|
|
|
qtVersionsToCheck += Utils::transform<QVector>(project->targets(), [](Target *t) {
|
2019-02-06 12:50:51 +01:00
|
|
|
return QTC_GUARD(t) ? QtSupport::QtKitAspect::qtVersion(t->kit()) : nullptr;
|
2016-09-28 17:14:53 +02:00
|
|
|
});
|
2011-06-20 12:32:22 +02:00
|
|
|
}
|
2016-09-28 17:14:53 +02:00
|
|
|
// default kit
|
2019-02-06 12:50:51 +01:00
|
|
|
qtVersionsToCheck << QtSupport::QtKitAspect::qtVersion(KitManager::defaultKit());
|
2016-09-28 17:14:53 +02:00
|
|
|
// all kits
|
2019-02-06 12:50:51 +01:00
|
|
|
qtVersionsToCheck += Utils::transform<QVector>(KitManager::kits(), QtSupport::QtKitAspect::qtVersion);
|
2016-09-28 17:14:53 +02:00
|
|
|
qtVersionsToCheck = Utils::filteredUnique(qtVersionsToCheck); // can still contain nullptr
|
|
|
|
|
data->binary = findFirstCommand(qtVersionsToCheck, m_commandForQtVersion);
|
|
|
|
|
// fallback
|
|
|
|
|
if (data->binary.isEmpty())
|
2016-09-28 15:43:13 +02:00
|
|
|
data->binary = Utils::SynchronousProcess::locateBinary(m_commandForQtVersion(nullptr));
|
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.push_back(fileName);
|
2016-09-28 15:43:13 +02:00
|
|
|
if (Utils::HostOsInfo::isMacHost())
|
|
|
|
|
*data = createMacOpenCommand(*data);
|
2009-05-19 14:54:52 +02:00
|
|
|
if (debug)
|
|
|
|
|
qDebug() << Q_FUNC_INFO << '\n' << data->binary << data->arguments;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-28 15:43:13 +02:00
|
|
|
bool ExternalQtEditor::startEditor(const QString &fileName, QString *errorMessage)
|
|
|
|
|
{
|
|
|
|
|
LaunchData data;
|
|
|
|
|
return getEditorLaunchData(fileName, &data, errorMessage)
|
|
|
|
|
&& startEditorProcess(data, errorMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ExternalQtEditor::startEditorProcess(const LaunchData &data, QString *errorMessage)
|
2009-05-19 14:54:52 +02:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --------------- DesignerExternalEditor with Designer Tcp remote control.
|
2016-09-28 15:43:13 +02:00
|
|
|
DesignerExternalEditor::DesignerExternalEditor() :
|
2013-03-19 13:22:38 +01:00
|
|
|
ExternalQtEditor(designerIdC,
|
2010-01-07 18:17:24 +01:00
|
|
|
QLatin1String(designerDisplayName),
|
2009-05-19 14:54:52 +02:00
|
|
|
QLatin1String(Designer::Constants::FORM_MIMETYPE),
|
2016-09-28 15:43:13 +02:00
|
|
|
designerBinary)
|
2009-05-19 14:54:52 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2016-09-28 15:43:13 +02:00
|
|
|
LaunchData data;
|
2009-05-19 14:54:52 +02:00
|
|
|
// Find the editor binary
|
2016-09-28 15:43:13 +02:00
|
|
|
if (!getEditorLaunchData(fileName, &data, errorMessage)) {
|
2009-05-19 14:54:52 +02:00
|
|
|
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;
|
|
|
|
|
// Insert into cache if socket is created, else try again next time
|
|
|
|
|
if (server.waitForNewConnection(3000)) {
|
|
|
|
|
QTcpSocket *socket = server.nextPendingConnection();
|
|
|
|
|
socket->setParent(this);
|
2016-07-19 23:22:41 +03:00
|
|
|
const QString binary = data.binary;
|
|
|
|
|
m_processCache.insert(binary, socket);
|
|
|
|
|
auto mapSlot = [this, binary] { processTerminated(binary); };
|
|
|
|
|
connect(socket, &QAbstractSocket::disconnected, this, mapSlot);
|
2020-06-18 16:51:54 +02:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
|
|
|
|
|
const auto errorOccurred = QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error);
|
|
|
|
|
#else
|
|
|
|
|
const auto errorOccurred = &QAbstractSocket::errorOccurred;
|
|
|
|
|
#endif
|
|
|
|
|
connect(socket, errorOccurred, this, mapSlot);
|
2009-05-19 14:54:52 +02:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
2013-10-16 11:02:37 +02:00
|
|
|
} // namespace QmakeProjectManager
|