File indexing completed on 2024-12-08 10:16:05
0001 /* 0002 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "entities.h" 0008 0009 #include <QJsonArray> 0010 #include <QVariant> 0011 0012 using namespace Wikidata; 0013 0014 Item::Item() = default; 0015 Item::Item(Wikidata::Q id, const QJsonObject& data) 0016 : m_id(id) 0017 , m_data(data) 0018 { 0019 } 0020 0021 Item::~Item() = default; 0022 0023 std::vector<QVariant> Item::values(P property) const 0024 { 0025 std::vector<QVariant> values; 0026 std::vector<bool> prefRank; 0027 0028 auto propA = m_data.value(QLatin1String("claims")).toObject().value(property.toString()).toArray(); 0029 for (const auto & propV : propA) { 0030 const auto propObj = propV.toObject(); 0031 const auto rank = propObj.value(QLatin1String("rank")).toString(); 0032 if (rank == QLatin1String("deprecated")) { 0033 continue; 0034 } 0035 const auto valueObj = propObj.value(QLatin1String("mainsnak")).toObject().value(QLatin1String("datavalue")).toObject(); 0036 const auto type = valueObj.value(QLatin1String("type")).toString(); 0037 if (type == QLatin1String("string")) { 0038 values.push_back(valueObj.value(QLatin1String("value")).toString()); 0039 } else if (type == QLatin1String("wikibase-entityid")) { 0040 values.push_back(QVariant::fromValue(Wikidata::Q(valueObj.value(QLatin1String("value")).toObject().value(QLatin1String("id")).toString()))); 0041 } 0042 // TODO other types 0043 0044 prefRank.push_back(rank == QLatin1String("preferred")); 0045 } 0046 0047 // if there are preferred rank values, take those, otherwise take all normal ranked ones 0048 if (std::any_of(prefRank.begin(), prefRank.end(), [](bool v) { return v; })) { 0049 int i = 0; 0050 for (auto it = values.begin(); it != values.end();) { 0051 if (prefRank[i++]) { 0052 ++it; 0053 } else { 0054 it = values.erase(it); 0055 } 0056 } 0057 } 0058 0059 return values; 0060 } 0061 0062 QVariant Item::value(Wikidata::P property) const 0063 { 0064 const auto vals = values(property); 0065 return vals.empty() ? QVariant() : vals[0]; 0066 } 0067 0068 0069 Image::Image() = default; 0070 Image::Image(const QJsonObject &obj) 0071 : m_data(obj) 0072 { 0073 } 0074 0075 Image::~Image() = default; 0076 0077 QString Image::name() const 0078 { 0079 return m_data.value(QLatin1String("title")).toString().mid(5); 0080 } 0081 0082 uint64_t Image::fileSize() const 0083 { 0084 return imageInfo().value(QLatin1String("size")).toInt(); 0085 } 0086 0087 uint32_t Image::width() const 0088 { 0089 return imageInfo().value(QLatin1String("width")).toDouble(); 0090 } 0091 0092 uint32_t Image::height() const 0093 { 0094 return imageInfo().value(QLatin1String("height")).toDouble(); 0095 } 0096 0097 QString Image::mimeType() const 0098 { 0099 return imageInfo().value(QLatin1String("mime")).toString(); 0100 } 0101 0102 QString Image::license() const 0103 { 0104 const auto extmeta = imageInfo().value(QLatin1String("extmetadata")).toObject(); 0105 return extmeta.value(QLatin1String("LicenseShortName")).toObject().value(QLatin1String("value")).toString(); 0106 } 0107 0108 QJsonObject Image::imageInfo() const 0109 { 0110 const auto ii = m_data.value(QLatin1String("imageinfo")).toArray(); 0111 if (ii.isEmpty()) { 0112 return {}; 0113 } 0114 return ii.at(0).toObject(); 0115 }