File indexing completed on 2024-05-12 05:36:48

0001 /*
0002  * LnfListModel
0003  * SPDX-FileCopyrightText: 2016 Marco Martin <mart@kde.org>
0004  * SPDX-FileCopyrightText: 2002 Karol Szwed <gallium@kde.org>
0005  * SPDX-FileCopyrightText: 2002 Daniel Molkentin <molkentin@kde.org>
0006  * SPDX-FileCopyrightText: 2007 Urs Wolfer <uwolfer @ kde.org>
0007  * SPDX-FileCopyrightText: 2009 Davide Bettio <davide.bettio@kdemail.net>
0008  * SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com>
0009  * SPDX-FileCopyrightText: 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
0010  * SPDX-FileCopyrightText: 2008 Petri Damsten <damu@iki.fi>
0011  * SPDX-FileCopyrightText: 2000 TrollTech AS.
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-only
0014  */
0015 
0016 #include "lnflistmodel.h"
0017 
0018 #include <QApplication>
0019 #include <QDir>
0020 #include <QFile>
0021 #include <QPainter>
0022 #include <QStandardPaths>
0023 
0024 #include <KConfigGroup>
0025 #include <KDesktopFile>
0026 
0027 #include <Plasma/Theme>
0028 #include <qstandardpaths.h>
0029 
0030 #include <QDebug>
0031 
0032 LnfListModel::LnfListModel(QObject *parent)
0033     : QAbstractListModel(parent)
0034 {
0035     m_roleNames.insert(Qt::DisplayRole, "displayRole");
0036     m_roleNames.insert(PackageNameRole, "packageNameRole");
0037     m_roleNames.insert(PackageDescriptionRole, "packageDescriptionRole");
0038     m_roleNames.insert(PackageAuthorRole, "packageAuthorRole");
0039     m_roleNames.insert(PackageVersionRole, "packageVersionRole");
0040 
0041     reload();
0042 }
0043 
0044 LnfListModel::~LnfListModel()
0045 {
0046     clearThemeList();
0047 }
0048 
0049 QHash<int, QByteArray> LnfListModel::roleNames() const
0050 {
0051     return m_roleNames;
0052 }
0053 
0054 void LnfListModel::clearThemeList()
0055 {
0056     m_themes.clear();
0057 }
0058 
0059 void LnfListModel::reload()
0060 {
0061     beginResetModel();
0062     clearThemeList();
0063 
0064     // get all desktop themes
0065     QStringList themes;
0066     const QStringList &packs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "plasma/look-and-feel", QStandardPaths::LocateDirectory);
0067     for (const QString &ppath : packs) {
0068         const QDir cd(ppath);
0069         const QStringList &entries = cd.entryList(QDir::Dirs | QDir::Hidden);
0070         for (const QString &pack : entries) {
0071             const QString _metadata = ppath + QLatin1Char('/') + pack + QStringLiteral("/metadata.desktop");
0072             if ((pack != "." && pack != "..") && (QFile::exists(_metadata))) {
0073                 themes << _metadata;
0074             }
0075         }
0076     }
0077 
0078     for (const QString &theme : themes) {
0079         int themeSepIndex = theme.lastIndexOf('/', -1);
0080         QString themeRoot = theme.left(themeSepIndex);
0081         int themeNameSepIndex = themeRoot.lastIndexOf('/', -1);
0082         QString packageName = themeRoot.right(themeRoot.length() - themeNameSepIndex - 1);
0083 
0084         KDesktopFile df(theme);
0085 
0086         if (df.noDisplay()) {
0087             continue;
0088         }
0089 
0090         QString name = df.readName();
0091         if (name.isEmpty()) {
0092             name = packageName;
0093         }
0094         const QString comment = df.readComment();
0095         const QString author = df.desktopGroup().readEntry("X-KDE-PluginInfo-Author", QString());
0096         const QString version = df.desktopGroup().readEntry("X-KDE-PluginInfo-Version", QString());
0097 
0098         ThemeInfo info;
0099         info.name = name;
0100         info.package = packageName;
0101         info.description = comment;
0102         info.author = author;
0103         info.version = version;
0104         info.themeRoot = themeRoot;
0105         m_themes << info;
0106     }
0107 
0108     endResetModel();
0109     emit countChanged();
0110 }
0111 
0112 int LnfListModel::rowCount(const QModelIndex &) const
0113 {
0114     return m_themes.size();
0115 }
0116 
0117 QVariant LnfListModel::data(const QModelIndex &index, int role) const
0118 {
0119     if (!index.isValid()) {
0120         return QVariant();
0121     }
0122 
0123     if (index.row() >= m_themes.size() || index.row() < 0) {
0124         return QVariant();
0125     }
0126 
0127     switch (role) {
0128     case Qt::DisplayRole:
0129         return m_themes.value(index.row()).name;
0130     case PackageNameRole:
0131         return m_themes.value(index.row()).package;
0132     case PackageDescriptionRole:
0133         return m_themes.value(index.row()).description;
0134     case PackageAuthorRole:
0135         return m_themes.value(index.row()).author;
0136     case PackageVersionRole:
0137         return m_themes.value(index.row()).version;
0138     default:
0139         return QVariant();
0140     }
0141 }
0142 
0143 QVariantMap LnfListModel::get(int row) const
0144 {
0145     QVariantMap item;
0146 
0147     QModelIndex idx = index(row, 0);
0148 
0149     item["display"] = data(idx, Qt::DisplayRole);
0150     item["packageNameRole"] = data(idx, PackageNameRole);
0151     item["packageDescriptionRole"] = data(idx, PackageDescriptionRole);
0152     item["packageAuthorRole"] = data(idx, PackageAuthorRole);
0153     item["packageVersionRole"] = data(idx, PackageVersionRole);
0154 
0155     return item;
0156 }
0157 
0158 QModelIndex LnfListModel::indexOf(const QString &name) const
0159 {
0160     QListIterator<ThemeInfo> it(m_themes);
0161     int i = -1;
0162     while (it.hasNext()) {
0163         ++i;
0164         if (it.next().package == name) {
0165             return index(i, 0);
0166         }
0167     }
0168 
0169     return QModelIndex();
0170 }
0171 
0172 #include "moc_lnflistmodel.cpp"