| 1 | /* |
|---|---|
| 2 | SPDX-FileCopyrightText: 2016 Dan Leinir Turthra Jensen <admin@leinir.dk> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 5 | */ |
| 6 | |
| 7 | #include "downloadlinkinfo.h" |
| 8 | #include <KFormat> |
| 9 | #include <QMimeDatabase> |
| 10 | |
| 11 | class DownloadLinkInfoPrivate : public QSharedData |
| 12 | { |
| 13 | public: |
| 14 | QString name; |
| 15 | QString priceAmount; |
| 16 | QString distributionType; |
| 17 | QString descriptionLink; |
| 18 | int id = 0; |
| 19 | bool isDownloadtypeLink = true; |
| 20 | quint64 size = 0; |
| 21 | QString mimeType; |
| 22 | QString icon; |
| 23 | }; |
| 24 | |
| 25 | DownloadLinkInfo::DownloadLinkInfo(const KNSCore::Entry::DownloadLinkInformation &data) |
| 26 | : d(new DownloadLinkInfoPrivate) |
| 27 | { |
| 28 | d->name = data.name; |
| 29 | d->priceAmount = data.priceAmount; |
| 30 | d->distributionType = data.distributionType; |
| 31 | d->descriptionLink = data.descriptionLink; |
| 32 | d->id = data.id; |
| 33 | d->isDownloadtypeLink = data.isDownloadtypeLink; |
| 34 | d->size = data.size; |
| 35 | QMimeDatabase db; |
| 36 | for (QString string : data.tags) { |
| 37 | if (string.startsWith(QStringLiteral("data##mimetype="))) { |
| 38 | d->mimeType = string.split(QStringLiteral("=")).last(); |
| 39 | } |
| 40 | } |
| 41 | d->icon = db.mimeTypeForName(nameOrAlias: d->mimeType).iconName(); |
| 42 | if (d->icon.isEmpty()) { |
| 43 | d->icon = db.mimeTypeForName(nameOrAlias: d->mimeType).genericIconName(); |
| 44 | } |
| 45 | if (d->icon.isEmpty()) { |
| 46 | d->icon = QStringLiteral("download"); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | DownloadLinkInfo::DownloadLinkInfo(const DownloadLinkInfo &) = default; |
| 51 | DownloadLinkInfo &DownloadLinkInfo::operator=(const DownloadLinkInfo &) = default; |
| 52 | DownloadLinkInfo::~DownloadLinkInfo() = default; |
| 53 | |
| 54 | QString DownloadLinkInfo::name() const |
| 55 | { |
| 56 | return d->name; |
| 57 | } |
| 58 | |
| 59 | QString DownloadLinkInfo::priceAmount() const |
| 60 | { |
| 61 | return d->priceAmount; |
| 62 | } |
| 63 | |
| 64 | QString DownloadLinkInfo::distributionType() const |
| 65 | { |
| 66 | return d->distributionType; |
| 67 | } |
| 68 | |
| 69 | QString DownloadLinkInfo::descriptionLink() const |
| 70 | { |
| 71 | return d->descriptionLink; |
| 72 | } |
| 73 | |
| 74 | int DownloadLinkInfo::id() const |
| 75 | { |
| 76 | return d->id; |
| 77 | } |
| 78 | |
| 79 | bool DownloadLinkInfo::isDownloadtypeLink() const |
| 80 | { |
| 81 | return d->isDownloadtypeLink; |
| 82 | } |
| 83 | |
| 84 | quint64 DownloadLinkInfo::size() const |
| 85 | { |
| 86 | return d->size; |
| 87 | } |
| 88 | |
| 89 | QString DownloadLinkInfo::formattedSize() const |
| 90 | { |
| 91 | static const KFormat formatter; |
| 92 | if (d->size == 0) { |
| 93 | return QString(); |
| 94 | } |
| 95 | return formatter.formatByteSize(size: d->size * 1000); |
| 96 | } |
| 97 | |
| 98 | QString DownloadLinkInfo::icon() const |
| 99 | { |
| 100 | return d->icon; |
| 101 | } |
| 102 | |
| 103 | #include "moc_downloadlinkinfo.cpp" |
| 104 |
