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

KNewStuff

  • sources
  • kde-4.14
  • kdelibs
  • 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 
25 using namespace KNS;
26 
27 EntryHandler::EntryHandler(const Entry& entry)
28 {
29  init();
30  mEntry = entry;
31  mEntryXML = serializeElement(entry);
32 }
33 
34 EntryHandler::EntryHandler(const QDomElement& entryxml)
35 {
36  init();
37  mEntryXML = entryxml;
38  mEntry = deserializeElement(entryxml);
39 }
40 
41 void EntryHandler::init()
42 {
43  mValid = false;
44  mCompat = false;
45 }
46 
47 void EntryHandler::setCompatibilityFormat()
48 {
49  mCompat = true;
50 }
51 
52 bool EntryHandler::isValid()
53 {
54  return mValid;
55 }
56 
57 QDomElement EntryHandler::entryXML()
58 {
59  return mEntryXML;
60 }
61 
62 Entry EntryHandler::entry()
63 {
64  return mEntry;
65 }
66 
67 Entry *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 
87 QDomElement 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 
164 Entry 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 
260 QDomElement 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::jabber
QString jabber() const
Retrieve the author's jabber address.
Definition: knewstuff2/core/author.cpp:78
KNS::EntryHandler::entryptr
Entry * entryptr()
Definition: entryhandler.cpp:67
KNS::Entry::setIdNumber
void setIdNumber(int number)
Definition: knewstuff2/core/entry.cpp:246
KNS::Entry::checksum
QString checksum() const
Returns the checksum for the entry.
Definition: knewstuff2/core/entry.cpp:201
kdebug.h
QDomNode::appendChild
QDomNode appendChild(const QDomNode &newChild)
QDate::toString
QString toString(Qt::DateFormat format) const
KNS::EntryHandler::EntryHandler
EntryHandler(const QDomElement &entryxml)
Definition: entryhandler.cpp:34
KNS::Entry::setCategory
void setCategory(const QString &category)
Sets the data category, e.g.
Definition: knewstuff2/core/entry.cpp:86
QDomElement::attribute
QString attribute(const QString &name, const QString &defValue) const
KNS::Entry
KNewStuff data entry container.
Definition: knewstuff2/core/entry.h:46
KNS::EntryHandler::entryXML
QDomElement entryXML()
Definition: entryhandler.cpp:57
KNS::Author::setEmail
void setEmail(const QString &email)
Sets the email address of the author.
Definition: knewstuff2/core/author.cpp:63
KNS::Entry::setName
void setName(const KTranslatable &name)
Sets the name for this data object.
Definition: knewstuff2/core/entry.cpp:76
KNS::KTranslatable
String class with multiple localized representations.
Definition: ktranslatable.h:41
KNS::Entry::releaseDate
QDate releaseDate() const
Retrieve the date of the object's publication.
Definition: knewstuff2/core/entry.cpp:151
KNS::Entry::setChecksum
void setChecksum(const QString &checksum)
Sets the checksum of the entry.
Definition: knewstuff2/core/entry.cpp:196
KNS::KTranslatable::addString
void addString(const QString &lang, const QString &string)
Adds a string to the contents of this object.
Definition: ktranslatable.cpp:59
KNS::Entry::version
QString version() const
Retrieve the version string of the object.
Definition: knewstuff2/core/entry.cpp:131
name
const char * name(StandardAction id)
KNS::Author::email
QString email() const
Retrieve the author's email address.
Definition: knewstuff2/core/author.cpp:68
KNS::Entry::setDownloads
void setDownloads(int downloads)
Sets the number of downloads.
Definition: knewstuff2/core/entry.cpp:186
KNS::Entry::release
int release() const
Retrieve the release number of the object.
Definition: knewstuff2/core/entry.cpp:141
QDomNode
KNS::Entry::summary
KTranslatable summary() const
Retrieve a short description about the object.
Definition: knewstuff2/core/entry.cpp:121
KNS::KTranslatable::translated
QString translated(const QString &lang) const
Returns the string which matches most closely the specified language.
Definition: ktranslatable.cpp:89
KNS::Author::setJabber
void setJabber(const QString &jabber)
Sets the jabber address of the author.
Definition: knewstuff2/core/author.cpp:73
KNS::Entry::category
QString category() const
Retrieve the category of the data object.
Definition: knewstuff2/core/entry.cpp:91
KNS::Entry::name
KTranslatable name() const
Retrieve the name of the data object.
Definition: knewstuff2/core/entry.cpp:81
KUrl
QDomNode::nextSibling
QDomNode nextSibling() const
KNS::EntryHandler::isValid
bool isValid()
Definition: entryhandler.cpp:52
KNS::Entry::setSummary
void setSummary(const KTranslatable &summary)
Sets a short description on what the object is all about.
Definition: knewstuff2/core/entry.cpp:116
QDomNode::toElement
QDomElement toElement() const
QDate::fromString
QDate fromString(const QString &string, Qt::DateFormat format)
QString::number
QString number(int n, int base)
KNS::Entry::payload
KTranslatable payload() const
Retrieve the file name of the object.
Definition: knewstuff2/core/entry.cpp:161
KNS::Entry::signature
QString signature() const
Returns the signature for the entry.
Definition: knewstuff2/core/entry.cpp:211
QDomElement::text
QString text() const
KNS::Author::setName
void setName(const QString &name)
Sets the full name of the author.
Definition: knewstuff2/core/author.cpp:53
KNS::Entry::setAuthor
void setAuthor(const Author &author)
Sets the author of the object.
Definition: knewstuff2/core/entry.cpp:96
KNS::Entry::setRelease
void setRelease(int release)
Sets the release number, which is increased for feature-equal objects with the same version number...
Definition: knewstuff2/core/entry.cpp:136
QDomElement::setAttribute
void setAttribute(const QString &name, const QString &value)
KNS::Entry::author
Author author() const
Retrieve the author of the object.
Definition: knewstuff2/core/entry.cpp:101
KNS::Entry::setInstalledFiles
void setInstalledFiles(const QStringList &files)
Set the files that have been installed by the install command.
Definition: knewstuff2/core/entry.cpp:236
QString::toInt
int toInt(bool *ok, int base) const
KNS::Author
KNewStuff author information.
Definition: knewstuff2/core/author.h:40
KNS::Entry::license
QString license() const
Retrieve the license name of the object.
Definition: knewstuff2/core/entry.cpp:111
QString::isEmpty
bool isEmpty() const
QString::trimmed
QString trimmed() const
entryhandler.h
KNS::Author::name
QString name() const
Retrieve the author's name.
Definition: knewstuff2/core/author.cpp:58
QDate
KNS::Entry::setVersion
void setVersion(const QString &version)
Sets the version number.
Definition: knewstuff2/core/entry.cpp:126
QString
KNS::Entry::setReleaseDate
void setReleaseDate(const QDate &releasedate)
Sets the release date.
Definition: knewstuff2/core/entry.cpp:146
KNS::Entry::installedFiles
QStringList installedFiles() const
Retrieve the locally installed files.
Definition: knewstuff2/core/entry.cpp:241
QStringList
KNS::Entry::setRating
void setRating(int rating)
Sets the rating between 0 (worst) and 100 (best).
Definition: knewstuff2/core/entry.cpp:176
KNS::EntryHandler::setCompatibilityFormat
void setCompatibilityFormat()
Definition: entryhandler.cpp:47
QDomDocument::createTextNode
QDomText createTextNode(const QString &value)
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
QDomDocument
QDomNode::isNull
bool isNull() const
QDomNode::firstChild
QDomNode firstChild() const
KNS::KTranslatable::languages
QStringList languages() const
Returns the list of all languages for which strings are stored.
Definition: ktranslatable.cpp:96
QList::ConstIterator
typedef ConstIterator
KNS::Entry::setPayload
void setPayload(const KTranslatable &url)
Sets the object's file.
Definition: knewstuff2/core/entry.cpp:156
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KNS::Entry::setLicense
void setLicense(const QString &license)
Sets the license (abbreviation) applicable to the object.
Definition: knewstuff2/core/entry.cpp:106
KNS::EntryHandler::entry
Entry entry()
Definition: entryhandler.cpp:62
KNS::Entry::idNumber
int idNumber() const
Definition: knewstuff2/core/entry.cpp:251
QDomElement::tagName
QString tagName() const
KNS::KTranslatable::isEmpty
bool isEmpty() const
Returns whether no content is set yet.
Definition: ktranslatable.cpp:116
QList::constEnd
const_iterator constEnd() const
QDomDocument::createElement
QDomElement createElement(const QString &tagName)
QList::constBegin
const_iterator constBegin() const
KNS::Author::homepage
QString homepage() const
Retrieve the author's homepage.
Definition: knewstuff2/core/author.cpp:88
QDomElement
KNS::Entry::preview
KTranslatable preview() const
Retrieve the file name of an image containing a preview of the object.
Definition: knewstuff2/core/entry.cpp:171
KNS::Author::setHomepage
void setHomepage(const QString &homepage)
Sets the homepage of the author.
Definition: knewstuff2/core/author.cpp:83
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::setSignature
void setSignature(const QString &signature)
Sets the signature of the entry.
Definition: knewstuff2/core/entry.cpp:206
KNS::Entry::setPreview
void setPreview(const KTranslatable &url)
Sets the object's preview file, if available.
Definition: knewstuff2/core/entry.cpp:166
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:25:43 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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