Kgapi

personphotodeletejob.cpp
1/*
2 * This file is part of LibKGAPI library
3 *
4 * SPDX-FileCopyrightText: 2023 Claudio Cambra <claudio.cambra@kde.org>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8
9#include "personphotodeletejob.h"
10#include "peopleservice.h"
11#include "person.h"
12#include "utils.h"
13
14#include <QNetworkRequest>
15#include <QNetworkReply>
16#include <QUrlQuery>
17#include <QJsonDocument>
18#include <QJsonObject>
19#include <QJsonArray>
20
21namespace KGAPI2::People
22{
23
24class Q_DECL_HIDDEN PersonPhotoDeleteJob::Private
25{
26public:
27 explicit Private(PersonPhotoDeleteJob *parent);
28 void processPersonPhoto();
29
30 QString personResourceName;
31 QByteArray photoRawData;
32
33private:
34 PersonPhotoDeleteJob * const q;
35};
36
37PersonPhotoDeleteJob::Private::Private(PersonPhotoDeleteJob *parent)
38 : q(parent)
39{
40}
41
42void PersonPhotoDeleteJob::Private::processPersonPhoto()
43{
44 const auto deleteUrl = PeopleService::deleteContactPhotoUrl(personResourceName, PeopleService::allPersonFields());
45 QNetworkRequest request(deleteUrl);
46 request.setRawHeader("Host", "people.googleapis.com");
47 q->enqueueRequest(request);
48}
49
50PersonPhotoDeleteJob::PersonPhotoDeleteJob(const QString &personResourceName, const AccountPtr &account, QObject* parent)
51 : ModifyJob(account, parent)
52 , d(std::make_unique<Private>(this))
53{
54 d->personResourceName = personResourceName;
55}
56
57PersonPhotoDeleteJob::~PersonPhotoDeleteJob() = default;
58
59void PersonPhotoDeleteJob::start()
60{
61 d->processPersonPhoto();
62}
63
64void PersonPhotoDeleteJob::dispatchRequest(QNetworkAccessManager *accessManager,
65 const QNetworkRequest &request,
66 const QByteArray &data,
67 const QString &contentType)
68{
69 Q_UNUSED(data);
70 Q_UNUSED(contentType);
71
72 QNetworkRequest r = request;
73 if (!r.hasRawHeader("If-Match")) {
74 r.setRawHeader("If-Match", "*");
75 }
76
77 accessManager->sendCustomRequest(r, "DELETE");
78}
79
80ObjectsList PersonPhotoDeleteJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
81{
82 const auto contentTypeString = reply->header(QNetworkRequest::ContentTypeHeader).toString();
83 const auto contentType = Utils::stringToContentType(contentTypeString);
84
85 if (contentType != KGAPI2::JSON) {
87 setErrorString(tr("Invalid response content type"));
88 emitFinished();
89 return {};
90 }
91
92 ObjectsList items;
93 const auto json = QJsonDocument::fromJson(rawData);
94 if (json.isObject()) {
95 const auto jsonObject = json.object();
96 const auto personJson = jsonObject.value(QStringLiteral("person")).toObject();
97 const auto person = Person::fromJSON(personJson);
98 items << person;
99 }
100
101 emitFinished();
102 return items;
103}
104
105}
106
107#include "moc_personphotodeletejob.cpp"
@ InvalidResponse
LibKGAPI error - Google returned invalid response.
Definition types.h:183
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QNetworkReply * sendCustomRequest(const QNetworkRequest &request, const QByteArray &verb, QHttpMultiPart *multiPart)
QVariant header(QNetworkRequest::KnownHeaders header) const const
bool hasRawHeader(const QByteArray &headerName) const const
void setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:11:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.