| 1 | /* |
| 2 | This file is part of KDE. |
| 3 | |
| 4 | SPDX-FileCopyrightText: 2008 Cornelius Schumacher <schumacher@kde.org> |
| 5 | |
| 6 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 7 | */ |
| 8 | |
| 9 | #include "content.h" |
| 10 | |
| 11 | #include <QDateTime> |
| 12 | |
| 13 | using namespace Qt::StringLiterals; |
| 14 | using namespace Attica; |
| 15 | |
| 16 | class Q_DECL_HIDDEN Content::Private : public QSharedData |
| 17 | { |
| 18 | public: |
| 19 | QString m_id; |
| 20 | QString m_name; |
| 21 | int m_downloads; |
| 22 | int ; |
| 23 | int m_rating; |
| 24 | QDateTime m_created; |
| 25 | QDateTime m_updated; |
| 26 | QList<Icon> m_icons; |
| 27 | QList<QUrl> m_videos; |
| 28 | QStringList m_tags; |
| 29 | |
| 30 | QMap<QString, QString> m_extendedAttributes; |
| 31 | |
| 32 | Private() |
| 33 | : m_downloads(0) |
| 34 | , m_numberOfComments(0) |
| 35 | , m_rating(0) |
| 36 | { |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | Content::Content() |
| 41 | : d(new Private) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | Content::Content(const Attica::Content &other) |
| 46 | : d(other.d) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | Content &Content::operator=(const Attica::Content &other) |
| 51 | { |
| 52 | d = other.d; |
| 53 | return *this; |
| 54 | } |
| 55 | |
| 56 | Content::~Content() |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | void Content::setId(const QString &u) |
| 61 | { |
| 62 | d->m_id = u; |
| 63 | } |
| 64 | |
| 65 | QString Content::id() const |
| 66 | { |
| 67 | return d->m_id; |
| 68 | } |
| 69 | |
| 70 | void Content::setName(const QString &name) |
| 71 | { |
| 72 | d->m_name = name; |
| 73 | } |
| 74 | |
| 75 | QString Content::name() const |
| 76 | { |
| 77 | return d->m_name; |
| 78 | } |
| 79 | |
| 80 | void Content::setRating(int v) |
| 81 | { |
| 82 | d->m_rating = v; |
| 83 | } |
| 84 | |
| 85 | int Content::rating() const |
| 86 | { |
| 87 | return d->m_rating; |
| 88 | } |
| 89 | |
| 90 | void Content::setDownloads(int v) |
| 91 | { |
| 92 | d->m_downloads = v; |
| 93 | } |
| 94 | |
| 95 | int Content::downloads() const |
| 96 | { |
| 97 | return d->m_downloads; |
| 98 | } |
| 99 | |
| 100 | void Content::(int v) |
| 101 | { |
| 102 | d->m_numberOfComments = v; |
| 103 | } |
| 104 | |
| 105 | int Content::() const |
| 106 | { |
| 107 | return d->m_numberOfComments; |
| 108 | } |
| 109 | |
| 110 | void Content::setCreated(const QDateTime &date) |
| 111 | { |
| 112 | d->m_created = date; |
| 113 | } |
| 114 | |
| 115 | QDateTime Content::created() const |
| 116 | { |
| 117 | return d->m_created; |
| 118 | } |
| 119 | |
| 120 | void Content::setUpdated(const QDateTime &date) |
| 121 | { |
| 122 | d->m_updated = date; |
| 123 | } |
| 124 | |
| 125 | QDateTime Content::updated() const |
| 126 | { |
| 127 | return d->m_updated; |
| 128 | } |
| 129 | |
| 130 | void Content::addAttribute(const QString &key, const QString &value) |
| 131 | { |
| 132 | d->m_extendedAttributes.insert(key, value); |
| 133 | } |
| 134 | |
| 135 | QString Content::attribute(const QString &key) const |
| 136 | { |
| 137 | return d->m_extendedAttributes.value(key); |
| 138 | } |
| 139 | |
| 140 | QMap<QString, QString> Content::attributes() const |
| 141 | { |
| 142 | return d->m_extendedAttributes; |
| 143 | } |
| 144 | |
| 145 | bool Content::isValid() const |
| 146 | { |
| 147 | return !(d->m_id.isEmpty()); |
| 148 | } |
| 149 | |
| 150 | QString Content::summary() const |
| 151 | { |
| 152 | return attribute(QStringLiteral("summary" )); |
| 153 | } |
| 154 | |
| 155 | QString Content::description() const |
| 156 | { |
| 157 | return attribute(QStringLiteral("description" )); |
| 158 | } |
| 159 | |
| 160 | QUrl Content::detailpage() const |
| 161 | { |
| 162 | return QUrl(attribute(QStringLiteral("detailpage" ))); |
| 163 | } |
| 164 | |
| 165 | QString Attica::Content::changelog() const |
| 166 | { |
| 167 | return attribute(QStringLiteral("changelog" )); |
| 168 | } |
| 169 | |
| 170 | QString Attica::Content::depend() const |
| 171 | { |
| 172 | return attribute(QStringLiteral("depend" )); |
| 173 | } |
| 174 | |
| 175 | QList<Attica::DownloadDescription> Attica::Content::downloadUrlDescriptions() const |
| 176 | { |
| 177 | QList<Attica::DownloadDescription> descriptions; |
| 178 | QMap<QString, QString>::const_iterator iter = d->m_extendedAttributes.constBegin(); |
| 179 | while (iter != d->m_extendedAttributes.constEnd()) { |
| 180 | const QString &key = iter.key(); |
| 181 | static const QLatin1String tag("downloadname" ); |
| 182 | if (key.startsWith(s: tag)) { |
| 183 | bool ok; |
| 184 | // remove "downloadlink", get the rest as number |
| 185 | const int num = QStringView(key).right(n: key.size() - tag.size()).toInt(ok: &ok); |
| 186 | if (ok) { |
| 187 | // check if the download actually has a name |
| 188 | if (!iter.value().isEmpty()) { |
| 189 | descriptions.append(t: downloadUrlDescription(number: num)); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | ++iter; |
| 194 | } |
| 195 | return descriptions; |
| 196 | } |
| 197 | |
| 198 | Attica::DownloadDescription Attica::Content::downloadUrlDescription(int number) const |
| 199 | { |
| 200 | QString num(QString::number(number)); |
| 201 | DownloadDescription desc; |
| 202 | |
| 203 | Attica::DownloadDescription::Type downloadType = Attica::DownloadDescription::LinkDownload; |
| 204 | if (attribute(key: QLatin1String("downloadway" ) + num) == QLatin1Char('0')) { |
| 205 | downloadType = Attica::DownloadDescription::FileDownload; |
| 206 | } else if (attribute(key: QLatin1String("downloadway" ) + num) == QLatin1Char('1')) { |
| 207 | downloadType = Attica::DownloadDescription::LinkDownload; |
| 208 | } else if (attribute(key: QLatin1String("downloadway" ) + num) == QLatin1Char('2')) { |
| 209 | downloadType = Attica::DownloadDescription::PackageDownload; |
| 210 | } |
| 211 | desc.setType(downloadType); |
| 212 | desc.setId(number); |
| 213 | desc.setName(attribute(key: QLatin1String("downloadname" ) + num)); |
| 214 | desc.setDistributionType(attribute(key: QLatin1String("downloadtype" ) + num)); |
| 215 | desc.setHasPrice(attribute(key: QLatin1String("downloadbuy" ) + num) == QLatin1Char('1')); |
| 216 | desc.setLink(attribute(key: QLatin1String("downloadlink" ) + num)); |
| 217 | desc.setPriceReason(attribute(key: QLatin1String("downloadreason" ) + num)); |
| 218 | desc.setPriceAmount(attribute(key: QLatin1String("downloadprice" ) + num)); |
| 219 | desc.setSize(attribute(key: QLatin1String("downloadsize" ) + num).toUInt()); |
| 220 | desc.setGpgFingerprint(attribute(key: QLatin1String("downloadgpgfingerprint" ) + num)); |
| 221 | desc.setGpgSignature(attribute(key: QLatin1String("downloadgpgsignature" ) + num)); |
| 222 | desc.setPackageName(attribute(key: QLatin1String("downloadpackagename" ) + num)); |
| 223 | desc.setRepository(attribute(key: QLatin1String("downloadrepository" ) + num)); |
| 224 | desc.setTags(attribute(key: QLatin1String("downloadtags" ) + num).split(sep: QLatin1Char(','))); |
| 225 | desc.setVersion(attribute(key: "download_version"_L1 + num)); |
| 226 | return desc; |
| 227 | } |
| 228 | |
| 229 | QList<HomePageEntry> Attica::Content::homePageEntries() |
| 230 | { |
| 231 | QList<Attica::HomePageEntry> homepages; |
| 232 | QMap<QString, QString>::const_iterator iter = d->m_extendedAttributes.constBegin(); |
| 233 | while (iter != d->m_extendedAttributes.constEnd()) { |
| 234 | QString key = iter.key(); |
| 235 | if (key.startsWith(s: QLatin1String("homepagetype" ))) { |
| 236 | bool ok; |
| 237 | // remove "homepage", get the rest as number |
| 238 | const int num = QStringView(key).right(n: key.size() - 12).toInt(ok: &ok); |
| 239 | if (ok) { |
| 240 | // check if the homepage actually has a valid type |
| 241 | if (!iter.value().isEmpty()) { |
| 242 | homepages.append(t: homePageEntry(number: num)); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | ++iter; |
| 247 | } |
| 248 | |
| 249 | return homepages; |
| 250 | } |
| 251 | |
| 252 | Attica::HomePageEntry Attica::Content::homePageEntry(int number) const |
| 253 | { |
| 254 | QString num(QString::number(number)); |
| 255 | HomePageEntry homepage; |
| 256 | |
| 257 | if (number == 1 && attribute(QStringLiteral("homepage1" )).isEmpty()) { |
| 258 | num.clear(); |
| 259 | } |
| 260 | homepage.setType(attribute(key: QLatin1String("homepagetype" ) + num)); |
| 261 | homepage.setUrl(QUrl(attribute(key: QLatin1String("homepage" ) + num))); |
| 262 | return homepage; |
| 263 | } |
| 264 | |
| 265 | QString Attica::Content::version() const |
| 266 | { |
| 267 | return attribute(QStringLiteral("version" )); |
| 268 | } |
| 269 | |
| 270 | QString Attica::Content::author() const |
| 271 | { |
| 272 | return attribute(QStringLiteral("personid" )); |
| 273 | } |
| 274 | |
| 275 | QString Attica::Content::license() const |
| 276 | { |
| 277 | return attribute(QStringLiteral("licensetype" )); |
| 278 | } |
| 279 | |
| 280 | QString Attica::Content::licenseName() const |
| 281 | { |
| 282 | return attribute(QStringLiteral("license" )); |
| 283 | } |
| 284 | |
| 285 | QString Attica::Content::previewPicture(const QString &number) const |
| 286 | { |
| 287 | return attribute(key: QLatin1String("previewpic" ) + number); |
| 288 | } |
| 289 | |
| 290 | QString Attica::Content::smallPreviewPicture(const QString &number) const |
| 291 | { |
| 292 | return attribute(key: QLatin1String("smallpreviewpic" ) + number); |
| 293 | } |
| 294 | |
| 295 | QList<Icon> Attica::Content::icons() |
| 296 | { |
| 297 | return d->m_icons; |
| 298 | } |
| 299 | |
| 300 | QList<Icon> Attica::Content::icons() const |
| 301 | { |
| 302 | return d->m_icons; |
| 303 | } |
| 304 | |
| 305 | void Attica::Content::setIcons(QList<Icon> icons) |
| 306 | { |
| 307 | d->m_icons = std::move(icons); // TODO KF6 Make QList const & and remove the std::move |
| 308 | } |
| 309 | |
| 310 | QList<QUrl> Attica::Content::videos() |
| 311 | { |
| 312 | return d->m_videos; |
| 313 | } |
| 314 | |
| 315 | void Attica::Content::setVideos(QList<QUrl> videos) |
| 316 | { |
| 317 | d->m_videos = std::move(videos); |
| 318 | } |
| 319 | |
| 320 | QStringList Attica::Content::tags() const |
| 321 | { |
| 322 | return d->m_tags; |
| 323 | } |
| 324 | |
| 325 | void Attica::Content::setTags(const QStringList &tags) |
| 326 | { |
| 327 | d->m_tags = tags; |
| 328 | } |
| 329 | |