2
Copyright (C) 2007 Mauricio Piacentini <mauricio@tabuleiro.com>
3
Copyright (C) 2007 Matt Williams <matt@milliams.com>
5
This library is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or
8
(at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
#include "kgametheme.h"
22
#include <KStandardDirs>
24
#include <KConfigGroup>
26
#include <QtCore/QFile>
27
#include <QtCore/QFileInfo>
28
#include <QtCore/QMap>
29
#include <QtGui/QPixmap>
31
class KGameThemePrivate
34
KGameThemePrivate() : loaded(false) {}
36
QMap<QString, QString> themeproperties;
37
QString fullPath; ///< Full path e.g. "/opt/kde/share/apps/appname/default.desktop"
38
QString fileName; ///< just e.g. "default.desktop"
39
QString graphics; ///< The full path of the svg file
41
QString prefix; ///< Filepath of the .desktop file without the filename e.g. "/opt/kde/share/apps/appname/"
47
KGameTheme::KGameTheme(const QString &themeGroup)
48
: d(new KGameThemePrivate)
50
d->themeGroup = themeGroup;
51
//KGlobal::dirs()->addResourceType("gametheme", KStandardDirs::kde_default("data") + KGlobal::mainComponent().componentName());
54
KGameTheme::~KGameTheme() {
58
bool KGameTheme::loadDefault()
60
return load(QLatin1String( "themes/default.desktop" )); //TODO make this editable to match custom directories.
61
// If this ever changes change findThemes in KGameThemeSelectorPrivate too
64
#define kThemeVersionFormat 1
66
bool KGameTheme::load(const QString &fileName) {
67
if( fileName.isEmpty() )
69
kDebug(11000) << "Refusing to load theme with no name";
72
QString filePath = KStandardDirs::locate("appdata", fileName);
73
kDebug(11000) << "Attempting to load .desktop at" << filePath;
74
if (filePath.isEmpty()) {
78
// verify if it is a valid file first and if we can open it
79
QFile themefile(filePath);
80
if (!themefile.open(QIODevice::ReadOnly)) {
81
kDebug(11000) << "Could not open .desktop theme file" << filePath;
84
d->prefix = QFileInfo(themefile).absolutePath() + QLatin1Char( '/' );
87
KConfig themeconfig(filePath, KConfig::SimpleConfig);
88
if (!themeconfig.hasGroup(d->themeGroup))
90
kDebug(11000) << "Config group" << d->themeGroup << "does not exist in" << filePath;
93
KConfigGroup group = themeconfig.group(d->themeGroup);
95
//Copy the whole entryMap, so we can inherit generic properties as well, reducing the need to subclass for simple implementations
96
d->themeproperties = group.entryMap();
99
int themeversion = group.readEntry("VersionFormat",0);
100
//Format is increased when we have incompatible changes, meaning that older clients are not able to use the remaining information safely
101
if (themeversion > kThemeVersionFormat) {
105
QString graphName = group.readEntry("FileName");
106
//d->graphics = KStandardDirs::locate("appdata", graphName);
107
d->graphics = d->prefix + graphName;
108
if (d->graphics.isEmpty()) return false;
110
// let's see if svg file exists and can be opened
111
QFile svgFile( d->graphics );
112
if ( !svgFile.open( QIODevice::ReadOnly ) ) {
113
kDebug(11000) << "Could not open file" << d->graphics;
117
QString previewName = group.readEntry("Preview");
118
//QString graphicsPath = KStandardDirs::locate("appdata", previewName);
119
QString graphicsPath = d->prefix + previewName;
120
d->preview = QPixmap(graphicsPath);
122
d->fileName = fileName;
123
d->fullPath = filePath;
128
QString KGameTheme::property(const QString &key) const
132
kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
135
KConfig themeconfig(path(), KConfig::SimpleConfig);
136
KConfigGroup group = themeconfig.group(d->themeGroup);
137
return group.readEntry(key, QString());
140
QString KGameTheme::path() const {
143
kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
149
QString KGameTheme::fileName() const {
152
kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
158
QString KGameTheme::graphics() const {
161
kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
167
QPixmap KGameTheme::preview() const {
170
kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
176
QString KGameTheme::themeProperty(const QString &key) const {
179
kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
182
return d->themeproperties[key];