Kgapi

locationfetchhistoryjob.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 "locationfetchhistoryjob.h"
10 #include "account.h"
11 #include "../debug.h"
12 #include "location.h"
13 #include "utils.h"
14 #include "latitudeservice.h"
15 
16 #include <QNetworkRequest>
17 #include <QNetworkReply>
18 
19 
20 using namespace KGAPI2;
21 
22 class Q_DECL_HIDDEN LocationFetchHistoryJob::Private
23 {
24  public:
25  Private(LocationFetchHistoryJob *parent);
26  QNetworkRequest createRequest(const QUrl &url);
27 
28  Latitude::Granularity granularity = Latitude::City;
29  int maxResults = 0;
30  qlonglong minTimestamp = 0;
31  qlonglong maxTimestamp = 0;
32 
33  private:
34  LocationFetchHistoryJob * const q;
35 };
36 
37 LocationFetchHistoryJob::Private::Private(LocationFetchHistoryJob *parent):
38  q(parent)
39 {
40 }
41 
42 QNetworkRequest LocationFetchHistoryJob::Private::createRequest(const QUrl& url)
43 {
44  QNetworkRequest request(url);
45  request.setRawHeader("GData-Version", LatitudeService::APIVersion().toLatin1());
46 
47  return request;
48 }
49 
50 
52  FetchJob(account, parent),
53  d(new Private(this))
54 {
55 }
56 
58 {
59  delete d;
60 }
61 
63 {
64  return d->maxResults;
65 }
66 
68 {
69  if (isRunning()) {
70  qCWarning(KGAPIDebug) << "Can't modify maxResults property while job is running";
71  }
72  d->maxResults = results;
73 }
74 
75 Latitude::Granularity LocationFetchHistoryJob::granularity() const
76 {
77  return d->granularity;
78 }
79 
81 {
82  if (isRunning()) {
83  qCWarning(KGAPIDebug) << "Can't modify maxResults property while job is running";
84  }
85  d->granularity = granularity;
86 }
87 
89 {
90  return d->minTimestamp;
91 }
92 
94 {
95  if (isRunning()) {
96  qCWarning(KGAPIDebug) << "Can't modify maxResults property while job is running";
97  }
98  d->minTimestamp = minTimestamp;
99 }
100 
102 {
103  return d->maxTimestamp;
104 }
105 
107 {
108  if (isRunning()) {
109  qCWarning(KGAPIDebug) << "Can't modify maxResults property while job is running";
110  }
111  d->maxTimestamp = maxTimestamp;
112 }
113 
115 {
116  const QUrl url = LatitudeService::locationHistoryUrl(d->granularity, d->maxResults, d->maxTimestamp, d->minTimestamp);
117  const QNetworkRequest request = d->createRequest(url);
118 
119  enqueueRequest(request);
120 }
121 
123 {
124  FeedData feedData;
125  const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString();
126  ContentType ct = Utils::stringToContentType(contentType);
128  if (ct == KGAPI2::JSON) {
129  items << LatitudeService::parseLocationJSONFeed(rawData, feedData);
130  } else {
132  setErrorString(tr("Invalid response content type"));
133  emitFinished();
134  }
135 
136  if (feedData.nextPageUrl.isValid()) {
137  const QNetworkRequest request = d->createRequest(feedData.nextPageUrl);
138  enqueueRequest(request);
139  }
140 
141  return items;
142 }
143 
144 #include "moc_locationfetchhistoryjob.cpp"
AccountPtr account() const
Returns account used to authenticate requests.
Definition: job.cpp:425
Latitude::Granularity granularity() const
Returns granularity of the requested locations.
bool isRunning() const
Whether job is running.
void start() override
KGAPI2::Job::start implementation.
LocationFetchHistoryJob(const AccountPtr &account, QObject *parent=nullptr)
Constructs a job that will fetch all past user&#39;s locations from Google Latitude service.
qlonglong minTimestamp() const
Returns lower date limit for fetched locations.
ObjectsList handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) override
KGAPI2::Job::handleReplyWithItems.
QUrl nextPageUrl
Link to next page of feed.
Definition: types.h:38
void setMaxResults(int results)
Sets maximum number of locations to fetch.
Structure to store additional information about a feed.
Definition: types.h:24
QString tr(const char *sourceText, const char *disambiguation, int n)
ObjectsList parseLocationJSONFeed(const QByteArray &jsonFeed, FeedData &feedData)
Parses a JSON feed into list of Locations.
virtual ObjectsList items() const
Returns all items fetched by this job.
Definition: fetchjob.cpp:41
virtual void enqueueRequest(const QNetworkRequest &request, const QByteArray &data=QByteArray(), const QString &contentType=QString())
Enqueues request in dispatcher queue.
Definition: job.cpp:497
A job to fetch a single map tile described by a StaticMapUrl.
Definition: blog.h:16
int maxResults() const
Returns maximum number of locations the job will fetch.
LibKGAPI error - Google returned invalid response.
Definition: types.h:180
QString APIVersion()
Supported version of API.
QVariant header(QNetworkRequest::KnownHeaders header) const const
virtual void emitFinished()
Emits Job::finished() signal.
Definition: job.cpp:479
bool isValid() const const
qlonglong maxTimestamp() const
Returns upper date limit for fetched locations.
void setErrorString(const QString &errorString)
Set job error description to errorString.
Definition: job.cpp:390
~LocationFetchHistoryJob() override
Destructor.
A job to fetch all past locations from user&#39;s account on Google Latitude.
QUrl locationHistoryUrl(const Latitude::Granularity granularity, const int maxResults, const qlonglong maxTime, const qlonglong minTime)
Returns URL for fetching history of user&#39;s locations.
void setGranularity(Latitude::Granularity granularity)
Sets granularity of the requested results.
void setError(KGAPI2::Error error)
Set job error to error.
Definition: job.cpp:375
Abstract superclass for all jobs that fetch resources from Google.
Definition: fetchjob.h:24
void setMaxTimestamp(qlonglong maxTimestamp)
Sets upper date limit for locations to fetch.
QObject * parent() const const
QString toString() const const
void setMinTimestamp(qlonglong minTimestamp)
Sets lower date limit for locations to fetch.
ContentType
Definition: types.h:207
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jan 20 2021 23:16:12 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.