KDEGames

kgametheme.cpp
1/*
2 SPDX-FileCopyrightText: 2012 Stefan Majewsky <majewsky@gmx.net>
3
4 SPDX-License-Identifier: LGPL-2.0-only
5*/
6
7#include "kgametheme.h"
8
9// own
10#include <kdegames_logging.h>
11// KF
12#include <KConfig>
13#include <KConfigGroup>
14// Qt
15#include <QDir>
16#include <QFileInfo>
17#include <QStandardPaths>
18// Std
19#include <utility>
20
21class KGameThemePrivate
22{
23public:
24 const QByteArray m_identifier;
25 QString m_name, m_description, m_author, m_authorEmail, m_graphicsPath, m_previewPath;
26 QString m_license;
27 QString m_copyrightText;
28 QString m_version;
29 QString m_website;
30 QString m_bugReportUrl;
31 QMap<QString, QString> m_customData;
32
33public:
34 explicit KGameThemePrivate(const QByteArray &id)
35 : m_identifier(id)
36 {
37 }
38
39 static QStringList s_configGroupNames;
40};
41
42/*static*/ QStringList KGameThemePrivate::s_configGroupNames;
43
44KGameTheme::KGameTheme(const QByteArray &identifier, QObject *parent)
45 : QObject(parent)
46 , d_ptr(new KGameThemePrivate(identifier))
47{
48}
49
50KGameTheme::~KGameTheme() = default;
51
52QByteArray KGameTheme::identifier() const
53{
54 Q_D(const KGameTheme);
55
56 return d->m_identifier;
57}
58
59#define KGAMETHEME_STRING_PROPERTY(PROP, SETTER) \
60 QString KGameTheme::PROP() const \
61 { \
62 Q_D(const KGameTheme); \
63 return d->m_##PROP; \
64 } \
65 void KGameTheme::SETTER(const QString &val) \
66 { \
67 Q_D(KGameTheme); \
68 d->m_##PROP = val; \
69 }
70
71KGAMETHEME_STRING_PROPERTY(name, setName)
72KGAMETHEME_STRING_PROPERTY(description, setDescription)
73KGAMETHEME_STRING_PROPERTY(license, setLicense)
74KGAMETHEME_STRING_PROPERTY(copyrightText, setCopyrightText)
75KGAMETHEME_STRING_PROPERTY(version, setVersion)
76KGAMETHEME_STRING_PROPERTY(website, setWebsite)
77KGAMETHEME_STRING_PROPERTY(bugReportUrl, setBugReportUrl)
78KGAMETHEME_STRING_PROPERTY(author, setAuthor)
79KGAMETHEME_STRING_PROPERTY(authorEmail, setAuthorEmail)
80KGAMETHEME_STRING_PROPERTY(graphicsPath, setGraphicsPath)
81KGAMETHEME_STRING_PROPERTY(previewPath, setPreviewPath)
82
83QMap<QString, QString> KGameTheme::customData() const
84{
85 Q_D(const KGameTheme);
86
87 return d->m_customData;
88}
89
90QString KGameTheme::customData(const QString &key, const QString &defaultValue) const
91{
92 Q_D(const KGameTheme);
93
94 return d->m_customData.value(key, defaultValue);
95}
96
98{
100
101 d->m_customData = customData;
102}
103
105{
106 if (path_.isEmpty()) {
107 qCDebug(KDEGAMES_LOG) << "Refusing to load theme with no name";
108 return false;
109 }
110 // legacy support: relative paths are resolved with KStandardDirs/appdata
111 QString path(path_);
112 if (QFileInfo(path).isRelative()) {
114 if (path.isEmpty()) {
115 qCDebug(KDEGAMES_LOG) << "Could not find theme description" << path;
116 return false;
117 }
118 }
119 // default group name
120 if (KGameThemePrivate::s_configGroupNames.isEmpty()) {
121 KGameThemePrivate::s_configGroupNames << QStringLiteral("KGameTheme");
122 }
123 // open file, look for a known config group
124 KConfig config(path, KConfig::SimpleConfig);
125 KConfigGroup group;
126 for (const QString &groupName : std::as_const(KGameThemePrivate::s_configGroupNames)) {
127 if (config.hasGroup(groupName)) {
128 group = config.group(groupName);
129 }
130 }
131 if (!group.isValid()) {
132 qCDebug(KDEGAMES_LOG) << "Could not read theme description at" << path;
133 return false;
134 }
135 // check format version
136 if (group.readEntry("VersionFormat", 1) > 1) {
137 qCDebug(KDEGAMES_LOG) << "Format of theme description too new at" << path;
138 return false;
139 }
140
141 // resolve paths
142 const QFileInfo fi(path);
143 const QDir dir = fi.dir();
144 QString graphicsPath = group.readEntry("FileName", QString());
145 if (!graphicsPath.isEmpty() && QFileInfo(graphicsPath).isRelative())
146 graphicsPath = dir.absoluteFilePath(graphicsPath);
147 QString previewPath = group.readEntry("Preview", QString());
148 if (!previewPath.isEmpty() && QFileInfo(previewPath).isRelative())
149 previewPath = dir.absoluteFilePath(previewPath);
150 // create theme
151 setName(group.readEntry("Name", QString()));
152 setDescription(group.readEntry("Description", QString()));
153 setLicense(group.readEntry("License", QString()));
154 setCopyrightText(group.readEntry("Copyright", QString()));
155 setVersion(group.readEntry("Version", QString()));
156 setWebsite(group.readEntry("Website", QString()));
157 setBugReportUrl(group.readEntry("BugReportUrl", QString()));
158 setAuthor(group.readEntry("Author", QString()));
159 setAuthorEmail(group.readEntry("AuthorEmail", QString()));
160 setGraphicsPath(graphicsPath);
161 setPreviewPath(previewPath);
162 setCustomData(group.entryMap());
163 // store modification date of this file in private property (KGameRenderer
164 // wants to clear its cache also if the theme description changes)
165 setProperty("_k_themeDescTimestamp", fi.lastModified().toSecsSinceEpoch());
166 return true;
167}
168
169#include "moc_kgametheme.cpp"
bool hasGroup(const QString &group) const
KConfigGroup group(const QString &group)
bool isValid() const
QString readEntry(const char *key, const char *aDefault=nullptr) const
QMap< QString, QString > entryMap() const
A theme describes the visual appearance of a game.
Definition kgametheme.h:60
void setBugReportUrl(const QString &bugReportUrl)
void setDescription(const QString &description)
~KGameTheme() override
Destructor.
void setWebsite(const QString &website)
void setPreviewPath(const QString &path)
void setGraphicsPath(const QString &path)
void setCustomData(const QMap< QString, QString > &customData)
void setName(const QString &name)
QMap< QString, QString > customData() const
void setVersion(const QString &version)
void setCopyrightText(const QString &copyrightText)
void setAuthorEmail(const QString &authorEmail)
void setLicense(const QString &license)
void setAuthor(const QString &author)
KGameTheme(const QByteArray &identifier, QObject *parent=nullptr)
Constructor. The identifier must be application-unique.
virtual bool readFromDesktopFile(const QString &path)
Initializes a KGameTheme instance by reading a description file.
qint64 toSecsSinceEpoch() const const
QDir dir() const const
QDateTime lastModified() const const
bool setProperty(const char *name, QVariant &&value)
QString locate(StandardLocation type, const QString &fileName, LocateOptions options)
bool isEmpty() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.