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

parley

  • sources
  • kde-4.14
  • 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 {
56  delete d;
57 }
58 
59 bool KGameTheme::loadDefault()
60 {
61  return load("themes/default.desktop"); //TODO make this editable to match custom directories.
62  // If this ever changes change findThemes in KGameThemeSelectorPrivate too
63 }
64 
65 #define kThemeVersionFormat 1
66 
67 bool KGameTheme::load(const QString &fileName)
68 {
69  if (fileName.isEmpty()) {
70  kDebug(11000) << "Refusing to load theme with no name";
71  return false;
72  }
73  QString filePath = KStandardDirs::locate("appdata", fileName);
74  kDebug(11000) << "Attempting to load .desktop at" << filePath;
75  if (filePath.isEmpty()) {
76  return false;
77  }
78 
79  // verify if it is a valid file first and if we can open it
80  QFile themefile(filePath);
81  if (!themefile.open(QIODevice::ReadOnly)) {
82  kDebug(11000) << "Could not open .desktop theme file" << filePath;
83  return false;
84  }
85  d->prefix = QFileInfo(themefile).absolutePath() + '/';
86  themefile.close();
87 
88  KConfig themeconfig(filePath, KConfig::SimpleConfig);
89  if (!themeconfig.hasGroup(d->themeGroup)) {
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  kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
132  return QString();
133  }
134  KConfig themeconfig(path(), KConfig::SimpleConfig);
135  KConfigGroup group = themeconfig.group(d->themeGroup);
136  return group.readEntry(key, QString());
137 }
138 
139 QString KGameTheme::path() const
140 {
141  if (!d->loaded) {
142  kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
143  return QString();
144  }
145  return d->fullPath;
146 }
147 
148 QString KGameTheme::fileName() const
149 {
150  if (!d->loaded) {
151  kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
152  return QString();
153  }
154  return d->fileName;
155 }
156 
157 QString KGameTheme::graphics() const
158 {
159  if (!d->loaded) {
160  kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
161  return QString();
162  }
163  return d->graphics;
164 }
165 
166 QPixmap KGameTheme::preview() const
167 {
168  if (!d->loaded) {
169  kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
170  return QPixmap();
171  }
172  return d->preview;
173 }
174 
175 QString KGameTheme::themeProperty(const QString &key) const
176 {
177  if (!d->loaded) {
178  kDebug(11000) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
179  return QString();
180  }
181  return d->themeproperties[key];
182 }
KGameTheme::KGameTheme
KGameTheme(const QString &themeGroup=QLatin1String("KGameTheme"))
Definition: kgametheme.cpp:47
QMap< QString, QString >
KGameTheme::path
QString path() const
Definition: kgametheme.cpp:139
KGameTheme::themeProperty
virtual QString themeProperty(const QString &key) const
Possible keys:
Definition: kgametheme.cpp:175
kThemeVersionFormat
#define kThemeVersionFormat
Definition: kgametheme.cpp:65
KGameTheme::graphics
virtual QString graphics() const
Definition: kgametheme.cpp:157
QFile
KGameTheme::~KGameTheme
virtual ~KGameTheme()
Definition: kgametheme.cpp:54
QString::isEmpty
bool isEmpty() const
KGameTheme::fileName
QString fileName() const
Definition: kgametheme.cpp:148
QString
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QPixmap
KGameTheme::load
virtual bool load(const QString &file)
Load a specific theme file.
Definition: kgametheme.cpp:67
QFileInfo
QFile::close
virtual void close()
kgametheme.h
KGameTheme::preview
QPixmap preview() const
Definition: kgametheme.cpp:166
KGameTheme::loadDefault
virtual bool loadDefault()
Load the default theme file.
Definition: kgametheme.cpp:59
QFileInfo::absolutePath
QString absolutePath() const
KGameTheme::property
QString property(const QString &key) const
Definition: kgametheme.cpp:128
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:15:56 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
  • 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