tasklistplugin.cpp 6.31 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Tobias Hunger's avatar
Tobias Hunger committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Tobias Hunger's avatar
Tobias Hunger committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Tobias Hunger's avatar
Tobias Hunger committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
Tobias Hunger's avatar
Tobias Hunger committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Tobias Hunger's avatar
Tobias Hunger committed
29 30 31

#include "tasklistplugin.h"

Tobias Hunger's avatar
Tobias Hunger committed
32 33
#include "stopmonitoringhandler.h"
#include "taskfile.h"
Tobias Hunger's avatar
Tobias Hunger committed
34
#include "taskfilefactory.h"
Tobias Hunger's avatar
Tobias Hunger committed
35
#include "tasklistconstants.h"
Tobias Hunger's avatar
Tobias Hunger committed
36 37 38

#include <coreplugin/icore.h>
#include <coreplugin/mimedatabase.h>
Tobias Hunger's avatar
Tobias Hunger committed
39
#include <projectexplorer/projectexplorer.h>
Tobias Hunger's avatar
Tobias Hunger committed
40 41 42
#include <projectexplorer/project.h>
#include <projectexplorer/task.h>
#include <projectexplorer/taskhub.h>
Tobias Hunger's avatar
Tobias Hunger committed
43

44 45 46
#include <QDir>
#include <QStringList>
#include <QtPlugin>
Tobias Hunger's avatar
Tobias Hunger committed
47

48 49
using namespace ProjectExplorer;
using namespace TaskList::Internal;
Tobias Hunger's avatar
Tobias Hunger committed
50

51 52 53
namespace TaskList {

static Task::TaskType typeFrom(const QString &typeName)
Tobias Hunger's avatar
Tobias Hunger committed
54
{
55
    Task::TaskType type = Task::Unknown;
Tobias Hunger's avatar
Tobias Hunger committed
56 57
    QString tmp = typeName.toLower();
    if (tmp.startsWith(QLatin1String("warn")))
58
        type = Task::Warning;
Tobias Hunger's avatar
Tobias Hunger committed
59
    else if (tmp.startsWith(QLatin1String("err")))
60
        type = Task::Error;
Tobias Hunger's avatar
Tobias Hunger committed
61 62 63
    return type;
}

64 65 66 67 68 69
static QStringList parseRawLine(const QByteArray &raw)
{
    QStringList result;
    QString line = QString::fromUtf8(raw.constData());
    if (line.startsWith(QLatin1Char('#')))
        return result;
Tobias Hunger's avatar
Tobias Hunger committed
70

71 72
    return line.split(QLatin1Char('\t'));
}
Tobias Hunger's avatar
Tobias Hunger committed
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
static QString unescape(const QString &input)
{
    QString result;
    for (int i = 0; i < input.count(); ++i) {
        if (input.at(i) == QLatin1Char('\\')) {
            if (i == input.count() - 1)
                continue;
            if (input.at(i + 1) == QLatin1Char('n')) {
                result.append(QLatin1Char('\n'));
                ++i;
                continue;
            } else if (input.at(i + 1) == QLatin1Char('t')) {
                result.append(QLatin1Char('\t'));
                ++i;
                continue;
            } else if (input.at(i + 1) == QLatin1Char('\\')) {
                result.append(QLatin1Char('\\'));
                ++i;
Tobias Hunger's avatar
Tobias Hunger committed
92 93
                continue;
            }
94
            continue;
Tobias Hunger's avatar
Tobias Hunger committed
95
        }
96
        result.append(input.at(i));
Tobias Hunger's avatar
Tobias Hunger committed
97
    }
98 99
    return result;
}
Tobias Hunger's avatar
Tobias Hunger committed
100

101 102 103 104 105 106 107
static bool parseTaskFile(QString *errorString, Project *context, const QString &name)
{
    QFile tf(name);
    if (!tf.open(QIODevice::ReadOnly)) {
        *errorString = TaskListPlugin::tr("Cannot open task file %1: %2").arg(
                QDir::toNativeSeparators(name), tf.errorString());
        return false;
Tobias Hunger's avatar
Tobias Hunger committed
108 109
    }

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    while (!tf.atEnd()) {
        QStringList chunks = parseRawLine(tf.readLine());
        if (chunks.isEmpty())
            continue;

        QString description;
        QString file;
        Task::TaskType type = Task::Unknown;
        int line = -1;

        if (chunks.count() == 1) {
            description = chunks.at(0);
        } else if (chunks.count() == 2) {
            type = typeFrom(chunks.at(0));
            description = chunks.at(1);
        } else if (chunks.count() == 3) {
            file = chunks.at(0);
            type = typeFrom(chunks.at(1));
            description = chunks.at(2);
        } else if (chunks.count() >= 4) {
            file = chunks.at(0);
            bool ok;
            line = chunks.at(1).toInt(&ok);
            if (!ok)
                line = -1;
            type = typeFrom(chunks.at(2));
            description = chunks.at(3);
        }
        if (!file.isEmpty()) {
            file = QDir::fromNativeSeparators(file);
            QFileInfo fi(file);
            if (fi.isRelative() && context) {
                QString fullPath = context->projectDirectory() + QLatin1Char('/') + file;
                fi.setFile(fullPath);
                file = fi.absoluteFilePath();
Tobias Hunger's avatar
Tobias Hunger committed
145 146
            }
        }
147
        description = unescape(description);
Tobias Hunger's avatar
Tobias Hunger committed
148

149 150 151 152 153
        TaskHub::addTask(type, description, Constants::TASKLISTTASK_ID,
                         Utils::FileName::fromUserInput(file), line);
    }
    return true;
}
Tobias Hunger's avatar
Tobias Hunger committed
154 155 156 157 158

// --------------------------------------------------------------------------
// TaskListPlugin
// --------------------------------------------------------------------------

159
static TaskFileFactory *m_fileFactory = 0;
Tobias Hunger's avatar
Tobias Hunger committed
160

Tobias Hunger's avatar
Tobias Hunger committed
161 162 163 164
bool TaskListPlugin::initialize(const QStringList &arguments, QString *errorMessage)
{
    Q_UNUSED(arguments)

165
    //: Category under which tasklist tasks are listed in Issues view
166
    TaskHub::addCategory(Constants::TASKLISTTASK_ID, tr("My Tasks"));
Tobias Hunger's avatar
Tobias Hunger committed
167

hjk's avatar
hjk committed
168
    if (!Core::ICore::mimeDatabase()->addMimeTypes(QLatin1String(":tasklist/TaskList.mimetypes.xml"), errorMessage))
Tobias Hunger's avatar
Tobias Hunger committed
169 170
        return false;

171 172 173
    m_fileFactory = new TaskFileFactory(this);
    addAutoReleasedObject(m_fileFactory);
    addAutoReleasedObject(new StopMonitoringHandler);
Tobias Hunger's avatar
Tobias Hunger committed
174 175 176
    return true;
}

177
bool TaskListPlugin::loadFile(QString *errorString, Project *context, const QString &fileName)
Tobias Hunger's avatar
Tobias Hunger committed
178 179
{
    clearTasks();
180
    return parseTaskFile(errorString, context, fileName);
Tobias Hunger's avatar
Tobias Hunger committed
181 182
}

183
bool TaskListPlugin::monitorFile(Project *context, const QString &fileName)
Tobias Hunger's avatar
Tobias Hunger committed
184
{
185
    return m_fileFactory->open(context, fileName);
Tobias Hunger's avatar
Tobias Hunger committed
186 187 188 189
}

void TaskListPlugin::stopMonitoring()
{
190
    m_fileFactory->closeAllFiles();
Tobias Hunger's avatar
Tobias Hunger committed
191 192 193 194
}

void TaskListPlugin::clearTasks()
{
195
    TaskHub::clearTasks(Constants::TASKLISTTASK_ID);
Tobias Hunger's avatar
Tobias Hunger committed
196 197
}

198 199
} // namespace TaskList

200
Q_EXPORT_PLUGIN(TaskList::TaskListPlugin)