• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

parley

  • sources
  • kde-4.12
  • kdeedu
  • parley
  • src
  • settings
  • kgametheme
kgametheme.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 Mauricio Piacentini <mauricio@tabuleiro.com>
3  Copyright (C) 2007 Matt Williams <matt@milliams.com>
4 
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.
9 
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.
14 
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.
18 */
19 
20 #include "kgametheme.h"
21 
22 #include <KStandardDirs>
23 #include <KConfig>
24 #include <KConfigGroup>
25 #include <KDebug>
26 #include <QtCore/QFile>
27 #include <QtCore/QFileInfo>
28 #include <QtCore/QMap>
29 #include <QtGui/QPixmap>
30 
31 class KGameThemePrivate
32 {
33  public:
34  KGameThemePrivate() : loaded(false) {}
35 
36  QMap<QString, QString> themeproperties;
37  QString fullPath;
38  QString fileName;
39  QString graphics;
40  QPixmap preview;
41  QString prefix;
42  QString themeGroup;
43 
44  bool loaded;
45 };
46 
47 KGameTheme::KGameTheme(const QString &themeGroup)
48  : d(new KGameThemePrivate)
49 {
50  d->themeGroup = themeGroup;
51  //KGlobal::dirs()->addResourceType("gametheme", KStandardDirs::kde_default("data") + KGlobal::mainComponent().componentName());
52 }
53 
54 KGameTheme::~KGameTheme() {
55  delete d;
56 }
57 
58 bool KGameTheme::loadDefault()
59 {
60  return load("themes/default.desktop"); //TODO make this editable to match custom directories.
61  // If this ever changes change findThemes in KGameThemeSelectorPrivate too
62 }
63 
64 #define kThemeVersionFormat 1
65 
66 bool KGameTheme::load(const QString &fileName) {
67  if( fileName.isEmpty() )
68  {
69  kDebug(11000) << "Refusing to load theme with no name";
70  return false;
71  }
72  QString filePath = KStandardDirs::locate("appdata", fileName);
73  kDebug(11000) << "Attempting to load .desktop at" << filePath;
74  if (filePath.isEmpty()) {
75  return false;
76  }
77 
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;
82  return false;
83  }
84  d->prefix = QFileInfo(themefile).absolutePath() + '/';
85  themefile.close();
86 
87  KConfig themeconfig(filePath, KConfig::SimpleConfig);
88  if (!themeconfig.hasGroup(d->themeGroup))
89  {
90  kDebug(11000) << "Config group" << d->themeGroup << "does not exist in" << filePath;
91  return false;
92  }
93  KConfigGroup group = themeconfig.group(d->themeGroup);
94 
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();
97 
98  //Version control
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) {
102  return false;
103  }
104 
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;
109 
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;
114  return false;
115  }
116 
117  QString previewName = group.readEntry("Preview");
118  //QString graphicsPath = KStandardDirs::locate("appdata", previewName);
119  QString graphicsPath = d->prefix + previewName;
120  d->preview = QPixmap(graphicsPath);
121 
122  d->fileName = fileName;
123  d->fullPath = filePath;
124  d->loaded = true;
125  return true;
126 }
127 
128 QString KGameTheme::property(const QString &key) const
129 {
130  if(!d->loaded)
131  {
132  kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
133  return QString();
134  }
135  KConfig themeconfig(path(), KConfig::SimpleConfig);
136  KConfigGroup group = themeconfig.group(d->themeGroup);
137  return group.readEntry(key, QString());
138 }
139 
140 QString KGameTheme::path() const {
141  if(!d->loaded)
142  {
143  kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
144  return QString();
145  }
146  return d->fullPath;
147 }
148 
149 QString KGameTheme::fileName() const {
150  if(!d->loaded)
151  {
152  kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
153  return QString();
154  }
155  return d->fileName;
156 }
157 
158 QString KGameTheme::graphics() const {
159  if(!d->loaded)
160  {
161  kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
162  return QString();
163  }
164  return d->graphics;
165 }
166 
167 QPixmap KGameTheme::preview() const {
168  if(!d->loaded)
169  {
170  kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
171  return QPixmap();
172  }
173  return d->preview;
174 }
175 
176 QString KGameTheme::themeProperty(const QString &key) const {
177  if(!d->loaded)
178  {
179  kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
180  return QString();
181  }
182  return d->themeproperties[key];
183 }
KGameTheme::KGameTheme
KGameTheme(const QString &themeGroup=QLatin1String("KGameTheme"))
Definition: kgametheme.cpp:47
KGameTheme::path
QString path() const
Definition: kgametheme.cpp:140
KGameTheme::themeProperty
virtual QString themeProperty(const QString &key) const
Possible keys:
Definition: kgametheme.cpp:176
kThemeVersionFormat
#define kThemeVersionFormat
Definition: kgametheme.cpp:64
KGameTheme::graphics
virtual QString graphics() const
Definition: kgametheme.cpp:158
KGameTheme::~KGameTheme
virtual ~KGameTheme()
Definition: kgametheme.cpp:54
KGameTheme::fileName
QString fileName() const
Definition: kgametheme.cpp:149
KGameTheme::load
virtual bool load(const QString &file)
Load a specific theme file.
Definition: kgametheme.cpp:66
kgametheme.h
KGameTheme::preview
QPixmap preview() const
Definition: kgametheme.cpp:167
KGameTheme::loadDefault
virtual bool loadDefault()
Load the default theme file.
Definition: kgametheme.cpp:58
KGameTheme::property
QString property(const QString &key) const
Definition: kgametheme.cpp:128
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:05 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

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

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal