Commit 8c616082 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧 Committed by Aleix Pol Gonzalez
Browse files

pk: Try harder to convert non-appstream packages licences

It will try to use as_license_to_spdx_id which does its best guess,
hopefully it will be enough on most cases, will submit some others that
I've seen upstream to catch some other cases.

BUG: 454480


(cherry picked from commit 2787a529)
parent c3495090
Pipeline #209058 canceled with stage
in 12 seconds
......@@ -698,7 +698,7 @@ DiscoverPage {
text: modelData.name
url: modelData.url
horizontalAlignment: Text.AlignLeft
color: text.toLowerCase().includes(i18n("proprietary")) ? Kirigami.Theme.neutralTextColor: enabled ? Kirigami.Theme.linkColor : Kirigami.Theme.textColor
color: !modelData.hasFreedom ? Kirigami.Theme.neutralTextColor: enabled ? Kirigami.Theme.linkColor : Kirigami.Theme.textColor
}
}
}
......
......@@ -66,27 +66,32 @@ QJsonArray AppStreamUtils::licenses(const AppStream::Component &appdata)
QJsonArray ret;
const auto licenses = AppStream::SPDX::tokenizeLicense(appdata.projectLicense());
for (const auto &token : licenses) {
QString license = token;
license.remove(0, 1); // tokenize prefixes with an @ for some reason
ret += license(token.mid(1)); // tokenize prefixes with an @ for some reason
}
return ret;
}
bool publicLicense = false;
QString name = license;
if (license.startsWith(QLatin1String("LicenseRef-proprietary"))) {
name = i18n("Proprietary");
} else if (license == QLatin1String("LicenseRef-public-domain")) {
name = i18n("Public Domain");
publicLicense = true;
}
QJsonObject AppStreamUtils::license(const QString &license)
{
bool publicLicense = false;
QString name = license;
if (license.startsWith(QLatin1String("LicenseRef-proprietary"))) {
name = i18n("Proprietary");
} else if (license == QLatin1String("LicenseRef-public-domain")) {
name = i18n("Public Domain");
publicLicense = true;
}
if (!AppStream::SPDX::isLicenseId(license))
continue;
ret.append(QJsonObject{
if (!AppStream::SPDX::isLicenseId(license))
return {
{QStringLiteral("name"), name},
{QStringLiteral("url"), {AppStream::SPDX::licenseUrl(license)}},
{QStringLiteral("hasFreedom"), AppStream::SPDX::isFreeLicense(license) || publicLicense},
});
}
return ret;
{QStringLiteral("hasFreedom"), true}, // give it the benefit of the doubt
};
return {
{QStringLiteral("name"), name},
{QStringLiteral("url"), {AppStream::SPDX::licenseUrl(license)}},
{QStringLiteral("hasFreedom"), AppStream::SPDX::isFreeLicense(license) || publicLicense},
};
}
QStringList AppStreamUtils::appstreamIds(const QUrl &appstreamUrl)
......
......@@ -22,6 +22,8 @@ Q_DECL_EXPORT QPair<QList<QUrl>, QList<QUrl>> fetchScreenshots(const AppStream::
Q_DECL_EXPORT QJsonArray licenses(const AppStream::Component &appdata);
Q_DECL_EXPORT QJsonObject license(const QString &spdxId);
Q_DECL_EXPORT QStringList appstreamIds(const QUrl &appstreamUrl);
/// Helps implement AbstractResource::versionString
......
......@@ -8,7 +8,9 @@
#include "PackageKitResource.h"
#include "PackageKitBackend.h"
#include "PackageKitMessages.h"
#include "appstream/AppStreamUtils.h"
#include "config-paths.h"
#include <AppStreamQt/spdx.h>
#include <KLocalizedString>
#include <KShell>
#include <PackageKit/Daemon>
......@@ -89,12 +91,45 @@ QVariant PackageKitResource::icon() const
return QStringLiteral("applications-other");
}
static QMap<QString, QString> s_translation = {
{"AGPL", "AGPL-3.0"},
{"AGPL3", "AGPL-3.0"},
{"Artistic2.0", "Artistic-2.0"},
{"Apache", "Apache-2.0"},
{"APACHE", "Apache-2.0"},
{"CCPL", "CC0-1.0"},
{"GPL2", "GPL-2.0"},
{"GPL3", "GPL-3.0"},
{"FDL1.2", "GFDL-1.2-only"},
{"FDL1.3", "GFDL-1.3-only"},
{"LGPL", "LGPL-2.1"},
{"LGPL3", "LGPL-3.0"},
{"MPL", "MPL-1.1"},
{"MPL2", "MPL-2.0"},
{"PerlArtistic", "Artistic-1.0-Perl"},
{"PHP", "PHP-3.01"},
{"PSF", "Python-2.0"},
{"RUBY", "Ruby"},
{"ZPL", "ZPL-2.1"},
};
QJsonArray PackageKitResource::licenses()
{
fetchDetails();
if (!m_details.license().isEmpty()) {
return {QJsonObject{{QStringLiteral("name"), m_details.license()}}};
QString id = m_details.license();
if (!AppStream::SPDX::isLicenseId(id)) {
auto spdxId = AppStream::SPDX::asSpdxId(id);
if (!spdxId.isEmpty()) {
id = spdxId;
}
}
if (!AppStream::SPDX::isLicenseId(id)) {
id = s_translation.value(id, id);
}
return {AppStreamUtils::license(id)};
}
return {QJsonObject{{QStringLiteral("name"), {}}}};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment