File indexing completed on 2024-12-01 06:50:33

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