File indexing completed on 2024-05-05 16:16:11
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 "engine.h" 0010 0011 #include <KConfig> 0012 #include <KConfigGroup> 0013 #include <QDir> 0014 0015 struct Entry { 0016 QString name; 0017 QString filePath; 0018 }; 0019 0020 class Private 0021 { 0022 public: 0023 QList<Entry *> m_entries; 0024 }; 0025 0026 KNSRCModel::KNSRCModel(QObject *parent) 0027 : QAbstractListModel(parent) 0028 , d(new Private()) 0029 { 0030 const QStringList files = KNSCore::Engine::availableConfigFiles(); 0031 for (const auto &file : files) { 0032 KConfig conf(file); 0033 KConfigGroup group; 0034 if (conf.hasGroup("KNewStuff3")) { 0035 group = conf.group("KNewStuff3"); 0036 } else if (conf.hasGroup("KNewStuff2")) { 0037 group = conf.group("KNewStuff2"); 0038 } else { 0039 qWarning() << file << " doesn't contain a KNewStuff3 (or KNewStuff2) section."; 0040 continue; 0041 } 0042 0043 QString constructedName{QFileInfo(file).fileName()}; 0044 constructedName = constructedName.left(constructedName.length() - 6); 0045 constructedName.replace(QLatin1Char{'_'}, QLatin1Char{' '}); 0046 constructedName[0] = constructedName[0].toUpper(); 0047 0048 Entry *entry = new Entry; 0049 entry->name = group.readEntry("Name", constructedName); 0050 entry->filePath = file; 0051 0052 d->m_entries << entry; 0053 } 0054 std::sort(d->m_entries.begin(), d->m_entries.end(), [](const Entry *a, const Entry *b) -> bool { 0055 return QString::localeAwareCompare(b->name, a->name) > 0; 0056 }); 0057 } 0058 0059 KNSRCModel::~KNSRCModel() = default; 0060 0061 QHash<int, QByteArray> KNSRCModel::roleNames() const 0062 { 0063 static const QHash<int, QByteArray> roleNames{{NameRole, "name"}, {FilePathRole, "filePath"}}; 0064 return roleNames; 0065 } 0066 0067 int KNSRCModel::rowCount(const QModelIndex &parent) const 0068 { 0069 if (parent.isValid()) { 0070 return 0; 0071 } 0072 return d->m_entries.count(); 0073 } 0074 0075 QVariant KNSRCModel::data(const QModelIndex &index, int role) const 0076 { 0077 QVariant result; 0078 if (checkIndex(index)) { 0079 Entry *entry = d->m_entries[index.row()]; 0080 switch (role) { 0081 case NameRole: 0082 result.setValue(entry->name); 0083 break; 0084 case FilePathRole: 0085 result.setValue(entry->filePath); 0086 break; 0087 default: 0088 break; 0089 } 0090 } 0091 return result; 0092 } 0093 0094 #include "moc_knsrcmodel.cpp"