File: sourcefiletemplate.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (379 lines) | stat: -rw-r--r-- 11,863 bytes parent folder | download
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * This file is part of KDevelop
 * Copyright 2012 Miha Čančula <miha@noughmad.eu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "sourcefiletemplate.h"
#include "templaterenderer.h"
#include <debug.h>

#include <interfaces/icore.h>

#include <KArchive>
#include <KConfig>
#include <KZip>
#include <KTar>
#include <KConfigGroup>

#include <QFileInfo>
#include <QDomDocument>
#include <QStandardPaths>
#include <QDir>

using namespace KDevelop;
using ConfigOption = SourceFileTemplate::ConfigOption;

class KDevelop::SourceFileTemplatePrivate
{
public:
    KArchive* archive;
    QString descriptionFileName;
    QStringList searchLocations;

    ConfigOption readEntry(const QDomElement& element, TemplateRenderer* renderer) const;
};

ConfigOption SourceFileTemplatePrivate::readEntry(const QDomElement& element,
                                                  TemplateRenderer* renderer) const
{
    ConfigOption entry;

    entry.name = element.attribute(QStringLiteral("name"));
    entry.type = element.attribute(QStringLiteral("type"), QStringLiteral("String"));

    bool isDefaultValueSet = false;
    for (QDomElement e = element.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
        QString tag = e.tagName();

        if (tag == QLatin1String("label")) {
            entry.label = e.text();
        } else if (tag == QLatin1String("tooltip")) {
            if (entry.label.isEmpty()) {
                entry.label = e.text();
            }
            entry.context = e.text();
        } else if (tag == QLatin1String("whatsthis")) {
            if (entry.label.isEmpty()) {
                entry.label = e.text();
            }
            if (entry.context.isEmpty()) {
                entry.context = e.text();
            }
        } else if (tag == QLatin1String("min")) {
            entry.minValue = e.text();
        } else if (tag == QLatin1String("max")) {
            entry.maxValue = e.text();
        } else if (tag == QLatin1String("default")) {
            entry.value = renderer->render(e.text(), entry.name);
            isDefaultValueSet = true;
        } else if (tag == QLatin1String("choices")) {
            QStringList values;
            QDomNodeList choices = element.elementsByTagName(QStringLiteral("choice"));
            values.reserve(choices.size());
            for (int j = 0; j < choices.size(); ++j) {
                QDomElement choiceElement = choices.at(j).toElement();
                values << choiceElement.attribute(QStringLiteral("name"));
            }

            Q_ASSERT(!values.isEmpty());
            if (values.isEmpty()) {
                qCWarning(LANGUAGE) << "Entry " << entry.name << "has an enum without any choices";
            }
            entry.values = values;
        }
    }

    qCDebug(LANGUAGE) << "Read entry" << entry.name << "with default value" << entry.value;

    // preset value for enum if needed
    if (!entry.values.isEmpty()) {
        if (isDefaultValueSet) {
            const bool isSaneDefaultValue = entry.values.contains(entry.value.toString());
            Q_ASSERT(isSaneDefaultValue);
            if (!isSaneDefaultValue) {
                qCWarning(LANGUAGE) << "Default value" << entry.value << "not in enum" << entry.values;
                entry.value = entry.values.at(0);
            }
        } else {
            entry.value = entry.values.at(0);
        }
    }
    return entry;
}

SourceFileTemplate::SourceFileTemplate (const QString& templateDescription)
    : d_ptr(new KDevelop::SourceFileTemplatePrivate)
{
    Q_D(SourceFileTemplate);

    d->archive = nullptr;
    setTemplateDescription(templateDescription);
}

SourceFileTemplate::SourceFileTemplate()
    : d_ptr(new KDevelop::SourceFileTemplatePrivate)
{
    Q_D(SourceFileTemplate);

    d->archive = nullptr;
}

SourceFileTemplate::SourceFileTemplate (const SourceFileTemplate& other)
    : d_ptr(new KDevelop::SourceFileTemplatePrivate)
{
    Q_D(SourceFileTemplate);

    d->archive = nullptr;
    *this = other;
}

SourceFileTemplate::~SourceFileTemplate()
{
    Q_D(SourceFileTemplate);

    delete d->archive;
}

SourceFileTemplate& SourceFileTemplate::operator=(const SourceFileTemplate& other)
{
    Q_D(SourceFileTemplate);

    if (other.d_ptr == d_ptr) {
        return *this;
    }

    delete d->archive;
    if (other.d_ptr->archive) {
        if (other.d_ptr->archive->fileName().endsWith(QLatin1String(".zip"))) {
            d->archive = new KZip(other.d_ptr->archive->fileName());
        } else {
            d->archive = new KTar(other.d_ptr->archive->fileName());
        }
        d->archive->open(QIODevice::ReadOnly);
    } else {
        d->archive = nullptr;
    }
    d->descriptionFileName = other.d_ptr->descriptionFileName;
    return *this;
}

void SourceFileTemplate::setTemplateDescription(const QString& templateDescription)
{
    Q_D(SourceFileTemplate);

    delete d->archive;

    d->descriptionFileName = templateDescription;
    QString archiveFileName;

    const QString templateBaseName = QFileInfo(templateDescription).baseName();

    d->searchLocations.append(QStandardPaths::locateAll(QStandardPaths::GenericDataLocation,
                                                        QStringLiteral("/kdevfiletemplates/templates/"),
                                                        QStandardPaths::LocateDirectory));

    for (const QString& dir : qAsConst(d->searchLocations)) {
        const auto fileEntries = QDir(dir).entryInfoList(QDir::Files);
        for (const auto& entry : fileEntries) {
            if (entry.baseName() == templateBaseName) {
                archiveFileName = entry.absoluteFilePath();
                qCDebug(LANGUAGE) << "Found template archive" << archiveFileName;
                break;
            }
        }
    }

    if (archiveFileName.isEmpty() || !QFileInfo::exists(archiveFileName)) {
        qCWarning(LANGUAGE) << "Could not find a template archive for description" << templateDescription <<
            ", archive file" << archiveFileName;
        d->archive = nullptr;
    } else {
        QFileInfo info(archiveFileName);

        if (info.suffix() == QLatin1String("zip")) {
            d->archive = new KZip(archiveFileName);
        } else {
            d->archive = new KTar(archiveFileName);
        }
        d->archive->open(QIODevice::ReadOnly);
    }
}

bool SourceFileTemplate::isValid() const
{
    Q_D(const SourceFileTemplate);

    return d->archive;
}

QString SourceFileTemplate::name() const
{
    Q_D(const SourceFileTemplate);

    KConfig templateConfig(d->descriptionFileName);
    KConfigGroup cg(&templateConfig, "General");
    return cg.readEntry("Name");
}

QString SourceFileTemplate::type() const
{
    Q_D(const SourceFileTemplate);

    KConfig templateConfig(d->descriptionFileName);
    KConfigGroup cg(&templateConfig, "General");
    return cg.readEntry("Type", QString());
}

QString SourceFileTemplate::languageName() const
{
    Q_D(const SourceFileTemplate);

    KConfig templateConfig(d->descriptionFileName);
    KConfigGroup cg(&templateConfig, "General");
    return cg.readEntry("Language", QString());
}

QStringList SourceFileTemplate::category() const
{
    Q_D(const SourceFileTemplate);

    KConfig templateConfig(d->descriptionFileName);
    KConfigGroup cg(&templateConfig, "General");
    return cg.readEntry("Category", QStringList());
}

QStringList SourceFileTemplate::defaultBaseClasses() const
{
    Q_D(const SourceFileTemplate);

    KConfig templateConfig(d->descriptionFileName);
    KConfigGroup cg(&templateConfig, "General");
    return cg.readEntry("BaseClasses", QStringList());
}

const KArchiveDirectory* SourceFileTemplate::directory() const
{
    Q_D(const SourceFileTemplate);

    Q_ASSERT(isValid());
    return d->archive->directory();
}

QVector<SourceFileTemplate::OutputFile> SourceFileTemplate::outputFiles() const
{
    Q_D(const SourceFileTemplate);

    QVector<SourceFileTemplate::OutputFile> outputFiles;

    KConfig templateConfig(d->descriptionFileName);
    KConfigGroup group(&templateConfig, "General");

    const QStringList files = group.readEntry("Files", QStringList());
    qCDebug(LANGUAGE) << "Files in template" << files;

    outputFiles.reserve(files.size());
    for (const QString& fileGroup : files) {
        KConfigGroup cg(&templateConfig, fileGroup);
        OutputFile f;
        f.identifier = cg.name();
        f.label = cg.readEntry("Name");
        f.fileName = cg.readEntry("File");
        f.outputName = cg.readEntry("OutputFile");
        outputFiles << f;
    }

    return outputFiles;
}

bool SourceFileTemplate::hasCustomOptions() const
{
    Q_D(const SourceFileTemplate);

    Q_ASSERT(isValid());

    KConfig templateConfig(d->descriptionFileName);
    KConfigGroup cg(&templateConfig, "General");
    bool hasOptions = d->archive->directory()->entries().contains(cg.readEntry("OptionsFile", "options.kcfg"));

    qCDebug(LANGUAGE) << cg.readEntry("OptionsFile", "options.kcfg") << hasOptions;
    return hasOptions;
}

QVector<SourceFileTemplate::ConfigOptionGroup> SourceFileTemplate::customOptions(TemplateRenderer* renderer) const
{
    Q_D(const SourceFileTemplate);

    Q_ASSERT(isValid());

    KConfig templateConfig(d->descriptionFileName);
    KConfigGroup cg(&templateConfig, "General");
    const KArchiveEntry* entry = d->archive->directory()->entry(cg.readEntry("OptionsFile", "options.kcfg"));

    QVector<ConfigOptionGroup> optionGroups;

    if (!entry->isFile()) {
        return optionGroups;
    }
    const auto* file = static_cast<const KArchiveFile*>(entry);

    /*
     * Copied from kconfig_compiler.kcfg
     */
    QDomDocument doc;
    QString errorMsg;
    int errorRow;
    int errorCol;
    if (!doc.setContent(file->data(), &errorMsg, &errorRow, &errorCol)) {
        qCDebug(LANGUAGE) << "Unable to load document.";
        qCDebug(LANGUAGE) << "Parse error in line " << errorRow << ", col " << errorCol << ": " << errorMsg;
        return optionGroups;
    }

    QDomElement cfgElement = doc.documentElement();
    if (cfgElement.isNull()) {
        qCDebug(LANGUAGE) << "No document in kcfg file";
        return optionGroups;
    }

    QDomNodeList groups = cfgElement.elementsByTagName(QStringLiteral("group"));
    optionGroups.reserve(groups.size());
    for (int i = 0; i < groups.size(); ++i) {
        QDomElement group = groups.at(i).toElement();
        ConfigOptionGroup optionGroup;
        optionGroup.name = group.attribute(QStringLiteral("name"));

        QDomNodeList entries = group.elementsByTagName(QStringLiteral("entry"));
        optionGroup.options.reserve(entries.size());
        for (int j = 0; j < entries.size(); ++j) {
            QDomElement entry = entries.at(j).toElement();
            optionGroup.options << d->readEntry(entry, renderer);
        }

        optionGroups << optionGroup;
    }

    return optionGroups;
}

void SourceFileTemplate::addAdditionalSearchLocation(const QString& location)
{
    Q_D(SourceFileTemplate);

    if (!d->searchLocations.contains(location))
        d->searchLocations.append(location);
}