1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "toolsettings.h"
35 #include "externaltool.h"
36 #include "coreconstants.h"
38 #include <utils/qtcassert.h>
40 #include <QtCore/QCoreApplication>
41 #include <QtCore/QFileInfo>
42 #include <QtCore/QDir>
43 #include <QtCore/QTime>
48 using namespace Core::Internal;
50 ToolSettings::ToolSettings(QObject *parent) :
55 QString ToolSettings::id() const
57 return QLatin1String(Core::Constants::SETTINGS_ID_TOOLS);
61 QString ToolSettings::displayName() const
63 return tr("External Tools");
67 QString ToolSettings::category() const
69 return QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE);
73 QString ToolSettings::displayCategory() const
75 return QCoreApplication::translate("Core", Core::Constants::SETTINGS_TR_CATEGORY_CORE);
79 QIcon ToolSettings::categoryIcon() const
81 return QIcon(QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE_ICON));
85 bool ToolSettings::matches(const QString & searchKeyWord) const
87 return m_searchKeywords.contains(searchKeyWord, Qt::CaseInsensitive);
90 QWidget *ToolSettings::createPage(QWidget *parent)
92 m_widget = new ExternalToolConfig(parent);
93 m_widget->setTools(ExternalToolManager::instance()->toolsByCategory());
94 if (m_searchKeywords.isEmpty()) {
95 m_searchKeywords = m_widget->searchKeywords();
101 static QString getUserFilePath(const QString &proposalFileName)
103 static bool seeded = false;
104 QDir resourceDir(ICore::instance()->userResourcePath());
105 if (!resourceDir.exists(QLatin1String("externaltools")))
106 resourceDir.mkpath(QLatin1String("externaltools"));
107 QFileInfo fi(proposalFileName);
108 const QString &suffix = QLatin1String(".") + fi.completeSuffix();
109 const QString &newFilePath = ICore::instance()->userResourcePath()
110 + QLatin1String("/externaltools/") + fi.baseName();
112 QString tryPath = newFilePath + suffix;
113 while (QFile::exists(tryPath)) {
119 qsrand(QTime::currentTime().msec());
121 int number = qrand() % 1000;
122 tryPath = newFilePath + QString::number(number) + suffix;
127 static QString idFromDisplayName(const QString &displayName)
129 QString id = displayName;
130 QChar *c = id.data();
131 while (!c->isNull()) {
132 if (!c->isLetterOrNumber())
133 *c = QLatin1Char('_');
139 static QString findUnusedId(const QString &proposal, const QMap<QString, QList<ExternalTool *> > &tools)
145 result = proposal + (number > 0 ? QString::number(number) : QString::fromLatin1(""));
148 QMapIterator<QString, QList<ExternalTool *> > it(tools);
149 while (!found && it.hasNext()) {
151 foreach (ExternalTool *tool, it.value()) {
152 if (tool->id() == result) {
162 void ToolSettings::apply()
167 QMap<QString, ExternalTool *> originalTools = ExternalToolManager::instance()->toolsById();
168 QMap<QString, QList<ExternalTool *> > newToolsMap = m_widget->tools();
169 QMap<QString, QList<ExternalTool *> > resultMap;
170 QMapIterator<QString, QList<ExternalTool *> > it(newToolsMap);
171 while (it.hasNext()) {
173 QList<ExternalTool *> items;
174 foreach (ExternalTool *tool, it.value()) {
175 ExternalTool *toolToAdd = 0;
176 if (ExternalTool *originalTool = originalTools.take(tool->id())) {
177 // check if it has different category and is custom tool
178 if (tool->displayCategory() != it.key() && !tool->preset()) {
179 tool->setDisplayCategory(it.key());
181 // check if the tool has changed
182 if ((*originalTool) == (*tool)) {
183 toolToAdd = originalTool;
185 // case 1: tool is changed preset
186 if (tool->preset() && (*tool) != (*(tool->preset()))) {
187 // check if we need to choose a new file name
188 if (tool->preset()->fileName() == tool->fileName()) {
189 const QString &fileName = QFileInfo(tool->preset()->fileName()).fileName();
190 const QString &newFilePath = getUserFilePath(fileName);
191 // TODO error handling if newFilePath.isEmpty() (i.e. failed to find a unused name)
192 tool->setFileName(newFilePath);
194 // TODO error handling
196 // case 2: tool is previously changed preset but now same as preset
197 } else if (tool->preset() && (*tool) == (*(tool->preset()))) {
198 // check if we need to delete the changed description
199 if (originalTool->fileName() != tool->preset()->fileName()
200 && QFile::exists(originalTool->fileName())) {
201 // TODO error handling
202 QFile::remove(originalTool->fileName());
204 tool->setFileName(tool->preset()->fileName());
205 // no need to save, it's the same as the preset
206 // case 3: tool is custom tool
208 // TODO error handling
212 // 'tool' is deleted by config page, 'originalTool' is deleted by setToolsByCategory
213 toolToAdd = new ExternalTool(tool);
216 // new tool. 'tool' is deleted by config page
217 QString id = idFromDisplayName(tool->displayName());
218 id = findUnusedId(id, newToolsMap);
220 // TODO error handling if newFilePath.isEmpty() (i.e. failed to find a unused name)
221 tool->setFileName(getUserFilePath(id + QLatin1String(".xml")));
222 // TODO error handling
224 toolToAdd = new ExternalTool(tool);
226 items.append(toolToAdd);
228 if (!items.isEmpty())
229 resultMap.insert(it.key(), items);
231 // Remove tools that have been deleted from the settings (and are no preset)
232 foreach (ExternalTool *tool, originalTools) {
233 QTC_ASSERT(!tool->preset(), continue);
234 // TODO error handling
235 QFile::remove(tool->fileName());
238 ExternalToolManager::instance()->setToolsByCategory(resultMap);
242 void ToolSettings::finish()