Kgapi

contactgroupfetchjob.cpp
1/*
2 * This file is part of LibKGAPI library
3 *
4 * SPDX-FileCopyrightText: 2022 Claudio Cambra <claudio.cambra@kde.org>
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 *
7 */
8
9#include "contactgroupfetchjob.h"
10#include "peopleservice.h"
11#include "contactgroup.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 ContactGroupFetchJob::Private
25{
26public:
27 explicit Private(ContactGroupFetchJob *parent);
28
29 QNetworkRequest createRequest(const QUrl &url);
30
31 QString resourceName;
32
33private:
34 ContactGroupFetchJob * const q;
35};
36
37ContactGroupFetchJob::Private::Private(ContactGroupFetchJob *parent)
38 : q(parent)
39{
40}
41
42ContactGroupFetchJob::ContactGroupFetchJob(const AccountPtr& account, QObject* parent)
43 : FetchJob(account, parent)
44 , d(std::make_unique<Private>(this))
45{
46}
47
48QNetworkRequest ContactGroupFetchJob::Private::createRequest(const QUrl& url)
49{
50 QNetworkRequest request(url);
51 request.setRawHeader("Host", "people.googleapis.com");
52 return request;
53}
54
55ContactGroupFetchJob::ContactGroupFetchJob(const QString &resourceName,
56 const AccountPtr &account,
57 QObject* parent)
58 : FetchJob(account, parent)
59 , d(std::make_unique<Private>(this))
60{
61 d->resourceName = resourceName;
62}
63
64ContactGroupFetchJob::~ContactGroupFetchJob() = default;
65
66void ContactGroupFetchJob::start()
67{
68 QUrl url;
69 if (d->resourceName.isEmpty()) {
70 url = PeopleService::fetchAllContactGroupsUrl();
71 } else {
72 url = PeopleService::fetchContactGroupUrl(d->resourceName);
73 }
74
75 const QNetworkRequest request = d->createRequest(url);
76 enqueueRequest(request);
77}
78
79
80ObjectsList ContactGroupFetchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
81{
82 FeedData feedData;
83 ObjectsList items;
84
85 const auto contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
86 ContentType ct = Utils::stringToContentType(contentType);
87
88 if (ct != KGAPI2::JSON) {
89 return ObjectsList();
90 }
91
92 if (d->resourceName.isEmpty()) {
93 items = PeopleService::parseContactGroupsJSONFeed(feedData, rawData);
94 } else {
95 const auto jsonDocumentFromData = QJsonDocument::fromJson(rawData);
96 if(jsonDocumentFromData.isObject()) {
97 items << People::ContactGroup::fromJSON(jsonDocumentFromData.object());
98 } else {
99 qDebug() << "JSON document does not have object";
100 }
101 }
102
103 if (feedData.nextPageUrl.isValid()) {
104 emitProgress(feedData.startIndex, feedData.totalResults);
105
106 const auto request = d->createRequest(feedData.nextPageUrl);
107 enqueueRequest(request);
108 } else {
109 emitFinished();
110 }
111
112 return items;
113}
114
115}
116
117#include "moc_contactgroupfetchjob.cpp"
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QVariant header(QNetworkRequest::KnownHeaders header) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.