diff --git a/src/content.cpp b/src/content.cpp index c2f769e..d8b9072 100644 --- a/src/content.cpp +++ b/src/content.cpp @@ -1,329 +1,341 @@ /* This file is part of KDE. Copyright (c) 2008 Cornelius Schumacher This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "content.h" #include using namespace Attica; class Content::Private : public QSharedData { public: QString m_id; QString m_name; int m_downloads; int m_numberOfComments; int m_rating; QDateTime m_created; QDateTime m_updated; QList m_icons; QList m_videos; + QStringList m_tags; QMap m_extendedAttributes; Private() : m_downloads(0), m_numberOfComments(0), m_rating(0) { } }; Content::Content() : d(new Private) { } Content::Content(const Attica::Content &other) : d(other.d) { } Content &Content::operator=(const Attica::Content &other) { d = other.d; return *this; } Content::~Content() { } void Content::setId(const QString &u) { d->m_id = u; } QString Content::id() const { return d->m_id; } void Content::setName(const QString &name) { d->m_name = name; } QString Content::name() const { return d->m_name; } void Content::setRating(int v) { d->m_rating = v; } int Content::rating() const { return d->m_rating; } void Content::setDownloads(int v) { d->m_downloads = v; } int Content::downloads() const { return d->m_downloads; } void Content::setNumberOfComments(int v) { d->m_numberOfComments = v; } int Content::numberOfComments() const { return d->m_numberOfComments; } void Content::setCreated(const QDateTime &date) { d->m_created = date; } QDateTime Content::created() const { return d->m_created; } void Content::setUpdated(const QDateTime &date) { d->m_updated = date; } QDateTime Content::updated() const { return d->m_updated; } void Content::addAttribute(const QString &key, const QString &value) { d->m_extendedAttributes.insert(key, value); } QString Content::attribute(const QString &key) const { return d->m_extendedAttributes.value(key); } QMap Content::attributes() const { return d->m_extendedAttributes; } bool Content::isValid() const { return !(d->m_id.isEmpty()); } QString Content::summary() const { return attribute(QLatin1String("summary")); } QString Content::description() const { return attribute(QLatin1String("description")); } QUrl Content::detailpage() const { return QUrl(attribute(QLatin1String("detailpage"))); } QString Attica::Content::changelog() const { return attribute(QLatin1String("changelog")); } QString Attica::Content::depend() const { return attribute(QLatin1String("depend")); } QList Attica::Content::downloadUrlDescriptions() const { QList descriptions; QMap::const_iterator iter = d->m_extendedAttributes.constBegin(); while (iter != d->m_extendedAttributes.constEnd()) { QString key = iter.key(); if (key.startsWith(QLatin1String("downloadname"))) { bool ok; // remove "downloadlink", get the rest as number int num = key.rightRef(key.size() - 12).toInt(&ok); if (ok) { // check if the download actually has a name if (!iter.value().isEmpty()) { descriptions.append(downloadUrlDescription(num)); } } } ++iter; } return descriptions; } Attica::DownloadDescription Attica::Content::downloadUrlDescription(int number) const { QString num(QString::number(number)); DownloadDescription desc; Attica::DownloadDescription::Type downloadType = Attica::DownloadDescription::LinkDownload; if (attribute(QLatin1String("downloadway") + num) == QLatin1String("0")) { downloadType = Attica::DownloadDescription::FileDownload; } else if (attribute(QLatin1String("downloadway") + num) == QLatin1String("1")) { downloadType = Attica::DownloadDescription::LinkDownload; } else if (attribute(QLatin1String("downloadway") + num) == QLatin1String("2")) { downloadType = Attica::DownloadDescription::PackageDownload; } desc.setType(downloadType); desc.setId(number); desc.setName(attribute(QLatin1String("downloadname") + num)); desc.setDistributionType(attribute(QLatin1String("downloadtype") + num)); desc.setHasPrice(attribute(QLatin1String("downloadbuy") + num) == QLatin1String("1")); desc.setLink(attribute(QLatin1String("downloadlink") + num)); desc.setPriceReason(attribute(QLatin1String("downloadreason") + num)); desc.setPriceAmount(attribute(QLatin1String("downloadprice") + num)); desc.setSize(attribute(QLatin1String("downloadsize") + num).toUInt()); desc.setGpgFingerprint(attribute(QLatin1String("downloadgpgfingerprint") + num)); desc.setGpgSignature(attribute(QLatin1String("downloadgpgsignature") + num)); desc.setPackageName(attribute(QLatin1String("downloadpackagename") + num)); desc.setRepository(attribute(QLatin1String("downloadrepository") + num)); + desc.setTags(attribute(QLatin1String("downloadtags") + num).split(QLatin1Char(','))); return desc; } QList Attica::Content::homePageEntries() { QList homepages; QMap::const_iterator iter = d->m_extendedAttributes.constBegin(); while (iter != d->m_extendedAttributes.constEnd()) { QString key = iter.key(); if (key.startsWith(QLatin1String("homepagetype"))) { bool ok; // remove "homepage", get the rest as number int num = key.rightRef(key.size() - 12).toInt(&ok); if (ok) { // check if the homepage actually has a valid type if (!iter.value().isEmpty()) { homepages.append(homePageEntry(num)); } } } ++iter; } return homepages; } Attica::HomePageEntry Attica::Content::homePageEntry(int number) const { QString num(QString::number(number)); HomePageEntry homepage; if (number == 1 && attribute(QLatin1String("homepage1")).isEmpty()) { num.clear(); } homepage.setType(attribute(QLatin1String("homepagetype") + num)); homepage.setUrl(QUrl(attribute(QLatin1String("homepage") + num))); return homepage; } QString Attica::Content::version() const { return attribute(QLatin1String("version")); } QString Attica::Content::author() const { return attribute(QLatin1String("personid")); } QString Attica::Content::license() const { return attribute(QLatin1String("licensetype")); } QString Attica::Content::licenseName() const { return attribute(QLatin1String("license")); } QString Attica::Content::previewPicture(const QString &number) const { return attribute(QLatin1String("previewpic") + number); } QString Attica::Content::smallPreviewPicture(const QString &number) const { return attribute(QLatin1String("smallpreviewpic") + number); } QList Attica::Content::icons() { return d->m_icons; } QList Attica::Content::icons() const { return d->m_icons; } void Attica::Content::setIcons(QList icons) { d->m_icons = icons; } QList Attica::Content::videos() { return d->m_videos; } void Attica::Content::setVideos(QList videos) { d->m_videos = videos; } + +QStringList Attica::Content::tags() const +{ + return d->m_tags; +} + +void Attica::Content::setTags(const QStringList &tags) +{ + d->m_tags = tags; +} diff --git a/src/content.h b/src/content.h index e220045..fb9783c 100644 --- a/src/content.h +++ b/src/content.h @@ -1,269 +1,281 @@ /* This file is part of KDE. Copyright (c) 2008 Cornelius Schumacher This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #ifndef ATTICA_CONTENT_H #define ATTICA_CONTENT_H #include #include #include #include #include #include "attica_export.h" #include "downloaddescription.h" #include "homepageentry.h" #include "icon.h" class QDateTime; namespace Attica { /** * Represents a single content */ class ATTICA_EXPORT Content { public: typedef QList List; class Parser; /** * Creates an empty Content */ Content(); /** * Copy constructor. * @param other the Content to copy from */ Content(const Content &other); /** * Assignment operator. * @param other the Content to assign from * @return pointer to this Content */ Content &operator=(const Content &other); /** * Destructor. */ ~Content(); /** * Sets the id of the Content. * The id uniquely identifies a Content with the OCS API. * @param id the new id */ void setId(const QString &id); /** * Gets the id of the Content. * The id uniquely identifies a Content with the OCS API. * @return the id */ QString id() const; /** * Sets the name of the Content. * @param name the new name */ void setName(const QString &name); /** * Gets the name of the Content. * @return the name */ QString name() const; /** * Sets the rating of the Content. * @param rating the new rating, has to be in the range 0-100 */ void setRating(int rating); /** * Gets the rating of the Content. * @return the rating in the range 0-100 */ int rating() const; /** * Sets the number of downloads for the Content. * @param downloads the new number of downloads */ void setDownloads(int downloads); /** * Gets the number of downloads for the Content (how often this has been downloaded from the server). * @return the number of downloads */ int downloads() const; /** * Sets the number of comments for the Content. * @param numComments the new number of downloads */ void setNumberOfComments(int numComments); /** * Gets the number of comments for the Content. * @return the number of comments */ int numberOfComments() const; /** * Sets the date and time the Content has been created. * @param created the new creation date and time */ void setCreated(const QDateTime &created); /** * Gets the date and time the Content has been created. * @return the date and time of the last update */ QDateTime created() const; /** * Sets the time the Content has been last updated. * @param updated the new date and time of the last update */ void setUpdated(const QDateTime &updated); /** * Gets the date and time the Content has been last updated. * @return the date and time of the last update */ QDateTime updated() const; /** * A summary description of this content. */ QString summary() const; /** * A description of this content. */ QString description() const; /** * A webpage with the detailed description of this content. */ QUrl detailpage() const; QString changelog() const; QString version() const; QString depend() const; /** Get the details about a download (a content can have multiple links, eg for different distros). This is not very helpful if we don't know the allowed numbers. */ DownloadDescription downloadUrlDescription(int number) const; /** Get all possible downloads. This is slow searching through lots of strings, so beware and don't call it too often. */ QList downloadUrlDescriptions() const; /** Get the details about a home page (a content can have multiple home pages, blog, bugs, ...). This is not very helpful if we don't know the allowed numbers. */ HomePageEntry homePageEntry(int number) const; /** Get all home pages for this content. This is slow searching through lots of strings, so beware and don't call it too often. */ QList homePageEntries(); QString previewPicture(const QString &number = QLatin1String("1")) const; QString smallPreviewPicture(const QString &number = QLatin1String("1")) const; QString license() const; QString licenseName() const; QString author() const; /** Get all icons for this content. */ QList icons(); /** Get all icons for this content. */ QList icons() const; /** * Set list of icons. * @param icons list of icons for this content */ void setIcons(QList icons); /** Get all videos for this content. */ QList videos(); /** * Set list of videos. * @param videos list of videos for this content */ void setVideos(QList videos); + /** + * Get all the tags for this content + * @since 5.50 + */ + QStringList tags() const; + /** + * Set the list of tags + * @param tags list of tags for this content + * @since 5.50 + */ + void setTags(const QStringList &tags); + /** * Add an attribute that is not included in the basis set of attributes exposed by the Content class. * If the attribute already exists it gets overwritten. * @param key the key of the attribute * @param value the value of the attribute */ void addAttribute(const QString &key, const QString &value); /** * Get an attribute that is not included in the basis set of attributes exposed by the Content class. * @param key the key of the attribute * @return the value of the attribute with the specified key, or an empty string, if the key has not been found */ QString attribute(const QString &key) const; /** * Get all attributes that are not included in the basis set of attributes exposed by the Content class. * @return the attribute mappings */ QMap attributes() const; /** * Checks whether this Content has an id * @return @c true if an id has been set, @c false otherwise */ bool isValid() const; private: class Private; QSharedDataPointer d; }; } #endif diff --git a/src/contentparser.cpp b/src/contentparser.cpp index e0b824d..1f3e9ab 100644 --- a/src/contentparser.cpp +++ b/src/contentparser.cpp @@ -1,102 +1,104 @@ /* This file is part of KDE. Copyright (c) 2008 Cornelius Schumacher This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "contentparser.h" #include #include using namespace Attica; Content Content::Parser::parseXml(QXmlStreamReader &xml) { Content content; while (!xml.atEnd()) { xml.readNext(); if (xml.isStartElement()) { if (xml.name() == QLatin1String("id")) { content.setId(xml.readElementText()); } else if (xml.name() == QLatin1String("name")) { content.setName(xml.readElementText()); } else if (xml.name() == QLatin1String("score")) { content.setRating(xml.readElementText().toInt()); } else if (xml.name() == QLatin1String("downloads")) { content.setDownloads(xml.readElementText().toInt()); } else if (xml.name() == QLatin1String("comments")) { content.setNumberOfComments(xml.readElementText().toInt()); } else if (xml.name() == QLatin1String("created")) { // Qt doesn't accept +-Timezone modifiers, truncate if the string contains them QString dateString = xml.readElementText().left(19); content.setCreated(QDateTime::fromString(dateString, Qt::ISODate)); } else if (xml.name() == QLatin1String("changed")) { // Qt doesn't accept +-Timezone modifiers, truncate if the string contains them QString dateString = xml.readElementText().left(19); content.setUpdated(QDateTime::fromString(dateString, Qt::ISODate)); } else if (xml.name() == QLatin1String("icon")) { Icon icon; icon.setUrl(QUrl(xml.readElementText())); const QXmlStreamAttributes attributes = xml.attributes(); const QStringRef width = attributes.value(QLatin1String("width")); const QStringRef height = attributes.value(QLatin1String("height")); if (!width.isEmpty()) { icon.setWidth(width.toInt()); } if (!height.isEmpty()) { icon.setHeight(height.toInt()); } // append the icon to the current list of icons QList icons; icons = content.icons(); icons.append(icon); content.setIcons(icons); } else if (xml.name() == QLatin1String("video")) { QUrl video(xml.readElementText()); // append the video to the current list of videos QList videos; videos = content.videos(); videos.append(video); content.setVideos(videos); + } else if (xml.name() == QLatin1String("tags")) { + content.setTags(xml.readElementText().split(QLatin1Char(','))); } else { content.addAttribute(xml.name().toString(), xml.readElementText()); } } if (xml.isEndElement() && xml.name() == QLatin1String("content")) { break; } } // in case the server only sets creation date, use that as updated also if (content.updated().isNull()) { content.setUpdated(content.created()); } return content; } QStringList Content::Parser::xmlElement() const { return QStringList(QLatin1String("content")); } diff --git a/src/downloaddescription.cpp b/src/downloaddescription.cpp index fa158aa..42790d7 100644 --- a/src/downloaddescription.cpp +++ b/src/downloaddescription.cpp @@ -1,294 +1,305 @@ /* This file is part of KDE. Copyright (c) 2009 Frederik Gladhorn This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "downloaddescription.h" #include namespace Attica { class DownloadDescription::Private : public QSharedData { public: int id = 0; Attica::DownloadDescription::Type type = Attica::DownloadDescription::FileDownload; bool hasPrice = false; QString category; QString name; QString link; QString distributionType; QString priceReason; QString priceAmount; QString gpgFingerprint; QString gpgSignature; QString packageName; QString repository; uint size = 0; + QStringList tags; }; } using namespace Attica; DownloadDescription::DownloadDescription() : d(new Private) { } DownloadDescription::DownloadDescription(const Attica::DownloadDescription &other) : d(other.d) { } DownloadDescription &DownloadDescription::operator=(const Attica::DownloadDescription &other) { d = other.d; return *this; } DownloadDescription::~DownloadDescription() { } QString Attica::DownloadDescription::category() { return d->category; } int DownloadDescription::id() { return d->id; } QString Attica::DownloadDescription::category() const { return d->category; } int DownloadDescription::id() const { return d->id; } void DownloadDescription::setId(int id) { d->id = id; } void DownloadDescription::setCategory(const QString &category) { d->category = category; } QString Attica::DownloadDescription::distributionType() { return d->distributionType; } QString Attica::DownloadDescription::distributionType() const { return d->distributionType; } void DownloadDescription::setDistributionType(const QString &distributionType) { d->distributionType = distributionType; } bool Attica::DownloadDescription::hasPrice() { return d->hasPrice; } bool Attica::DownloadDescription::hasPrice() const { return d->hasPrice; } void DownloadDescription::setHasPrice(bool hasPrice) { d->hasPrice = hasPrice; } Attica::DownloadDescription::Type DownloadDescription::type() { return d->type; } Attica::DownloadDescription::Type DownloadDescription::type() const { return d->type; } void DownloadDescription::setType(Attica::DownloadDescription::Type type) { d->type = type; } bool Attica::DownloadDescription::isDownloadtypLink() { return d->type == Attica::DownloadDescription::LinkDownload; } void DownloadDescription::setDownloadtypLink(bool isLink) { if (isLink) { d->type = Attica::DownloadDescription::LinkDownload; } else { d->type = Attica::DownloadDescription::FileDownload; } } QString Attica::DownloadDescription::link() { return d->link; } QString Attica::DownloadDescription::link() const { return d->link; } void DownloadDescription::setLink(const QString &link) { d->link = link; } QString Attica::DownloadDescription::name() { return d->name; } QString Attica::DownloadDescription::name() const { return d->name; } void DownloadDescription::setName(const QString &name) { d->name = name; } QString Attica::DownloadDescription::priceAmount() { return d->priceAmount; } QString Attica::DownloadDescription::priceAmount() const { return d->priceAmount; } void DownloadDescription::setPriceAmount(const QString &priceAmount) { d->priceAmount = priceAmount; } QString Attica::DownloadDescription::priceReason() { return d->priceReason; } QString Attica::DownloadDescription::priceReason() const { return d->priceReason; } void Attica::DownloadDescription::setPriceReason(const QString &priceReason) { d->priceReason = priceReason; } uint Attica::DownloadDescription::size() { return d->size; } uint Attica::DownloadDescription::size() const { return d->size; } void Attica::DownloadDescription::setSize(uint size) { d->size = size; } QString Attica::DownloadDescription::gpgFingerprint() { return d->gpgFingerprint; } QString Attica::DownloadDescription::gpgFingerprint() const { return d->gpgFingerprint; } void Attica::DownloadDescription::setGpgFingerprint(const QString &fingerprint) { d->gpgFingerprint = fingerprint; } QString Attica::DownloadDescription::gpgSignature() { return d->gpgSignature; } QString Attica::DownloadDescription::gpgSignature() const { return d->gpgSignature; } void Attica::DownloadDescription::setGpgSignature(const QString &signature) { d->gpgSignature = signature; } QString Attica::DownloadDescription::packageName() { return d->packageName; } QString Attica::DownloadDescription::packageName() const { return d->packageName; } void Attica::DownloadDescription::setPackageName(const QString &packageName) { d->packageName = packageName; } QString Attica::DownloadDescription::repository() { return d->repository; } QString Attica::DownloadDescription::repository() const { return d->repository; } void Attica::DownloadDescription::setRepository(const QString &repository) { d->repository = repository; } + +QStringList Attica::DownloadDescription::tags() const +{ + return d->tags; +} + +void Attica::DownloadDescription::setTags(const QStringList &tags) +{ + d->tags = tags; +} diff --git a/src/downloaddescription.h b/src/downloaddescription.h index adeab2f..96cbec0 100644 --- a/src/downloaddescription.h +++ b/src/downloaddescription.h @@ -1,110 +1,120 @@ /* This file is part of KDE. Copyright (c) 2009 Frederik Gladhorn This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #ifndef DOWNLOADDESCRIPTION_H #define DOWNLOADDESCRIPTION_H #include #include #include "attica_export.h" namespace Attica { class ATTICA_EXPORT DownloadDescription { public: enum Type { FileDownload = 0, LinkDownload, PackageDownload }; DownloadDescription(); DownloadDescription(const DownloadDescription &other); DownloadDescription &operator=(const DownloadDescription &other); ~DownloadDescription(); /** The id of the description - as one Content can have multiple download descriptions associated. This will simply be 1, 2, ... */ Q_DECL_DEPRECATED int id(); int id() const; // TODO KF6 remove deprecated methods. Q_DECL_DEPRECATED Attica::DownloadDescription::Type type(); Attica::DownloadDescription::Type type() const; Q_DECL_DEPRECATED bool isDownloadtypLink(); Q_DECL_DEPRECATED bool hasPrice(); bool hasPrice() const; Q_DECL_DEPRECATED QString category(); QString category() const; Q_DECL_DEPRECATED QString name(); QString name() const; Q_DECL_DEPRECATED QString link(); QString link() const; Q_DECL_DEPRECATED QString distributionType(); QString distributionType() const; Q_DECL_DEPRECATED QString priceReason(); QString priceReason() const; Q_DECL_DEPRECATED QString priceAmount(); QString priceAmount() const; Q_DECL_DEPRECATED uint size(); uint size() const; Q_DECL_DEPRECATED QString gpgFingerprint(); QString gpgFingerprint() const; Q_DECL_DEPRECATED QString gpgSignature(); QString gpgSignature() const; Q_DECL_DEPRECATED QString packageName(); QString packageName() const; Q_DECL_DEPRECATED QString repository(); QString repository() const; + /** + * Get the list of tags for this download description + * @since 5.50 + */ + QStringList tags() const; void setId(int id); void setType(Attica::DownloadDescription::Type type); Q_DECL_DEPRECATED void setDownloadtypLink(bool isLink); void setHasPrice(bool hasPrice); void setCategory(const QString &category); void setName(const QString &name); void setLink(const QString &link); void setDistributionType(const QString &distributionType); void setPriceReason(const QString &priceReason); void setPriceAmount(const QString &priceAmount); void setSize(uint size); void setGpgFingerprint(const QString &fingerprint); void setGpgSignature(const QString &signature); void setPackageName(const QString &packageName); void setRepository(const QString &repository); + /** + * Set the list of tags for this download description + * @since 5.50 + */ + void setTags(const QStringList &tags); private: class Private; QSharedDataPointer d; }; } #endif // DOWNLOADDESCRIPTION_H diff --git a/src/parser.cpp b/src/parser.cpp index c7c6d60..b1d19f6 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1,190 +1,190 @@ /* This file is part of KDE. Copyright (c) 2009 Eckhart Wörner This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "parser.h" #include #include using namespace Attica; static bool stringList_contains_stringRef(const QStringList &stringList, const QStringRef &str) { for (const auto &string : stringList) { if (str == string) return true; } return false; } template Parser::~Parser() { } template T Parser::parse(const QString &xmlString) { QStringList elements = xmlElement(); T item; QXmlStreamReader xml(xmlString); while (!xml.atEnd()) { xml.readNext(); if (xml.isStartElement()) { if (xml.name() == QLatin1String("meta")) { parseMetadataXml(xml); } else if (stringList_contains_stringRef(elements, xml.name())) { item = parseXml(xml); } } } if (xml.hasError()) { // TODO: error handling in metadata? qWarning() << "parse():: XML Error: " << xml.errorString() << "\nIn XML:\n" << xmlString; } return item; } template typename T::List Parser::parseList(const QString &xmlString) { /* QString testxml = QString("\ \ \ ok\ 100\ \ \ \ \ obs\ openSUSE Build Service\ foobar.com\ \ openSUSE 11.2 32bit Intel\ openSUSE 11.3 64bit Intel\ openSUSE 11.3 32bit Intel\ openSUSE 11.3 64bit Intel\ \ \ \ mbs\ MeeGo Build Service\ foobar42.com\ \ MeeGo 1.0 Intel\ MeeGo 1.0 ARM\ MeeGo 1.1 Intel\ MeeGo 1.1 ARM\ \ \ \ sbs\ Sebas' Build Service\ foobar42.com\ \ sebasix 1.3 33bit\ sebasis 4.4 14bit\ sebasix 1.3 65bit\ sebasis 4.4 37bit\ \ \ \ \ "); qCDebug(ATTICA) << "parsing list:" << xmlString; */ QStringList elements = xmlElement(); typename T::List items; //QXmlStreamReader xml( xmlString ); QXmlStreamReader xml(xmlString); while (!xml.atEnd()) { xml.readNext(); //qCDebug(ATTICA) << "parseList():: Looking for:" << xml.name().toString(); if (xml.isStartElement()) { if (xml.name() == QLatin1String("data")) { while (!xml.atEnd()) { xml.readNext(); if (xml.isEndElement() && xml.name() == QLatin1String("data")) { break; } if (xml.isStartElement() && stringList_contains_stringRef(elements, xml.name())) { //qCDebug(ATTICA) << "xxxxxxxxx New Item!" << xml.name().toString(); items.append(parseXml(xml)); } } } else if (xml.name() == QLatin1String("meta")) { parseMetadataXml(xml); } } } if (xml.hasError()) { // TODO: error handling in metadata? - qWarning() << "parseList():: XML Error: " << xml.errorString() << "\nIn XML:\n" << xmlString; + qWarning() << "parseList():: XML Error: " << xml.errorString() << "\nIn xml name" << xml.name() << "with text" << xml.text() << "at offset:\n" << xml.characterOffset() << "\nIn XML:\n" << xmlString; } return items; } template void Parser::parseMetadataXml(QXmlStreamReader &xml) { while (!xml.atEnd()) { xml.readNext(); if (xml.isEndElement() && xml.name() == QLatin1String("meta")) { break; } else if (xml.isStartElement()) { if (xml.name() == QLatin1String("status")) { m_metadata.setStatusString(xml.readElementText()); } else if (xml.name() == QLatin1String("statuscode")) { m_metadata.setStatusCode(xml.readElementText().toInt()); } else if (xml.name() == QLatin1String("message")) { m_metadata.setMessage(xml.readElementText()); } else if (xml.name() == QLatin1String("totalitems")) { m_metadata.setTotalItems(xml.readElementText().toInt()); } else if (xml.name() == QLatin1String("itemsperpage")) { m_metadata.setItemsPerPage(xml.readElementText().toInt()); } } } if (xml.hasError()) { // TODO: error handling in metadata? qWarning() << "XML Error: " << xml.errorString(); } } template Metadata Parser::metadata() const { return m_metadata; }