2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-01-06 18:26:09 +01:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-01-06 18:26:09 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-01-06 18:26:09 +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
|
|
|
|
|
** 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.
|
2011-01-06 18:26:09 +01:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2011-02-21 14:57:44 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-01-06 18:26:09 +01:00
|
|
|
|
|
|
|
|
#include "toolsettings.h"
|
2011-08-17 12:54:58 +02:00
|
|
|
#include "dialogs/externaltoolconfig.h"
|
2011-01-06 18:26:09 +01:00
|
|
|
#include "externaltool.h"
|
2011-08-17 12:54:58 +02:00
|
|
|
#include "externaltoolmanager.h"
|
2011-01-06 18:26:09 +01:00
|
|
|
#include "coreconstants.h"
|
2011-08-17 12:54:58 +02:00
|
|
|
#include "icore.h"
|
2011-01-06 18:26:09 +01:00
|
|
|
|
2011-02-21 15:27:56 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QDir>
|
2011-02-02 12:35:32 +01:00
|
|
|
|
2012-08-06 13:42:46 +02:00
|
|
|
#include <QDebug>
|
2011-01-06 18:26:09 +01:00
|
|
|
|
|
|
|
|
using namespace Core;
|
|
|
|
|
using namespace Core::Internal;
|
|
|
|
|
|
|
|
|
|
ToolSettings::ToolSettings(QObject *parent) :
|
|
|
|
|
IOptionsPage(parent)
|
|
|
|
|
{
|
2013-01-16 15:22:58 +01:00
|
|
|
setId(Core::Constants::SETTINGS_ID_TOOLS);
|
2012-05-22 11:17:13 +02:00
|
|
|
setDisplayName(tr("External Tools"));
|
2012-12-29 03:37:27 +01:00
|
|
|
setCategory(Core::Constants::SETTINGS_CATEGORY_CORE);
|
2012-05-22 11:17:13 +02:00
|
|
|
setDisplayCategory(QCoreApplication::translate("Core", Constants::SETTINGS_TR_CATEGORY_CORE));
|
|
|
|
|
setCategoryIcon(QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE_ICON));
|
2011-01-06 18:26:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-12-03 14:17:03 +01:00
|
|
|
QWidget *ToolSettings::widget()
|
2011-01-06 18:26:09 +01:00
|
|
|
{
|
2013-12-03 14:17:03 +01:00
|
|
|
if (!m_widget) {
|
|
|
|
|
m_widget = new ExternalToolConfig;
|
|
|
|
|
m_widget->setTools(ExternalToolManager::toolsByCategory());
|
|
|
|
|
}
|
2011-01-06 18:26:09 +01:00
|
|
|
return m_widget;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-02 12:35:32 +01:00
|
|
|
static QString getUserFilePath(const QString &proposalFileName)
|
|
|
|
|
{
|
2014-02-02 20:31:55 +01:00
|
|
|
const QDir resourceDir(ICore::userResourcePath());
|
2011-02-02 12:35:32 +01:00
|
|
|
if (!resourceDir.exists(QLatin1String("externaltools")))
|
|
|
|
|
resourceDir.mkpath(QLatin1String("externaltools"));
|
2014-02-02 20:31:55 +01:00
|
|
|
const QFileInfo fi(proposalFileName);
|
2011-02-02 12:35:32 +01:00
|
|
|
const QString &suffix = QLatin1String(".") + fi.completeSuffix();
|
2012-01-24 15:36:40 +01:00
|
|
|
const QString &newFilePath = ICore::userResourcePath()
|
2011-02-02 12:35:32 +01:00
|
|
|
+ QLatin1String("/externaltools/") + fi.baseName();
|
|
|
|
|
int count = 0;
|
|
|
|
|
QString tryPath = newFilePath + suffix;
|
|
|
|
|
while (QFile::exists(tryPath)) {
|
2014-02-02 20:31:55 +01:00
|
|
|
if (++count > 15)
|
2011-02-02 12:35:32 +01:00
|
|
|
return QString();
|
|
|
|
|
// add random number
|
2014-02-02 20:31:55 +01:00
|
|
|
const int number = qrand() % 1000;
|
2011-02-02 12:35:32 +01:00
|
|
|
tryPath = newFilePath + QString::number(number) + suffix;
|
|
|
|
|
}
|
|
|
|
|
return tryPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QString idFromDisplayName(const QString &displayName)
|
|
|
|
|
{
|
|
|
|
|
QString id = displayName;
|
|
|
|
|
QChar *c = id.data();
|
|
|
|
|
while (!c->isNull()) {
|
|
|
|
|
if (!c->isLetterOrNumber())
|
|
|
|
|
*c = QLatin1Char('_');
|
|
|
|
|
++c;
|
|
|
|
|
}
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QString findUnusedId(const QString &proposal, const QMap<QString, QList<ExternalTool *> > &tools)
|
|
|
|
|
{
|
|
|
|
|
int number = 0;
|
|
|
|
|
QString result;
|
|
|
|
|
bool found = false;
|
|
|
|
|
do {
|
|
|
|
|
result = proposal + (number > 0 ? QString::number(number) : QString::fromLatin1(""));
|
|
|
|
|
++number;
|
|
|
|
|
found = false;
|
|
|
|
|
QMapIterator<QString, QList<ExternalTool *> > it(tools);
|
|
|
|
|
while (!found && it.hasNext()) {
|
|
|
|
|
it.next();
|
|
|
|
|
foreach (ExternalTool *tool, it.value()) {
|
2011-02-02 17:04:30 +01:00
|
|
|
if (tool->id() == result) {
|
2011-02-02 12:35:32 +01:00
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while (found);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-06 18:26:09 +01:00
|
|
|
void ToolSettings::apply()
|
|
|
|
|
{
|
2011-01-21 10:58:06 +01:00
|
|
|
if (!m_widget)
|
|
|
|
|
return;
|
2011-01-24 15:01:48 +01:00
|
|
|
m_widget->apply();
|
2013-09-05 18:48:06 +02:00
|
|
|
QMap<QString, ExternalTool *> originalTools = ExternalToolManager::toolsById();
|
2011-01-21 10:58:06 +01:00
|
|
|
QMap<QString, QList<ExternalTool *> > newToolsMap = m_widget->tools();
|
|
|
|
|
QMap<QString, QList<ExternalTool *> > resultMap;
|
|
|
|
|
QMapIterator<QString, QList<ExternalTool *> > it(newToolsMap);
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
it.next();
|
|
|
|
|
QList<ExternalTool *> items;
|
|
|
|
|
foreach (ExternalTool *tool, it.value()) {
|
|
|
|
|
ExternalTool *toolToAdd = 0;
|
|
|
|
|
if (ExternalTool *originalTool = originalTools.take(tool->id())) {
|
2011-02-10 10:03:46 +01:00
|
|
|
// check if it has different category and is custom tool
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (tool->displayCategory() != it.key() && !tool->preset())
|
2011-02-10 10:03:46 +01:00
|
|
|
tool->setDisplayCategory(it.key());
|
2011-01-21 10:58:06 +01:00
|
|
|
// check if the tool has changed
|
|
|
|
|
if ((*originalTool) == (*tool)) {
|
|
|
|
|
toolToAdd = originalTool;
|
|
|
|
|
} else {
|
2011-02-01 15:55:40 +01:00
|
|
|
// case 1: tool is changed preset
|
|
|
|
|
if (tool->preset() && (*tool) != (*(tool->preset()))) {
|
|
|
|
|
// check if we need to choose a new file name
|
|
|
|
|
if (tool->preset()->fileName() == tool->fileName()) {
|
|
|
|
|
const QString &fileName = QFileInfo(tool->preset()->fileName()).fileName();
|
2011-02-02 12:35:32 +01:00
|
|
|
const QString &newFilePath = getUserFilePath(fileName);
|
|
|
|
|
// TODO error handling if newFilePath.isEmpty() (i.e. failed to find a unused name)
|
2011-02-01 15:55:40 +01:00
|
|
|
tool->setFileName(newFilePath);
|
|
|
|
|
}
|
|
|
|
|
// TODO error handling
|
|
|
|
|
tool->save();
|
|
|
|
|
// case 2: tool is previously changed preset but now same as preset
|
|
|
|
|
} else if (tool->preset() && (*tool) == (*(tool->preset()))) {
|
|
|
|
|
// check if we need to delete the changed description
|
|
|
|
|
if (originalTool->fileName() != tool->preset()->fileName()
|
|
|
|
|
&& QFile::exists(originalTool->fileName())) {
|
|
|
|
|
// TODO error handling
|
|
|
|
|
QFile::remove(originalTool->fileName());
|
|
|
|
|
}
|
|
|
|
|
tool->setFileName(tool->preset()->fileName());
|
|
|
|
|
// no need to save, it's the same as the preset
|
|
|
|
|
// case 3: tool is custom tool
|
|
|
|
|
} else {
|
|
|
|
|
// TODO error handling
|
|
|
|
|
tool->save();
|
2011-01-24 18:14:30 +01:00
|
|
|
}
|
2011-02-01 15:55:40 +01:00
|
|
|
|
|
|
|
|
// 'tool' is deleted by config page, 'originalTool' is deleted by setToolsByCategory
|
2011-01-21 10:58:06 +01:00
|
|
|
toolToAdd = new ExternalTool(tool);
|
|
|
|
|
}
|
2011-02-02 12:35:32 +01:00
|
|
|
} else {
|
|
|
|
|
// new tool. 'tool' is deleted by config page
|
|
|
|
|
QString id = idFromDisplayName(tool->displayName());
|
|
|
|
|
id = findUnusedId(id, newToolsMap);
|
|
|
|
|
tool->setId(id);
|
|
|
|
|
// TODO error handling if newFilePath.isEmpty() (i.e. failed to find a unused name)
|
|
|
|
|
tool->setFileName(getUserFilePath(id + QLatin1String(".xml")));
|
|
|
|
|
// TODO error handling
|
|
|
|
|
tool->save();
|
|
|
|
|
toolToAdd = new ExternalTool(tool);
|
2011-01-21 10:58:06 +01:00
|
|
|
}
|
|
|
|
|
items.append(toolToAdd);
|
|
|
|
|
}
|
2011-02-02 17:04:30 +01:00
|
|
|
if (!items.isEmpty())
|
|
|
|
|
resultMap.insert(it.key(), items);
|
2011-01-21 10:58:06 +01:00
|
|
|
}
|
2011-02-21 15:27:56 +01:00
|
|
|
// Remove tools that have been deleted from the settings (and are no preset)
|
|
|
|
|
foreach (ExternalTool *tool, originalTools) {
|
|
|
|
|
QTC_ASSERT(!tool->preset(), continue);
|
|
|
|
|
// TODO error handling
|
|
|
|
|
QFile::remove(tool->fileName());
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-05 18:48:06 +02:00
|
|
|
ExternalToolManager::setToolsByCategory(resultMap);
|
2011-01-06 18:26:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ToolSettings::finish()
|
|
|
|
|
{
|
2013-12-03 14:17:03 +01:00
|
|
|
delete m_widget;
|
2011-01-06 18:26:09 +01:00
|
|
|
}
|