~ubuntu-branches/ubuntu/wily/libkdegames/wily-proposed

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
/***************************************************************************
 *   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 "kgtheme.h"

#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QStandardPaths>

#include <KConfig>
#include <KConfigGroup>

Q_LOGGING_CATEGORY(GAMES_LIB, "games.lib")

class KgTheme::Private
{
    public:
        const QByteArray m_identifier;
        QString m_name, m_description, m_author, m_authorEmail, m_graphicsPath, m_previewPath;
        QMap<QString, QString> m_customData;

        Private(const QByteArray& id) : m_identifier(id) {}

        static QStringList s_configGroupNames;
};

/*static*/ QStringList KgTheme::Private::s_configGroupNames;

KgTheme::KgTheme(const QByteArray& identifier, QObject* parent)
	: QObject(parent)
	, d(new Private(identifier))
{
    QLoggingCategory::setFilterRules(QLatin1Literal("games.lib.debug = true"));
}

KgTheme::~KgTheme()
{
	delete d;
}

QByteArray KgTheme::identifier() const
{
	return d->m_identifier;
}

#define KGTHEME_STRING_PROPERTY(PROP, SETTER) \
	QString KgTheme::PROP() const { return d->m_##PROP; } \
	void KgTheme::SETTER(const QString& val) { d->m_##PROP = val; }

KGTHEME_STRING_PROPERTY(name, setName)
KGTHEME_STRING_PROPERTY(description, setDescription)
KGTHEME_STRING_PROPERTY(author, setAuthor)
KGTHEME_STRING_PROPERTY(authorEmail, setAuthorEmail)
KGTHEME_STRING_PROPERTY(graphicsPath, setGraphicsPath)
KGTHEME_STRING_PROPERTY(previewPath, setPreviewPath)

QMap<QString, QString> KgTheme::customData() const
{
	return d->m_customData;
}

QString KgTheme::customData(const QString& key, const QString& defaultValue) const
{
	return d->m_customData.value(key, defaultValue);
}

void KgTheme::setCustomData(const QMap<QString, QString>& customData)
{
	d->m_customData = customData;
}

bool KgTheme::readFromDesktopFile(const QString& path_)
{
	if (path_.isEmpty())
	{
		qCDebug(GAMES_LIB) << "Refusing to load theme with no name";
		return false;
	}
	//legacy support: relative paths are resolved with KStandardDirs/appdata
	QString path(path_);
	if (QFileInfo(path).isRelative())
	{
		path = QStandardPaths::locate(QStandardPaths::DataLocation, path);
		if (path.isEmpty())
		{
			qCDebug(GAMES_LIB) << "Could not find theme description" << path;
			return false;
		}
	}
	//default group name
	if (Private::s_configGroupNames.isEmpty())
	{
		Private::s_configGroupNames << QLatin1String("KGameTheme");
	}
	//open file, look for a known config group
	KConfig config(path, KConfig::SimpleConfig);
	KConfigGroup group;
	foreach (const QString& groupName, Private::s_configGroupNames)
	{
		if (config.hasGroup(groupName))
		{
			group = config.group(groupName);
		}
	}
	if (!group.isValid())
	{
		qCDebug(GAMES_LIB) << "Could not read theme description at" << path;
		return false;
	}
	//check format version
	if (group.readEntry("VersionFormat", 1) > 1)
	{
		qCDebug(GAMES_LIB) << "Format of theme description too new at" << path;
		return false;
	}

	//resolve paths
	const QFileInfo fi(path);
	const QDir dir = fi.dir();
	QString graphicsPath = group.readEntry("FileName", QString());
	if (!graphicsPath.isEmpty() && QFileInfo(graphicsPath).isRelative())
		graphicsPath = dir.absoluteFilePath(graphicsPath);
	QString previewPath = group.readEntry("Preview", QString());
	if (!previewPath.isEmpty() && QFileInfo(previewPath).isRelative())
		previewPath = dir.absoluteFilePath(previewPath);
	//create theme
	setName(group.readEntry("Name", QString()));
	setDescription(group.readEntry("Description", QString()));
	setAuthor(group.readEntry("Author", QString()));
	setAuthorEmail(group.readEntry("AuthorEmail", QString()));
	setGraphicsPath(graphicsPath);
	setPreviewPath(previewPath);
	setCustomData(group.entryMap());
	//store modification date of this file in private property (KGameRenderer
	//wants to clear its cache also if the theme description changes)
	setProperty("_k_themeDescTimestamp", fi.lastModified().toTime_t());
	return true;
}