File indexing completed on 2024-09-22 04:58:54

0001 /*
0002 This file is part of LightDM-KDE.
0003 
0004 Copyright 2011, 2012 David Edmundson <kde@davidedmundson.co.uk>
0005 
0006 LightDM-KDE is free software: you can redistribute it and/or modify
0007 it under the terms of the GNU General Public License as published by
0008 the Free Software Foundation, either version 3 of the License, or
0009 (at your option) any later version.
0010 
0011 LightDM-KDE is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with LightDM-KDE.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 #include "themesmodel.h"
0020 
0021 #include "lightdmpackagestructure.h"
0022 
0023 #include <QString>
0024 #include <QPixmap>
0025 #include <QList>
0026 #include <QDir>
0027 #include <QSettings>
0028 
0029 #include <KStandardDirs>
0030 #include <KGlobal>
0031 #include <KDebug>
0032 
0033 #include <Plasma/Package>
0034 
0035 
0036 class ThemeItem {
0037 public:
0038     QString id;
0039     QString name;
0040     QString description;
0041     QString author;
0042     QString version;
0043     QPixmap preview;
0044     QString path;
0045 };
0046 
0047 ThemesModel::ThemesModel(QObject *parent) :
0048     QAbstractListModel(parent)
0049 {
0050     load();
0051 }
0052 
0053 ThemesModel::~ThemesModel()
0054 {
0055 }
0056 
0057 int ThemesModel::rowCount(const QModelIndex &parent) const
0058 {
0059     Q_UNUSED(parent);
0060     return m_themes.size();
0061 }
0062 
0063 QVariant ThemesModel::data(const QModelIndex &index, int role) const
0064 {
0065     int row = index.row();
0066     const Plasma::Package package(m_themes[row]);
0067     const Plasma::PackageMetadata metaData = package.metadata();
0068 
0069     switch(role) {
0070     case Qt::DisplayRole:
0071         return metaData.name();
0072     case ThemesModel::PreviewRole:
0073         return package.filePath("preview");
0074     case ThemesModel::IdRole:
0075         return metaData.pluginName();
0076     case ThemesModel::DescriptionRole:
0077         return metaData.description();
0078     case ThemesModel::VersionRole:
0079         return metaData.version();
0080     case ThemesModel::AuthorRole:
0081         return metaData.author();
0082     case ThemesModel::PathRole:
0083         return package.path();
0084     }
0085 
0086     return QVariant();
0087 }
0088 
0089 void ThemesModel::load()
0090 {
0091     m_themes.clear();
0092     reset();
0093 
0094     QStringList themeDirPaths = KGlobal::dirs()->findDirs("data", "lightdm-kde-greeter/themes");
0095     foreach(const QString &themeDirPath, themeDirPaths) {
0096         foreach(const QString &themeName, Plasma::Package::listInstalledPaths(themeDirPath)) {
0097             //A new package structure is apparently needed for each new package, otherwise instances of different packages represent the same object in a horrendously broken way
0098             Plasma::PackageStructure::Ptr packageStructure(new LightDMPackageStructure(this));
0099 
0100             Plasma::Package package(themeDirPath, themeName, packageStructure);
0101             if (package.isValid()) {
0102                 addTheme(package);
0103             }
0104         }
0105     }
0106 }
0107 
0108 void ThemesModel::addTheme(const Plasma::Package &package)
0109 {
0110     beginInsertRows(QModelIndex(), m_themes.size(), m_themes.size());
0111     m_themes.append(package);
0112     endInsertRows();
0113 }