Kgapi

contactsgroupfetchjob.cpp
1 /*
2  * This file is part of LibKGAPI library
3  *
4  * SPDX-FileCopyrightText: 2013 Daniel Vrátil <[email protected]>
5  *
6  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7  */
8 
9 #include "contactsgroupfetchjob.h"
10 #include "contactsgroup.h"
11 #include "contactsservice.h"
12 #include "../debug.h"
13 #include "utils.h"
14 #include "account.h"
15 
16 #include <QNetworkRequest>
17 #include <QNetworkReply>
18 
19 
20 using namespace KGAPI2;
21 
22 class Q_DECL_HIDDEN ContactsGroupFetchJob::Private
23 {
24  public:
25  Private(ContactsGroupFetchJob *parent);
26  QNetworkRequest createRequest(const QUrl &url);
27 
28  QString groupId;
29 
30  private:
31  ContactsGroupFetchJob * const q;
32 };
33 
34 ContactsGroupFetchJob::Private::Private(ContactsGroupFetchJob* parent):
35  q(parent)
36 {
37 }
38 
39 QNetworkRequest ContactsGroupFetchJob::Private::createRequest(const QUrl& url)
40 {
41  QNetworkRequest request(url);
42  request.setRawHeader("GData-Version", ContactsService::APIVersion().toLatin1());
43 
44  QStringList headers;
45  auto rawHeaderList = request.rawHeaderList();
46  headers.reserve(rawHeaderList.size());
47  for (const QByteArray &str : qAsConst(rawHeaderList)) {
48  headers << QLatin1String(str) + QLatin1String(": ") + QLatin1String(request.rawHeader(str));
49  }
50 
51  return request;
52 }
53 
54 
56  FetchJob(account, parent),
57  d(new Private(this))
58 {
59 }
60 
62  FetchJob(account, parent),
63  d(new Private(this))
64 {
65  d->groupId = groupId;
66 }
67 
69 {
70  delete d;
71 }
72 
74 {
75  QUrl url;
76  if (d->groupId.isEmpty()) {
77  url = ContactsService::fetchAllGroupsUrl(account()->accountName());
78  } else {
79  url = ContactsService::fetchGroupUrl(account()->accountName(), d->groupId);
80  }
81  const QNetworkRequest request = d->createRequest(url);
82  enqueueRequest(request);
83 }
84 
86 {
87  FeedData feedData;
89  QString itemId;
90  const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
91  ContentType ct = Utils::stringToContentType(contentType);
92  if (ct == KGAPI2::JSON) {
93  if (d->groupId.isEmpty()) {
94  items = ContactsService::parseJSONFeed(rawData, feedData);
95  } else {
96  items << ContactsService::JSONToContactsGroup(rawData);
97  }
98  } else {
100  setErrorString(tr("Invalid response content type"));
101  emitFinished();
102  return items;
103  }
104 
105  if (feedData.nextPageUrl.isValid()) {
106  emitProgress(feedData.itemsPerPage * feedData.startIndex, feedData.totalResults);
107 
108  const QNetworkRequest request = d->createRequest(feedData.nextPageUrl);
109  enqueueRequest(request);
110  }
111 
112  return items;
113 }
114 
115 
AccountPtr account() const
Returns account used to authenticate requests.
Definition: job.cpp:423
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::FetchJob::handleReplyWithItems implementation.
~ContactsGroupFetchJob() override
Destructor.
int itemsPerPage
Number of items per feed page.
Definition: types.h:34
void reserve(int alloc)
QString APIVersion()
Supported API version.
QUrl nextPageUrl
Link to next page of feed.
Definition: types.h:38
Structure to store additional information about a feed.
Definition: types.h:24
A job to fetch contacts groups from user&#39;s addressbook on Google Contacts.
QString tr(const char *sourceText, const char *disambiguation, int n)
virtual ObjectsList items() const
Returns all items fetched by this job.
Definition: fetchjob.cpp:41
virtual void emitProgress(int processed, int total)
Emit progress() signal.
Definition: job.cpp:490
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition: job.cpp:495
QUrl fetchAllGroupsUrl(const QString &user)
Returns URL for KGAPI::Request::FetchAll requests.
A job to fetch a single map tile described by a StaticMapUrl.
Definition: blog.h:16
ContactsGroupPtr JSONToContactsGroup(const QByteArray &jsonData)
Parses JSON data into ContactsGroup object.
QUrl fetchGroupUrl(const QString &user, const QString &groupId)
Returns URL for fetching all contacts groups.
LibKGAPI error - Google returned invalid response.
Definition: types.h:180
ObjectsList parseJSONFeed(const QByteArray &jsonFeed, FeedData &feedData)
Parses JSON feed into list of ContactsGroups or Events.
QVariant header(QNetworkRequest::KnownHeaders header) const const
virtual void emitFinished()
Emits Job::finished() signal.
Definition: job.cpp:477
bool isValid() const const
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition: job.cpp:388
void start() override
KGAPI2::Job::start implementation.
void setError(KGAPI2::Error error)
Set job error to error.
Definition: job.cpp:373
int totalResults
Number of all items.
Definition: types.h:37
Abstract superclass for all jobs that fetch resources from Google.
Definition: fetchjob.h:24
QObject * parent() const const
ContactsGroupFetchJob(const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will fetch all groups from user&#39;s addressbook.
QString toString() const const
int startIndex
Index of first item on current feed page.
Definition: types.h:33
ContentType
Definition: types.h:205
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Jul 8 2020 23:08:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.