File indexing completed on 2024-05-19 07:45:41
0001 /* 0002 SPDX-FileCopyrightText: 2020 Dan Leinir Turthra Jensen <admin@leinir.dk> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include "knsrcmodel.h" 0008 0009 #include "enginebase.h" 0010 0011 #include <KConfig> 0012 #include <KConfigGroup> 0013 #include <QDir> 0014 0015 KNSRCModel::KNSRCModel(QObject *parent) 0016 : QAbstractListModel(parent) 0017 { 0018 const QStringList files = KNSCore::EngineBase::availableConfigFiles(); 0019 for (const auto &file : files) { 0020 KConfig conf(file); 0021 KConfigGroup group; 0022 if (conf.hasGroup(QStringLiteral("KNewStuff3"))) { 0023 group = conf.group(QStringLiteral("KNewStuff3")); 0024 } else if (conf.hasGroup(QStringLiteral("KNewStuff"))) { 0025 group = conf.group(QStringLiteral("KNewStuff")); 0026 } else { 0027 qWarning() << file << "doesn't contain a KNewStuff (or KNewStuff3) section."; 0028 continue; 0029 } 0030 0031 QString constructedName{QFileInfo(file).fileName()}; 0032 constructedName = constructedName.left(constructedName.length() - 6); 0033 constructedName.replace(QLatin1Char{'_'}, QLatin1Char{' '}); 0034 constructedName[0] = constructedName[0].toUpper(); 0035 0036 Entry *entry = new Entry; 0037 entry->name = group.readEntry("Name", constructedName); 0038 entry->filePath = file; 0039 0040 m_entries << entry; 0041 } 0042 std::sort(m_entries.begin(), m_entries.end(), [](const Entry *a, const Entry *b) -> bool { 0043 return QString::localeAwareCompare(b->name, a->name) > 0; 0044 }); 0045 } 0046 0047 KNSRCModel::~KNSRCModel() = default; 0048 0049 QHash<int, QByteArray> KNSRCModel::roleNames() const 0050 { 0051 static const QHash<int, QByteArray> roleNames{{NameRole, "name"}, {FilePathRole, "filePath"}}; 0052 return roleNames; 0053 } 0054 0055 int KNSRCModel::rowCount(const QModelIndex &parent) const 0056 { 0057 if (parent.isValid()) { 0058 return 0; 0059 } 0060 return m_entries.count(); 0061 } 0062 0063 QVariant KNSRCModel::data(const QModelIndex &index, int role) const 0064 { 0065 if (checkIndex(index)) { 0066 Entry *entry = m_entries[index.row()]; 0067 switch (role) { 0068 case NameRole: 0069 return entry->name; 0070 case FilePathRole: 0071 return entry->filePath; 0072 } 0073 } 0074 return QVariant(); 0075 } 0076 0077 #include "moc_knsrcmodel.cpp"