1/******************************************************************************
2* Copyright 2007 by Riccardo Iaconelli <riccardo@kde.org> *
3* *
4* This library is free software; you can redistribute it and/or *
5* modify it under the terms of the GNU Library General Public *
6* License as published by the Free Software Foundation; either *
7* version 2 of the License, or (at your option) any later version. *
8* *
9* This library is distributed in the hope that it will be useful, *
10* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12* Library General Public License for more details. *
13* *
14* You should have received a copy of the GNU Library General Public License *
15* along with this library; see the file COPYING.LIB. If not, write to *
16* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17* Boston, MA 02110-1301, USA. *
18*******************************************************************************/
19
20#include <packagemetadata.h>
21
22#include <QDir>
23
24#include <kconfiggroup.h>
25#include <kdesktopfile.h>
26
27namespace Plasma
28{
29
30class PackageMetadataPrivate
31{
32 public:
33 PackageMetadataPrivate()
34 : type("Service")
35 {
36 }
37
38 QString name;
39 QString icon;
40 QString description;
41 QStringList keywords;
42 QString author;
43 QString email;
44 QString version;
45 QString website;
46 QString license;
47 QString app;
48 QString category;
49 QString requiredVersion;
50 QString pluginName;
51 QString type;
52 QString serviceType;
53 QString api;
54 KUrl location;
55};
56
57PackageMetadata::PackageMetadata(const PackageMetadata &other)
58 : d(new PackageMetadataPrivate(*other.d))
59{
60}
61
62PackageMetadata &PackageMetadata::operator=(const PackageMetadata &other)
63{
64 *d = *other.d;
65 return *this;
66}
67
68PackageMetadata::PackageMetadata(const QString &path)
69 : d(new PackageMetadataPrivate)
70{
71 read(path);
72}
73
74PackageMetadata::~PackageMetadata()
75{
76 delete d;
77}
78
79bool PackageMetadata::isValid() const
80{
81 return ! (d->name.isEmpty() ||
82 d->author.isEmpty() ||
83 d->license.isEmpty() ||
84 d->type.isEmpty());
85}
86
87void PackageMetadata::write(const QString &filename) const
88{
89 KDesktopFile cfg(filename);
90 KConfigGroup config = cfg.desktopGroup();
91 config.writeEntry("Encoding", "UTF-8");
92
93 config.writeEntry("Name", d->name);
94 config.writeEntry("Icon", d->icon);
95 config.writeEntry("Comment", d->description);
96 config.writeEntry("Keywords", d->keywords);
97 config.deleteEntry("X-KDE-Keywords");
98 config.writeEntry("X-KDE-ServiceTypes", d->serviceType);
99 config.deleteEntry("ServiceTypes");
100 config.writeEntry("X-KDE-PluginInfo-Name", d->pluginName);
101 config.writeEntry("X-KDE-PluginInfo-Author", d->author);
102 config.writeEntry("X-KDE-PluginInfo-Email", d->email);
103 config.writeEntry("X-KDE-PluginInfo-Version", d->version);
104 config.writeEntry("X-KDE-PluginInfo-Website", d->website);
105 config.writeEntry("X-KDE-PluginInfo-License", d->license);
106 config.writeEntry("X-KDE-PluginInfo-Category", d->category);
107 config.writeEntry("X-Plasma-API", d->api);
108 config.writeEntry("X-KDE-ParentApp", d->app);
109 config.writeEntry("Type", d->type);
110 config.writeEntry("X-Plasma-RemoteLocation", d->location);
111}
112
113void PackageMetadata::read(const QString &filename)
114{
115 if (filename.isEmpty()) {
116 return;
117 }
118
119 KDesktopFile cfg(filename);
120 KConfigGroup config = cfg.desktopGroup();
121
122 d->name = config.readEntry("Name", d->name);
123 d->icon = config.readEntry("Icon", d->icon);
124 d->description = config.readEntry("Comment", d->description);
125 bool hasKeywords = config.hasKey("Keywords");
126 bool hasXKdeKeywords = config.hasKey("X-KDE-Keywords");
127 if (hasKeywords && hasXKdeKeywords) {
128 d->keywords = config.readEntry("Keywords", d->keywords);
129 d->keywords.append(config.readEntry("X-KDE-Keywords", d->keywords));
130 } else if (hasKeywords) {
131 d->keywords = config.readEntry("Keywords", d->keywords);
132 } else if (hasXKdeKeywords) {
133 d->keywords = config.readEntry("X-KDE-Keywords", d->keywords);
134 }
135 bool hasServiceTypes = config.hasKey("ServiceTypes");
136 bool hasXKdeServiceTypes = config.hasKey("X-KDE-ServiceTypes");
137 if (hasServiceTypes && hasXKdeServiceTypes) {
138 d->serviceType = config.readEntry("ServiceTypes", d->serviceType);
139 d->serviceType.append(',');
140 d->serviceType.append(config.readEntry("X-KDE-ServiceTypes", d->serviceType));
141 } else if (hasServiceTypes) {
142 d->serviceType = config.readEntry("ServiceTypes", d->serviceType);
143 } else if (hasXKdeServiceTypes) {
144 d->serviceType = config.readEntry("X-KDE-ServiceTypes", d->serviceType);
145 }
146 d->pluginName = config.readEntry("X-KDE-PluginInfo-Name", d->pluginName);
147 d->author = config.readEntry("X-KDE-PluginInfo-Author", d->author);
148 d->email = config.readEntry("X-KDE-PluginInfo-Email", d->email);
149 d->version = config.readEntry("X-KDE-PluginInfo-Version", d->version);
150 d->website = config.readEntry("X-KDE-PluginInfo-Website", d->website);
151 d->license = config.readEntry("X-KDE-PluginInfo-License", d->license);
152 d->category = config.readEntry("X-KDE-PluginInfo-Category", d->category);
153 d->api = config.readEntry("X-Plasma-API", d->api);
154 d->app = config.readEntry("X-KDE-ParentApp", d->app);
155 d->type = config.readEntry("Type", d->type);
156 d->location = config.readEntry("X-Plasma-RemoteLocation", d->location);
157}
158
159QString PackageMetadata::name() const
160{
161 return d->name;
162}
163
164QString PackageMetadata::description() const
165{
166 return d->description;
167}
168
169QString PackageMetadata::serviceType() const
170{
171 return d->serviceType;
172}
173
174QString PackageMetadata::author() const
175{
176 return d->author;
177}
178
179QString PackageMetadata::email() const
180{
181 return d->email;
182}
183
184QString PackageMetadata::icon() const
185{
186 return d->icon;
187}
188
189void PackageMetadata::setIcon(const QString &icon)
190{
191 d->icon = icon;
192}
193
194QString PackageMetadata::version() const
195{
196 return d->version;
197}
198
199QString PackageMetadata::website() const
200{
201 return d->website;
202}
203
204QString PackageMetadata::license() const
205{
206 return d->license;
207}
208
209QString PackageMetadata::application() const
210{
211 return d->app;
212}
213
214QString PackageMetadata::category() const
215{
216 return d->category;
217}
218
219void PackageMetadata::setKeywords(const QStringList &keywords)
220{
221 d->keywords = keywords;
222}
223
224QStringList PackageMetadata::keywords() const
225{
226 return d->keywords;
227}
228
229QString PackageMetadata::requiredVersion() const
230{
231 return d->requiredVersion;
232}
233
234KUrl PackageMetadata::remoteLocation() const
235{
236 return d->location;
237}
238
239QString PackageMetadata::type() const
240{
241 return d->type;
242}
243
244QString PackageMetadata::implementationApi() const
245{
246 return d->api;
247}
248
249void PackageMetadata::setImplementationApi(const QString &api)
250{
251 d->api = api;
252}
253
254QString PackageMetadata::pluginName() const
255{
256 return d->pluginName;
257}
258
259void PackageMetadata::setPluginName(const QString &pluginName)
260{
261 d->pluginName = pluginName;
262}
263
264void PackageMetadata::setName(const QString &name)
265{
266 d->name = name;
267}
268
269void PackageMetadata::setDescription(const QString &description)
270{
271 d->description = description;
272}
273
274void PackageMetadata::setServiceType(const QString &serviceType)
275{
276 d->serviceType = serviceType;
277}
278
279void PackageMetadata::setAuthor(const QString &author)
280{
281 d->author = author;
282}
283
284void PackageMetadata::setEmail(const QString &email)
285{
286 d->email = email;
287}
288
289void PackageMetadata::setVersion(const QString &version)
290{
291 d->version = version;
292}
293
294void PackageMetadata::setWebsite(const QString &website)
295{
296 d->website = website;
297}
298
299void PackageMetadata::setLicense(const QString &license)
300{
301 d->license = license;
302}
303
304void PackageMetadata::setApplication(const QString &application)
305{
306 d->app = application;
307}
308
309void PackageMetadata::setCategory(const QString &category)
310{
311 d->category = category;
312}
313
314void PackageMetadata::setRequiredVersion(const QString &requiredVersion)
315{
316 d->requiredVersion = requiredVersion;
317}
318
319void PackageMetadata::setRemoteLocation(const KUrl &location)
320{
321 d->location = location;
322}
323
324void PackageMetadata::setType(const QString &type)
325{
326 d->type = type;
327}
328
329} // namespace Plasma
330
331