File indexing completed on 2024-04-14 03:49:31
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 const int num = QStringView(key).right(key.size() - tag.size()).toInt(&ok); 0185 if (ok) { 0186 // check if the download actually has a name 0187 if (!iter.value().isEmpty()) { 0188 descriptions.append(downloadUrlDescription(num)); 0189 } 0190 } 0191 } 0192 ++iter; 0193 } 0194 return descriptions; 0195 } 0196 0197 Attica::DownloadDescription Attica::Content::downloadUrlDescription(int number) const 0198 { 0199 QString num(QString::number(number)); 0200 DownloadDescription desc; 0201 0202 Attica::DownloadDescription::Type downloadType = Attica::DownloadDescription::LinkDownload; 0203 if (attribute(QLatin1String("downloadway") + num) == QLatin1Char('0')) { 0204 downloadType = Attica::DownloadDescription::FileDownload; 0205 } else if (attribute(QLatin1String("downloadway") + num) == QLatin1Char('1')) { 0206 downloadType = Attica::DownloadDescription::LinkDownload; 0207 } else if (attribute(QLatin1String("downloadway") + num) == QLatin1Char('2')) { 0208 downloadType = Attica::DownloadDescription::PackageDownload; 0209 } 0210 desc.setType(downloadType); 0211 desc.setId(number); 0212 desc.setName(attribute(QLatin1String("downloadname") + num)); 0213 desc.setDistributionType(attribute(QLatin1String("downloadtype") + num)); 0214 desc.setHasPrice(attribute(QLatin1String("downloadbuy") + num) == QLatin1Char('1')); 0215 desc.setLink(attribute(QLatin1String("downloadlink") + num)); 0216 desc.setPriceReason(attribute(QLatin1String("downloadreason") + num)); 0217 desc.setPriceAmount(attribute(QLatin1String("downloadprice") + num)); 0218 desc.setSize(attribute(QLatin1String("downloadsize") + num).toUInt()); 0219 desc.setGpgFingerprint(attribute(QLatin1String("downloadgpgfingerprint") + num)); 0220 desc.setGpgSignature(attribute(QLatin1String("downloadgpgsignature") + num)); 0221 desc.setPackageName(attribute(QLatin1String("downloadpackagename") + num)); 0222 desc.setRepository(attribute(QLatin1String("downloadrepository") + num)); 0223 desc.setTags(attribute(QLatin1String("downloadtags") + num).split(QLatin1Char(','))); 0224 return desc; 0225 } 0226 0227 QList<HomePageEntry> Attica::Content::homePageEntries() 0228 { 0229 QList<Attica::HomePageEntry> homepages; 0230 QMap<QString, QString>::const_iterator iter = d->m_extendedAttributes.constBegin(); 0231 while (iter != d->m_extendedAttributes.constEnd()) { 0232 QString key = iter.key(); 0233 if (key.startsWith(QLatin1String("homepagetype"))) { 0234 bool ok; 0235 // remove "homepage", get the rest as number 0236 const int num = QStringView(key).right(key.size() - 12).toInt(&ok); 0237 if (ok) { 0238 // check if the homepage actually has a valid type 0239 if (!iter.value().isEmpty()) { 0240 homepages.append(homePageEntry(num)); 0241 } 0242 } 0243 } 0244 ++iter; 0245 } 0246 0247 return homepages; 0248 } 0249 0250 Attica::HomePageEntry Attica::Content::homePageEntry(int number) const 0251 { 0252 QString num(QString::number(number)); 0253 HomePageEntry homepage; 0254 0255 if (number == 1 && attribute(QStringLiteral("homepage1")).isEmpty()) { 0256 num.clear(); 0257 } 0258 homepage.setType(attribute(QLatin1String("homepagetype") + num)); 0259 homepage.setUrl(QUrl(attribute(QLatin1String("homepage") + num))); 0260 return homepage; 0261 } 0262 0263 QString Attica::Content::version() const 0264 { 0265 return attribute(QStringLiteral("version")); 0266 } 0267 0268 QString Attica::Content::author() const 0269 { 0270 return attribute(QStringLiteral("personid")); 0271 } 0272 0273 QString Attica::Content::license() const 0274 { 0275 return attribute(QStringLiteral("licensetype")); 0276 } 0277 0278 QString Attica::Content::licenseName() const 0279 { 0280 return attribute(QStringLiteral("license")); 0281 } 0282 0283 QString Attica::Content::previewPicture(const QString &number) const 0284 { 0285 return attribute(QLatin1String("previewpic") + number); 0286 } 0287 0288 QString Attica::Content::smallPreviewPicture(const QString &number) const 0289 { 0290 return attribute(QLatin1String("smallpreviewpic") + number); 0291 } 0292 0293 QList<Icon> Attica::Content::icons() 0294 { 0295 return d->m_icons; 0296 } 0297 0298 QList<Icon> Attica::Content::icons() const 0299 { 0300 return d->m_icons; 0301 } 0302 0303 void Attica::Content::setIcons(QList<Icon> icons) 0304 { 0305 d->m_icons = std::move(icons); // TODO KF6 Make QList const & and remove the std::move 0306 } 0307 0308 QList<QUrl> Attica::Content::videos() 0309 { 0310 return d->m_videos; 0311 } 0312 0313 void Attica::Content::setVideos(QList<QUrl> videos) 0314 { 0315 d->m_videos = std::move(videos); 0316 } 0317 0318 QStringList Attica::Content::tags() const 0319 { 0320 return d->m_tags; 0321 } 0322 0323 void Attica::Content::setTags(const QStringList &tags) 0324 { 0325 d->m_tags = tags; 0326 }