KNewStuff

knsrcmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Dan Leinir Turthra Jensen <admin@leinir.dk>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "knsrcmodel.h"
8
9#include "enginebase.h"
10
11#include <KConfig>
12#include <KConfigGroup>
13#include <QDir>
14
15KNSRCModel::KNSRCModel(QObject *parent)
16 : QAbstractListModel(parent)
17{
19 for (const auto &file : files) {
20 KConfig conf(file);
21 KConfigGroup group;
22 if (conf.hasGroup(QStringLiteral("KNewStuff3"))) {
23 group = conf.group(QStringLiteral("KNewStuff3"));
24 } else if (conf.hasGroup(QStringLiteral("KNewStuff"))) {
25 group = conf.group(QStringLiteral("KNewStuff"));
26 } else {
27 qWarning() << file << "doesn't contain a KNewStuff (or KNewStuff3) section.";
28 continue;
29 }
30
33 constructedName.replace(QLatin1Char{'_'}, QLatin1Char{' '});
34 constructedName[0] = constructedName[0].toUpper();
35
36 Entry *entry = new Entry;
37 entry->name = group.readEntry("Name", constructedName);
38 entry->filePath = file;
39
40 m_entries << entry;
41 }
42 std::sort(m_entries.begin(), m_entries.end(), [](const Entry *a, const Entry *b) -> bool {
43 return QString::localeAwareCompare(b->name, a->name) > 0;
44 });
45}
46
47KNSRCModel::~KNSRCModel() = default;
48
49QHash<int, QByteArray> KNSRCModel::roleNames() const
50{
51 static const QHash<int, QByteArray> roleNames{{NameRole, "name"}, {FilePathRole, "filePath"}};
52 return roleNames;
53}
54
55int KNSRCModel::rowCount(const QModelIndex &parent) const
56{
57 if (parent.isValid()) {
58 return 0;
59 }
60 return m_entries.count();
61}
62
63QVariant KNSRCModel::data(const QModelIndex &index, int role) const
64{
65 if (checkIndex(index)) {
66 Entry *entry = m_entries[index.row()];
67 switch (role) {
68 case NameRole:
69 return entry->name;
70 case FilePathRole:
71 return entry->filePath;
72 }
73 }
74 return QVariant();
75}
76
77#include "moc_knsrcmodel.cpp"
QString readEntry(const char *key, const char *aDefault=nullptr) const
static QStringList availableConfigFiles()
List of all available config files.
bool checkIndex(const QModelIndex &index, CheckIndexOptions options) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
QString fileName() const const
qsizetype count() const const
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:54:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.