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

libkdegames

  • sources
  • kde-4.14
  • kdegames
  • libkdegames
kgtheme.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright 2012 Stefan Majewsky <majewsky@gmx.net> *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU Library General Public License *
6  * version 2 as published by the Free Software Foundation *
7  * *
8  * This program is distributed in the hope that it will be useful, *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11  * GNU Library General Public License for more details. *
12  * *
13  * You should have received a copy of the GNU Library General Public *
14  * License along with this program; if not, write to the *
15  * Free Software Foundation, Inc., *
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
17  ***************************************************************************/
18 
19 #include "kgtheme.h"
20 
21 #include <QtCore/QDir>
22 #include <QtCore/QFileInfo>
23 #include <KDE/KConfig>
24 #include <KDE/KConfigGroup>
25 #include <KDE/KDebug>
26 #include <KDE/KStandardDirs>
27 
28 class KgTheme::Private
29 {
30  public:
31  const QByteArray m_identifier;
32  QString m_name, m_description, m_author, m_authorEmail, m_graphicsPath, m_previewPath;
33  QMap<QString, QString> m_customData;
34 
35  Private(const QByteArray& id) : m_identifier(id) {}
36 
37  static QStringList s_configGroupNames;
38 };
39 
40 /*static*/ QStringList KgTheme::Private::s_configGroupNames;
41 
42 KgTheme::KgTheme(const QByteArray& identifier, QObject* parent)
43  : QObject(parent)
44  , d(new Private(identifier))
45 {
46 }
47 
48 KgTheme::~KgTheme()
49 {
50  delete d;
51 }
52 
53 QByteArray KgTheme::identifier() const
54 {
55  return d->m_identifier;
56 }
57 
58 #define KGTHEME_STRING_PROPERTY(PROP, SETTER) \
59  QString KgTheme::PROP() const { return d->m_##PROP; } \
60  void KgTheme::SETTER(const QString& val) { d->m_##PROP = val; }
61 
62 KGTHEME_STRING_PROPERTY(name, setName)
63 KGTHEME_STRING_PROPERTY(description, setDescription)
64 KGTHEME_STRING_PROPERTY(author, setAuthor)
65 KGTHEME_STRING_PROPERTY(authorEmail, setAuthorEmail)
66 KGTHEME_STRING_PROPERTY(graphicsPath, setGraphicsPath)
67 KGTHEME_STRING_PROPERTY(previewPath, setPreviewPath)
68 
69 QMap<QString, QString> KgTheme::customData() const
70 {
71  return d->m_customData;
72 }
73 
74 QString KgTheme::customData(const QString& key, const QString& defaultValue) const
75 {
76  return d->m_customData.value(key, defaultValue);
77 }
78 
79 void KgTheme::setCustomData(const QMap<QString, QString>& customData)
80 {
81  d->m_customData = customData;
82 }
83 
84 bool KgTheme::readFromDesktopFile(const QString& path_)
85 {
86  if (path_.isEmpty())
87  {
88  kDebug(11000) << "Refusing to load theme with no name";
89  return false;
90  }
91  //legacy support: relative paths are resolved with KStandardDirs/appdata
92  QString path(path_);
93  if (QFileInfo(path).isRelative())
94  {
95  path = KStandardDirs::locate("appdata", path);
96  if (path.isEmpty())
97  {
98  kDebug(11000) << "Could not find theme description" << path;
99  return false;
100  }
101  }
102  //default group name
103  if (Private::s_configGroupNames.isEmpty())
104  {
105  Private::s_configGroupNames << QLatin1String("KGameTheme");
106  }
107  //open file, look for a known config group
108  KConfig config(path, KConfig::SimpleConfig);
109  KConfigGroup group;
110  foreach (const QString& groupName, Private::s_configGroupNames)
111  {
112  if (config.hasGroup(groupName))
113  {
114  group = config.group(groupName);
115  }
116  }
117  if (!group.isValid())
118  {
119  kDebug(11000) << "Could not read theme description at" << path;
120  return false;
121  }
122  //check format version
123  if (group.readEntry("VersionFormat", 1) > 1)
124  {
125  kDebug(11000) << "Format of theme description too new at" << path;
126  return false;
127  }
128 
129  //resolve paths
130  const QFileInfo fi(path);
131  const QDir dir = fi.dir();
132  QString graphicsPath = group.readEntry("FileName", QString());
133  if (!graphicsPath.isEmpty() && QFileInfo(graphicsPath).isRelative())
134  graphicsPath = dir.absoluteFilePath(graphicsPath);
135  QString previewPath = group.readEntry("Preview", QString());
136  if (!previewPath.isEmpty() && QFileInfo(previewPath).isRelative())
137  previewPath = dir.absoluteFilePath(previewPath);
138  //create theme
139  setName(group.readEntry("Name", QString()));
140  setDescription(group.readEntry("Description", QString()));
141  setAuthor(group.readEntry("Author", QString()));
142  setAuthorEmail(group.readEntry("AuthorEmail", QString()));
143  setGraphicsPath(graphicsPath);
144  setPreviewPath(previewPath);
145  setCustomData(group.entryMap());
146  //store modification date of this file in private property (KGameRenderer
147  //wants to clear its cache also if the theme description changes)
148  setProperty("_k_themeDescTimestamp", fi.lastModified().toTime_t());
149  return true;
150 }
151 
152 #include "kgtheme.moc"
KGTHEME_STRING_PROPERTY
#define KGTHEME_STRING_PROPERTY(PROP, SETTER)
Definition: kgtheme.cpp:58
KgTheme::setAuthor
void setAuthor(const QString &author)
KgTheme::~KgTheme
virtual ~KgTheme()
Destructor.
Definition: kgtheme.cpp:48
QByteArray
KgTheme::readFromDesktopFile
virtual bool readFromDesktopFile(const QString &path)
Initializes a KgTheme instance by reading a description file.
Definition: kgtheme.cpp:84
KgTheme::setCustomData
void setCustomData(const QMap< QString, QString > &customData)
Definition: kgtheme.cpp:79
KgTheme::KgTheme
KgTheme(const QByteArray &identifier, QObject *parent=0)
Constructor. The identifier must be application-unique.
Definition: kgtheme.cpp:42
QMap< QString, QString >
KgTheme::setGraphicsPath
void setGraphicsPath(const QString &path)
KgTheme::setDescription
void setDescription(const QString &description)
QObject
KStandardGameAction::name
KDEGAMES_EXPORT const char * name(StandardGameAction id)
This will return the internal name of a given standard action.
Definition: kstandardgameaction.cpp:146
QString::isEmpty
bool isEmpty() const
QFileInfo::dir
QDir dir() const
QString
QFileInfo::lastModified
QDateTime lastModified() const
QStringList
KgTheme::customData
QMap< QString, QString > customData() const
Definition: kgtheme.cpp:69
QFileInfo
QDateTime::toTime_t
uint toTime_t() const
QDir
KgTheme::previewPath
QString previewPath() const
QFileInfo::isRelative
bool isRelative() const
QLatin1String
KgTheme::identifier
QByteArray identifier() const
KgTheme::setPreviewPath
void setPreviewPath(const QString &path)
QDir::absoluteFilePath
QString absoluteFilePath(const QString &fileName) const
KgTheme::setName
void setName(const QString &name)
QObject::setProperty
bool setProperty(const char *name, const QVariant &value)
KgTheme
A theme describes the visual appearance of a game.
Definition: kgtheme.h:68
KgTheme::graphicsPath
QString graphicsPath() const
KgTheme::setAuthorEmail
void setAuthorEmail(const QString &authorEmail)
kgtheme.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:42 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdegames

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

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

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