KDEGames

kgtheme.cpp
1 /*
2  SPDX-FileCopyrightText: 2012 Stefan Majewsky <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-only
5 */
6 
7 #include "kgtheme.h"
8 
9 // KF
10 #include <KConfig>
11 #include <KConfigGroup>
12 // Qt
13 #include <QDir>
14 #include <QFileInfo>
15 #include <QStandardPaths>
16 
17 Q_LOGGING_CATEGORY(GAMES_LIB, "org.kde.games.lib", QtWarningMsg)
18 
19 class KgThemePrivate
20 {
21  public:
22  const QByteArray m_identifier;
23  QString m_name, m_description, m_author, m_authorEmail, m_graphicsPath, m_previewPath;
24  QMap<QString, QString> m_customData;
25 
26  KgThemePrivate(const QByteArray& id) : m_identifier(id) {}
27 
28  static QStringList s_configGroupNames;
29 };
30 
31 /*static*/ QStringList KgThemePrivate::s_configGroupNames;
32 
33 KgTheme::KgTheme(const QByteArray& identifier, QObject* parent)
34  : QObject(parent)
35  , d(new KgThemePrivate(identifier))
36 {
37 }
38 
39 KgTheme::~KgTheme() = default;
40 
42 {
43  return d->m_identifier;
44 }
45 
46 #define KGTHEME_STRING_PROPERTY(PROP, SETTER) \
47  QString KgTheme::PROP() const { return d->m_##PROP; } \
48  void KgTheme::SETTER(const QString& val) { d->m_##PROP = val; }
49 
50 KGTHEME_STRING_PROPERTY(name, setName)
51 KGTHEME_STRING_PROPERTY(description, setDescription)
52 KGTHEME_STRING_PROPERTY(author, setAuthor)
53 KGTHEME_STRING_PROPERTY(authorEmail, setAuthorEmail)
54 KGTHEME_STRING_PROPERTY(graphicsPath, setGraphicsPath)
55 KGTHEME_STRING_PROPERTY(previewPath, setPreviewPath)
56 
57 QMap<QString, QString> KgTheme::customData() const
58 {
59  return d->m_customData;
60 }
61 
62 QString KgTheme::customData(const QString& key, const QString& defaultValue) const
63 {
64  return d->m_customData.value(key, defaultValue);
65 }
66 
68 {
69  d->m_customData = customData;
70 }
71 
72 bool KgTheme::readFromDesktopFile(const QString& path_)
73 {
74  if (path_.isEmpty())
75  {
76  qCDebug(GAMES_LIB) << "Refusing to load theme with no name";
77  return false;
78  }
79  //legacy support: relative paths are resolved with KStandardDirs/appdata
80  QString path(path_);
81  if (QFileInfo(path).isRelative())
82  {
84  if (path.isEmpty())
85  {
86  qCDebug(GAMES_LIB) << "Could not find theme description" << path;
87  return false;
88  }
89  }
90  //default group name
91  if (KgThemePrivate::s_configGroupNames.isEmpty())
92  {
93  KgThemePrivate::s_configGroupNames << QStringLiteral("KGameTheme");
94  }
95  //open file, look for a known config group
96  KConfig config(path, KConfig::SimpleConfig);
97  KConfigGroup group;
98  for (const QString& groupName : qAsConst(KgThemePrivate::s_configGroupNames)) {
99  if (config.hasGroup(groupName))
100  {
101  group = config.group(groupName);
102  }
103  }
104  if (!group.isValid())
105  {
106  qCDebug(GAMES_LIB) << "Could not read theme description at" << path;
107  return false;
108  }
109  //check format version
110  if (group.readEntry("VersionFormat", 1) > 1)
111  {
112  qCDebug(GAMES_LIB) << "Format of theme description too new at" << path;
113  return false;
114  }
115 
116  //resolve paths
117  const QFileInfo fi(path);
118  const QDir dir = fi.dir();
119  QString graphicsPath = group.readEntry("FileName", QString());
120  if (!graphicsPath.isEmpty() && QFileInfo(graphicsPath).isRelative())
121  graphicsPath = dir.absoluteFilePath(graphicsPath);
122  QString previewPath = group.readEntry("Preview", QString());
123  if (!previewPath.isEmpty() && QFileInfo(previewPath).isRelative())
124  previewPath = dir.absoluteFilePath(previewPath);
125  //create theme
126  setName(group.readEntry("Name", QString()));
127  setDescription(group.readEntry("Description", QString()));
128  setAuthor(group.readEntry("Author", QString()));
129  setAuthorEmail(group.readEntry("AuthorEmail", QString()));
130  setGraphicsPath(graphicsPath);
131  setPreviewPath(previewPath);
132  setCustomData(group.entryMap());
133  //store modification date of this file in private property (KGameRenderer
134  //wants to clear its cache also if the theme description changes)
135  setProperty("_k_themeDescTimestamp", fi.lastModified().toSecsSinceEpoch());
136  return true;
137 }
138 
139 
void setAuthor(const QString &author)
virtual bool readFromDesktopFile(const QString &path)
Initializes a KgTheme instance by reading a description file.
Definition: kgtheme.cpp:72
void setCustomData(const QMap< QString, QString > &customData)
Definition: kgtheme.cpp:67
QString description() const
bool hasGroup(const QString &group) const
void setGraphicsPath(const QString &path)
bool isValid() const
QString authorEmail() const
~KgTheme() override
Destructor.
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:33
QDateTime lastModified() const const
QMap< QString, QString > customData() const
Definition: kgtheme.cpp:57
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:58
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-2021 The KDE developers.
Generated on Tue Dec 7 2021 22:34:15 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.