OSDN Git Service

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