• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libkdegames

kgametheme.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2007 Mauricio Piacentini   <mauricio@tabuleiro.com>
00003     Copyright (C) 2007 Matt Williams   <matt@milliams.com>
00004 
00005     This library is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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     //KGlobal::dirs()->addResourceType("gametheme", KStandardDirs::kde_default("data") + KGlobal::mainComponent().componentName());
00052 }
00053 
00054 KGameTheme::~KGameTheme() {
00055     delete d;
00056 }
00057 
00058 bool KGameTheme::loadDefault()
00059 {
00060     return load("themes/default.desktop"); //TODO make this editable to match custom directories.
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     // verify if it is a valid file first and if we can open it
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     //Copy the whole entryMap, so we can inherit generic properties as well, reducing the need to subclass for simple implementations
00095     d->themeproperties = group.entryMap();
00096 
00097     //Version control
00098     int themeversion = group.readEntry("VersionFormat",0);
00099     //Format is increased when we have incompatible changes, meaning that older clients are not able to use the remaining information safely
00100     if (themeversion > kThemeVersionFormat) {
00101         return false;
00102     }
00103 
00104     QString graphName = group.readEntry("FileName");
00105     //d->graphics = KStandardDirs::locate("appdata", graphName);
00106     d->graphics = d->prefix + graphName;
00107     if (d->graphics.isEmpty()) return false;
00108 
00109     // let's see if svg file exists and can be opened
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     //QString graphicsPath = KStandardDirs::locate("appdata", previewName);
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 }

libkdegames

Skip menu "libkdegames"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal