1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
/*
Copyright (C) 2007 Mauricio Piacentini <mauricio@tabuleiro.com>
Copyright (C) 2007 Matt Williams <matt@milliams.com>
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kgametheme.h"
#include <KConfig>
#include <KConfigGroup>
#include <QFile>
#include <QFileInfo>
#include <QMap>
#include <QStandardPaths>
#include <QPixmap>
Q_LOGGING_CATEGORY(GAMES_LIB, "org.kde.games.lib", QtWarningMsg)
class KGameThemePrivate
{
public:
KGameThemePrivate() : loaded(false) {}
QMap<QString, QString> themeproperties;
QString fullPath; ///< Full path e.g. "/opt/kde/share/apps/appname/default.desktop"
QString fileName; ///< just e.g. "default.desktop"
QString graphics; ///< The full path of the svg file
QPixmap preview;
QString prefix; ///< Filepath of the .desktop file without the filename e.g. "/opt/kde/share/apps/appname/"
QString themeGroup;
bool loaded;
};
KGameTheme::KGameTheme(const QString &themeGroup)
: d(new KGameThemePrivate)
{
d->themeGroup = themeGroup;
//KGlobal::dirs()->addResourceType("gametheme", KStandardDirs::kde_default("data") + KGlobal::mainComponent().componentName());
}
KGameTheme::~KGameTheme() {
delete d;
}
bool KGameTheme::loadDefault()
{
return load(QStringLiteral( "themes/default.desktop" )); //TODO make this editable to match custom directories.
// If this ever changes change findThemes in KGameThemeSelectorPrivate too
}
#define kThemeVersionFormat 1
bool KGameTheme::load(const QString &fileName) {
if( fileName.isEmpty() )
{
qCDebug(GAMES_LIB) << "Refusing to load theme with no name";
return false;
}
QString filePath = QStandardPaths::locate(QStandardPaths::AppDataLocation, fileName);
qCDebug(GAMES_LIB) << "Attempting to load .desktop at" << filePath;
if (filePath.isEmpty()) {
return false;
}
// verify if it is a valid file first and if we can open it
QFile themefile(filePath);
if (!themefile.open(QIODevice::ReadOnly)) {
qCDebug(GAMES_LIB) << "Could not open .desktop theme file" << filePath;
return false;
}
d->prefix = QFileInfo(themefile).absolutePath() + QLatin1Char( '/' );
themefile.close();
KConfig themeconfig(filePath, KConfig::SimpleConfig);
if (!themeconfig.hasGroup(d->themeGroup))
{
qCDebug(GAMES_LIB) << "Config group" << d->themeGroup << "does not exist in" << filePath;
return false;
}
KConfigGroup group = themeconfig.group(d->themeGroup);
//Copy the whole entryMap, so we can inherit generic properties as well, reducing the need to subclass for simple implementations
d->themeproperties = group.entryMap();
//Version control
int themeversion = group.readEntry("VersionFormat",0);
//Format is increased when we have incompatible changes, meaning that older clients are not able to use the remaining information safely
if (themeversion > kThemeVersionFormat) {
return false;
}
QString graphName = group.readEntry("FileName");
//d->graphics = QStandardPaths::locate(QStandardPaths::AppDataLocation, graphName);
d->graphics = d->prefix + graphName;
if (d->graphics.isEmpty()) return false;
// let's see if svg file exists and can be opened
QFile svgFile( d->graphics );
if ( !svgFile.open( QIODevice::ReadOnly ) ) {
qCDebug(GAMES_LIB) << "Could not open file" << d->graphics;
return false;
}
QString previewName = group.readEntry("Preview");
//QString graphicsPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, previewName);
QString graphicsPath = d->prefix + previewName;
d->preview = QPixmap(graphicsPath);
d->fileName = fileName;
d->fullPath = filePath;
d->loaded = true;
return true;
}
QString KGameTheme::property(const QString &key) const
{
if(!d->loaded)
{
qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
return QString();
}
KConfig themeconfig(path(), KConfig::SimpleConfig);
KConfigGroup group = themeconfig.group(d->themeGroup);
return group.readEntry(key, QString());
}
QString KGameTheme::path() const {
if(!d->loaded)
{
qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
return QString();
}
return d->fullPath;
}
QString KGameTheme::prefix() const {
if(!d->loaded)
{
qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
return QString();
}
return d->prefix;
}
QString KGameTheme::fileName() const {
if(!d->loaded)
{
qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
return QString();
}
return d->fileName;
}
QString KGameTheme::graphics() const {
if(!d->loaded)
{
qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
return QString();
}
return d->graphics;
}
QPixmap KGameTheme::preview() const {
if(!d->loaded)
{
qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
return QPixmap();
}
return d->preview;
}
QString KGameTheme::themeProperty(const QString &key) const {
if(!d->loaded)
{
qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
return QString();
}
return d->themeproperties[key];
}
|