OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / coreplugin / toolsettings.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "toolsettings.h"
34
35 #include "externaltool.h"
36 #include "coreconstants.h"
37
38 #include <utils/qtcassert.h>
39
40 #include <QtCore/QCoreApplication>
41 #include <QtCore/QFileInfo>
42 #include <QtCore/QDir>
43 #include <QtCore/QTime>
44
45 #include <QtDebug>
46
47 using namespace Core;
48 using namespace Core::Internal;
49
50 ToolSettings::ToolSettings(QObject *parent) :
51     IOptionsPage(parent)
52 {
53 }
54
55 QString ToolSettings::id() const
56 {
57     return QLatin1String(Core::Constants::SETTINGS_ID_TOOLS);
58 }
59
60
61 QString ToolSettings::displayName() const
62 {
63     return tr("External Tools");
64 }
65
66
67 QString ToolSettings::category() const
68 {
69     return QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE);
70 }
71
72
73 QString ToolSettings::displayCategory() const
74 {
75     return QCoreApplication::translate("Core", Core::Constants::SETTINGS_TR_CATEGORY_CORE);
76 }
77
78
79 QIcon ToolSettings::categoryIcon() const
80 {
81     return QIcon(QLatin1String(Core::Constants::SETTINGS_CATEGORY_CORE_ICON));
82 }
83
84
85 bool ToolSettings::matches(const QString & searchKeyWord) const
86 {
87     return m_searchKeywords.contains(searchKeyWord, Qt::CaseInsensitive);
88 }
89
90 QWidget *ToolSettings::createPage(QWidget *parent)
91 {
92     m_widget = new ExternalToolConfig(parent);
93     m_widget->setTools(ExternalToolManager::instance()->toolsByCategory());
94     if (m_searchKeywords.isEmpty()) {
95         m_searchKeywords = m_widget->searchKeywords();
96     }
97     return m_widget;
98 }
99
100
101 static QString getUserFilePath(const QString &proposalFileName)
102 {
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();
111     int count = 0;
112     QString tryPath = newFilePath + suffix;
113     while (QFile::exists(tryPath)) {
114         if (count > 15)
115             return QString();
116         // add random number
117         if (!seeded) {
118             seeded = true;
119             qsrand(QTime::currentTime().msec());
120         }
121         int number = qrand() % 1000;
122         tryPath = newFilePath + QString::number(number) + suffix;
123     }
124     return tryPath;
125 }
126
127 static QString idFromDisplayName(const QString &displayName)
128 {
129     QString id = displayName;
130     QChar *c = id.data();
131     while (!c->isNull()) {
132         if (!c->isLetterOrNumber())
133             *c = QLatin1Char('_');
134         ++c;
135     }
136     return id;
137 }
138
139 static QString findUnusedId(const QString &proposal, const QMap<QString, QList<ExternalTool *> > &tools)
140 {
141     int number = 0;
142     QString result;
143     bool found = false;
144     do {
145         result = proposal + (number > 0 ? QString::number(number) : QString::fromLatin1(""));
146         ++number;
147         found = false;
148         QMapIterator<QString, QList<ExternalTool *> > it(tools);
149         while (!found && it.hasNext()) {
150             it.next();
151             foreach (ExternalTool *tool, it.value()) {
152                 if (tool->id() == result) {
153                     found = true;
154                     break;
155                 }
156             }
157         }
158     } while (found);
159     return result;
160 }
161
162 void ToolSettings::apply()
163 {
164     if (!m_widget)
165         return;
166     m_widget->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()) {
172         it.next();
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());
180                 }
181                 // check if the tool has changed
182                 if ((*originalTool) == (*tool)) {
183                     toolToAdd = originalTool;
184                 } else {
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);
193                         }
194                         // TODO error handling
195                         tool->save();
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());
203                         }
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
207                     } else {
208                         // TODO error handling
209                         tool->save();
210                     }
211
212                      // 'tool' is deleted by config page, 'originalTool' is deleted by setToolsByCategory
213                     toolToAdd = new ExternalTool(tool);
214                 }
215             } else {
216                 // new tool. 'tool' is deleted by config page
217                 QString id = idFromDisplayName(tool->displayName());
218                 id = findUnusedId(id, newToolsMap);
219                 tool->setId(id);
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
223                 tool->save();
224                 toolToAdd = new ExternalTool(tool);
225             }
226             items.append(toolToAdd);
227         }
228         if (!items.isEmpty())
229             resultMap.insert(it.key(), items);
230     }
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());
236     }
237
238     ExternalToolManager::instance()->setToolsByCategory(resultMap);
239 }
240
241
242 void ToolSettings::finish()
243 {
244 }