2011-01-06 18:26:09 +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).
|
2011-01-06 18:26:09 +01:00
|
|
|
**
|
2012-07-19 12:26:56 +02:00
|
|
|
** Contact: http://www.qt-project.org/
|
2011-01-06 18:26:09 +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.
|
2011-01-06 18:26:09 +01:00
|
|
|
**
|
2011-02-21 14:57:44 +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
|
2011-02-21 14:57:44 +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.
|
|
|
|
|
**
|
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>
|
|
|
|
|
#include <QTime>
|
|
|
|
|
#include <QIcon>
|
2011-02-02 12:35:32 +01:00
|
|
|
|
|
|
|
|
#include <QtDebug>
|
2011-01-06 18:26:09 +01:00
|
|
|
|
|
|
|
|
using namespace Core;
|
|
|
|
|
using namespace Core::Internal;
|
|
|
|
|
|
|
|
|
|
ToolSettings::ToolSettings(QObject *parent) :
|
|
|
|
|
IOptionsPage(parent)
|
|
|
|
|
{
|
2012-05-22 11:17:13 +02:00
|
|
|
setId(QLatin1String(Core::Constants::SETTINGS_ID_TOOLS));
|
|
|
|
|
setDisplayName(tr("External Tools"));
|
|
|
|
|
setCategory(QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE));
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ToolSettings::matches(const QString & searchKeyWord) const
|
|
|
|
|
{
|
|
|
|
|
return m_searchKeywords.contains(searchKeyWord, Qt::CaseInsensitive);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *ToolSettings::createPage(QWidget *parent)
|
|
|
|
|
{
|
|
|
|
|
m_widget = new ExternalToolConfig(parent);
|
2011-01-21 10:58:06 +01:00
|
|
|
m_widget->setTools(ExternalToolManager::instance()->toolsByCategory());
|
2011-01-06 18:26:09 +01:00
|
|
|
if (m_searchKeywords.isEmpty()) {
|
|
|
|
|
m_searchKeywords = m_widget->searchKeywords();
|
|
|
|
|
}
|
|
|
|
|
return m_widget;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-02-02 12:35:32 +01:00
|
|
|
static QString getUserFilePath(const QString &proposalFileName)
|
|
|
|
|
{
|
|
|
|
|
static bool seeded = false;
|
2012-01-24 15:36:40 +01:00
|
|
|
QDir resourceDir(ICore::userResourcePath());
|
2011-02-02 12:35:32 +01:00
|
|
|
if (!resourceDir.exists(QLatin1String("externaltools")))
|
|
|
|
|
resourceDir.mkpath(QLatin1String("externaltools"));
|
|
|
|
|
QFileInfo fi(proposalFileName);
|
|
|
|
|
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)) {
|
|
|
|
|
if (count > 15)
|
|
|
|
|
return QString();
|
|
|
|
|
// add random number
|
|
|
|
|
if (!seeded) {
|
|
|
|
|
seeded = true;
|
|
|
|
|
qsrand(QTime::currentTime().msec());
|
|
|
|
|
}
|
|
|
|
|
int number = qrand() % 1000;
|
|
|
|
|
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();
|
2011-01-21 10:58:06 +01:00
|
|
|
QMap<QString, ExternalTool *> originalTools = ExternalToolManager::instance()->toolsById();
|
|
|
|
|
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
|
|
|
|
|
if (tool->displayCategory() != it.key() && !tool->preset()) {
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-21 10:58:06 +01:00
|
|
|
ExternalToolManager::instance()->setToolsByCategory(resultMap);
|
2011-01-06 18:26:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ToolSettings::finish()
|
|
|
|
|
{
|
|
|
|
|
}
|