KDEGames

kgtheme.cpp
1 /***************************************************************************
2  * Copyright 2012 Stefan Majewsky <[email protected]> *
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 <QDir>
22 #include <QFileInfo>
23 #include <QStandardPaths>
24 
25 #include <KConfig>
26 #include <KConfigGroup>
27 
28 Q_LOGGING_CATEGORY(GAMES_LIB, "org.kde.games.lib", QtWarningMsg)
29 
30 class KgTheme::Private
31 {
32  public:
33  const QByteArray m_identifier;
34  QString m_name, m_description, m_author, m_authorEmail, m_graphicsPath, m_previewPath;
35  QMap<QString, QString> m_customData;
36 
37  Private(const QByteArray& id) : m_identifier(id) {}
38 
39  static QStringList s_configGroupNames;
40 };
41 
42 /*static*/ QStringList KgTheme::Private::s_configGroupNames;
43 
44 KgTheme::KgTheme(const QByteArray& identifier, QObject* parent)
45  : QObject(parent)
46  , d(new Private(identifier))
47 {
48 }
49 
51 {
52  delete d;
53 }
54 
56 {
57  return d->m_identifier;
58 }
59 
60 #define KGTHEME_STRING_PROPERTY(PROP, SETTER) \
61  QString KgTheme::PROP() const { return d->m_##PROP; } \
62  void KgTheme::SETTER(const QString& val) { d->m_##PROP = val; }
63 
64 KGTHEME_STRING_PROPERTY(name, setName)
65 KGTHEME_STRING_PROPERTY(description, setDescription)
66 KGTHEME_STRING_PROPERTY(author, setAuthor)
67 KGTHEME_STRING_PROPERTY(authorEmail, setAuthorEmail)
68 KGTHEME_STRING_PROPERTY(graphicsPath, setGraphicsPath)
69 KGTHEME_STRING_PROPERTY(previewPath, setPreviewPath)
70 
71 QMap<QString, QString> KgTheme::customData() const
72 {
73  return d->m_customData;
74 }
75 
76 QString KgTheme::customData(const QString& key, const QString& defaultValue) const
77 {
78  return d->m_customData.value(key, defaultValue);
79 }
80 
82 {
83  d->m_customData = customData;
84 }
85 
86 bool KgTheme::readFromDesktopFile(const QString& path_)
87 {
88  if (path_.isEmpty())
89  {
90  qCDebug(GAMES_LIB) << "Refusing to load theme with no name";
91  return false;
92  }
93  //legacy support: relative paths are resolved with KStandardDirs/appdata
94  QString path(path_);
95  if (QFileInfo(path).isRelative())
96  {
98  if (path.isEmpty())
99  {
100  qCDebug(GAMES_LIB) << "Could not find theme description" << path;
101  return false;
102  }
103  }
104  //default group name
105  if (Private::s_configGroupNames.isEmpty())
106  {
107  Private::s_configGroupNames << QStringLiteral("KGameTheme");
108  }
109  //open file, look for a known config group
110  KConfig config(path, KConfig::SimpleConfig);
111  KConfigGroup group;
112  for (const QString& groupName : qAsConst(Private::s_configGroupNames)) {
113  if (config.hasGroup(groupName))
114  {
115  group = config.group(groupName);
116  }
117  }
118  if (!group.isValid())
119  {
120  qCDebug(GAMES_LIB) << "Could not read theme description at" << path;
121  return false;
122  }
123  //check format version
124  if (group.readEntry("VersionFormat", 1) > 1)
125  {
126  qCDebug(GAMES_LIB) << "Format of theme description too new at" << path;
127  return false;
128  }
129 
130  //resolve paths
131  const QFileInfo fi(path);
132  const QDir dir = fi.dir();
133  QString graphicsPath = group.readEntry("FileName", QString());
134  if (!graphicsPath.isEmpty() && QFileInfo(graphicsPath).isRelative())
135  graphicsPath = dir.absoluteFilePath(graphicsPath);
136  QString previewPath = group.readEntry("Preview", QString());
137  if (!previewPath.isEmpty() && QFileInfo(previewPath).isRelative())
138  previewPath = dir.absoluteFilePath(previewPath);
139  //create theme
140  setName(group.readEntry("Name", QString()));
141  setDescription(group.readEntry("Description", QString()));
142  setAuthor(group.readEntry("Author", QString()));
143  setAuthorEmail(group.readEntry("AuthorEmail", QString()));
144  setGraphicsPath(graphicsPath);
145  setPreviewPath(previewPath);
146  setCustomData(group.entryMap());
147  //store modification date of this file in private property (KGameRenderer
148  //wants to clear its cache also if the theme description changes)
149  setProperty("_k_themeDescTimestamp", fi.lastModified().toSecsSinceEpoch());
150  return true;
151 }
152 
153 
void setAuthor(const QString &author)
virtual ~KgTheme()
Destructor.
Definition: kgtheme.cpp:50
virtual bool readFromDesktopFile(const QString &path)
Initializes a KgTheme instance by reading a description file.
Definition: kgtheme.cpp:86
void setCustomData(const QMap< QString, QString > &customData)
Definition: kgtheme.cpp:81
QString description() const
bool hasGroup(const QString &group) const
void setGraphicsPath(const QString &path)
bool isValid() const
QString authorEmail() const
void setDescription(const QString &description)
QString name() const
QString author() const
bool isEmpty() const const
QDir dir() const const
KgTheme(const QByteArray &identifier, QObject *parent=nullptr)
Constructor. The identifier must be application-unique.
Definition: kgtheme.cpp:44
QDateTime lastModified() const const
QMap< QString, QString > customData() const
Definition: kgtheme.cpp:71
KConfigGroup group(const QString &group)
QString previewPath() const
bool isRelative() const const
QByteArray identifier() const
qint64 toSecsSinceEpoch() const const
void setPreviewPath(const QString &path)
QString absoluteFilePath(const QString &fileName) const const
void setName(const QString &name)
bool setProperty(const char *name, const QVariant &value)
A theme describes the visual appearance of a game.
Definition: kgtheme.h:72
QString graphicsPath() const
T readEntry(const QString &key, const T &aDefault) const
QMap< QString, QString > entryMap() const
void setAuthorEmail(const QString &authorEmail)
QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Nov 30 2020 22:37:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.