KDEGames

kgametheme.cpp
1 /*
2  Copyright (C) 2007 Mauricio Piacentini <[email protected]>
3  Copyright (C) 2007 Matt Williams <[email protected]>
4 
5  This library is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kgametheme.h"
21 
22 #include <KConfig>
23 #include <KConfigGroup>
24 
25 #include <QFile>
26 #include <QFileInfo>
27 #include <QMap>
28 #include <QStandardPaths>
29 #include <QPixmap>
30 
31 Q_LOGGING_CATEGORY(GAMES_LIB, "org.kde.games.lib", QtWarningMsg)
32 
33 class KGameThemePrivate
34 {
35  public:
36  KGameThemePrivate() : loaded(false) {}
37 
38  QMap<QString, QString> themeproperties;
39  QString fullPath;
40  QString fileName;
41  QString graphics;
42  QPixmap preview;
43  QString prefix;
44  QString themeGroup;
45 
46  bool loaded;
47 };
48 
49 KGameTheme::KGameTheme(const QString &themeGroup)
50  : d(new KGameThemePrivate)
51 {
52  d->themeGroup = themeGroup;
53  //KGlobal::dirs()->addResourceType("gametheme", KStandardDirs::kde_default("data") + KGlobal::mainComponent().componentName());
54 }
55 
56 KGameTheme::~KGameTheme() {
57  delete d;
58 }
59 
61 {
62  return load(QStringLiteral( "themes/default.desktop" )); //TODO make this editable to match custom directories.
63  // If this ever changes change findThemes in KGameThemeSelectorPrivate too
64 }
65 
66 #define kThemeVersionFormat 1
67 
68 bool KGameTheme::load(const QString &fileName) {
69  if( fileName.isEmpty() )
70  {
71  qCDebug(GAMES_LIB) << "Refusing to load theme with no name";
72  return false;
73  }
75  qCDebug(GAMES_LIB) << "Attempting to load .desktop at" << filePath;
76  if (filePath.isEmpty()) {
77  return false;
78  }
79 
80  // verify if it is a valid file first and if we can open it
81  QFile themefile(filePath);
82  if (!themefile.open(QIODevice::ReadOnly)) {
83  qCDebug(GAMES_LIB) << "Could not open .desktop theme file" << filePath;
84  return false;
85  }
86  d->prefix = QFileInfo(themefile).absolutePath() + QLatin1Char( '/' );
87  themefile.close();
88 
89  KConfig themeconfig(filePath, KConfig::SimpleConfig);
90  if (!themeconfig.hasGroup(d->themeGroup))
91  {
92  qCDebug(GAMES_LIB) << "Config group" << d->themeGroup << "does not exist in" << filePath;
93  return false;
94  }
95  KConfigGroup group = themeconfig.group(d->themeGroup);
96 
97  //Copy the whole entryMap, so we can inherit generic properties as well, reducing the need to subclass for simple implementations
98  d->themeproperties = group.entryMap();
99 
100  //Version control
101  int themeversion = group.readEntry("VersionFormat",0);
102  //Format is increased when we have incompatible changes, meaning that older clients are not able to use the remaining information safely
103  if (themeversion > kThemeVersionFormat) {
104  return false;
105  }
106 
107  QString graphName = group.readEntry("FileName");
108  //d->graphics = QStandardPaths::locate(QStandardPaths::AppDataLocation, graphName);
109  d->graphics = d->prefix + graphName;
110  if (d->graphics.isEmpty()) return false;
111 
112  // let's see if svg file exists and can be opened
113  QFile svgFile( d->graphics );
114  if ( !svgFile.open( QIODevice::ReadOnly ) ) {
115  qCDebug(GAMES_LIB) << "Could not open file" << d->graphics;
116  return false;
117  }
118 
119  QString previewName = group.readEntry("Preview");
120  //QString graphicsPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, previewName);
121  QString graphicsPath = d->prefix + previewName;
122  d->preview = QPixmap(graphicsPath);
123 
124  d->fileName = fileName;
125  d->fullPath = filePath;
126  d->loaded = true;
127  return true;
128 }
129 
131 {
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  KConfig themeconfig(path(), KConfig::SimpleConfig);
138  KConfigGroup group = themeconfig.group(d->themeGroup);
139  return group.readEntry(key, QString());
140 }
141 
143  if(!d->loaded)
144  {
145  qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
146  return QString();
147  }
148  return d->fullPath;
149 }
150 
152  if(!d->loaded)
153  {
154  qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
155  return QString();
156  }
157  return d->prefix;
158 }
159 
161  if(!d->loaded)
162  {
163  qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
164  return QString();
165  }
166  return d->fileName;
167 }
168 
170  if(!d->loaded)
171  {
172  qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
173  return QString();
174  }
175  return d->graphics;
176 }
177 
179  if(!d->loaded)
180  {
181  qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
182  return QPixmap();
183  }
184  return d->preview;
185 }
186 
188  if(!d->loaded)
189  {
190  qCDebug(GAMES_LIB) << "No theme file has been loaded. KGameTheme::load() or KGameTheme::loadDefault() must be called.";
191  return QString();
192  }
193  return d->themeproperties[key];
194 }
QString path() const
Definition: kgametheme.cpp:142
bool hasGroup(const QString &group) const
virtual QString themeProperty(const QString &key) const
Possible keys:
Definition: kgametheme.cpp:187
virtual QString graphics() const
Definition: kgametheme.cpp:169
bool isEmpty() const const
QString fileName() const
Definition: kgametheme.cpp:160
virtual bool open(QIODevice::OpenMode mode) override
virtual bool load(const QString &file)
Load a specific theme file.
Definition: kgametheme.cpp:68
void load()
Reads the SVG file svgFilename() into DOM.
KConfigGroup group(const QString &group)
virtual void close() override
QPixmap preview() const
Definition: kgametheme.cpp:178
virtual bool loadDefault()
Load the default theme file.
Definition: kgametheme.cpp:60
QString absolutePath() const const
T readEntry(const QString &key, const T &aDefault) const
QString prefix() const
Definition: kgametheme.cpp:151
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:130
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.