aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/projectexplorer/taskfile.cpp
blob: 6375d12ee7e9e2f12736b4d9c7a003c107aa79a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "taskfile.h"

#include "projectexplorer.h"
#include "projectexplorerconstants.h"
#include "projectexplorertr.h"
#include "session.h"
#include "taskhub.h"

#include <coreplugin/documentmanager.h>
#include <coreplugin/icore.h>

#include <utils/algorithm.h>
#include <utils/filepath.h>
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>

#include <QAction>
#include <QMessageBox>

using namespace Utils;

namespace ProjectExplorer {
namespace Internal {

// --------------------------------------------------------------------------
// TaskFile
// --------------------------------------------------------------------------

QList<TaskFile *> TaskFile::openFiles;

TaskFile::TaskFile(QObject *parent) : Core::IDocument(parent)
{
    setId("TaskList.TaskFile");
}

Core::IDocument::ReloadBehavior TaskFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
{
    Q_UNUSED(state)
    Q_UNUSED(type)
    return BehaviorSilent;
}

bool TaskFile::reload(QString *errorString, ReloadFlag flag, ChangeType type)
{
    Q_UNUSED(flag)

    if (type == TypeRemoved) {
        deleteLater();
        return true;
    }
    return load(errorString, filePath());
}

static Task::TaskType typeFrom(const QString &typeName)
{
    QString tmp = typeName.toLower();
    if (tmp.startsWith("warn"))
        return Task::Warning;
    if (tmp.startsWith("err"))
        return Task::Error;
    return Task::Unknown;
}

static QStringList parseRawLine(const QByteArray &raw)
{
    QStringList result;
    QString line = QString::fromUtf8(raw.constData());
    if (line.startsWith('#'))
        return result;

    return line.split('\t');
}

static QString unescape(const QString &input)
{
    QString result;
    for (int i = 0; i < input.count(); ++i) {
        if (input.at(i) == '\\') {
            if (i == input.count() - 1)
                continue;
            if (input.at(i + 1) == 'n') {
                result.append('\n');
                ++i;
                continue;
            } else if (input.at(i + 1) == 't') {
                result.append('\t');
                ++i;
                continue;
            } else if (input.at(i + 1) == '\\') {
                result.append('\\');
                ++i;
                continue;
            }
            continue;
        }
        result.append(input.at(i));
    }
    return result;
}

static bool parseTaskFile(QString *errorString, const FilePath &name)
{
    QFile tf(name.toString());
    if (!tf.open(QIODevice::ReadOnly)) {
        *errorString = Tr::tr("Cannot open task file %1: %2")
                           .arg(name.toUserOutput(), tf.errorString());
        return false;
    }

    const FilePath parentDir = name.parentDir();
    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())
                file = parentDir.pathAppended(file).toString();
        }
        description = unescape(description);

        TaskHub::addTask(Task(type, description, FilePath::fromUserInput(file), line,
                              Constants::TASK_CATEGORY_TASKLIST_ID));
    }
    return true;
}

static void clearTasks()
{
    TaskHub::clearTasks(Constants::TASK_CATEGORY_TASKLIST_ID);
}

void TaskFile::stopMonitoring()
{
    SessionManager::setValue(Constants::SESSION_TASKFILE_KEY, {});

    for (TaskFile *document : std::as_const(openFiles))
        document->deleteLater();
    openFiles.clear();
}

bool TaskFile::load(QString *errorString, const FilePath &fileName)
{
    setFilePath(fileName);
    clearTasks();

    bool result = parseTaskFile(errorString, fileName);
    if (result) {
        if (!SessionManager::isDefaultSession(SessionManager::activeSession()))
            SessionManager::setValue(Constants::SESSION_TASKFILE_KEY, fileName.toSettings());
    } else {
        stopMonitoring();
    }

    return result;
}

TaskFile *TaskFile::openTasks(const FilePath &filePath)
{
    TaskFile *file = Utils::findOrDefault(openFiles, Utils::equal(&TaskFile::filePath, filePath));
    QString errorString;
    if (file) {
        file->load(&errorString, filePath);
    } else {
        file = new TaskFile(ProjectExplorerPlugin::instance());

        if (!file->load(&errorString, filePath)) {
            QMessageBox::critical(Core::ICore::dialogParent(), Tr::tr("File Error"), errorString);
            delete file;
            return nullptr;
        }

        openFiles.append(file);

        // Register with filemanager:
        Core::DocumentManager::addDocument(file);
    }
    return file;
}

bool StopMonitoringHandler::canHandle(const ProjectExplorer::Task &task) const
{
    return task.category == Constants::TASK_CATEGORY_TASKLIST_ID;
}

void StopMonitoringHandler::handle(const ProjectExplorer::Task &task)
{
    QTC_ASSERT(canHandle(task), return);
    Q_UNUSED(task)
    TaskFile::stopMonitoring();
}

QAction *StopMonitoringHandler::createAction(QObject *parent) const
{
    const QString text = Tr::tr("Stop Monitoring");
    const QString toolTip = Tr::tr("Stop monitoring task files.");
    auto stopMonitoringAction = new QAction(text, parent);
    stopMonitoringAction->setToolTip(toolTip);
    return stopMonitoringAction;
}

} // namespace Internal
} // namespace ProjectExplorer