File indexing completed on 2024-04-21 03:51:08

0001 /*
0002     SPDX-FileCopyrightText: 2007 Mauricio Piacentini <mauricio@tabuleiro.com>
0003     SPDX-FileCopyrightText: 2007 Matt Williams <matt@milliams.com>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kgametheme.h"
0008 
0009 #include <KConfig>
0010 #include <KConfigGroup>
0011 #include <QDebug>
0012 #include <QFile>
0013 #include <QFileInfo>
0014 #include <QMap>
0015 #include <QPixmap>
0016 
0017 class KGameThemePrivate
0018 {
0019 public:
0020     KGameThemePrivate()
0021         : loaded(false)
0022     {
0023     }
0024 
0025     QMap<QString, QString> themeproperties;
0026     QString fullPath; ///< Full path e.g. "/opt/kde/share/apps/appname/default.desktop"
0027     QString fileName; ///< just e.g. "default.desktop"
0028     QString graphics; ///< The full path of the svg file
0029     QPixmap preview;
0030     QString prefix; ///< Filepath of the .desktop file without the filename e.g. "/opt/kde/share/apps/appname/"
0031     QString themeGroup;
0032 
0033     bool loaded;
0034 };
0035 
0036 KGameTheme::KGameTheme(const QString &themeGroup)
0037     : d(new KGameThemePrivate)
0038 {
0039     d->themeGroup = themeGroup;
0040     // KGlobal::dirs()->addResourceType("gametheme", KStandardDirs::kde_default("data") + KGlobal::mainComponent().componentName());
0041 }
0042 
0043 KGameTheme::~KGameTheme()
0044 {
0045     delete d;
0046 }
0047 
0048 bool KGameTheme::loadDefault()
0049 {
0050     return load(QStringLiteral("themes/default.desktop")); // TODO make this editable to match custom directories.
0051     // If this ever changes change findThemes in KGameThemeSelectorPrivate too
0052 }
0053 
0054 #define kThemeVersionFormat 1
0055 
0056 bool KGameTheme::load(const QString &fileName)
0057 {
0058     if (fileName.isEmpty()) {
0059         qDebug() << "Refusing to load theme with no name";
0060         return false;
0061     }
0062     QString filePath = QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, fileName, QStandardPaths::LocateFile);
0063     qDebug() << "Attempting to load .desktop at" << filePath;
0064     if (filePath.isEmpty()) {
0065         return false;
0066     }
0067 
0068     // verify if it is a valid file first and if we can open it
0069     QFile themefile(filePath);
0070     if (!themefile.open(QIODevice::ReadOnly)) {
0071         qDebug() << "Could not open .desktop theme file" << filePath;
0072         return false;
0073     }
0074     d->prefix = QFileInfo(themefile).absolutePath() + '/';
0075     themefile.close();
0076 
0077     KConfig themeconfig(filePath, KConfig::SimpleConfig);
0078     if (!themeconfig.hasGroup(d->themeGroup)) {
0079         qDebug() << "Config group" << d->themeGroup << "does not exist in" << filePath;
0080         return false;
0081     }
0082     KConfigGroup group = themeconfig.group(d->themeGroup);
0083 
0084     // Copy the whole entryMap, so we can inherit generic properties as well, reducing the need to subclass for simple implementations
0085     d->themeproperties = group.entryMap();
0086 
0087     // Version control
0088     int themeversion = group.readEntry("VersionFormat", 0);
0089     // Format is increased when we have incompatible changes, meaning that older clients are not able to use the remaining information safely
0090     if (themeversion > kThemeVersionFormat) {
0091         return false;
0092     }
0093 
0094     QString graphName = group.readEntry("FileName");
0095     // d->graphics = KStandardDirs::locate("appdata", graphName);
0096     d->graphics = d->prefix + graphName;
0097     if (d->graphics.isEmpty())
0098         return false;
0099 
0100     // let's see if svg file exists and can be opened
0101     QFile svgFile(d->graphics);
0102     if (!svgFile.open(QIODevice::ReadOnly)) {
0103         qDebug() << "Could not open file" << d->graphics;
0104         return false;
0105     }
0106 
0107     QString previewName = group.readEntry("Preview");
0108     // QString graphicsPath = KStandardDirs::locate("appdata", previewName);
0109     QString graphicsPath = d->prefix + previewName;
0110     d->preview = QPixmap(graphicsPath);
0111 
0112     d->fileName = fileName;
0113     d->fullPath = filePath;
0114     d->loaded = true;
0115     return true;
0116 }
0117 
0118 QString KGameTheme::property(const QString &key) const
0119 {
0120     if (!d->loaded) {
0121         qDebug() << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
0122         return QString();
0123     }
0124     KConfig themeconfig(path(), KConfig::SimpleConfig);
0125     KConfigGroup group = themeconfig.group(d->themeGroup);
0126     return group.readEntry(key, QString());
0127 }
0128 
0129 QString KGameTheme::path() const
0130 {
0131     if (!d->loaded) {
0132         qDebug() << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
0133         return QString();
0134     }
0135     return d->fullPath;
0136 }
0137 
0138 QString KGameTheme::fileName() const
0139 {
0140     if (!d->loaded) {
0141         qDebug() << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
0142         return QString();
0143     }
0144     return d->fileName;
0145 }
0146 
0147 QString KGameTheme::graphics() const
0148 {
0149     if (!d->loaded) {
0150         qDebug() << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
0151         return QString();
0152     }
0153     return d->graphics;
0154 }
0155 
0156 QPixmap KGameTheme::preview() const
0157 {
0158     if (!d->loaded) {
0159         qDebug() << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
0160         return QPixmap();
0161     }
0162     return d->preview;
0163 }
0164 
0165 QString KGameTheme::themeProperty(const QString &key) const
0166 {
0167     if (!d->loaded) {
0168         qDebug() << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
0169         return QString();
0170     }
0171     return d->themeproperties[key];
0172 }