File indexing completed on 2024-05-12 05:46:29

0001 /*
0002  * LnfListModel
0003  * Copyright (C) 2016 Marco Martin <mart@kde.org>
0004  * Copyright (C) 2002 Karol Szwed <gallium@kde.org>
0005  * Copyright (C) 2002 Daniel Molkentin <molkentin@kde.org>
0006  * Copyright (C) 2007 Urs Wolfer <uwolfer @ kde.org>
0007  * Copyright (C) 2009 by Davide Bettio <davide.bettio@kdemail.net>
0008 
0009  * Portions Copyright (C) 2007 Paolo Capriotti <p.capriotti@gmail.com>
0010  * Portions Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
0011  * Portions Copyright (C) 2008 by Petri Damsten <damu@iki.fi>
0012  * Portions Copyright (C) 2000 TrollTech AS.
0013  *
0014  * This program is free software; you can redistribute it and/or
0015  * modify it under the terms of the GNU General Public
0016  * License version 2 as published by the Free Software Foundation.
0017  *
0018  * This program is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0021  * General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program; see the file COPYING.  If not, write to
0025  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0026  * Boston, MA 02110-1301, USA.
0027  */
0028 
0029 #include "lnflistmodel.h"
0030 
0031 #include <QApplication>
0032 #include <QDir>
0033 #include <QFile>
0034 #include <QPainter>
0035 #include <QStandardPaths>
0036 
0037 #include <KDesktopFile>
0038 
0039 #include <Plasma/Theme>
0040 #include <qstandardpaths.h>
0041 
0042 #include <QDebug>
0043 
0044 LnfListModel::LnfListModel( QObject *parent )
0045 : QAbstractListModel( parent )
0046 {
0047     m_roleNames.insert(Qt::DisplayRole, "displayRole");
0048     m_roleNames.insert(PackageNameRole, "packageNameRole");
0049     m_roleNames.insert(PackageDescriptionRole, "packageDescriptionRole");
0050     m_roleNames.insert(PackageAuthorRole, "packageAuthorRole");
0051     m_roleNames.insert(PackageVersionRole, "packageVersionRole");
0052 
0053     reload();
0054 }
0055 
0056 LnfListModel::~LnfListModel()
0057 {
0058     clearThemeList();
0059 }
0060 
0061 QHash<int, QByteArray> LnfListModel::roleNames() const
0062 {
0063     return m_roleNames;
0064 }
0065 
0066 void LnfListModel::clearThemeList()
0067 {
0068     m_themes.clear();
0069 }
0070 
0071 void LnfListModel::reload()
0072 {
0073     beginResetModel();
0074     clearThemeList();
0075 
0076     // get all desktop themes
0077     QStringList themes;
0078     const QStringList &packs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "plasma/look-and-feel", QStandardPaths::LocateDirectory);
0079     foreach (const QString &ppath, packs) {
0080         const QDir cd(ppath);
0081         const QStringList &entries = cd.entryList(QDir::Dirs | QDir::Hidden);
0082         foreach (const QString pack, entries) {
0083             const QString _metadata = ppath+QLatin1Char('/')+pack+QStringLiteral("/metadata.desktop");
0084             if ((pack != "." && pack != "..") &&
0085                 (QFile::exists(_metadata))) {
0086                 themes << _metadata;
0087             }
0088         }
0089     }
0090 
0091     foreach (const QString &theme, themes) {
0092         int themeSepIndex = theme.lastIndexOf('/', -1);
0093         QString themeRoot = theme.left(themeSepIndex);
0094         int themeNameSepIndex = themeRoot.lastIndexOf('/', -1);
0095         QString packageName = themeRoot.right(themeRoot.length() - themeNameSepIndex - 1);
0096 
0097         KDesktopFile df(theme);
0098 
0099         if (df.noDisplay()) {
0100             continue;
0101         }
0102 
0103         QString name = df.readName();
0104         if (name.isEmpty()) {
0105             name = packageName;
0106         }
0107         const QString comment = df.readComment();
0108         const QString author = df.desktopGroup().readEntry("X-KDE-PluginInfo-Author",QString());
0109         const QString version = df.desktopGroup().readEntry("X-KDE-PluginInfo-Version",QString());
0110 
0111         ThemeInfo info;
0112         info.name = name;
0113         info.package = packageName;
0114         info.description = comment;
0115         info.author = author;
0116         info.version = version;
0117         info.themeRoot = themeRoot;
0118         m_themes << info;
0119     }
0120 
0121     endResetModel();
0122     emit countChanged();
0123 }
0124 
0125 int LnfListModel::rowCount(const QModelIndex &) const
0126 {
0127     return m_themes.size();
0128 }
0129 
0130 QVariant LnfListModel::data(const QModelIndex &index, int role) const
0131 {
0132     if (!index.isValid()) {
0133         return QVariant();
0134     }
0135 
0136     if (index.row() >= m_themes.size() || index.row() < 0) {
0137         return QVariant();
0138     }
0139 
0140     switch (role) {
0141         case Qt::DisplayRole:
0142             return m_themes.value(index.row()).name;
0143         case PackageNameRole:
0144             return m_themes.value(index.row()).package;
0145         case PackageDescriptionRole:
0146             return m_themes.value(index.row()).description;
0147         case PackageAuthorRole:
0148             return m_themes.value(index.row()).author;
0149         case PackageVersionRole:
0150             return m_themes.value(index.row()).version;
0151         default:
0152             return QVariant();
0153     }
0154 }
0155 
0156 QVariantMap LnfListModel::get(int row) const
0157 {
0158     QVariantMap item;
0159 
0160     QModelIndex idx = index(row, 0);
0161 
0162     item["display"] = data(idx, Qt::DisplayRole);
0163     item["packageNameRole"] = data(idx, PackageNameRole);
0164     item["packageDescriptionRole"] = data(idx, PackageDescriptionRole);
0165     item["packageAuthorRole"] = data(idx, PackageAuthorRole);
0166     item["packageVersionRole"] = data(idx, PackageVersionRole);
0167 
0168     return item;
0169 }
0170 
0171 QModelIndex LnfListModel::indexOf(const QString &name) const
0172 {
0173     QListIterator<ThemeInfo> it(m_themes);
0174     int i = -1;
0175     while (it.hasNext()) {
0176         ++i;
0177         if (it.next().package == name) {
0178             return index(i, 0);
0179         }
0180     }
0181 
0182     return QModelIndex();
0183 }
0184 
0185 #include "moc_lnflistmodel.cpp"