Kgapi

contactfetchjob.cpp
1 /*
2  * This file is part of LibKGAPI library
3  *
4  * Copyright (C) 2013 Daniel Vrátil <[email protected]>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) version 3, or any
10  * later version accepted by the membership of KDE e.V. (or its
11  * successor approved by the membership of KDE e.V.), which shall
12  * act as a proxy defined in Section 6 of version 3 of the license.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library. If not, see <https://www.gnu.org/licenses/>.
21  */
22 
23 #include "contactfetchjob.h"
24 #include "contact.h"
25 #include "contactsservice.h"
26 #include "../debug.h"
27 #include "utils.h"
28 #include "account.h"
29 
30 #include <QNetworkRequest>
31 #include <QNetworkReply>
32 #include <QUrlQuery>
33 
34 using namespace KGAPI2;
35 
36 class Q_DECL_HIDDEN ContactFetchJob::Private
37 {
38  public:
39  Private(ContactFetchJob *parent);
40 
41  QNetworkRequest createRequest(const QUrl &url);
42 
43  bool fetchDeleted;
44  QString contactId;
45  quint64 timestamp;
47 
48  private:
49  ContactFetchJob * const q;
50 };
51 
52 ContactFetchJob::Private::Private(ContactFetchJob *parent):
53  fetchDeleted(true),
54  timestamp(0),
55  q(parent)
56 {
57 }
58 
59 QNetworkRequest ContactFetchJob::Private::createRequest(const QUrl& url)
60 {
61  QNetworkRequest request(url);
62  request.setRawHeader("GData-Version", ContactsService::APIVersion().toLatin1());
63 
64  QStringList headers;
65  auto rawHeaderList = request.rawHeaderList();
66  headers.reserve(rawHeaderList.size());
67  for (const QByteArray &str : qAsConst(rawHeaderList)) {
68  headers << QLatin1String(str) + QLatin1String(": ") + QLatin1String(request.rawHeader(str));
69  }
70 
71  return request;
72 }
73 
75  FetchJob(account, parent),
76  d(new Private(this))
77 {
78 }
79 
81  FetchJob(account, parent),
82  d(new Private(this))
83 {
84  d->contactId = contactId;
85 }
86 
88 {
89  delete d;
90 }
91 
93 {
94  return d->fetchDeleted;
95 }
96 
98 {
99  if (isRunning()) {
100  qCWarning(KGAPIDebug) << "Can't modify fetchDeleted property when job is running";
101  return;
102  }
103 
104  d->fetchDeleted = fetchDeleted;
105 }
106 
108 {
109  return d->timestamp;
110 }
111 
113 {
114  if (isRunning()) {
115  qCWarning(KGAPIDebug) << "Can't modify fetchOnlyUpdated property when job is running";
116  return;
117  }
118 
119  d->timestamp = timestamp;
120 }
121 
123 {
124  return d->filter;
125 }
126 
128 {
129  if (isRunning()) {
130  qCWarning(KGAPIDebug) << "Can't modify filter property when job is running";
131  return;
132  }
133 
134  d->filter = query;
135 }
136 
138 {
139  QUrl url;
140  if (d->contactId.isEmpty()) {
141  url = ContactsService::fetchAllContactsUrl(account()->accountName(), d->fetchDeleted);
142  QUrlQuery query(url);
143  if (d->timestamp > 0) {
144  query.addQueryItem(QStringLiteral("updated-min"), Utils::ts2Str(d->timestamp));
145  }
146  if (!d->filter.isEmpty()) {
147  query.addQueryItem(QStringLiteral("q"), d->filter);
148  }
149  url.setQuery(query);
150  } else {
151  url = ContactsService::fetchContactUrl(account()->accountName(), d->contactId);
152  }
153 
154  const QNetworkRequest request = d->createRequest(url);
155  enqueueRequest(request);
156 }
157 
159 {
160  FeedData feedData;
162  QString itemId;
163  const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
164  ContentType ct = Utils::stringToContentType(contentType);
165  if (ct == KGAPI2::JSON) {
166  if (d->contactId.isEmpty()) {
167  items = ContactsService::parseJSONFeed(rawData, feedData);
168  } else {
169  items << ContactsService::JSONToContact(rawData);
170  }
171 
172  if (feedData.nextPageUrl.isValid()) {
173  emitProgress(feedData.startIndex, feedData.totalResults);
174 
175  const QNetworkRequest request = d->createRequest(feedData.nextPageUrl);
176  enqueueRequest(request);
177  } else {
178  emitFinished();
179  }
180 
181  return items;
182  }
183 
184  return ObjectsList();
185 }
186 
187 
188 
AccountPtr account() const
Returns account used to authenticate requests.
Definition: job.cpp:437
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:52
void start() override
KGAPI2::Job::start implementation.
Structure to store additional information about a feed.
Definition: types.h:38
void addQueryItem(const QString &key, const QString &value)
virtual ObjectsList items() const
Returns all items fetched by this job.
Definition: fetchjob.cpp:55
virtual void emitProgress(int processed, int total)
Emit progress() signal.
Definition: job.cpp:504
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition: job.cpp:509
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:30
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:491
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:51
Abstract superclass for all jobs that fetch resources from Google.
Definition: fetchjob.h:38
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:47
ContentType
Definition: types.h:219
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun May 31 2020 22:38:47 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.