Kgapi

contactfetchjob.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 "contactfetchjob.h"
10 #include "contact.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 #include <QUrlQuery>
19 
20 using namespace KGAPI2;
21 
22 class Q_DECL_HIDDEN ContactFetchJob::Private
23 {
24  public:
25  Private(ContactFetchJob *parent);
26 
27  QNetworkRequest createRequest(const QUrl &url);
28 
29  bool fetchDeleted = true;
30  QString contactId;
31  quint64 timestamp = 0;
33 
34  private:
35  ContactFetchJob * const q;
36 };
37 
38 ContactFetchJob::Private::Private(ContactFetchJob *parent):
39  q(parent)
40 {
41 }
42 
43 QNetworkRequest ContactFetchJob::Private::createRequest(const QUrl& url)
44 {
45  QNetworkRequest request(url);
46  request.setRawHeader("GData-Version", ContactsService::APIVersion().toLatin1());
47 
48  QStringList headers;
49  auto rawHeaderList = request.rawHeaderList();
50  headers.reserve(rawHeaderList.size());
51  for (const QByteArray &str : qAsConst(rawHeaderList)) {
52  headers << QLatin1String(str) + QLatin1String(": ") + QLatin1String(request.rawHeader(str));
53  }
54 
55  return request;
56 }
57 
59  FetchJob(account, parent),
60  d(new Private(this))
61 {
62 }
63 
65  FetchJob(account, parent),
66  d(new Private(this))
67 {
68  d->contactId = contactId;
69 }
70 
72 {
73  delete d;
74 }
75 
77 {
78  return d->fetchDeleted;
79 }
80 
82 {
83  if (isRunning()) {
84  qCWarning(KGAPIDebug) << "Can't modify fetchDeleted property when job is running";
85  return;
86  }
87 
88  d->fetchDeleted = fetchDeleted;
89 }
90 
92 {
93  return d->timestamp;
94 }
95 
96 void ContactFetchJob::setFetchOnlyUpdated(quint64 timestamp)
97 {
98  if (isRunning()) {
99  qCWarning(KGAPIDebug) << "Can't modify fetchOnlyUpdated property when job is running";
100  return;
101  }
102 
103  d->timestamp = timestamp;
104 }
105 
107 {
108  return d->filter;
109 }
110 
112 {
113  if (isRunning()) {
114  qCWarning(KGAPIDebug) << "Can't modify filter property when job is running";
115  return;
116  }
117 
118  d->filter = query;
119 }
120 
122 {
123  QUrl url;
124  if (d->contactId.isEmpty()) {
125  url = ContactsService::fetchAllContactsUrl(account()->accountName(), d->fetchDeleted);
126  QUrlQuery query(url);
127  if (d->timestamp > 0) {
128  query.addQueryItem(QStringLiteral("updated-min"), Utils::ts2Str(d->timestamp));
129  }
130  if (!d->filter.isEmpty()) {
131  query.addQueryItem(QStringLiteral("q"), d->filter);
132  }
133  url.setQuery(query);
134  } else {
135  url = ContactsService::fetchContactUrl(account()->accountName(), d->contactId);
136  }
137 
138  const QNetworkRequest request = d->createRequest(url);
139  enqueueRequest(request);
140 }
141 
143 {
144  FeedData feedData;
146  QString itemId;
147  const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
148  ContentType ct = Utils::stringToContentType(contentType);
149  if (ct == KGAPI2::JSON) {
150  if (d->contactId.isEmpty()) {
151  items = ContactsService::parseJSONFeed(rawData, feedData);
152  } else {
153  items << ContactsService::JSONToContact(rawData);
154  }
155 
156  if (feedData.nextPageUrl.isValid()) {
157  emitProgress(feedData.startIndex, feedData.totalResults);
158 
159  const QNetworkRequest request = d->createRequest(feedData.nextPageUrl);
160  enqueueRequest(request);
161  } else {
162  emitFinished();
163  }
164 
165  return items;
166  }
167 
168  return ObjectsList();
169 }
170 
171 
172 
AccountPtr account() const
Returns account used to authenticate requests.
Definition: job.cpp:425
bool isRunning() const
Whether job is running.
QString filter() const
Returns fulltext filter string.
void reserve(int alloc)
void setFetchDeleted(bool fetchDeleted)
Sets whether to fetch deleted contacts.
QString APIVersion()
Supported API version.
ContactFetchJob(const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will fetch all contacts from user&#39;s addressbook.
QUrl nextPageUrl
Link to next page of feed.
Definition: types.h:38
void start() override
KGAPI2::Job::start implementation.
Structure to store additional information about a feed.
Definition: types.h:24
void addQueryItem(const QString &key, const QString &value)
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:492
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition: job.cpp:497
QUrl fetchContactUrl(const QString &user, const QString &contactID)
Returns URL for fetching specific contact.
A job to fetch a single map tile described by a StaticMapUrl.
Definition: blog.h:16
A job to fetch contacts from user&#39;s addressbook on Google Contacts.
ContactPtr JSONToContact(const QByteArray &jsonData)
Parses JSON data into Contact object.
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
void setFilter(const QString &query)
Sets fulltext filter.
quint64 fetchOnlyUpdated()
Returns whether the job will fetch only modified events.
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:479
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::Job::handleReply implementation.
bool isValid() const const
~ContactFetchJob() override
Destructor.
void setFetchOnlyUpdated(quint64 timestamp)
Sets the job to fetch only events modified since timestamp.
void setQuery(const QString &query, QUrl::ParsingMode mode)
int totalResults
Number of all items.
Definition: types.h:37
Abstract superclass for all jobs that fetch resources from Google.
Definition: fetchjob.h:24
QUrl fetchAllContactsUrl(const QString &user, bool showDeleted)
Returns URL for fetching all contacts in addressbook.
bool fetchDeleted() const
Returns whether deleted contacts are fetched.
QObject * parent() const const
QString toString() const const
int startIndex
Index of first item on current feed page.
Definition: types.h:33
ContentType
Definition: types.h:207
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Sep 23 2020 23:10:52 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.