Changeset View
Changeset View
Standalone View
Standalone View
src/blogger/postsearchjob.cpp
Show All 32 Lines | |||||
33 | 33 | | |||
34 | class Q_DECL_HIDDEN PostSearchJob::Private | 34 | class Q_DECL_HIDDEN PostSearchJob::Private | ||
35 | { | 35 | { | ||
36 | public: | 36 | public: | ||
37 | Private(const QString &blogId, | 37 | Private(const QString &blogId, | ||
38 | const QString &query, | 38 | const QString &query, | ||
39 | PostSearchJob *parent); | 39 | PostSearchJob *parent); | ||
40 | 40 | | |||
41 | QNetworkRequest createRequest(const QUrl &url); | | |||
42 | | ||||
43 | QString blogId; | 41 | QString blogId; | ||
44 | QString query; | 42 | QString query; | ||
45 | bool fetchBodies; | 43 | bool fetchBodies; | ||
46 | 44 | | |||
47 | private: | 45 | private: | ||
48 | PostSearchJob *q; | 46 | PostSearchJob *q; | ||
49 | }; | 47 | }; | ||
50 | 48 | | |||
51 | PostSearchJob::Private::Private(const QString &blogId_, | 49 | PostSearchJob::Private::Private(const QString &blogId_, | ||
52 | const QString &query_, | 50 | const QString &query_, | ||
53 | PostSearchJob *parent) | 51 | PostSearchJob *parent) | ||
54 | : blogId(blogId_) | 52 | : blogId(blogId_) | ||
55 | , query(query_) | 53 | , query(query_) | ||
56 | , fetchBodies(true) | 54 | , fetchBodies(true) | ||
57 | , q(parent) | 55 | , q(parent) | ||
58 | { | 56 | { | ||
59 | } | 57 | } | ||
60 | 58 | | |||
61 | QNetworkRequest PostSearchJob::Private::createRequest(const QUrl &url) | | |||
62 | { | | |||
63 | QNetworkRequest request; | | |||
64 | if (q->account()) { | | |||
65 | request.setRawHeader("Authorization", "Bearer " + q->account()->accessToken().toLatin1()); | | |||
66 | } | | |||
67 | request.setUrl(url); | | |||
68 | | ||||
69 | return request; | | |||
70 | } | | |||
71 | | ||||
72 | PostSearchJob::PostSearchJob(const QString &blogId, | 59 | PostSearchJob::PostSearchJob(const QString &blogId, | ||
73 | const QString &query, | 60 | const QString &query, | ||
74 | const AccountPtr &account, | 61 | const AccountPtr &account, | ||
75 | QObject *parent) | 62 | QObject *parent) | ||
76 | : FetchJob(account, parent) | 63 | : FetchJob(account, parent) | ||
77 | , d(new Private(blogId, query, this)) | 64 | , d(new Private(blogId, query, this)) | ||
78 | { | 65 | { | ||
79 | } | 66 | } | ||
Show All 15 Lines | |||||
95 | 82 | | |||
96 | void PostSearchJob::start() | 83 | void PostSearchJob::start() | ||
97 | { | 84 | { | ||
98 | QUrl url = BloggerService::searchPostUrl(d->blogId); | 85 | QUrl url = BloggerService::searchPostUrl(d->blogId); | ||
99 | QUrlQuery query(url); | 86 | QUrlQuery query(url); | ||
100 | query.addQueryItem(QStringLiteral("q"), d->query); | 87 | query.addQueryItem(QStringLiteral("q"), d->query); | ||
101 | query.addQueryItem(QStringLiteral("fetchBodies"), Utils::bool2Str(d->fetchBodies)); | 88 | query.addQueryItem(QStringLiteral("fetchBodies"), Utils::bool2Str(d->fetchBodies)); | ||
102 | url.setQuery(query); | 89 | url.setQuery(query); | ||
103 | const QNetworkRequest request = d->createRequest(url); | 90 | const QNetworkRequest request(url); | ||
104 | enqueueRequest(request); | 91 | enqueueRequest(request); | ||
105 | } | 92 | } | ||
106 | 93 | | |||
107 | ObjectsList PostSearchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) | 94 | ObjectsList PostSearchJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData) | ||
108 | { | 95 | { | ||
109 | FeedData feedData; | 96 | FeedData feedData; | ||
110 | feedData.requestUrl = reply->request().url(); | 97 | feedData.requestUrl = reply->request().url(); | ||
111 | 98 | | |||
112 | ObjectsList items; | 99 | ObjectsList items; | ||
113 | QString itemId; | 100 | QString itemId; | ||
114 | 101 | | |||
115 | const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString(); | 102 | const QString contentType = reply->header(QNetworkRequest::ContentTypeHeader).toString(); | ||
116 | ContentType ct = Utils::stringToContentType(contentType); | 103 | ContentType ct = Utils::stringToContentType(contentType); | ||
117 | if (ct == KGAPI2::JSON) { | 104 | if (ct == KGAPI2::JSON) { | ||
118 | items = Post::fromJSONFeed(rawData, feedData); | 105 | items = Post::fromJSONFeed(rawData, feedData); | ||
119 | } else { | 106 | } else { | ||
120 | setError(KGAPI2::InvalidResponse); | 107 | setError(KGAPI2::InvalidResponse); | ||
121 | setErrorString(tr("Invalid response content type")); | 108 | setErrorString(tr("Invalid response content type")); | ||
122 | emitFinished(); | 109 | emitFinished(); | ||
123 | return items; | 110 | return items; | ||
124 | } | 111 | } | ||
125 | 112 | | |||
126 | if (feedData.nextPageUrl.isValid()) { | 113 | if (feedData.nextPageUrl.isValid()) { | ||
127 | const QNetworkRequest request = d->createRequest(feedData.nextPageUrl); | 114 | const QNetworkRequest request(feedData.nextPageUrl); | ||
128 | enqueueRequest(request); | 115 | enqueueRequest(request); | ||
129 | } else { | 116 | } else { | ||
130 | emitFinished(); | 117 | emitFinished(); | ||
131 | } | 118 | } | ||
132 | 119 | | |||
133 | return items; | 120 | return items; | ||
134 | } | 121 | } | ||
135 | 122 | |