1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "toolsettings.h"
36 #include "externaltool.h"
37 #include "coreconstants.h"
39 #include <utils/qtcassert.h>
41 #include <QtCore/QCoreApplication>
42 #include <QtCore/QFileInfo>
43 #include <QtCore/QDir>
44 #include <QtCore/QTime>
49 using namespace Core::Internal;
51 ToolSettings::ToolSettings(QObject *parent) :
56 QString ToolSettings::id() const
58 return QLatin1String(Core::Constants::SETTINGS_ID_TOOLS);
62 QString ToolSettings::displayName() const
64 return tr("External Tools");
68 QString ToolSettings::category() const
70 return QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE);
74 QString ToolSettings::displayCategory() const
76 return QCoreApplication::translate("Core", Core::Constants::SETTINGS_TR_CATEGORY_CORE);
80 QIcon ToolSettings::categoryIcon() const
82 return QIcon(QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE_ICON));
86 bool ToolSettings::matches(const QString & searchKeyWord) const
88 return m_searchKeywords.contains(searchKeyWord, Qt::CaseInsensitive);
91 QWidget *ToolSettings::createPage(QWidget *parent)
93 m_widget = new ExternalToolConfig(parent);
94 m_widget->setTools(ExternalToolManager::instance()->toolsByCategory());
95 if (m_searchKeywords.isEmpty()) {
96 m_searchKeywords = m_widget->searchKeywords();
102 static QString getUserFilePath(const QString &proposalFileName)
104 static bool seeded = false;
105 QDir resourceDir(ICore::instance()->userResourcePath());
106 if (!resourceDir.exists(QLatin1String("externaltools")))
107 resourceDir.mkpath(QLatin1String("externaltools"));
108 QFileInfo fi(proposalFileName);
109 const QString &suffix = QLatin1String(".") + fi.completeSuffix();
110 const QString &newFilePath = ICore::instance()->userResourcePath()
111 + QLatin1String("/externaltools/") + fi.baseName();
113 QString tryPath = newFilePath + suffix;
114 while (QFile::exists(tryPath)) {
120 qsrand(QTime::currentTime().msec());
122 int number = qrand() % 1000;
123 tryPath = newFilePath + QString::number(number) + suffix;
128 static QString idFromDisplayName(const QString &displayName)
130 QString id = displayName;
131 QChar *c = id.data();
132 while (!c->isNull()) {
133 if (!c->isLetterOrNumber())
134 *c = QLatin1Char('_');
140 static QString findUnusedId(const QString &proposal, const QMap<QString, QList<ExternalTool *> > &tools)
146 result = proposal + (number > 0 ? QString::number(number) : QString::fromLatin1(""));
149 QMapIterator<QString, QList<ExternalTool *> > it(tools);
150 while (!found && it.hasNext()) {
152 foreach (ExternalTool *tool, it.value()) {
153 if (tool->id() == result) {
163 void ToolSettings::apply()
168 QMap<QString, ExternalTool *> originalTools = ExternalToolManager::instance()->toolsById();
169 QMap<QString, QList<ExternalTool *> > newToolsMap = m_widget->tools();
170 QMap<QString, QList<ExternalTool *> > resultMap;
171 QMapIterator<QString, QList<ExternalTool *> > it(newToolsMap);
172 while (it.hasNext()) {
174 QList<ExternalTool *> items;
175 foreach (ExternalTool *tool, it.value()) {
176 ExternalTool *toolToAdd = 0;
177 if (ExternalTool *originalTool = originalTools.take(tool->id())) {
178 // check if it has different category and is custom tool
179 if (tool->displayCategory() != it.key() && !tool->preset()) {
180 tool->setDisplayCategory(it.key());
182 // check if the tool has changed
183 if ((*originalTool) == (*tool)) {
184 toolToAdd = originalTool;
186 // case 1: tool is changed preset
187 if (tool->preset() && (*tool) != (*(tool->preset()))) {
188 // check if we need to choose a new file name
189 if (tool->preset()->fileName() == tool->fileName()) {
190 const QString &fileName = QFileInfo(tool->preset()->fileName()).fileName();
191 const QString &newFilePath = getUserFilePath(fileName);
192 // TODO error handling if newFilePath.isEmpty() (i.e. failed to find a unused name)
193 tool->setFileName(newFilePath);
195 // TODO error handling
197 // case 2: tool is previously changed preset but now same as preset
198 } else if (tool->preset() && (*tool) == (*(tool->preset()))) {
199 // check if we need to delete the changed description
200 if (originalTool->fileName() != tool->preset()->fileName()
201 && QFile::exists(originalTool->fileName())) {
202 // TODO error handling
203 QFile::remove(originalTool->fileName());
205 tool->setFileName(tool->preset()->fileName());
206 // no need to save, it's the same as the preset
207 // case 3: tool is custom tool
209 // TODO error handling
213 // 'tool' is deleted by config page, 'originalTool' is deleted by setToolsByCategory
214 toolToAdd = new ExternalTool(tool);
217 // new tool. 'tool' is deleted by config page
218 QString id = idFromDisplayName(tool->displayName());
219 id = findUnusedId(id, newToolsMap);
221 // TODO error handling if newFilePath.isEmpty() (i.e. failed to find a unused name)
222 tool->setFileName(getUserFilePath(id + QLatin1String(".xml")));
223 // TODO error handling
225 toolToAdd = new ExternalTool(tool);
227 items.append(toolToAdd);
229 if (!items.isEmpty())
230 resultMap.insert(it.key(), items);
232 // Remove tools that have been deleted from the settings (and are no preset)
233 foreach (ExternalTool *tool, originalTools) {
234 QTC_ASSERT(!tool->preset(), continue);
235 // TODO error handling
236 QFile::remove(tool->fileName());
239 ExternalToolManager::instance()->setToolsByCategory(resultMap);
243 void ToolSettings::finish()