1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/***************************************************************************
* Copyright 2012 Stefan Majewsky <majewsky@gmx.net> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License *
* version 2 as published by the Free Software Foundation *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "kgthemeprovider.h"
#include "kgimageprovider_p.h"
#include <QFileInfo>
#include <QStandardPaths>
#include <QGuiApplication>
#include <KConfig>
#include <KConfigGroup>
#include <KSharedConfig>
class KgThemeProvider::Private
{
public:
KgThemeProvider *q;
QString m_name;
QList<const KgTheme*> m_themes;
const QByteArray m_configKey;
const KgTheme* m_currentTheme;
const KgTheme* m_defaultTheme;
//this stores the arguments which were passed to discoverThemes()
QByteArray m_dtResource;
QString m_dtDirectory;
QString m_dtDefaultThemeName;
const QMetaObject* m_dtThemeClass;
//this remembers which themes were already discovered
QStringList m_discoveredThemes;
//this disables the addTheme() lock during rediscoverThemes()
bool m_inRediscover;
Private(KgThemeProvider *parent, const QByteArray& key) : q(parent), m_configKey(key), m_currentTheme(nullptr), m_defaultTheme(nullptr), m_inRediscover(false) {}
void updateThemeName()
{
Q_EMIT q->currentThemeNameChanged(q->currentThemeName());
}
};
KgThemeProvider::KgThemeProvider(const QByteArray& configKey, QObject* parent)
: QObject(parent)
, d(new Private(this, configKey))
{
qRegisterMetaType<const KgTheme*>();
qRegisterMetaType<KgThemeProvider*>();
connect(this, SIGNAL(currentThemeChanged(const KgTheme*)), this, SLOT(updateThemeName()));
}
KgThemeProvider::~KgThemeProvider()
{
if (!d->m_themes.isEmpty())
{
//save current theme in config file (no sync() call here; this will most
//likely be called at application shutdown when others are also writing to
//KGlobal::config(); also KConfig's dtor will sync automatically)
//but do not save if there is no choice; this is esp. helpful for the
//KGameRenderer constructor overload that uses a single KgTheme instance
if (d->m_themes.size() > 1 && !d->m_configKey.isEmpty())
{
KConfigGroup cg(KSharedConfig::openConfig(), "KgTheme");
cg.writeEntry(d->m_configKey.data(), currentTheme()->identifier());
}
//cleanup
while (!d->m_themes.isEmpty())
{
delete const_cast<KgTheme*>(d->m_themes.takeFirst());
}
delete d;
}
}
QString KgThemeProvider::name() const
{
return d->m_name;
}
QList<const KgTheme*> KgThemeProvider::themes() const
{
return d->m_themes;
}
void KgThemeProvider::addTheme(KgTheme* theme)
{
//The intended use is to create the KgThemeProvider object, add themes,
//*then* start to work with the currentLevel(). The first call to
//currentTheme() will load the previous selection from the config, and the
//level list will be considered immutable from this point.
Q_ASSERT_X(d->m_currentTheme == nullptr || d->m_inRediscover,
"KgThemeProvider::addTheme",
"Only allowed before currentTheme() is called."
);
//add theme
d->m_themes.append(theme);
theme->setParent(this);
}
const KgTheme* KgThemeProvider::defaultTheme() const
{
return d->m_defaultTheme;
}
void KgThemeProvider::setDefaultTheme(const KgTheme* theme)
{
if (d->m_currentTheme)
{
qCDebug(GAMES_LIB) << "You're calling setDefaultTheme after the current "
"theme has already been determined. That's not gonna work.";
return;
}
Q_ASSERT(d->m_themes.contains(theme));
d->m_defaultTheme = theme;
}
const KgTheme* KgThemeProvider::currentTheme() const
{
if (d->m_currentTheme)
{
return d->m_currentTheme;
}
Q_ASSERT(!d->m_themes.isEmpty());
//check configuration file for saved theme
if (!d->m_configKey.isEmpty())
{
KConfigGroup cg(KSharedConfig::openConfig(), "KgTheme");
const QByteArray id = cg.readEntry(d->m_configKey.data(), QByteArray());
//look for a theme with this id
for (const KgTheme* theme : qAsConst(d->m_themes)) {
if (theme->identifier() == id)
{
return d->m_currentTheme = theme;
}
}
}
//fall back to default theme (or first theme if no default specified)
return d->m_currentTheme = (d->m_defaultTheme ? d->m_defaultTheme : d->m_themes.first());
}
void KgThemeProvider::setCurrentTheme(const KgTheme* theme)
{
Q_ASSERT(d->m_themes.contains(theme));
if (d->m_currentTheme != theme)
{
d->m_currentTheme = theme;
Q_EMIT currentThemeChanged(theme);
}
}
QString KgThemeProvider::currentThemeName() const
{
return currentTheme()->name();
}
void KgThemeProvider::discoverThemes(const QByteArray& resource, const QString& directory, const QString& defaultThemeName, const QMetaObject* themeClass)
{
d->m_dtResource = resource;
d->m_dtDirectory = directory;
d->m_dtDefaultThemeName = defaultThemeName;
d->m_dtThemeClass = themeClass;
rediscoverThemes();
}
// Function to replace KStandardDirs::relativeLocation()
static QString relativeToApplications(const QString& file)
{
const QString canonical = QFileInfo(file).canonicalFilePath();
const QStringList dirs = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
for (const QString& base : dirs) {
if (canonical.startsWith(base))
return canonical.mid(base.length()+1);
}
return file;
}
void KgThemeProvider::rediscoverThemes()
{
if (d->m_dtResource.isEmpty())
{
return; //discoverThemes() was never called
}
KgTheme* defaultTheme = nullptr;
d->m_inRediscover = true;
const QString defaultFileName = d->m_dtDefaultThemeName + QLatin1String(".desktop");
QStringList themePaths;
const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::AppDataLocation, d->m_dtDirectory, QStandardPaths::LocateDirectory);
for (const QString &dir : dirs) {
const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.desktop"));
for (const QString &file : fileNames) {
if (!themePaths.contains(file)) {
themePaths.append(dir + QLatin1Char('/') + file);
}
}
}
//create themes from result, order default theme at the front (that's not
//needed by KgThemeProvider, but nice for the theme selector)
QList<KgTheme*> themes;
for (const QString& themePath : qAsConst(themePaths)) {
const QFileInfo fi(themePath);
if (d->m_discoveredThemes.contains(fi.fileName()))
{
continue;
}
d->m_discoveredThemes << fi.fileName();
//the identifier is constructed such that it is compatible with
//KGameTheme (e.g. "themes/default.desktop")
const QByteArray id = relativeToApplications(themePath).toUtf8();
//create theme
KgTheme* theme;
if (d->m_dtThemeClass)
{
theme = qobject_cast<KgTheme*>(d->m_dtThemeClass->newInstance(
Q_ARG(QByteArray, id), Q_ARG(QObject*, this)
));
Q_ASSERT_X(theme,
"KgThemeProvider::discoverThemes",
"Could not create theme instance. Is your constructor Q_INVOKABLE?"
);
}
else
{
theme = new KgTheme(id, this);
}
//silently discard invalid theme files
if (!theme->readFromDesktopFile(themePath))
{
delete theme;
continue;
}
//order default theme at the front (that's not necessarily needed by
//KgThemeProvider, but nice for the theme selector)
if (fi.fileName() == defaultFileName)
{
themes.prepend(theme);
defaultTheme = theme;
}
else
{
themes.append(theme);
}
}
//add themes in the determined order
for (KgTheme* theme : qAsConst(themes))
{
addTheme(theme);
}
if(defaultTheme != nullptr)
{
setDefaultTheme(defaultTheme);
}
else if(d->m_defaultTheme == nullptr && themes.count() != 0)
{
setDefaultTheme(themes.value(0));
}
d->m_inRediscover = false;
}
QPixmap KgThemeProvider::generatePreview(const KgTheme* theme, const QSize& size)
{
const qreal dpr = qApp->testAttribute(Qt::AA_UseHighDpiPixmaps) ? qApp->devicePixelRatio() : 1;
QPixmap pixmap = QPixmap(theme->previewPath()).scaled(size * dpr, Qt::KeepAspectRatio);
pixmap.setDevicePixelRatio(dpr);
return pixmap;
}
void KgThemeProvider::setDeclarativeEngine(const QString& name, QQmlEngine* engine)
{
if (d->m_name != name) { // prevent multiple declarations
d->m_name = name;
engine->addImageProvider(name, new KgImageProvider(this));
engine->rootContext()->setContextProperty(name, this);
}
}
#include "moc_kgthemeprovider.cpp"
|