1 | /*************************************************************************** |
2 | * Copyright 2012 Stefan Majewsky <majewsky@gmx.net> * |
3 | * * |
4 | * This program is free software; you can redistribute it and/or modify * |
5 | * it under the terms of the GNU Library General Public License * |
6 | * version 2 as published by the Free Software Foundation * |
7 | * * |
8 | * This program is distributed in the hope that it will be useful, * |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
11 | * GNU Library General Public License for more details. * |
12 | * * |
13 | * You should have received a copy of the GNU Library General Public * |
14 | * License along with this program; if not, write to the * |
15 | * Free Software Foundation, Inc., * |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
17 | ***************************************************************************/ |
18 | |
19 | #include "kgtheme.h" |
20 | |
21 | #include <QtCore/QDir> |
22 | #include <QtCore/QFileInfo> |
23 | #include <KDE/KConfig> |
24 | #include <KDE/KConfigGroup> |
25 | #include <KDE/KDebug> |
26 | #include <KDE/KStandardDirs> |
27 | |
28 | class KgTheme::Private |
29 | { |
30 | public: |
31 | const QByteArray m_identifier; |
32 | QString m_name, m_description, m_author, m_authorEmail, m_graphicsPath, m_previewPath; |
33 | QMap<QString, QString> m_customData; |
34 | |
35 | Private(const QByteArray& id) : m_identifier(id) {} |
36 | |
37 | static QStringList s_configGroupNames; |
38 | }; |
39 | |
40 | /*static*/ QStringList KgTheme::Private::s_configGroupNames; |
41 | |
42 | KgTheme::KgTheme(const QByteArray& identifier, QObject* parent) |
43 | : QObject(parent) |
44 | , d(new Private(identifier)) |
45 | { |
46 | } |
47 | |
48 | KgTheme::~KgTheme() |
49 | { |
50 | delete d; |
51 | } |
52 | |
53 | QByteArray KgTheme::identifier() const |
54 | { |
55 | return d->m_identifier; |
56 | } |
57 | |
58 | #define KGTHEME_STRING_PROPERTY(PROP, SETTER) \ |
59 | QString KgTheme::PROP() const { return d->m_##PROP; } \ |
60 | void KgTheme::SETTER(const QString& val) { d->m_##PROP = val; } |
61 | |
62 | KGTHEME_STRING_PROPERTY(name, setName) |
63 | KGTHEME_STRING_PROPERTY(description, setDescription) |
64 | KGTHEME_STRING_PROPERTY(author, setAuthor) |
65 | KGTHEME_STRING_PROPERTY(authorEmail, setAuthorEmail) |
66 | KGTHEME_STRING_PROPERTY(graphicsPath, setGraphicsPath) |
67 | KGTHEME_STRING_PROPERTY(previewPath, setPreviewPath) |
68 | |
69 | QMap<QString, QString> KgTheme::customData() const |
70 | { |
71 | return d->m_customData; |
72 | } |
73 | |
74 | QString KgTheme::customData(const QString& key, const QString& defaultValue) const |
75 | { |
76 | return d->m_customData.value(key, defaultValue); |
77 | } |
78 | |
79 | void KgTheme::setCustomData(const QMap<QString, QString>& customData) |
80 | { |
81 | d->m_customData = customData; |
82 | } |
83 | |
84 | bool KgTheme::readFromDesktopFile(const QString& path_) |
85 | { |
86 | if (path_.isEmpty()) |
87 | { |
88 | kDebug(11000) << "Refusing to load theme with no name" ; |
89 | return false; |
90 | } |
91 | //legacy support: relative paths are resolved with KStandardDirs/appdata |
92 | QString path(path_); |
93 | if (QFileInfo(path).isRelative()) |
94 | { |
95 | path = KStandardDirs::locate("appdata" , path); |
96 | if (path.isEmpty()) |
97 | { |
98 | kDebug(11000) << "Could not find theme description" << path; |
99 | return false; |
100 | } |
101 | } |
102 | //default group name |
103 | if (Private::s_configGroupNames.isEmpty()) |
104 | { |
105 | Private::s_configGroupNames << QLatin1String("KGameTheme" ); |
106 | } |
107 | //open file, look for a known config group |
108 | KConfig config(path, KConfig::SimpleConfig); |
109 | KConfigGroup group; |
110 | foreach (const QString& groupName, Private::s_configGroupNames) |
111 | { |
112 | if (config.hasGroup(groupName)) |
113 | { |
114 | group = config.group(groupName); |
115 | } |
116 | } |
117 | if (!group.isValid()) |
118 | { |
119 | kDebug(11000) << "Could not read theme description at" << path; |
120 | return false; |
121 | } |
122 | //check format version |
123 | if (group.readEntry("VersionFormat" , 1) > 1) |
124 | { |
125 | kDebug(11000) << "Format of theme description too new at" << path; |
126 | return false; |
127 | } |
128 | |
129 | //resolve paths |
130 | const QFileInfo fi(path); |
131 | const QDir dir = fi.dir(); |
132 | QString graphicsPath = group.readEntry("FileName" , QString()); |
133 | if (!graphicsPath.isEmpty() && QFileInfo(graphicsPath).isRelative()) |
134 | graphicsPath = dir.absoluteFilePath(graphicsPath); |
135 | QString previewPath = group.readEntry("Preview" , QString()); |
136 | if (!previewPath.isEmpty() && QFileInfo(previewPath).isRelative()) |
137 | previewPath = dir.absoluteFilePath(previewPath); |
138 | //create theme |
139 | setName(group.readEntry("Name" , QString())); |
140 | setDescription(group.readEntry("Description" , QString())); |
141 | setAuthor(group.readEntry("Author" , QString())); |
142 | setAuthorEmail(group.readEntry("AuthorEmail" , QString())); |
143 | setGraphicsPath(graphicsPath); |
144 | setPreviewPath(previewPath); |
145 | setCustomData(group.entryMap()); |
146 | //store modification date of this file in private property (KGameRenderer |
147 | //wants to clear its cache also if the theme description changes) |
148 | setProperty("_k_themeDescTimestamp" , fi.lastModified().toTime_t()); |
149 | return true; |
150 | } |
151 | |
152 | #include "kgtheme.moc" |
153 | |