• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KNewStuff

  • knewstuff
  • knewstuff2
  • core
entryhandler.cpp
Go to the documentation of this file.
1/*
2 This file is part of KNewStuff2.
3 Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
4 Copyright (c) 2003 - 2007 Josef Spillner <spillner@kde.org>
5 Copyright (c) 2007 Dirk Mueller <mueller@kde.org>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include "entryhandler.h"
22
23#include <kdebug.h>
24
25using namespace KNS;
26
27EntryHandler::EntryHandler(const Entry& entry)
28{
29 init();
30 mEntry = entry;
31 mEntryXML = serializeElement(entry);
32}
33
34EntryHandler::EntryHandler(const QDomElement& entryxml)
35{
36 init();
37 mEntryXML = entryxml;
38 mEntry = deserializeElement(entryxml);
39}
40
41void EntryHandler::init()
42{
43 mValid = false;
44 mCompat = false;
45}
46
47void EntryHandler::setCompatibilityFormat()
48{
49 mCompat = true;
50}
51
52bool EntryHandler::isValid()
53{
54 return mValid;
55}
56
57QDomElement EntryHandler::entryXML()
58{
59 return mEntryXML;
60}
61
62Entry EntryHandler::entry()
63{
64 return mEntry;
65}
66
67Entry *EntryHandler::entryptr()
68{
69 Entry *entry = new Entry();
70 entry->setName(mEntry.name());
71 entry->setAuthor(mEntry.author());
72 entry->setCategory(mEntry.category());
73 entry->setLicense(mEntry.license());
74 entry->setSummary(mEntry.summary());
75 entry->setVersion(mEntry.version());
76 entry->setRelease(mEntry.release());
77 entry->setReleaseDate(mEntry.releaseDate());
78 entry->setPayload(mEntry.payload());
79 entry->setPreview(mEntry.preview());
80 entry->setRating(mEntry.rating());
81 entry->setDownloads(mEntry.downloads());
82 entry->setInstalledFiles(mEntry.installedFiles());
83 entry->setIdNumber(mEntry.idNumber());
84 return entry;
85}
86
87QDomElement EntryHandler::serializeElement(const Entry& entry)
88{
89 QDomDocument doc;
90
91 QDomElement el = doc.createElement("stuff");
92 el.setAttribute("category", entry.category());
93
94 KTranslatable name = entry.name();
95
96 QStringList::ConstIterator it;
97 QDomElement e;
98 QStringList langs;
99
100 langs = name.languages();
101 for (it = langs.constBegin(); it != langs.constEnd(); ++it) {
102 e = addElement(doc, el, "name", name.translated(*it));
103 e.setAttribute("lang", *it);
104 }
105
106 QDomElement author = addElement(doc, el, "author", entry.author().name());
107 if (!entry.author().email().isEmpty())
108 author.setAttribute("email", entry.author().email());
109 if (!entry.author().homepage().isEmpty())
110 author.setAttribute("homepage", entry.author().homepage());
111 if (!entry.author().jabber().isEmpty())
112 author.setAttribute("im", entry.author().jabber());
113 // FIXME: 'jabber' or 'im'? consult with kopete guys...
114
115 (void)addElement(doc, el, "licence", entry.license()); // krazy:exclude=spelling
116 (void)addElement(doc, el, "version", entry.version());
117 if (mCompat)
118 (void)addElement(doc, el, "release", QString::number(entry.release()));
119 if ((entry.rating() > 0) || (entry.downloads() > 0)) {
120 (void)addElement(doc, el, "rating", QString::number(entry.rating()));
121 (void)addElement(doc, el, "downloads", QString::number(entry.downloads()));
122 }
123 if (!entry.signature().isEmpty()) {
124 (void)addElement(doc, el, "signature", entry.signature());
125 }
126 if (!entry.checksum().isEmpty()) {
127 (void)addElement(doc, el, "checksum", entry.checksum());
128 }
129 foreach(const QString &file, entry.installedFiles()) {
130 (void)addElement(doc, el, "installedfile", file);
131 }
132 if (entry.idNumber() > 0) {
133 addElement(doc, el, "id", QString::number(entry.idNumber()));
134 }
135
136 (void)addElement(doc, el, "releasedate",
137 entry.releaseDate().toString(Qt::ISODate));
138
139 KTranslatable summary = entry.summary();
140 KTranslatable preview = entry.preview();
141 KTranslatable payload = entry.payload();
142
143 langs = summary.languages();
144 for (it = langs.constBegin(); it != langs.constEnd(); ++it) {
145 e = addElement(doc, el, "summary", summary.translated(*it));
146 e.setAttribute("lang", *it);
147 }
148
149 langs = preview.languages();
150 for (it = langs.constBegin(); it != langs.constEnd(); ++it) {
151 e = addElement(doc, el, "preview", KUrl(preview.translated(*it)).fileName());
152 e.setAttribute("lang", *it);
153 }
154
155 langs = payload.languages();
156 for (it = langs.constBegin(); it != langs.constEnd(); ++it) {
157 e = addElement(doc, el, "payload", KUrl(payload.translated(*it)).fileName());
158 e.setAttribute("lang", *it);
159 }
160
161 return el;
162}
163
164Entry EntryHandler::deserializeElement(const QDomElement& entryxml)
165{
166 Entry entry;
167 KTranslatable name, summary, preview, payload;
168 int idNumber = 0;
169 QStringList installedFiles;
170
171 if (entryxml.tagName() != "stuff") return entry;
172
173 if (!mCompat) {
174 QString category = entryxml.attribute("category");
175 entry.setCategory(category);
176 }
177
178 QDomNode n;
179 for (n = entryxml.firstChild(); !n.isNull(); n = n.nextSibling()) {
180 QDomElement e = n.toElement();
181 if (e.tagName() == "name") {
182 QString lang = e.attribute("lang");
183 name.addString(lang, e.text().trimmed());
184 } else if (e.tagName() == "author") {
185 Author author;
186 QString email = e.attribute("email");
187 QString jabber = e.attribute("im");
188 QString homepage = e.attribute("homepage");
189 author.setName(e.text().trimmed());
190 author.setEmail(email);
191 author.setJabber(jabber);
192 author.setHomepage(homepage);
193 entry.setAuthor(author);
194 } else if (e.tagName() == "licence") { // krazy:exclude=spelling
195 entry.setLicense(e.text().trimmed());
196 } else if (e.tagName() == "summary") {
197 QString lang = e.attribute("lang");
198 //kDebug() << "adding " << e.tagName() << " to summary as language " << lang;
199 summary.addString(lang, e.text().trimmed());
200 } else if (e.tagName() == "version") {
201 entry.setVersion(e.text().trimmed());
202 } else if (e.tagName() == "release") {
203 if (mCompat) {
204 entry.setRelease(e.text().toInt());
205 }
206 } else if (e.tagName() == "releasedate") {
207 QDate date = QDate::fromString(e.text().trimmed(), Qt::ISODate);
208 entry.setReleaseDate(date);
209 } else if (e.tagName() == "preview") {
210 QString lang = e.attribute("lang");
211 preview.addString(lang, e.text().trimmed());
212 } else if (e.tagName() == "payload") {
213 QString lang = e.attribute("lang");
214 payload.addString(lang, e.text().trimmed());
215 } else if (e.tagName() == "rating") {
216 entry.setRating(e.text().toInt());
217 } else if (e.tagName() == "downloads") {
218 entry.setDownloads(e.text().toInt());
219 } else if (e.tagName() == "category") {
220 if (mCompat) {
221 entry.setCategory(e.text());
222 }
223 } else if (e.tagName() == "signature") {
224 entry.setSignature(e.text());
225 } else if (e.tagName() == "checksum") {
226 entry.setChecksum(e.text());
227 } else if (e.tagName() == "installedfile") {
228 installedFiles << e.text();
229 } else if (e.tagName() == "id") {
230 idNumber = e.text().toInt();
231 //kDebug() << "got id number: " << idNumber;
232 }
233 }
234
235 entry.setName(name);
236 entry.setSummary(summary);
237 entry.setPreview(preview);
238 entry.setPayload(payload);
239 entry.setInstalledFiles(installedFiles);
240 entry.setIdNumber(idNumber);
241
242 // Validation
243
244 if (entry.name().isEmpty()) {
245 kWarning(550) << "EntryHandler: no name given";
246 return entry;
247 }
248
249 if (entry.payload().isEmpty()) {
250 kWarning(550) << "EntryHandler: no payload URL given";
251 return entry;
252 }
253
254 // Entry is valid
255
256 mValid = true;
257 return entry;
258}
259
260QDomElement EntryHandler::addElement(QDomDocument& doc, QDomElement& parent,
261 const QString& tag, const QString& value)
262{
263 QDomElement n = doc.createElement(tag);
264 n.appendChild(doc.createTextNode(value));
265 parent.appendChild(n);
266
267 return n;
268}
KNS::Author::homepage
QString homepage() const
Retrieve the author's homepage.
Definition knewstuff2/core/author.cpp:88
KNS::Author::setJabber
void setJabber(const QString &jabber)
Sets the jabber address of the author.
Definition knewstuff2/core/author.cpp:73
KNS::Author::setName
void setName(const QString &name)
Sets the full name of the author.
Definition knewstuff2/core/author.cpp:53
KNS::Author::setEmail
void setEmail(const QString &email)
Sets the email address of the author.
Definition knewstuff2/core/author.cpp:63
KNS::Author::setHomepage
void setHomepage(const QString &homepage)
Sets the homepage of the author.
Definition knewstuff2/core/author.cpp:83
KNS::Author::jabber
QString jabber() const
Retrieve the author's jabber address.
Definition knewstuff2/core/author.cpp:78
KNS::Author::name
QString name() const
Retrieve the author's name.
Definition knewstuff2/core/author.cpp:58
KNS::Author::email
QString email() const
Retrieve the author's email address.
Definition knewstuff2/core/author.cpp:68
KNS::EntryHandler::setCompatibilityFormat
void setCompatibilityFormat()
Definition entryhandler.cpp:47
KNS::EntryHandler::entryXML
QDomElement entryXML()
Definition entryhandler.cpp:57
KNS::EntryHandler::isValid
bool isValid()
Definition entryhandler.cpp:52
KNS::EntryHandler::EntryHandler
EntryHandler(const QDomElement &entryxml)
Definition entryhandler.cpp:34
KNS::EntryHandler::entry
Entry entry()
Definition entryhandler.cpp:62
KNS::EntryHandler::entryptr
Entry * entryptr()
Definition entryhandler.cpp:67
KNS::Entry
KNewStuff data entry container.
Definition knewstuff2/core/entry.h:47
KNS::Entry::name
KTranslatable name() const
Retrieve the name of the data object.
Definition knewstuff2/core/entry.cpp:81
KNS::Entry::downloads
int downloads() const
Retrieve the download count for the object, which has been determined by its hosting sites and thus m...
Definition knewstuff2/core/entry.cpp:191
KNS::Entry::category
QString category() const
Retrieve the category of the data object.
Definition knewstuff2/core/entry.cpp:91
KNS::Entry::release
int release() const
Retrieve the release number of the object.
Definition knewstuff2/core/entry.cpp:141
KNS::Entry::version
QString version() const
Retrieve the version string of the object.
Definition knewstuff2/core/entry.cpp:131
KNS::Entry::rating
int rating() const
Retrieve the rating for the object, which has been determined by its users and thus might change over...
Definition knewstuff2/core/entry.cpp:181
KNS::Entry::author
Author author() const
Retrieve the author of the object.
Definition knewstuff2/core/entry.cpp:101
KNS::Entry::license
QString license() const
Retrieve the license name of the object.
Definition knewstuff2/core/entry.cpp:111
KNS::KTranslatable
String class with multiple localized representations.
Definition ktranslatable.h:42
KNS::KTranslatable::translated
QString translated(const QString &lang) const
Returns the string which matches most closely the specified language.
Definition ktranslatable.cpp:89
KNS::KTranslatable::languages
QStringList languages() const
Returns the list of all languages for which strings are stored.
Definition ktranslatable.cpp:96
KNS::KTranslatable::addString
void addString(const QString &lang, const QString &string)
Adds a string to the contents of this object.
Definition ktranslatable.cpp:59
entryhandler.h
kWarning
#define kWarning
kdebug.h
KNS
Definition knewstuff2/core/author.h:27
KStandardAction::name
const char * name(StandardAction id)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 17 2025 00:00:00 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KNewStuff

Skip menu "KNewStuff"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal