File indexing completed on 2024-05-12 05:22:16
0001 /* 0002 SPDX-FileCopyrightText: 2012 Andrius da Costa Ribas <andriusmao@gmail.com> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include "childreference.h" 0008 #include "utils_p.h" 0009 0010 #include <QJsonDocument> 0011 #include <QVariantMap> 0012 0013 using namespace KGAPI2; 0014 using namespace KGAPI2::Drive; 0015 0016 class Q_DECL_HIDDEN ChildReference::Private 0017 { 0018 public: 0019 Private(); 0020 Private(const Private &other); 0021 0022 QString id; 0023 QUrl selfLink; 0024 QUrl childLink; 0025 0026 static ChildReferencePtr fromJSON(const QVariantMap &map); 0027 }; 0028 0029 ChildReference::Private::Private() 0030 { 0031 } 0032 0033 ChildReference::Private::Private(const Private &other) 0034 : id(other.id) 0035 , selfLink(other.selfLink) 0036 , childLink(other.childLink) 0037 { 0038 } 0039 0040 ChildReferencePtr ChildReference::Private::fromJSON(const QVariantMap &map) 0041 { 0042 if (!map.contains(QLatin1StringView("kind")) || map[QStringLiteral("kind")].toString() != QLatin1StringView("drive#childReference")) { 0043 return ChildReferencePtr(); 0044 } 0045 0046 ChildReferencePtr reference(new ChildReference(map[QStringLiteral("id")].toString())); 0047 reference->d->selfLink = map[QStringLiteral("selfLink")].toUrl(); 0048 reference->d->childLink = map[QStringLiteral("childLink")].toUrl(); 0049 0050 return reference; 0051 } 0052 0053 ChildReference::ChildReference(const QString &id) 0054 : KGAPI2::Object() 0055 , d(new Private) 0056 { 0057 d->id = id; 0058 } 0059 0060 ChildReference::ChildReference(const ChildReference &other) 0061 : KGAPI2::Object(other) 0062 , d(new Private(*(other.d))) 0063 { 0064 } 0065 0066 ChildReference::~ChildReference() 0067 { 0068 delete d; 0069 } 0070 0071 bool ChildReference::operator==(const ChildReference &other) const 0072 { 0073 if (!Object::operator==(other)) { 0074 return false; 0075 } 0076 GAPI_COMPARE(id) 0077 GAPI_COMPARE(selfLink) 0078 GAPI_COMPARE(childLink) 0079 return true; 0080 } 0081 0082 QString ChildReference::id() const 0083 { 0084 return d->id; 0085 } 0086 0087 QUrl ChildReference::selfLink() const 0088 { 0089 return d->selfLink; 0090 } 0091 0092 QUrl ChildReference::childLink() const 0093 { 0094 return d->childLink; 0095 } 0096 0097 ChildReferencePtr ChildReference::fromJSON(const QByteArray &jsonData) 0098 { 0099 QJsonDocument document = QJsonDocument::fromJson(jsonData); 0100 if (document.isNull()) { 0101 return ChildReferencePtr(); 0102 } 0103 0104 const QVariant data = document.toVariant(); 0105 return Private::fromJSON(data.toMap()); 0106 } 0107 0108 ChildReferencesList ChildReference::fromJSONFeed(const QByteArray &jsonData, FeedData &feedData) 0109 { 0110 QJsonDocument document = QJsonDocument::fromJson(jsonData); 0111 if (document.isNull()) { 0112 return ChildReferencesList(); 0113 } 0114 0115 const QVariant data = document.toVariant(); 0116 const QVariantMap map = data.toMap(); 0117 if (!map.contains(QLatin1StringView("kind")) || map[QStringLiteral("kind")].toString() != QLatin1StringView("drive#childList")) { 0118 return ChildReferencesList(); 0119 } 0120 0121 ChildReferencesList list; 0122 const QVariantList items = map[QStringLiteral("items")].toList(); 0123 for (const QVariant &item : items) { 0124 ChildReferencePtr reference = Private::fromJSON(item.toMap()); 0125 0126 if (!reference.isNull()) { 0127 list << reference; 0128 } 0129 } 0130 0131 if (map.contains(QLatin1StringView("nextLink"))) { 0132 feedData.nextPageUrl = map[QStringLiteral("nextLink")].toUrl(); 0133 } 0134 0135 return list; 0136 } 0137 0138 QByteArray ChildReference::toJSON(const ChildReferencePtr &reference) 0139 { 0140 QVariantMap map; 0141 0142 map[QStringLiteral("id")] = reference->id(); 0143 0144 QJsonDocument document = QJsonDocument::fromVariant(map); 0145 return document.toJson(QJsonDocument::Compact); 0146 }