| 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 | |
| 15 | KNSRCModel::KNSRCModel(QObject *parent) |
| 16 | : QAbstractListModel(parent) |
| 17 | { |
| 18 | const QStringList files = KNSCore::EngineBase::availableConfigFiles(); |
| 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 | |
| 31 | QString constructedName{QFileInfo(file).fileName()}; |
| 32 | constructedName = constructedName.left(n: constructedName.length() - 6); |
| 33 | constructedName.replace(before: QLatin1Char{'_'}, after: QLatin1Char{' '}); |
| 34 | constructedName[0] = constructedName[0].toUpper(); |
| 35 | |
| 36 | Entry *entry = new Entry; |
| 37 | entry->name = group.readEntry(key: "Name" , aDefault: constructedName); |
| 38 | entry->filePath = file; |
| 39 | |
| 40 | m_entries << entry; |
| 41 | } |
| 42 | std::sort(first: m_entries.begin(), last: m_entries.end(), comp: [](const Entry *a, const Entry *b) -> bool { |
| 43 | return QString::localeAwareCompare(s1: b->name, s2: a->name) > 0; |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | KNSRCModel::~KNSRCModel() = default; |
| 48 | |
| 49 | QHash<int, QByteArray> KNSRCModel::roleNames() const |
| 50 | { |
| 51 | static const QHash<int, QByteArray> roleNames{{NameRole, "name" }, {FilePathRole, "filePath" }}; |
| 52 | return roleNames; |
| 53 | } |
| 54 | |
| 55 | int KNSRCModel::rowCount(const QModelIndex &parent) const |
| 56 | { |
| 57 | if (parent.isValid()) { |
| 58 | return 0; |
| 59 | } |
| 60 | return m_entries.count(); |
| 61 | } |
| 62 | |
| 63 | QVariant 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" |
| 78 | |