File indexing completed on 2024-05-12 05:22:21
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 "parentreference.h" 0008 #include "parentreference_p.h" 0009 #include "utils_p.h" 0010 0011 #include <QJsonDocument> 0012 #include <QVariantMap> 0013 0014 using namespace KGAPI2; 0015 using namespace KGAPI2::Drive; 0016 0017 ParentReference::Private::Private() 0018 : isRoot(false) 0019 { 0020 } 0021 0022 ParentReference::Private::Private(const Private &other) 0023 : id(other.id) 0024 , selfLink(other.selfLink) 0025 , parentLink(other.parentLink) 0026 , isRoot(other.isRoot) 0027 { 0028 } 0029 0030 ParentReferencePtr ParentReference::Private::fromJSON(const QVariantMap &map) 0031 { 0032 if (!map.contains(QLatin1StringView("kind")) || map[QStringLiteral("kind")].toString() != QLatin1StringView("drive#parentReference")) { 0033 return ParentReferencePtr(); 0034 } 0035 0036 ParentReferencePtr reference(new ParentReference(map[QStringLiteral("id")].toString())); 0037 reference->d->selfLink = map[QStringLiteral("selfLink")].toUrl(); 0038 reference->d->parentLink = map[QStringLiteral("parentLink")].toUrl(); 0039 reference->d->isRoot = map[QStringLiteral("isRoot")].toBool(); 0040 0041 return reference; 0042 } 0043 0044 QVariantMap ParentReference::Private::toJSON(const ParentReferencePtr &reference) 0045 { 0046 QVariantMap map; 0047 if (!reference->d->id.isEmpty()) { 0048 map[QStringLiteral("id")] = reference->id(); 0049 } 0050 if (!reference->d->selfLink.isEmpty()) { 0051 map[QStringLiteral("selfLink")] = reference->d->selfLink; 0052 } 0053 if (!reference->d->parentLink.isEmpty()) { 0054 map[QStringLiteral("parentLink")] = reference->d->parentLink; 0055 } 0056 if (reference->d->isRoot) { // default is false 0057 map[QStringLiteral("isRoot")] = reference->d->isRoot; 0058 } 0059 0060 return map; 0061 } 0062 0063 ParentReference::ParentReference(const QString &id) 0064 : KGAPI2::Object() 0065 , d(new Private) 0066 { 0067 d->id = id; 0068 } 0069 0070 ParentReference::ParentReference(const ParentReference &other) 0071 : KGAPI2::Object(other) 0072 , d(new Private(*(other.d))) 0073 { 0074 } 0075 0076 ParentReference::~ParentReference() 0077 { 0078 delete d; 0079 } 0080 0081 bool ParentReference::operator==(const ParentReference &other) const 0082 { 0083 if (!Object::operator==(other)) { 0084 return false; 0085 } 0086 GAPI_COMPARE(id) 0087 GAPI_COMPARE(selfLink) 0088 GAPI_COMPARE(parentLink) 0089 GAPI_COMPARE(isRoot) 0090 return true; 0091 } 0092 0093 QString ParentReference::id() const 0094 { 0095 return d->id; 0096 } 0097 0098 QUrl ParentReference::selfLink() const 0099 { 0100 return d->selfLink; 0101 } 0102 0103 QUrl ParentReference::parentLink() const 0104 { 0105 return d->parentLink; 0106 } 0107 0108 bool ParentReference::isRoot() const 0109 { 0110 return d->isRoot; 0111 } 0112 0113 ParentReferencePtr ParentReference::fromJSON(const QByteArray &jsonData) 0114 { 0115 QJsonDocument document = QJsonDocument::fromJson(jsonData); 0116 if (document.isNull()) { 0117 return ParentReferencePtr(); 0118 } 0119 0120 const QVariant data = document.toVariant(); 0121 return Private::fromJSON(data.toMap()); 0122 } 0123 0124 ParentReferencesList ParentReference::fromJSONFeed(const QByteArray &jsonData) 0125 { 0126 QJsonDocument document = QJsonDocument::fromJson(jsonData); 0127 if (document.isNull()) { 0128 return ParentReferencesList(); 0129 } 0130 0131 const QVariant data = document.toVariant(); 0132 const QVariantMap map = data.toMap(); 0133 if (!map.contains(QLatin1StringView("kind")) || map[QStringLiteral("kind")].toString() != QLatin1StringView("drive#parentList")) { 0134 return ParentReferencesList(); 0135 } 0136 0137 ParentReferencesList list; 0138 const QVariantList items = map[QStringLiteral("items")].toList(); 0139 for (const QVariant &item : items) { 0140 const ParentReferencePtr reference = Private::fromJSON(item.toMap()); 0141 0142 if (!reference.isNull()) { 0143 list << reference; 0144 } 0145 } 0146 0147 return list; 0148 } 0149 0150 QByteArray ParentReference::toJSON(const ParentReferencePtr &reference) 0151 { 0152 const QVariantMap map = Private::toJSON(reference); 0153 0154 QJsonDocument document = QJsonDocument::fromVariant(map); 0155 return document.toJson(QJsonDocument::Compact); 0156 }