File indexing completed on 2024-04-14 14:16:53

0001 /*
0002     This file is part of KDE.
0003 
0004     SPDX-FileCopyrightText: 2008 Cornelius Schumacher <schumacher@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "content.h"
0010 
0011 #include <QDateTime>
0012 
0013 using namespace Attica;
0014 
0015 class Q_DECL_HIDDEN Content::Private : public QSharedData
0016 {
0017 public:
0018     QString m_id;
0019     QString m_name;
0020     int m_downloads;
0021     int m_numberOfComments;
0022     int m_rating;
0023     QDateTime m_created;
0024     QDateTime m_updated;
0025     QList<Icon> m_icons;
0026     QList<QUrl> m_videos;
0027     QStringList m_tags;
0028 
0029     QMap<QString, QString> m_extendedAttributes;
0030 
0031     Private()
0032         : m_downloads(0)
0033         , m_numberOfComments(0)
0034         , m_rating(0)
0035     {
0036     }
0037 };
0038 
0039 Content::Content()
0040     : d(new Private)
0041 {
0042 }
0043 
0044 Content::Content(const Attica::Content &other)
0045     : d(other.d)
0046 {
0047 }
0048 
0049 Content &Content::operator=(const Attica::Content &other)
0050 {
0051     d = other.d;
0052     return *this;
0053 }
0054 
0055 Content::~Content()
0056 {
0057 }
0058 
0059 void Content::setId(const QString &u)
0060 {
0061     d->m_id = u;
0062 }
0063 
0064 QString Content::id() const
0065 {
0066     return d->m_id;
0067 }
0068 
0069 void Content::setName(const QString &name)
0070 {
0071     d->m_name = name;
0072 }
0073 
0074 QString Content::name() const
0075 {
0076     return d->m_name;
0077 }
0078 
0079 void Content::setRating(int v)
0080 {
0081     d->m_rating = v;
0082 }
0083 
0084 int Content::rating() const
0085 {
0086     return d->m_rating;
0087 }
0088 
0089 void Content::setDownloads(int v)
0090 {
0091     d->m_downloads = v;
0092 }
0093 
0094 int Content::downloads() const
0095 {
0096     return d->m_downloads;
0097 }
0098 
0099 void Content::setNumberOfComments(int v)
0100 {
0101     d->m_numberOfComments = v;
0102 }
0103 
0104 int Content::numberOfComments() const
0105 {
0106     return d->m_numberOfComments;
0107 }
0108 
0109 void Content::setCreated(const QDateTime &date)
0110 {
0111     d->m_created = date;
0112 }
0113 
0114 QDateTime Content::created() const
0115 {
0116     return d->m_created;
0117 }
0118 
0119 void Content::setUpdated(const QDateTime &date)
0120 {
0121     d->m_updated = date;
0122 }
0123 
0124 QDateTime Content::updated() const
0125 {
0126     return d->m_updated;
0127 }
0128 
0129 void Content::addAttribute(const QString &key, const QString &value)
0130 {
0131     d->m_extendedAttributes.insert(key, value);
0132 }
0133 
0134 QString Content::attribute(const QString &key) const
0135 {
0136     return d->m_extendedAttributes.value(key);
0137 }
0138 
0139 QMap<QString, QString> Content::attributes() const
0140 {
0141     return d->m_extendedAttributes;
0142 }
0143 
0144 bool Content::isValid() const
0145 {
0146     return !(d->m_id.isEmpty());
0147 }
0148 
0149 QString Content::summary() const
0150 {
0151     return attribute(QStringLiteral("summary"));
0152 }
0153 
0154 QString Content::description() const
0155 {
0156     return attribute(QStringLiteral("description"));
0157 }
0158 
0159 QUrl Content::detailpage() const
0160 {
0161     return QUrl(attribute(QStringLiteral("detailpage")));
0162 }
0163 
0164 QString Attica::Content::changelog() const
0165 {
0166     return attribute(QStringLiteral("changelog"));
0167 }
0168 
0169 QString Attica::Content::depend() const
0170 {
0171     return attribute(QStringLiteral("depend"));
0172 }
0173 
0174 QList<Attica::DownloadDescription> Attica::Content::downloadUrlDescriptions() const
0175 {
0176     QList<Attica::DownloadDescription> descriptions;
0177     QMap<QString, QString>::const_iterator iter = d->m_extendedAttributes.constBegin();
0178     while (iter != d->m_extendedAttributes.constEnd()) {
0179         const QString &key = iter.key();
0180         static const QLatin1String tag("downloadname");
0181         if (key.startsWith(tag)) {
0182             bool ok;
0183             // remove "downloadlink", get the rest as number
0184 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0185             const int num = QStringView(key).right(key.size() - tag.size()).toInt(&ok);
0186 #else
0187             const int num = key.rightRef(key.size() - tag.size()).toInt(&ok);
0188 #endif
0189             if (ok) {
0190                 // check if the download actually has a name
0191                 if (!iter.value().isEmpty()) {
0192                     descriptions.append(downloadUrlDescription(num));
0193                 }
0194             }
0195         }
0196         ++iter;
0197     }
0198     return descriptions;
0199 }
0200 
0201 Attica::DownloadDescription Attica::Content::downloadUrlDescription(int number) const
0202 {
0203     QString num(QString::number(number));
0204     DownloadDescription desc;
0205 
0206     Attica::DownloadDescription::Type downloadType = Attica::DownloadDescription::LinkDownload;
0207     if (attribute(QLatin1String("downloadway") + num) == QLatin1Char('0')) {
0208         downloadType = Attica::DownloadDescription::FileDownload;
0209     } else if (attribute(QLatin1String("downloadway") + num) == QLatin1Char('1')) {
0210         downloadType = Attica::DownloadDescription::LinkDownload;
0211     } else if (attribute(QLatin1String("downloadway") + num) == QLatin1Char('2')) {
0212         downloadType = Attica::DownloadDescription::PackageDownload;
0213     }
0214     desc.setType(downloadType);
0215     desc.setId(number);
0216     desc.setName(attribute(QLatin1String("downloadname") + num));
0217     desc.setDistributionType(attribute(QLatin1String("downloadtype") + num));
0218     desc.setHasPrice(attribute(QLatin1String("downloadbuy") + num) == QLatin1Char('1'));
0219     desc.setLink(attribute(QLatin1String("downloadlink") + num));
0220     desc.setPriceReason(attribute(QLatin1String("downloadreason") + num));
0221     desc.setPriceAmount(attribute(QLatin1String("downloadprice") + num));
0222     desc.setSize(attribute(QLatin1String("downloadsize") + num).toUInt());
0223     desc.setGpgFingerprint(attribute(QLatin1String("downloadgpgfingerprint") + num));
0224     desc.setGpgSignature(attribute(QLatin1String("downloadgpgsignature") + num));
0225     desc.setPackageName(attribute(QLatin1String("downloadpackagename") + num));
0226     desc.setRepository(attribute(QLatin1String("downloadrepository") + num));
0227     desc.setTags(attribute(QLatin1String("downloadtags") + num).split(QLatin1Char(',')));
0228     return desc;
0229 }
0230 
0231 QList<HomePageEntry> Attica::Content::homePageEntries()
0232 {
0233     QList<Attica::HomePageEntry> homepages;
0234     QMap<QString, QString>::const_iterator iter = d->m_extendedAttributes.constBegin();
0235     while (iter != d->m_extendedAttributes.constEnd()) {
0236         QString key = iter.key();
0237         if (key.startsWith(QLatin1String("homepagetype"))) {
0238             bool ok;
0239             // remove "homepage", get the rest as number
0240 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0241             const int num = QStringView(key).right(key.size() - 12).toInt(&ok);
0242 #else
0243             const int num = key.rightRef(key.size() - 12).toInt(&ok);
0244 #endif
0245             if (ok) {
0246                 // check if the homepage actually has a valid type
0247                 if (!iter.value().isEmpty()) {
0248                     homepages.append(homePageEntry(num));
0249                 }
0250             }
0251         }
0252         ++iter;
0253     }
0254 
0255     return homepages;
0256 }
0257 
0258 Attica::HomePageEntry Attica::Content::homePageEntry(int number) const
0259 {
0260     QString num(QString::number(number));
0261     HomePageEntry homepage;
0262 
0263     if (number == 1 && attribute(QStringLiteral("homepage1")).isEmpty()) {
0264         num.clear();
0265     }
0266     homepage.setType(attribute(QLatin1String("homepagetype") + num));
0267     homepage.setUrl(QUrl(attribute(QLatin1String("homepage") + num)));
0268     return homepage;
0269 }
0270 
0271 QString Attica::Content::version() const
0272 {
0273     return attribute(QStringLiteral("version"));
0274 }
0275 
0276 QString Attica::Content::author() const
0277 {
0278     return attribute(QStringLiteral("personid"));
0279 }
0280 
0281 QString Attica::Content::license() const
0282 {
0283     return attribute(QStringLiteral("licensetype"));
0284 }
0285 
0286 QString Attica::Content::licenseName() const
0287 {
0288     return attribute(QStringLiteral("license"));
0289 }
0290 
0291 QString Attica::Content::previewPicture(const QString &number) const
0292 {
0293     return attribute(QLatin1String("previewpic") + number);
0294 }
0295 
0296 QString Attica::Content::smallPreviewPicture(const QString &number) const
0297 {
0298     return attribute(QLatin1String("smallpreviewpic") + number);
0299 }
0300 
0301 QList<Icon> Attica::Content::icons()
0302 {
0303     return d->m_icons;
0304 }
0305 
0306 QList<Icon> Attica::Content::icons() const
0307 {
0308     return d->m_icons;
0309 }
0310 
0311 void Attica::Content::setIcons(QList<Icon> icons)
0312 {
0313     d->m_icons = std::move(icons); // TODO KF6 Make QList const & and remove the std::move
0314 }
0315 
0316 QList<QUrl> Attica::Content::videos()
0317 {
0318     return d->m_videos;
0319 }
0320 
0321 void Attica::Content::setVideos(QList<QUrl> videos)
0322 {
0323     d->m_videos = std::move(videos);
0324 }
0325 
0326 QStringList Attica::Content::tags() const
0327 {
0328     return d->m_tags;
0329 }
0330 
0331 void Attica::Content::setTags(const QStringList &tags)
0332 {
0333     d->m_tags = tags;
0334 }