Kgapi

fileas.cpp
1/*
2 * SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
3 * SPDX-FileCopyrightText: 2022 Claudio Cambra <claudio.cambra@kde.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only
6 * SPDX-License-Identifier: LGPL-3.0-only
7 * SPDX-License-Identifier: LicenseRef-KDE-Accepted-LGPL
8 */
9
10#include "fileas.h"
11
12#include "fieldmetadata.h"
13#include "peopleservice.h"
14
15#include <QJsonArray>
16#include <QJsonObject>
17#include <QJsonValue>
18#include <QSharedData>
19
20#include <algorithm>
21
22namespace KGAPI2::People
23{
24class FileAs::Private : public QSharedData
25{
26public:
27 explicit Private() = default;
28 Private(const Private &) = default;
29 Private(Private &&) noexcept = delete;
30 Private &operator=(const Private &) = delete;
31 Private &operator=(Private &&) noexcept = delete;
32 ~Private() = default;
33
34 bool operator==(const Private &other) const
35 {
36 return metadata == other.metadata && value == other.value;
37 }
38
39 bool operator!=(const Private &other) const
40 {
41 return !(*this == other);
42 }
43
44 FieldMetadata metadata{};
45 QString value{};
46};
47
49 : d(new Private)
50{
51}
52
53FileAs::FileAs(const FileAs &) = default;
54FileAs::FileAs(FileAs &&) noexcept = default;
55FileAs &FileAs::operator=(const FileAs &) = default;
56FileAs &FileAs::operator=(FileAs &&) noexcept = default;
57FileAs::~FileAs() = default;
58
59bool FileAs::operator==(const FileAs &other) const
60{
61 return *d == *other.d;
62}
63
64bool FileAs::operator!=(const FileAs &other) const
65{
66 return !(*this == other);
67}
68
70{
71 return d->metadata;
72}
73
75{
76 d->metadata = value;
77}
79{
80 return d->value;
81}
82
83void FileAs::setValue(const QString &value)
84{
85 d->value = value;
86}
87
88FileAs FileAs::fromJSON(const QJsonObject &obj)
89{
90 FileAs fileAs;
91
92 if(!obj.isEmpty()) {
93 const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
94 fileAs.setMetadata(FieldMetadata::fromJSON(metadata));
95 fileAs.setValue(obj.value(QStringLiteral("value")).toString());
96 }
97
98 return fileAs;
99}
100
101QList<FileAs> FileAs::fromJSONArray(const QJsonArray &data)
102{
103 QList<FileAs> fileAses;
104
105 for(const auto &fileAs : data) {
106 if(fileAs.isObject()) {
107 const auto objectifiedFileAs = fileAs.toObject();
108 fileAses.append(fromJSON(objectifiedFileAs));
109 }
110 }
111
112 return fileAses;
113}
114
115QJsonValue FileAs::toJSON() const
116{
117 QJsonObject obj;
118
119 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
120 PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
121 return obj;
122}
123
124} // namespace KGAPI2::People
Metadata about a field.
The name that should be used to sort the person in a list.
Definition fileas.h:34
void setValue(const QString &value)
Sets value of the value property.
Definition fileas.cpp:83
FieldMetadata metadata() const
Metadata about the file-as.
Definition fileas.cpp:69
QString value() const
The file-as value.
Definition fileas.cpp:78
FileAs()
Constructs a new FileAs.
Definition fileas.cpp:48
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
Definition fileas.cpp:74
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QJsonObject toObject() const const
void append(QList< T > &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.