2010-11-10 08:49:19 +01:00
|
|
|
/**************************************************************************
|
|
|
|
**
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
**
|
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
**
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
**
|
|
|
|
** Commercial Usage
|
|
|
|
**
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Nokia.
|
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
|
|
|
**
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include "externaltool.h"
|
2010-12-08 14:52:53 +01:00
|
|
|
#include "actionmanager/actionmanager.h"
|
2010-11-11 15:47:23 +01:00
|
|
|
#include "coreconstants.h"
|
2010-12-08 12:08:21 +01:00
|
|
|
#include "variablemanager.h"
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
#include <coreplugin/messagemanager.h>
|
2010-12-13 17:35:40 +01:00
|
|
|
#include <coreplugin/filemanager.h>
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
#include <coreplugin/editormanager/ieditor.h>
|
2010-12-08 12:08:21 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
#include <utils/stringutils.h>
|
2010-12-09 14:57:22 +01:00
|
|
|
#include <utils/environment.h>
|
2010-11-10 08:49:19 +01:00
|
|
|
|
|
|
|
#include <QtCore/QXmlStreamReader>
|
2011-01-24 18:14:30 +01:00
|
|
|
#include <QtCore/QXmlStreamWriter>
|
2010-11-11 15:47:23 +01:00
|
|
|
#include <QtCore/QDir>
|
|
|
|
#include <QtCore/QFile>
|
2011-01-24 18:14:30 +01:00
|
|
|
#include <QtCore/QDateTime>
|
2010-11-11 15:47:23 +01:00
|
|
|
#include <QtGui/QMenu>
|
|
|
|
#include <QtGui/QMenuItem>
|
|
|
|
#include <QtGui/QAction>
|
2010-11-10 08:49:19 +01:00
|
|
|
|
|
|
|
#include <QtDebug>
|
|
|
|
|
2010-11-11 15:47:23 +01:00
|
|
|
using namespace Core;
|
2010-11-10 08:49:19 +01:00
|
|
|
using namespace Core::Internal;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
const char * const kExternalTool = "externaltool";
|
2011-01-24 18:14:30 +01:00
|
|
|
const char * const kId = "id";
|
2010-11-10 08:49:19 +01:00
|
|
|
const char * const kDescription = "description";
|
|
|
|
const char * const kDisplayName = "displayname";
|
|
|
|
const char * const kCategory = "category";
|
|
|
|
const char * const kOrder = "order";
|
|
|
|
const char * const kExecutable = "executable";
|
|
|
|
const char * const kPath = "path";
|
|
|
|
const char * const kArguments = "arguments";
|
2010-12-09 14:57:22 +01:00
|
|
|
const char * const kInput = "input";
|
2010-11-10 08:49:19 +01:00
|
|
|
const char * const kWorkingDirectory = "workingdirectory";
|
|
|
|
|
|
|
|
const char * const kXmlLang = "xml:lang";
|
|
|
|
const char * const kOutput = "output";
|
2010-12-08 14:52:53 +01:00
|
|
|
const char * const kError = "error";
|
2010-11-10 08:49:19 +01:00
|
|
|
const char * const kOutputShowInPane = "showinpane";
|
|
|
|
const char * const kOutputReplaceSelection = "replaceselection";
|
2010-12-08 14:52:53 +01:00
|
|
|
const char * const kOutputIgnore = "ignore";
|
2011-01-24 12:21:42 +01:00
|
|
|
const char * const kModifiesDocument = "modifiesdocument";
|
|
|
|
const char * const kYes = "yes";
|
|
|
|
const char * const kNo = "no";
|
|
|
|
const char * const kTrue= "true";
|
|
|
|
const char * const kFalse = "false";
|
2010-11-10 08:49:19 +01:00
|
|
|
}
|
|
|
|
|
2010-11-11 15:47:23 +01:00
|
|
|
// #pragma mark -- ExternalTool
|
|
|
|
|
2010-11-10 08:49:19 +01:00
|
|
|
ExternalTool::ExternalTool() :
|
|
|
|
m_order(-1),
|
2010-12-08 14:52:53 +01:00
|
|
|
m_outputHandling(ShowInPane),
|
2011-01-21 10:58:06 +01:00
|
|
|
m_errorHandling(ShowInPane),
|
2011-01-24 18:14:30 +01:00
|
|
|
m_modifiesCurrentDocument(false)
|
2010-11-10 08:49:19 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-01-06 18:26:09 +01:00
|
|
|
ExternalTool::ExternalTool(const ExternalTool *other)
|
|
|
|
: m_id(other->m_id),
|
|
|
|
m_description(other->m_description),
|
|
|
|
m_displayName(other->m_displayName),
|
|
|
|
m_displayCategory(other->m_displayCategory),
|
|
|
|
m_order(other->m_order),
|
|
|
|
m_executables(other->m_executables),
|
|
|
|
m_arguments(other->m_arguments),
|
|
|
|
m_input(other->m_input),
|
|
|
|
m_workingDirectory(other->m_workingDirectory),
|
|
|
|
m_outputHandling(other->m_outputHandling),
|
2011-01-21 10:58:06 +01:00
|
|
|
m_errorHandling(other->m_errorHandling),
|
2011-01-24 12:21:42 +01:00
|
|
|
m_modifiesCurrentDocument(other->m_modifiesCurrentDocument),
|
2011-01-24 18:14:30 +01:00
|
|
|
m_fileName(other->m_fileName),
|
2011-02-01 15:55:40 +01:00
|
|
|
m_presetTool(other->m_presetTool)
|
2011-01-06 18:26:09 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-02-08 12:28:35 +01:00
|
|
|
ExternalTool &ExternalTool::operator=(const ExternalTool &other)
|
|
|
|
{
|
|
|
|
m_id = other.m_id;
|
|
|
|
m_description = other.m_description;
|
|
|
|
m_displayName = other.m_displayName;
|
|
|
|
m_displayCategory = other.m_displayCategory;
|
|
|
|
m_order = other.m_order;
|
|
|
|
m_executables = other.m_executables;
|
|
|
|
m_arguments = other.m_arguments;
|
|
|
|
m_input = other.m_input;
|
|
|
|
m_workingDirectory = other.m_workingDirectory;
|
|
|
|
m_outputHandling = other.m_outputHandling;
|
|
|
|
m_errorHandling = other.m_errorHandling;
|
|
|
|
m_modifiesCurrentDocument = other.m_modifiesCurrentDocument;
|
|
|
|
m_fileName = other.m_fileName;
|
|
|
|
m_presetTool = other.m_presetTool;
|
|
|
|
}
|
|
|
|
|
2010-12-08 12:08:21 +01:00
|
|
|
ExternalTool::~ExternalTool()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-11-11 14:57:29 +01:00
|
|
|
QString ExternalTool::id() const
|
|
|
|
{
|
|
|
|
return m_id;
|
|
|
|
}
|
|
|
|
|
2010-11-10 08:49:19 +01:00
|
|
|
QString ExternalTool::description() const
|
|
|
|
{
|
|
|
|
return m_description;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ExternalTool::displayName() const
|
|
|
|
{
|
|
|
|
return m_displayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ExternalTool::displayCategory() const
|
|
|
|
{
|
|
|
|
return m_displayCategory;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ExternalTool::order() const
|
|
|
|
{
|
|
|
|
return m_order;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList ExternalTool::executables() const
|
|
|
|
{
|
|
|
|
return m_executables;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ExternalTool::arguments() const
|
|
|
|
{
|
|
|
|
return m_arguments;
|
|
|
|
}
|
|
|
|
|
2010-12-09 14:57:22 +01:00
|
|
|
QString ExternalTool::input() const
|
|
|
|
{
|
|
|
|
return m_input;
|
|
|
|
}
|
|
|
|
|
2010-11-10 08:49:19 +01:00
|
|
|
QString ExternalTool::workingDirectory() const
|
|
|
|
{
|
|
|
|
return m_workingDirectory;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExternalTool::OutputHandling ExternalTool::outputHandling() const
|
|
|
|
{
|
|
|
|
return m_outputHandling;
|
|
|
|
}
|
|
|
|
|
2010-12-08 14:52:53 +01:00
|
|
|
ExternalTool::OutputHandling ExternalTool::errorHandling() const
|
|
|
|
{
|
|
|
|
return m_errorHandling;
|
|
|
|
}
|
|
|
|
|
2011-01-24 12:21:42 +01:00
|
|
|
bool ExternalTool::modifiesCurrentDocument() const
|
|
|
|
{
|
|
|
|
return m_modifiesCurrentDocument;
|
|
|
|
}
|
|
|
|
|
2011-01-24 18:14:30 +01:00
|
|
|
void ExternalTool::setFileName(const QString &fileName)
|
|
|
|
{
|
|
|
|
m_fileName = fileName;
|
|
|
|
}
|
|
|
|
|
2011-02-01 15:55:40 +01:00
|
|
|
void ExternalTool::setPreset(QSharedPointer<ExternalTool> preset)
|
2011-01-24 18:14:30 +01:00
|
|
|
{
|
2011-02-01 15:55:40 +01:00
|
|
|
m_presetTool = preset;
|
2011-01-24 18:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString ExternalTool::fileName() const
|
|
|
|
{
|
|
|
|
return m_fileName;
|
|
|
|
}
|
|
|
|
|
2011-02-01 15:55:40 +01:00
|
|
|
QSharedPointer<ExternalTool> ExternalTool::preset() const
|
2011-01-24 18:14:30 +01:00
|
|
|
{
|
2011-02-01 15:55:40 +01:00
|
|
|
return m_presetTool;
|
2011-01-24 18:14:30 +01:00
|
|
|
}
|
|
|
|
|
2011-02-02 12:35:32 +01:00
|
|
|
void ExternalTool::setId(const QString &id)
|
|
|
|
{
|
|
|
|
m_id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalTool::setCategory(const QString &category)
|
|
|
|
{
|
|
|
|
m_displayCategory = category;
|
|
|
|
}
|
|
|
|
|
2011-01-24 15:01:48 +01:00
|
|
|
void ExternalTool::setDisplayName(const QString &name)
|
|
|
|
{
|
|
|
|
m_displayName = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalTool::setDescription(const QString &description)
|
|
|
|
{
|
|
|
|
m_description = description;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ExternalTool::setOutputHandling(OutputHandling handling)
|
|
|
|
{
|
|
|
|
m_outputHandling = handling;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ExternalTool::setErrorHandling(OutputHandling handling)
|
|
|
|
{
|
|
|
|
m_errorHandling = handling;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ExternalTool::setModifiesCurrentDocument(bool modifies)
|
|
|
|
{
|
|
|
|
m_modifiesCurrentDocument = modifies;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ExternalTool::setExecutables(const QStringList &executables)
|
|
|
|
{
|
|
|
|
m_executables = executables;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ExternalTool::setArguments(const QString &arguments)
|
|
|
|
{
|
|
|
|
m_arguments = arguments;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ExternalTool::setInput(const QString &input)
|
|
|
|
{
|
|
|
|
m_input = input;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ExternalTool::setWorkingDirectory(const QString &workingDirectory)
|
|
|
|
{
|
|
|
|
m_workingDirectory = workingDirectory;
|
|
|
|
}
|
|
|
|
|
2010-11-11 14:26:14 +01:00
|
|
|
static QStringList splitLocale(const QString &locale)
|
2010-11-10 08:49:19 +01:00
|
|
|
{
|
2010-11-11 14:26:14 +01:00
|
|
|
QString value = locale;
|
|
|
|
QStringList values;
|
|
|
|
if (!value.isEmpty())
|
|
|
|
values << value;
|
|
|
|
int index = value.indexOf(QLatin1Char('.'));
|
|
|
|
if (index >= 0) {
|
|
|
|
value = value.left(index);
|
|
|
|
if (!value.isEmpty())
|
|
|
|
values << value;
|
|
|
|
}
|
|
|
|
index = value.indexOf(QLatin1Char('_'));
|
|
|
|
if (index >= 0) {
|
|
|
|
value = value.left(index);
|
|
|
|
if (!value.isEmpty())
|
|
|
|
values << value;
|
|
|
|
}
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void localizedText(const QStringList &locales, QXmlStreamReader *reader, int *currentLocale, QString *currentText)
|
|
|
|
{
|
|
|
|
Q_ASSERT(reader);
|
|
|
|
Q_ASSERT(currentLocale);
|
|
|
|
Q_ASSERT(currentText);
|
|
|
|
if (reader->attributes().hasAttribute(QLatin1String(kXmlLang))) {
|
|
|
|
int index = locales.indexOf(reader->attributes().value(QLatin1String(kXmlLang)).toString());
|
|
|
|
if (index >= 0 && (index < *currentLocale || *currentLocale < 0)) {
|
|
|
|
*currentText = reader->readElementText();
|
|
|
|
*currentLocale = index;
|
|
|
|
} else {
|
|
|
|
reader->skipCurrentElement();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (*currentLocale < 0 && currentText->isEmpty()) {
|
|
|
|
*currentText = reader->readElementText();
|
|
|
|
} else {
|
|
|
|
reader->skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-08 14:52:53 +01:00
|
|
|
static bool parseOutputAttribute(const QString &attribute, QXmlStreamReader *reader, ExternalTool::OutputHandling *value)
|
|
|
|
{
|
|
|
|
const QString output = reader->attributes().value(attribute).toString();
|
|
|
|
if (output == QLatin1String(kOutputShowInPane)) {
|
|
|
|
*value = ExternalTool::ShowInPane;
|
|
|
|
} else if (output == QLatin1String(kOutputReplaceSelection)) {
|
|
|
|
*value = ExternalTool::ReplaceSelection;
|
|
|
|
} else if (output == QLatin1String(kOutputIgnore)) {
|
|
|
|
*value = ExternalTool::Ignore;
|
|
|
|
} else {
|
2011-01-24 12:21:42 +01:00
|
|
|
reader->raiseError(QLatin1String("Allowed values for output attribute are 'showinpane','replaceselection','ignore'"));
|
2010-12-08 14:52:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-11 15:47:23 +01:00
|
|
|
ExternalTool * ExternalTool::createFromXml(const QByteArray &xml, QString *errorMessage, const QString &locale)
|
2010-11-11 14:26:14 +01:00
|
|
|
{
|
|
|
|
int descriptionLocale = -1;
|
|
|
|
int nameLocale = -1;
|
|
|
|
int categoryLocale = -1;
|
|
|
|
const QStringList &locales = splitLocale(locale);
|
2010-11-10 08:49:19 +01:00
|
|
|
ExternalTool *tool = new ExternalTool;
|
|
|
|
QXmlStreamReader reader(xml);
|
2010-11-11 14:26:14 +01:00
|
|
|
|
2010-11-10 08:49:19 +01:00
|
|
|
if (!reader.readNextStartElement() || reader.name() != QLatin1String(kExternalTool))
|
|
|
|
reader.raiseError(QLatin1String("Missing start element <externaltool>"));
|
2011-01-24 18:14:30 +01:00
|
|
|
tool->m_id = reader.attributes().value(QLatin1String(kId)).toString();
|
2010-11-11 14:57:29 +01:00
|
|
|
if (tool->m_id.isEmpty())
|
|
|
|
reader.raiseError(QLatin1String("Missing or empty id attribute for <externaltool>"));
|
2010-11-10 08:49:19 +01:00
|
|
|
while (reader.readNextStartElement()) {
|
|
|
|
if (reader.name() == QLatin1String(kDescription)) {
|
2010-11-11 14:26:14 +01:00
|
|
|
localizedText(locales, &reader, &descriptionLocale, &tool->m_description);
|
2010-11-10 08:49:19 +01:00
|
|
|
} else if (reader.name() == QLatin1String(kDisplayName)) {
|
2010-11-11 14:26:14 +01:00
|
|
|
localizedText(locales, &reader, &nameLocale, &tool->m_displayName);
|
2010-11-10 08:49:19 +01:00
|
|
|
} else if (reader.name() == QLatin1String(kCategory)) {
|
2010-11-11 14:26:14 +01:00
|
|
|
localizedText(locales, &reader, &categoryLocale, &tool->m_displayCategory);
|
2010-11-10 08:49:19 +01:00
|
|
|
} else if (reader.name() == QLatin1String(kOrder)) {
|
|
|
|
if (tool->m_order >= 0) {
|
|
|
|
reader.raiseError(QLatin1String("only one <order> element allowed"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bool ok;
|
|
|
|
tool->m_order = reader.readElementText().toInt(&ok);
|
|
|
|
if (!ok || tool->m_order < 0)
|
|
|
|
reader.raiseError(QLatin1String("<order> element requires non-negative integer value"));
|
|
|
|
} else if (reader.name() == QLatin1String(kExecutable)) {
|
|
|
|
if (reader.attributes().hasAttribute(QLatin1String(kOutput))) {
|
2010-12-08 14:52:53 +01:00
|
|
|
if (!parseOutputAttribute(QLatin1String(kOutput), &reader, &tool->m_outputHandling))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (reader.attributes().hasAttribute(QLatin1String(kError))) {
|
|
|
|
if (!parseOutputAttribute(QLatin1String(kError), &reader, &tool->m_errorHandling))
|
2010-11-10 08:49:19 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-01-24 12:21:42 +01:00
|
|
|
if (reader.attributes().hasAttribute(QLatin1String(kModifiesDocument))) {
|
|
|
|
const QString &value = reader.attributes().value(QLatin1String(kModifiesDocument)).toString();
|
|
|
|
if (value == QLatin1String(kYes) || value == QLatin1String(kTrue)) {
|
|
|
|
tool->m_modifiesCurrentDocument = true;
|
|
|
|
} else if (value == QLatin1String(kNo) || value == QLatin1String(kFalse)) {
|
|
|
|
tool->m_modifiesCurrentDocument = false;
|
|
|
|
} else {
|
|
|
|
reader.raiseError(QLatin1String("Allowed values for modifiesdocument attribute are 'yes','true','no','false'"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-11-10 08:49:19 +01:00
|
|
|
while (reader.readNextStartElement()) {
|
|
|
|
if (reader.name() == QLatin1String(kPath)) {
|
|
|
|
tool->m_executables.append(reader.readElementText());
|
|
|
|
} else if (reader.name() == QLatin1String(kArguments)) {
|
|
|
|
if (!tool->m_arguments.isEmpty()) {
|
|
|
|
reader.raiseError(QLatin1String("only one <arguments> element allowed"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tool->m_arguments = reader.readElementText();
|
2010-12-09 14:57:22 +01:00
|
|
|
} else if (reader.name() == QLatin1String(kInput)) {
|
|
|
|
if (!tool->m_input.isEmpty()) {
|
|
|
|
reader.raiseError(QLatin1String("only one <input> element allowed"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tool->m_input = reader.readElementText();
|
2010-11-10 08:49:19 +01:00
|
|
|
} else if (reader.name() == QLatin1String(kWorkingDirectory)) {
|
|
|
|
if (!tool->m_workingDirectory.isEmpty()) {
|
|
|
|
reader.raiseError(QLatin1String("only one <workingdirectory> element allowed"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tool->m_workingDirectory = reader.readElementText();
|
2010-12-08 12:07:06 +01:00
|
|
|
} else {
|
|
|
|
reader.raiseError(QString::fromLatin1("Unknown element <%1> as subelement of <%2>").arg(
|
|
|
|
reader.qualifiedName().toString(), QLatin1String(kExecutable)));
|
|
|
|
break;
|
2010-11-10 08:49:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reader.raiseError(QString::fromLatin1("Unknown element <%1>").arg(reader.qualifiedName().toString()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (reader.hasError()) {
|
|
|
|
if (errorMessage)
|
|
|
|
*errorMessage = reader.errorString();
|
|
|
|
delete tool;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return tool;
|
|
|
|
}
|
2010-11-11 15:47:23 +01:00
|
|
|
|
2011-02-01 15:55:40 +01:00
|
|
|
ExternalTool * ExternalTool::createFromFile(const QString &fileName, QString *errorMessage, const QString &locale)
|
2011-01-24 18:14:30 +01:00
|
|
|
{
|
|
|
|
QFileInfo info(fileName);
|
|
|
|
QFile file(info.absoluteFilePath());
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
if (errorMessage)
|
|
|
|
*errorMessage = tr("Could not open tool specification %1 for reading").arg(fileName);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const QByteArray &bytes = file.readAll();
|
|
|
|
file.close();
|
|
|
|
ExternalTool *tool = ExternalTool::createFromXml(bytes, errorMessage, locale);
|
|
|
|
if (!tool) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
tool->m_fileName = file.fileName();
|
|
|
|
return tool;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QLatin1String stringForOutputHandling(ExternalTool::OutputHandling handling)
|
|
|
|
{
|
|
|
|
switch (handling) {
|
|
|
|
case Core::Internal::ExternalTool::Ignore:
|
|
|
|
return QLatin1String(kOutputIgnore);
|
|
|
|
case Core::Internal::ExternalTool::ShowInPane:
|
|
|
|
return QLatin1String(kOutputShowInPane);
|
|
|
|
case Core::Internal::ExternalTool::ReplaceSelection:
|
|
|
|
return QLatin1String(kOutputReplaceSelection);
|
|
|
|
}
|
|
|
|
return QLatin1String("");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExternalTool::save(QString *errorMessage) const
|
|
|
|
{
|
|
|
|
if (m_fileName.isEmpty())
|
|
|
|
return false;
|
|
|
|
QFile file(m_fileName);
|
|
|
|
if (!file.open(QIODevice::WriteOnly)) {
|
|
|
|
if (errorMessage)
|
|
|
|
*errorMessage = tr("Could not write tool specification %1").arg(m_fileName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QXmlStreamWriter out(&file);
|
|
|
|
out.setAutoFormatting(true);
|
|
|
|
out.writeStartDocument(QLatin1String("1.0"));
|
|
|
|
out.writeComment(QString::fromLatin1("Written on %1 by Qt Creator %2")
|
|
|
|
.arg(QDateTime::currentDateTime().toString(), QLatin1String(Constants::IDE_VERSION_LONG)));
|
|
|
|
out.writeStartElement(QLatin1String(kExternalTool));
|
|
|
|
out.writeAttribute(QLatin1String(kId), m_id);
|
|
|
|
out.writeTextElement(QLatin1String(kDescription), m_description);
|
|
|
|
out.writeTextElement(QLatin1String(kDisplayName), m_displayName);
|
|
|
|
out.writeTextElement(QLatin1String(kCategory), m_displayCategory);
|
|
|
|
if (m_order != -1)
|
|
|
|
out.writeTextElement(QLatin1String(kOrder), QString::number(m_order));
|
|
|
|
|
|
|
|
out.writeStartElement(QLatin1String(kExecutable));
|
|
|
|
out.writeAttribute(QLatin1String(kOutput), stringForOutputHandling(m_outputHandling));
|
|
|
|
out.writeAttribute(QLatin1String(kError), stringForOutputHandling(m_errorHandling));
|
|
|
|
out.writeAttribute(QLatin1String(kModifiesDocument), m_modifiesCurrentDocument ? QLatin1String(kYes) : QLatin1String(kNo));
|
|
|
|
foreach (const QString &executable, m_executables)
|
|
|
|
out.writeTextElement(QLatin1String(kPath), executable);
|
|
|
|
if (!m_arguments.isEmpty())
|
|
|
|
out.writeTextElement(QLatin1String(kArguments), m_arguments);
|
|
|
|
if (!m_input.isEmpty())
|
|
|
|
out.writeTextElement(QLatin1String(kInput), m_input);
|
|
|
|
if (!m_workingDirectory.isEmpty())
|
|
|
|
out.writeTextElement(QLatin1String(kWorkingDirectory), m_workingDirectory);
|
|
|
|
out.writeEndElement();
|
|
|
|
|
|
|
|
out.writeEndDocument();
|
|
|
|
file.close();
|
2011-02-01 15:55:40 +01:00
|
|
|
return true;
|
2011-01-24 18:14:30 +01:00
|
|
|
}
|
|
|
|
|
2011-01-21 10:58:06 +01:00
|
|
|
bool ExternalTool::operator==(const ExternalTool &other)
|
|
|
|
{
|
|
|
|
return m_id == other.m_id
|
|
|
|
&& m_description == other.m_description
|
|
|
|
&& m_displayName == other.m_displayName
|
|
|
|
&& m_displayCategory == other.m_displayCategory
|
|
|
|
&& m_order == other.m_order
|
|
|
|
&& m_executables == other.m_executables
|
|
|
|
&& m_arguments == other.m_arguments
|
|
|
|
&& m_input == other.m_input
|
|
|
|
&& m_workingDirectory == other.m_workingDirectory
|
|
|
|
&& m_outputHandling == other.m_outputHandling
|
2011-01-24 12:21:42 +01:00
|
|
|
&& m_modifiesCurrentDocument == other.m_modifiesCurrentDocument
|
2011-02-01 15:55:40 +01:00
|
|
|
&& m_errorHandling == other.m_errorHandling
|
|
|
|
&& m_fileName == other.m_fileName;
|
2011-01-21 10:58:06 +01:00
|
|
|
}
|
|
|
|
|
2010-12-08 12:08:21 +01:00
|
|
|
// #pragma mark -- ExternalToolRunner
|
|
|
|
|
|
|
|
ExternalToolRunner::ExternalToolRunner(const ExternalTool *tool)
|
2011-01-21 10:58:06 +01:00
|
|
|
: m_tool(new ExternalTool(tool)),
|
2010-12-08 12:08:21 +01:00
|
|
|
m_process(0),
|
|
|
|
m_outputCodec(QTextCodec::codecForLocale())
|
|
|
|
{
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
|
2011-01-21 10:58:06 +01:00
|
|
|
ExternalToolRunner::~ExternalToolRunner()
|
|
|
|
{
|
|
|
|
if (m_tool)
|
|
|
|
delete m_tool;
|
|
|
|
}
|
|
|
|
|
2010-12-08 12:08:21 +01:00
|
|
|
bool ExternalToolRunner::resolve()
|
|
|
|
{
|
|
|
|
if (!m_tool)
|
|
|
|
return false;
|
|
|
|
m_resolvedExecutable = QString::null;
|
2010-12-15 16:48:11 +01:00
|
|
|
m_resolvedArguments = QString::null;
|
2010-12-08 12:08:21 +01:00
|
|
|
m_resolvedWorkingDirectory = QString::null;
|
|
|
|
{ // executable
|
|
|
|
foreach (const QString &executable, m_tool->executables()) {
|
|
|
|
QString resolved = Utils::expandMacros(executable,
|
|
|
|
Core::VariableManager::instance()->macroExpander());
|
2010-12-09 14:57:22 +01:00
|
|
|
m_resolvedExecutable =
|
|
|
|
Utils::Environment::systemEnvironment().searchInPath(resolved);
|
2010-12-08 12:08:21 +01:00
|
|
|
}
|
2010-12-09 14:57:22 +01:00
|
|
|
if (m_resolvedExecutable.isEmpty())
|
2010-12-08 12:08:21 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
{ // arguments
|
2010-12-15 16:48:11 +01:00
|
|
|
m_resolvedArguments = Utils::expandMacros(m_tool->arguments(),
|
2010-12-08 12:08:21 +01:00
|
|
|
Core::VariableManager::instance()->macroExpander());
|
|
|
|
}
|
2010-12-09 14:57:22 +01:00
|
|
|
{ // input
|
|
|
|
m_resolvedInput = Utils::expandMacros(m_tool->input(),
|
|
|
|
Core::VariableManager::instance()->macroExpander());
|
|
|
|
}
|
2010-12-08 12:08:21 +01:00
|
|
|
{ // working directory
|
|
|
|
m_resolvedWorkingDirectory = Utils::expandMacros(m_tool->workingDirectory(),
|
|
|
|
Core::VariableManager::instance()->macroExpander());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalToolRunner::run()
|
|
|
|
{
|
|
|
|
if (!resolve()) {
|
|
|
|
deleteLater();
|
|
|
|
return;
|
|
|
|
}
|
2011-01-24 12:21:42 +01:00
|
|
|
if (m_tool->modifiesCurrentDocument()) {
|
2010-12-13 17:35:40 +01:00
|
|
|
if (IEditor *editor = EditorManager::instance()->currentEditor()) {
|
|
|
|
m_expectedFileName = editor->file()->fileName();
|
2010-12-13 18:01:20 +01:00
|
|
|
bool cancelled = false;
|
|
|
|
FileManager::instance()->saveModifiedFiles(QList<IFile *>() << editor->file(), &cancelled);
|
|
|
|
if (cancelled) {
|
|
|
|
deleteLater();
|
|
|
|
return;
|
|
|
|
}
|
2010-12-13 17:35:40 +01:00
|
|
|
FileManager::instance()->expectFileChange(m_expectedFileName);
|
|
|
|
}
|
2010-12-13 15:17:33 +01:00
|
|
|
}
|
2010-12-15 16:48:11 +01:00
|
|
|
m_process = new Utils::QtcProcess(this);
|
2010-12-09 14:57:22 +01:00
|
|
|
connect(m_process, SIGNAL(started()), this, SLOT(started()));
|
2010-12-13 17:35:40 +01:00
|
|
|
connect(m_process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(finished(int,QProcess::ExitStatus)));
|
2010-12-08 12:08:21 +01:00
|
|
|
connect(m_process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(error(QProcess::ProcessError)));
|
|
|
|
connect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(readStandardOutput()));
|
|
|
|
connect(m_process, SIGNAL(readyReadStandardError()), this, SLOT(readStandardError()));
|
|
|
|
if (!m_resolvedWorkingDirectory.isEmpty())
|
|
|
|
m_process->setWorkingDirectory(m_resolvedWorkingDirectory);
|
2010-12-15 16:48:11 +01:00
|
|
|
m_process->setCommand(m_resolvedExecutable, m_resolvedArguments);
|
2010-12-09 14:57:22 +01:00
|
|
|
ICore::instance()->messageManager()->printToOutputPane(
|
|
|
|
tr("Starting external tool '%1'").arg(m_resolvedExecutable), false);
|
2010-12-15 16:48:11 +01:00
|
|
|
if (!m_resolvedArguments.isEmpty()) {
|
|
|
|
ICore::instance()->messageManager()->printToOutputPane(
|
|
|
|
tr("with arguments '%1'").arg(m_resolvedArguments), false);
|
|
|
|
}
|
|
|
|
m_process->start();
|
2010-12-09 14:57:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalToolRunner::started()
|
|
|
|
{
|
|
|
|
if (!m_resolvedInput.isEmpty()) {
|
|
|
|
m_process->write(m_resolvedInput.toLocal8Bit());
|
|
|
|
}
|
|
|
|
m_process->closeWriteChannel();
|
2010-12-08 12:08:21 +01:00
|
|
|
}
|
|
|
|
|
2010-12-13 17:35:40 +01:00
|
|
|
void ExternalToolRunner::finished(int exitCode, QProcess::ExitStatus status)
|
2010-12-08 12:08:21 +01:00
|
|
|
{
|
2010-12-13 17:35:40 +01:00
|
|
|
if (status == QProcess::NormalExit && exitCode == 0) {
|
|
|
|
if (m_tool->outputHandling() == ExternalTool::ReplaceSelection
|
|
|
|
|| m_tool->errorHandling() == ExternalTool::ReplaceSelection) {
|
|
|
|
emit ExternalToolManager::instance()->replaceSelectionRequested(m_processOutput);
|
2011-01-24 12:21:42 +01:00
|
|
|
}
|
|
|
|
if (m_tool->modifiesCurrentDocument()) {
|
2010-12-13 17:35:40 +01:00
|
|
|
FileManager::instance()->unexpectFileChange(m_expectedFileName);
|
|
|
|
}
|
2010-12-09 15:53:43 +01:00
|
|
|
}
|
2010-12-09 14:57:22 +01:00
|
|
|
ICore::instance()->messageManager()->printToOutputPane(
|
|
|
|
tr("'%1' finished").arg(m_resolvedExecutable), false);
|
2010-12-08 12:08:21 +01:00
|
|
|
deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalToolRunner::error(QProcess::ProcessError error)
|
|
|
|
{
|
2011-01-24 12:21:42 +01:00
|
|
|
if (m_tool->modifiesCurrentDocument()) {
|
2010-12-13 17:35:40 +01:00
|
|
|
FileManager::instance()->unexpectFileChange(m_expectedFileName);
|
|
|
|
}
|
2010-12-08 12:08:21 +01:00
|
|
|
// TODO inform about errors
|
|
|
|
deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalToolRunner::readStandardOutput()
|
|
|
|
{
|
2010-12-08 14:52:53 +01:00
|
|
|
if (m_tool->outputHandling() == ExternalTool::Ignore)
|
|
|
|
return;
|
2010-12-08 12:08:21 +01:00
|
|
|
QByteArray data = m_process->readAllStandardOutput();
|
|
|
|
QString output = m_outputCodec->toUnicode(data.constData(), data.length(), &m_outputCodecState);
|
|
|
|
if (m_tool->outputHandling() == ExternalTool::ShowInPane) {
|
|
|
|
ICore::instance()->messageManager()->printToOutputPane(output, true);
|
2010-12-09 15:53:43 +01:00
|
|
|
} else if (m_tool->outputHandling() == ExternalTool::ReplaceSelection) {
|
|
|
|
m_processOutput.append(output);
|
2010-12-08 12:08:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalToolRunner::readStandardError()
|
|
|
|
{
|
2010-12-08 14:52:53 +01:00
|
|
|
if (m_tool->errorHandling() == ExternalTool::Ignore)
|
|
|
|
return;
|
2010-12-08 12:08:21 +01:00
|
|
|
QByteArray data = m_process->readAllStandardError();
|
|
|
|
QString output = m_outputCodec->toUnicode(data.constData(), data.length(), &m_errorCodecState);
|
2010-12-08 14:52:53 +01:00
|
|
|
if (m_tool->errorHandling() == ExternalTool::ShowInPane) {
|
2010-12-08 12:08:21 +01:00
|
|
|
ICore::instance()->messageManager()->printToOutputPane(output, true);
|
2010-12-09 15:53:43 +01:00
|
|
|
} else if (m_tool->errorHandling() == ExternalTool::ReplaceSelection) {
|
|
|
|
m_processOutput.append(output);
|
2010-12-08 12:08:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-11 15:47:23 +01:00
|
|
|
// #pragma mark -- ExternalToolManager
|
|
|
|
|
2010-12-09 15:53:43 +01:00
|
|
|
ExternalToolManager *ExternalToolManager::m_instance = 0;
|
|
|
|
|
2010-11-11 15:47:23 +01:00
|
|
|
ExternalToolManager::ExternalToolManager(Core::ICore *core)
|
|
|
|
: QObject(core), m_core(core)
|
|
|
|
{
|
2010-12-09 15:53:43 +01:00
|
|
|
m_instance = this;
|
2010-11-11 15:47:23 +01:00
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
ExternalToolManager::~ExternalToolManager()
|
|
|
|
{
|
2011-01-21 10:58:06 +01:00
|
|
|
writeSettings();
|
2010-12-13 18:02:43 +01:00
|
|
|
// TODO kill running tools
|
2010-11-11 15:47:23 +01:00
|
|
|
qDeleteAll(m_tools);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalToolManager::initialize()
|
|
|
|
{
|
2011-01-21 10:58:06 +01:00
|
|
|
// add the external tools menu
|
2010-11-11 15:47:23 +01:00
|
|
|
ActionManager *am = m_core->actionManager();
|
|
|
|
ActionContainer *mexternaltools = am->createMenu(Id(Constants::M_TOOLS_EXTERNAL));
|
|
|
|
mexternaltools->menu()->setTitle(tr("External"));
|
|
|
|
ActionContainer *mtools = am->actionContainer(Constants::M_TOOLS);
|
|
|
|
mtools->addMenu(mexternaltools, Constants::G_DEFAULT_THREE);
|
|
|
|
|
2011-01-21 10:58:06 +01:00
|
|
|
QMap<QString, QMultiMap<int, ExternalTool*> > categoryPriorityMap;
|
|
|
|
QMap<QString, ExternalTool *> tools;
|
2011-01-06 11:01:57 +01:00
|
|
|
parseDirectory(m_core->userResourcePath() + QLatin1String("/externaltools"),
|
2011-01-21 10:58:06 +01:00
|
|
|
&categoryPriorityMap,
|
|
|
|
&tools);
|
2011-01-06 11:01:57 +01:00
|
|
|
parseDirectory(m_core->resourcePath() + QLatin1String("/externaltools"),
|
2011-01-21 10:58:06 +01:00
|
|
|
&categoryPriorityMap,
|
|
|
|
&tools,
|
|
|
|
true);
|
2011-01-06 18:26:09 +01:00
|
|
|
|
2011-01-21 10:58:06 +01:00
|
|
|
// adapt overridden names and categories etc
|
|
|
|
readSettings(tools, &categoryPriorityMap);
|
2011-01-06 11:01:57 +01:00
|
|
|
|
2011-01-21 10:58:06 +01:00
|
|
|
QMap<QString, QList<Internal::ExternalTool *> > categoryMap;
|
|
|
|
QMapIterator<QString, QMultiMap<int, ExternalTool*> > it(categoryPriorityMap);
|
2011-01-06 11:01:57 +01:00
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
2011-01-21 10:58:06 +01:00
|
|
|
categoryMap.insert(it.key(), it.value().values());
|
2011-01-06 11:01:57 +01:00
|
|
|
}
|
2011-01-21 10:58:06 +01:00
|
|
|
|
|
|
|
setToolsByCategory(categoryMap);
|
2011-01-06 11:01:57 +01:00
|
|
|
}
|
|
|
|
|
2011-01-21 10:58:06 +01:00
|
|
|
void ExternalToolManager::parseDirectory(const QString &directory,
|
|
|
|
QMap<QString, QMultiMap<int, Internal::ExternalTool*> > *categoryMenus,
|
|
|
|
QMap<QString, ExternalTool *> *tools,
|
2011-01-24 18:14:30 +01:00
|
|
|
bool isPreset)
|
2011-01-06 11:01:57 +01:00
|
|
|
{
|
|
|
|
QTC_ASSERT(categoryMenus, return);
|
2011-01-21 10:58:06 +01:00
|
|
|
QTC_ASSERT(tools, return);
|
2011-01-06 11:01:57 +01:00
|
|
|
QDir dir(directory, QLatin1String("*.xml"), QDir::Unsorted, QDir::Files | QDir::Readable);
|
2010-11-11 15:47:23 +01:00
|
|
|
foreach (const QFileInfo &info, dir.entryInfoList()) {
|
2011-01-24 18:14:30 +01:00
|
|
|
const QString &fileName = info.absoluteFilePath();
|
|
|
|
QString error;
|
2011-02-01 15:55:40 +01:00
|
|
|
ExternalTool *tool = ExternalTool::createFromFile(fileName, &error, m_core->userInterfaceLanguage());
|
2011-01-24 18:14:30 +01:00
|
|
|
if (!tool) {
|
|
|
|
qWarning() << tr("Error while parsing external tool %1: %2").arg(fileName, error);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (tools->contains(tool->id())) {
|
|
|
|
if (isPreset) {
|
2011-02-01 15:55:40 +01:00
|
|
|
// preset that was changed
|
2011-01-24 18:14:30 +01:00
|
|
|
ExternalTool *other = tools->value(tool->id());
|
2011-02-01 15:55:40 +01:00
|
|
|
other->setPreset(QSharedPointer<ExternalTool>(tool));
|
2011-01-24 18:14:30 +01:00
|
|
|
} else {
|
|
|
|
qWarning() << tr("Error: External tool in %1 has duplicate id").arg(fileName);
|
2011-02-01 15:55:40 +01:00
|
|
|
delete tool;
|
2010-11-11 15:47:23 +01:00
|
|
|
}
|
2011-01-24 18:14:30 +01:00
|
|
|
continue;
|
2011-01-05 17:48:26 +01:00
|
|
|
}
|
2011-02-01 15:55:40 +01:00
|
|
|
if (isPreset) {
|
|
|
|
// preset that wasn't changed --> save original values
|
|
|
|
tool->setPreset(QSharedPointer<ExternalTool>(new ExternalTool(tool)));
|
|
|
|
}
|
2011-01-24 18:14:30 +01:00
|
|
|
tools->insert(tool->id(), tool);
|
|
|
|
(*categoryMenus)[tool->displayCategory()].insert(tool->order(), tool);
|
2011-01-05 17:48:26 +01:00
|
|
|
}
|
2010-11-11 15:47:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalToolManager::menuActivated()
|
|
|
|
{
|
2010-12-08 12:08:21 +01:00
|
|
|
QAction *action = qobject_cast<QAction *>(sender());
|
|
|
|
QTC_ASSERT(action, return);
|
|
|
|
ExternalTool *tool = m_tools.value(action->data().toString());
|
|
|
|
QTC_ASSERT(tool, return);
|
|
|
|
new ExternalToolRunner(tool);
|
2010-11-11 15:47:23 +01:00
|
|
|
}
|
2011-01-06 18:26:09 +01:00
|
|
|
|
2011-01-21 10:58:06 +01:00
|
|
|
QMap<QString, QList<Internal::ExternalTool *> > ExternalToolManager::toolsByCategory() const
|
2011-01-06 18:26:09 +01:00
|
|
|
{
|
|
|
|
return m_categoryMap;
|
|
|
|
}
|
|
|
|
|
2011-01-21 10:58:06 +01:00
|
|
|
QMap<QString, ExternalTool *> ExternalToolManager::toolsById() const
|
|
|
|
{
|
|
|
|
return m_tools;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalToolManager::setToolsByCategory(const QMap<QString, QList<Internal::ExternalTool *> > &tools)
|
|
|
|
{
|
|
|
|
// clear menu
|
|
|
|
ActionManager *am = m_core->actionManager();
|
|
|
|
ActionContainer *mexternaltools = am->actionContainer(Id(Constants::M_TOOLS_EXTERNAL));
|
|
|
|
mexternaltools->clear();
|
|
|
|
|
|
|
|
// delete old tools and create list of new ones
|
|
|
|
QMap<QString, ExternalTool *> newTools;
|
|
|
|
QMap<QString, QAction *> newActions;
|
|
|
|
QMapIterator<QString, QList<ExternalTool *> > it(tools);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
foreach (ExternalTool *tool, it.value()) {
|
|
|
|
const QString &id = tool->id();
|
|
|
|
if (m_tools.value(id) == tool) {
|
|
|
|
newActions.insert(id, m_actions.value(id));
|
|
|
|
// remove from list to prevent deletion
|
|
|
|
m_tools.remove(id);
|
|
|
|
m_actions.remove(id);
|
|
|
|
}
|
|
|
|
newTools.insert(id, tool);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qDeleteAll(m_tools);
|
|
|
|
QMapIterator<QString, QAction *> remainingActions(m_actions);
|
|
|
|
while (remainingActions.hasNext()) {
|
|
|
|
remainingActions.next();
|
|
|
|
am->unregisterAction(remainingActions.value(), Id("Tools.External." + remainingActions.key()));
|
|
|
|
delete remainingActions.value();
|
|
|
|
}
|
|
|
|
m_actions.clear();
|
|
|
|
// assign the new stuff
|
|
|
|
m_tools = newTools;
|
|
|
|
m_actions = newActions;
|
|
|
|
m_categoryMap = tools;
|
|
|
|
// create menu structure and remove no-longer used containers
|
|
|
|
// add all the category menus, QMap is nicely sorted
|
|
|
|
QMap<QString, ActionContainer *> newContainers;
|
|
|
|
it.toFront();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
ActionContainer *container = 0;
|
|
|
|
const QString &containerName = it.key();
|
|
|
|
if (containerName == QString()) { // no displayCategory, so put into external tools menu directly
|
|
|
|
container = mexternaltools;
|
|
|
|
} else {
|
|
|
|
if (m_containers.contains(containerName)) {
|
|
|
|
container = m_containers.take(containerName); // remove to avoid deletion below
|
|
|
|
} else {
|
|
|
|
container = am->createMenu(Id("Tools.External.Category." + containerName));
|
|
|
|
}
|
|
|
|
newContainers.insert(containerName, container);
|
|
|
|
mexternaltools->addMenu(container, Constants::G_DEFAULT_ONE);
|
|
|
|
container->menu()->setTitle(containerName);
|
|
|
|
}
|
|
|
|
foreach (ExternalTool *tool, it.value()) {
|
|
|
|
const QString &toolId = tool->id();
|
|
|
|
// tool action and command
|
|
|
|
QAction *action = 0;
|
|
|
|
Command *command = 0;
|
|
|
|
if (m_actions.contains(toolId)) {
|
|
|
|
action = m_actions.value(toolId);
|
|
|
|
command = am->command(Id("Tools.External." + toolId));
|
|
|
|
} else {
|
|
|
|
action = new QAction(tool->displayName(), this);
|
|
|
|
action->setData(toolId);
|
|
|
|
m_actions.insert(toolId, action);
|
|
|
|
connect(action, SIGNAL(triggered()), this, SLOT(menuActivated()));
|
|
|
|
command = am->registerAction(action, Id("Tools.External." + toolId), Context(Constants::C_GLOBAL));
|
|
|
|
command->setAttribute(Command::CA_UpdateText);
|
|
|
|
}
|
|
|
|
action->setText(tool->displayName());
|
|
|
|
action->setToolTip(tool->description());
|
|
|
|
action->setWhatsThis(tool->description());
|
|
|
|
container->addAction(command, Constants::G_DEFAULT_TWO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the unused containers
|
|
|
|
qDeleteAll(m_containers);
|
|
|
|
// remember the new containers
|
|
|
|
m_containers = newContainers;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalToolManager::readSettings(const QMap<QString, ExternalTool *> &tools,
|
|
|
|
QMap<QString, QMultiMap<int, Internal::ExternalTool*> > *categoryPriorityMap)
|
|
|
|
{
|
|
|
|
QSettings *settings = m_core->settings();
|
|
|
|
settings->beginGroup(QLatin1String("ExternalTools"));
|
|
|
|
|
|
|
|
if (categoryPriorityMap) {
|
|
|
|
settings->beginGroup(QLatin1String("OverrideCategories"));
|
|
|
|
foreach (const QString &id, settings->allKeys()) {
|
|
|
|
if (tools.contains(id)) {
|
|
|
|
const QString &newCategory = settings->value(id).toString();
|
|
|
|
ExternalTool *tool = tools.value(id);
|
|
|
|
if (tool->displayCategory() != newCategory) {
|
|
|
|
(*categoryPriorityMap)[tool->displayCategory()].remove(tool->order(), tool);
|
|
|
|
(*categoryPriorityMap)[newCategory].insert(tool->order(), tool);
|
|
|
|
if (categoryPriorityMap->value(tool->displayCategory()).isEmpty())
|
|
|
|
categoryPriorityMap->remove(tool->displayCategory());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
settings->endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
settings->endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExternalToolManager::writeSettings()
|
|
|
|
{
|
|
|
|
QSettings *settings = m_core->settings();
|
|
|
|
settings->beginGroup(QLatin1String("ExternalTools"));
|
|
|
|
settings->remove(QLatin1String(""));
|
|
|
|
|
|
|
|
settings->beginGroup(QLatin1String("OverrideCategories"));
|
|
|
|
QMapIterator<QString, QList<ExternalTool *> > it(m_categoryMap);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
const QString &category = it.key();
|
|
|
|
foreach (ExternalTool *tool, it.value()) {
|
|
|
|
if (tool->displayCategory() != category)
|
|
|
|
settings->setValue(tool->id(), category);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
settings->endGroup();
|
|
|
|
|
|
|
|
settings->endGroup();
|
|
|
|
}
|