libkdegames
kgametheme.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kgametheme.h"
00021
00022 #include <KStandardDirs>
00023 #include <KConfig>
00024 #include <KConfigGroup>
00025 #include <KDebug>
00026 #include <QtCore/QFile>
00027 #include <QtCore/QFileInfo>
00028 #include <QtCore/QMap>
00029 #include <QtGui/QPixmap>
00030
00031 class KGameThemePrivate
00032 {
00033 public:
00034 KGameThemePrivate() : loaded(false) {}
00035
00036 QMap<QString, QString> themeproperties;
00037 QString fullPath;
00038 QString fileName;
00039 QString graphics;
00040 QPixmap preview;
00041 QString prefix;
00042 QString themeGroup;
00043
00044 bool loaded;
00045 };
00046
00047 KGameTheme::KGameTheme(const QString &themeGroup)
00048 : d(new KGameThemePrivate)
00049 {
00050 d->themeGroup = themeGroup;
00051
00052 }
00053
00054 KGameTheme::~KGameTheme() {
00055 delete d;
00056 }
00057
00058 bool KGameTheme::loadDefault()
00059 {
00060 return load("themes/default.desktop");
00061 }
00062
00063 #define kThemeVersionFormat 1
00064
00065 bool KGameTheme::load(const QString &fileName) {
00066 if( fileName.isEmpty() )
00067 {
00068 kDebug(11000) << "Refusing to load theme with no name";
00069 return false;
00070 }
00071 QString filePath = KStandardDirs::locate("appdata", fileName);
00072 kDebug(11000) << "Attempting to load .desktop at" << filePath;
00073 if (filePath.isEmpty()) {
00074 return false;
00075 }
00076
00077
00078 QFile themefile(filePath);
00079 if (!themefile.open(QIODevice::ReadOnly)) {
00080 kDebug(11000) << "Could not open .desktop theme file" << filePath;
00081 return false;
00082 }
00083 d->prefix = QFileInfo(themefile).absolutePath() + '/';
00084 themefile.close();
00085
00086 KConfig themeconfig(filePath, KConfig::SimpleConfig);
00087 if (!themeconfig.hasGroup(d->themeGroup))
00088 {
00089 kDebug(11000) << "Config group" << d->themeGroup << "does not exist in" << filePath;
00090 return false;
00091 }
00092 KConfigGroup group = themeconfig.group(d->themeGroup);
00093
00094
00095 d->themeproperties = group.entryMap();
00096
00097
00098 int themeversion = group.readEntry("VersionFormat",0);
00099
00100 if (themeversion > kThemeVersionFormat) {
00101 return false;
00102 }
00103
00104 QString graphName = group.readEntry("FileName");
00105
00106 d->graphics = d->prefix + graphName;
00107 if (d->graphics.isEmpty()) return false;
00108
00109
00110 QFile svgFile( d->graphics );
00111 if ( !svgFile.open( QIODevice::ReadOnly ) ) {
00112 kDebug(11000) << "Could not open file" << d->graphics;
00113 return false;
00114 }
00115
00116 QString previewName = group.readEntry("Preview");
00117
00118 QString graphicsPath = d->prefix + previewName;
00119 d->preview = QPixmap(graphicsPath);
00120
00121 d->fileName = fileName;
00122 d->fullPath = filePath;
00123 d->loaded = true;
00124 return true;
00125 }
00126
00127 QString KGameTheme::property(const QString &key) const
00128 {
00129 if(!d->loaded)
00130 {
00131 kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
00132 return QString();
00133 }
00134 KConfig themeconfig(path(), KConfig::SimpleConfig);
00135 KConfigGroup group = themeconfig.group(d->themeGroup);
00136 return group.readEntry(key, QString());
00137 }
00138
00139 QString KGameTheme::path() const {
00140 if(!d->loaded)
00141 {
00142 kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
00143 return QString();
00144 }
00145 return d->fullPath;
00146 }
00147
00148 QString KGameTheme::fileName() const {
00149 if(!d->loaded)
00150 {
00151 kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
00152 return QString();
00153 }
00154 return d->fileName;
00155 }
00156
00157 QString KGameTheme::graphics() const {
00158 if(!d->loaded)
00159 {
00160 kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
00161 return QString();
00162 }
00163 return d->graphics;
00164 }
00165
00166 QPixmap KGameTheme::preview() const {
00167 if(!d->loaded)
00168 {
00169 kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
00170 return QPixmap();
00171 }
00172 return d->preview;
00173 }
00174
00175 QString KGameTheme::themeProperty(const QString &key) const {
00176 if(!d->loaded)
00177 {
00178 kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
00179 return QString();
00180 }
00181 return d->themeproperties[key];
00182 }