OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / tasklist / tasklistplugin.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 "tasklistplugin.h"
34
35 #include "stopmonitoringhandler.h"
36 #include "taskfile.h"
37 #include "taskfilefactory.h"
38 #include "tasklistconstants.h"
39
40 #include <coreplugin/icore.h>
41 #include <coreplugin/mimedatabase.h>
42 #include <extensionsystem/pluginmanager.h>
43 #include <projectexplorer/project.h>
44 #include <projectexplorer/task.h>
45 #include <projectexplorer/taskhub.h>
46
47 #include <QtCore/QDir>
48 #include <QtCore/QStringList>
49 #include <QtCore/QtPlugin>
50
51 namespace {
52
53 ProjectExplorer::Task::TaskType typeFrom(const QString &typeName)
54 {
55     ProjectExplorer::Task::TaskType type = ProjectExplorer::Task::Unknown;
56     QString tmp = typeName.toLower();
57     if (tmp.startsWith(QLatin1String("warn")))
58         type = ProjectExplorer::Task::Warning;
59     else if (tmp.startsWith(QLatin1String("err")))
60         type = ProjectExplorer::Task::Error;
61     return type;
62 }
63
64 } // namespace
65
66 using namespace TaskList;
67
68 TaskListPlugin *TaskListPlugin::m_instance = 0;
69
70 // --------------------------------------------------------------------------
71 // TaskListPluginPrivate
72 // --------------------------------------------------------------------------
73
74 class Internal::TaskListPluginPrivate {
75 public:
76     bool parseTaskFile(ProjectExplorer::Project *context, const QString &name)
77     {
78         QFile tf(name);
79         if (!tf.open(QIODevice::ReadOnly))
80             return false;
81
82         while (!tf.atEnd())
83         {
84             QStringList chunks = parseRawLine(tf.readLine());
85             if (chunks.isEmpty())
86                 continue;
87
88             QString description;
89             QString file;
90             ProjectExplorer::Task::TaskType type = ProjectExplorer::Task::Unknown;
91             int line = -1;
92
93             if (chunks.count() == 1) {
94                 description = chunks.at(0);
95             } else if (chunks.count() == 2) {
96                 type = typeFrom(chunks.at(0));
97                 description = chunks.at(1);
98             } else if (chunks.count() == 3) {
99                 file = chunks.at(0);
100                 type = typeFrom(chunks.at(1));
101                 description = chunks.at(2);
102             } else if (chunks.count() >= 4) {
103                 file = chunks.at(0);
104                 bool ok;
105                 line = chunks.at(1).toInt(&ok);
106                 if (!ok)
107                     line = -1;
108                 type = typeFrom(chunks.at(2));
109                 description = chunks.at(3);
110             }
111             if (!file.isEmpty()) {
112                 file = QDir::fromNativeSeparators(file);
113                 QFileInfo fi(file);
114                 if (fi.isRelative() && context) {
115                     QString fullPath = context->projectDirectory() + '/' + file;
116                     fi.setFile(fullPath);
117                     file = fi.absoluteFilePath();
118                 }
119             }
120
121             hub->addTask(ProjectExplorer::Task(type, description, file, line, QLatin1String(Constants::TASKLISTTASK_ID)));
122         }
123         return true;
124     }
125
126     QStringList parseRawLine(const QByteArray &raw)
127     {
128         QStringList result;
129         QString line = QString::fromUtf8(raw.constData());
130         if (line.startsWith(QChar('#')))
131             return result;
132
133         result = line.split(QChar('\t'));
134         for (int i = 0; i < result.count(); ++i)
135             result[i] = unescape(result.at(i));
136
137         return result;
138     }
139
140     QString unescape(const QString &input) const
141     {
142         QString result;
143         for (int i = 0; i < input.count(); ++i) {
144             if (input.at(i) == QChar('\\')) {
145                 if (i == input.count() - 1)
146                     continue;
147                 if (input.at(i + 1) == QChar('n')) {
148                     result.append(QChar('\n'));
149                     ++i;
150                     continue;
151                 } else if (input.at(i + 1) == QChar('t')) {
152                     result.append(QChar('\t'));
153                     ++i;
154                     continue;
155                 } else if (input.at(i + 1) == QChar('\\')) {
156                     result.append(QChar('\\'));
157                     ++i;
158                     continue;
159                 }
160                 continue;
161             }
162             result.append(input.at(i));
163         }
164         return result;
165     }
166
167     ProjectExplorer::TaskHub *hub;
168     TaskFileFactory *fileFactory;
169 };
170
171 // --------------------------------------------------------------------------
172 // TaskListPlugin
173 // --------------------------------------------------------------------------
174
175 TaskListPlugin::TaskListPlugin() :
176     d(new Internal::TaskListPluginPrivate)
177 {
178     m_instance = this;
179 }
180
181 TaskListPlugin::~TaskListPlugin()
182 {
183     delete d;
184 }
185
186 TaskListPlugin *TaskListPlugin::instance()
187 {
188     return m_instance;
189 }
190
191 bool TaskListPlugin::initialize(const QStringList &arguments, QString *errorMessage)
192 {
193     Q_UNUSED(arguments)
194
195     ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
196     d->hub = pm->getObject<ProjectExplorer::TaskHub>();
197
198     //: Category under which tasklist tasks are listed in build issues view
199     d->hub->addCategory(QLatin1String(Constants::TASKLISTTASK_ID), tr("My Tasks"));
200
201     Core::ICore *core = Core::ICore::instance();
202     if (!core->mimeDatabase()->addMimeTypes(QLatin1String(":tasklist/TaskList.mimetypes.xml"), errorMessage))
203         return false;
204
205     d->fileFactory = new Internal::TaskFileFactory(this);
206     addAutoReleasedObject(d->fileFactory);
207     addAutoReleasedObject(new Internal::StopMonitoringHandler);
208     return true;
209 }
210
211 void TaskListPlugin::extensionsInitialized()
212 { }
213
214 bool TaskListPlugin::loadFile(ProjectExplorer::Project *context, const QString &fileName)
215 {
216     clearTasks();
217     return d->parseTaskFile(context, fileName);
218 }
219
220 bool TaskListPlugin::monitorFile(ProjectExplorer::Project *context, const QString &fileName)
221 {
222     return d->fileFactory->open(context, fileName);
223 }
224
225 void TaskListPlugin::stopMonitoring()
226 {
227     d->fileFactory->closeAllFiles();
228 }
229
230 void TaskListPlugin::clearTasks()
231 {
232     d->hub->clearTasks(QLatin1String(Constants::TASKLISTTASK_ID));
233 }
234
235 Q_EXPORT_PLUGIN(TaskListPlugin)