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

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