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