KDEGames

kgametheme.cpp
1 /*
2  SPDX-FileCopyrightText: 2007 Mauricio Piacentini <[email protected]>
3  SPDX-FileCopyrightText: 2007 Matt Williams <[email protected]>
4 
5  SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #include "kgametheme.h"
9 
10 // KF
11 #include <KConfig>
12 #include <KConfigGroup>
13 // Qt
14 #include <QFile>
15 #include <QFileInfo>
16 #include <QMap>
17 #include <QStandardPaths>
18 #include <QPixmap>
19 
20 Q_LOGGING_CATEGORY(GAMES_LIB, "org.kde.games.lib", QtWarningMsg)
21 
22 #if KDEGAMESPRIVATE_BUILD_DEPRECATED_SINCE(4, 9)
23 
24 class KGameThemePrivate
25 {
26  public:
27  KGameThemePrivate() : loaded(false) {}
28 
29  QMap<QString, QString> themeproperties;
30  QString fullPath; ///< Full path e.g. "/opt/kde/share/apps/appname/default.desktop"
31  QString fileName; ///< just e.g. "default.desktop"
32  QString graphics; ///< The full path of the svg file
33  QPixmap preview;
34  QString prefix; ///< Filepath of the .desktop file without the filename e.g. "/opt/kde/share/apps/appname/"
35  QString themeGroup;
36 
37  bool loaded;
38 };
39 
40 KGameTheme::KGameTheme(const QString &themeGroup)
41  : d(new KGameThemePrivate)
42 {
43  d->themeGroup = themeGroup;
44  //KGlobal::dirs()->addResourceType("gametheme", KStandardDirs::kde_default("data") + KGlobal::mainComponent().componentName());
45 }
46 
47 KGameTheme::~KGameTheme() = default;
48 
50 {
51  return load(QStringLiteral( "themes/default.desktop" )); //TODO make this editable to match custom directories.
52  // If this ever changes change findThemes in KGameThemeSelectorPrivate too
53 }
54 
55 #define kThemeVersionFormat 1
56 
57 bool KGameTheme::load(const QString &fileName) {
58  if( fileName.isEmpty() )
59  {
60  qCDebug(GAMES_LIB) << "Refusing to load theme with no name";
61  return false;
62  }
64  qCDebug(GAMES_LIB) << "Attempting to load .desktop at" << filePath;
65  if (filePath.isEmpty()) {
66  return false;
67  }
68 
69  // verify if it is a valid file first and if we can open it
70  QFile themefile(filePath);
71  if (!themefile.open(QIODevice::ReadOnly)) {
72  qCDebug(GAMES_LIB) << "Could not open .desktop theme file" << filePath;
73  return false;
74  }
75  d->prefix = QFileInfo(themefile).absolutePath() + QLatin1Char( '/' );
76  themefile.close();
77 
78  KConfig themeconfig(filePath, KConfig::SimpleConfig);
79  if (!themeconfig.hasGroup(d->themeGroup))
80  {
81  qCDebug(GAMES_LIB) << "Config group" << d->themeGroup << "does not exist in" << filePath;
82  return false;
83  }
84  KConfigGroup group = themeconfig.group(d->themeGroup);
85 
86  //Copy the whole entryMap, so we can inherit generic properties as well, reducing the need to subclass for simple implementations
87  d->themeproperties = group.entryMap();
88 
89  //Version control
90  int themeversion = group.readEntry("VersionFormat",0);
91  //Format is increased when we have incompatible changes, meaning that older clients are not able to use the remaining information safely
92  if (themeversion > kThemeVersionFormat) {
93  return false;
94  }
95 
96  QString graphName = group.readEntry("FileName");
97  //d->graphics = QStandardPaths::locate(QStandardPaths::AppDataLocation, graphName);
98  d->graphics = d->prefix + graphName;
99  if (d->graphics.isEmpty()) return false;
100 
101  // let's see if svg file exists and can be opened
102  QFile svgFile( d->graphics );
103  if ( !svgFile.open( QIODevice::ReadOnly ) ) {
104  qCDebug(GAMES_LIB) << "Could not open file" << d->graphics;
105  return false;
106  }
107 
108  QString previewName = group.readEntry("Preview");
109  //QString graphicsPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, previewName);
110  QString graphicsPath = d->prefix + previewName;
111  d->preview = QPixmap(graphicsPath);
112 
113  d->fileName = fileName;
114  d->fullPath = filePath;
115  d->loaded = true;
116  return true;
117 }
118 
120 {
121  if(!d->loaded)
122  {
123  qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
124  return QString();
125  }
126  KConfig themeconfig(path(), KConfig::SimpleConfig);
127  KConfigGroup group = themeconfig.group(d->themeGroup);
128  return group.readEntry(key, QString());
129 }
130 
132  if(!d->loaded)
133  {
134  qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
135  return QString();
136  }
137  return d->fullPath;
138 }
139 
141  if(!d->loaded)
142  {
143  qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
144  return QString();
145  }
146  return d->prefix;
147 }
148 
150  if(!d->loaded)
151  {
152  qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
153  return QString();
154  }
155  return d->fileName;
156 }
157 
159  if(!d->loaded)
160  {
161  qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
162  return QString();
163  }
164  return d->graphics;
165 }
166 
168  if(!d->loaded)
169  {
170  qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
171  return QPixmap();
172  }
173  return d->preview;
174 }
175 
177  if(!d->loaded)
178  {
179  qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
180  return QString();
181  }
182  return d->themeproperties[key];
183 }
184 
185 #endif
QString path() const
Definition: kgametheme.cpp:131
bool hasGroup(const QString &group) const
virtual QString themeProperty(const QString &key) const
Possible keys:
Definition: kgametheme.cpp:176
virtual QString graphics() const
Definition: kgametheme.cpp:158
bool isEmpty() const const
QString fileName() const
Definition: kgametheme.cpp:149
virtual bool open(QIODevice::OpenMode mode) override
virtual bool load(const QString &file)
Load a specific theme file.
Definition: kgametheme.cpp:57
void load()
Reads the SVG file svgFilename() into DOM.
KConfigGroup group(const QString &group)
virtual void close() override
QPixmap preview() const
Definition: kgametheme.cpp:167
virtual bool loadDefault()
Load the default theme file.
Definition: kgametheme.cpp:49
QString absolutePath() const const
T readEntry(const QString &key, const T &aDefault) const
QString prefix() const
Definition: kgametheme.cpp:140
QMap< QString, QString > entryMap() const
QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
QString property(const QString &key) const
Definition: kgametheme.cpp:119
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.