-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleReader.cpp
639 lines (478 loc) · 18.9 KB
/
googleReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
#include "googleReader.h"
#include "functions.h"
#include <QDebug>
#include <QObject>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkCookie>
#include <QUrl>
/*!
\brief Vychozi konstruktor
*/
GoogleReader::GoogleReader() {
_googleCookie = new QNetworkCookie();
_timerRefreshSessionID = new QTimer(this);
_refreshingSID = false;
connect(_timerRefreshSessionID, SIGNAL(timeout()),
this, SLOT(refreshSID()));
}
/******************************************************************************
LOGIN TO GOOGLE READER
******************************************************************************/
/*!
\brief Login to Google Reader
*/
void GoogleReader::login(QString username, QString password) {
qDebug() << "GoogleReader::login(username, password)";
_username = username;
_password = password;
this->makeLogin();
}
/*!
\brief Login to Google Reader
*/
void GoogleReader::login() {
qDebug() << "GoogleReader::login()";
this->makeLogin();
}
void GoogleReader::refreshSID() {
qDebug() << "GoogleReader::refreshSID()";
_refreshingSID = true;
this->makeLogin();
}
/*!
\brief Makes actual login
*/
void GoogleReader::makeLogin() {
if (_username.isEmpty() || _password.isEmpty())
return;
qDebug() << "Login with username: " << _username;
QNetworkAccessManager* connectionLogin = new QNetworkAccessManager(this);
QString url = "https://www.google.com/accounts/ClientLogin";
QString postData = QString("service=reader&Email=%1&Passwd=%2&Source=GooDer").arg(_username, _password);
QNetworkRequest postRequest = QNetworkRequest(QUrl(url));
postRequest.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
connectionLogin->post(postRequest, postData.toAscii());
connect(connectionLogin, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseSessionID(QNetworkReply*)));
}
/*!
\brief V prijmute odpovedi hleda Session ID a vytvori cookie, ktera je potreba pro vykonavani pozadavku.
\brief Parse session ID from reply
*/
void GoogleReader::parseSessionID(QNetworkReply* reply) {
qDebug() << "GoogleReader::parseSessionID()";
reply->deleteLater();
// check if response contains errors
if (reply->error() != QNetworkReply::NoError) {
if (reply->error() == QNetworkReply::HostNotFoundError) {
qDebug() << "Host not found -> maybe offline?";
emit signalConnected(false);
}
else {
QString serverReply = reply->errorString();
if (serverReply.contains("Forbidden"))
qDebug() << "Bad username or password";
qDebug() << reply->readAll();
emit signalConnected(false);
}
return;
}
QString SIDReply = reply->readAll();
if (SIDReply.contains("Auth=")) {
int startOfSID = SIDReply.indexOf("Auth=")+5;
QString tempSID = SIDReply.mid(startOfSID);
_sessionID = tempSID.left(tempSID.length()-1);
qDebug() << "Session ID: " << _sessionID;
this->getToken();
}
}
/*!
\brief Cookie setup
*/
void GoogleReader::setCookie() {
qDebug() << "GoogleReader::setCookie()";
QString strTokenRequest = "GoogleLogin auth=";
strTokenRequest.append(_sessionID);
_googleCookie->setName("Authorization");
_googleCookie->setValue(strTokenRequest.toAscii());
}
/*!
\brief Get Google Token
*/
void GoogleReader::getToken() {
qDebug() << "GoogleReader::getToken()";
this->setCookie();
QNetworkRequest tokenRequest = QNetworkRequest(QUrl("http://www.google.com/reader/api/0/token"));
tokenRequest.setRawHeader("Authorization", _googleCookie->value());
QNetworkAccessManager* connectionToken = new QNetworkAccessManager(this);
connect(connectionToken, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseToken(QNetworkReply*)));
connectionToken->get(tokenRequest);
}
/*!
\brief Parse token from reply
*/
void GoogleReader::parseToken(QNetworkReply* reply) {
qDebug() << "GoogleReader::parseToken";
reply->deleteLater();
//zkontrolujeme odpoved na chyby
if (reply->error() != QNetworkReply::NoError ) {
qDebug() << reply->readAll();
emit signalStatusLogin(false);
emit signalConnected(false);
return;
}
//ulozime si token
_googleToken = reply->readAll();
qDebug() << "Token: " << _googleToken;
// Session ID expires in about 20 minutes
_timerRefreshSessionID->start(1820000);
if (!_refreshingSID) {
emit signalConnected(true);
emit signalStatusLogin(true);
}
_refreshingSID = false;
}
/******************************************************************************
GET FEEDS
******************************************************************************/
/*!
\brief Get list of subscribed feeds
*/
void GoogleReader::getFeeds() {
qDebug() << "GoogleReader::getFeeds()";
if (_googleCookie == NULL) {
emit signalConnected(false);
this->login();
}
QNetworkAccessManager* managerGetFeeds = new QNetworkAccessManager(this);
QString feedUrl = QString("http://www.google.com/reader/api/0/subscription/list?output=xml");
QNetworkRequest getRequest= QNetworkRequest(QUrl(feedUrl));
getRequest.setRawHeader("Authorization", _googleCookie->value());
connect(managerGetFeeds, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseFeeds(QNetworkReply*)));
managerGetFeeds->get(getRequest);
}
/*!
\brief Z odpovedi od serveru ziska seznam zdroju s neprectenymi polozkami
*/
void GoogleReader::parseFeeds(QNetworkReply* source) {
qDebug() << "GoogleReader::parseFeeds()";
source->deleteLater();
if (source->error() != QNetworkReply::NoError) {
qDebug() << source->readAll();
emit signalStatusGetFeeds(false);
return;
}
emit signalStatusGetFeeds(true);
emit shareRawFeeds(source->readAll());
}
/*!
\brief Ziska seznam zdroju, ve kterych jsou neprectene polozky
*/
void GoogleReader::getUnreadFeeds() {
qDebug() << "GoogleReader::getUnreadFeeds()";
if (_googleCookie == NULL) {
emit signalConnected(false);
this->login();
}
QNetworkAccessManager* managerGetUnreadFeeds = new QNetworkAccessManager(this);
QString unixTime = QString::number(getUnixTime());
QString feedUrl = QString("http://www.google.com/reader/api/0/unread-count?allcomments=true&output=xml&ck=%1&client=GooDer").arg(unixTime);
QNetworkRequest getRequest= QNetworkRequest(QUrl(feedUrl));
getRequest.setRawHeader("Authorization", _googleCookie->value());
managerGetUnreadFeeds->get(getRequest);
connect(managerGetUnreadFeeds, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseUnreadFeeds(QNetworkReply*)));
}
/*!
\brief Parse list with subscribed feeds (and with number of new entries in feeds)
*/
void GoogleReader::parseUnreadFeeds(QNetworkReply* reply) {
qDebug() << "GoogleReader::ParseUnreadFeeds()";
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) {
qDebug() << reply->readAll();
emit signalStatusGetUnreadFeeds(false);
return;
}
emit signalStatusGetUnreadFeeds(true);
emit shareRawUnreadFeeds(reply->readAll());
}
/******************************************************************************
GET ENTRIES
******************************************************************************/
/*!
\brief Get list of all entries
*/
void GoogleReader::getEntries() {
qDebug() << "GoogleReader::getEntries()";
if (_googleCookie == NULL) {
emit signalConnected(false);
this->login();
}
QString feedUrl;
int numberOfEntries = 1000;
QNetworkAccessManager* managerGetEntriesList = new QNetworkAccessManager(this);
feedUrl = QString("http://www.google.com/reader/atom/user/-/state/com.google/reading-list?n=%1").arg(numberOfEntries);
QNetworkRequest getRequest= QNetworkRequest(QUrl(feedUrl));
getRequest.setRawHeader("Authorization", _googleCookie->value());
connect(managerGetEntriesList, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseEntries(QNetworkReply*)));
managerGetEntriesList->get(getRequest);
}
/*!
\brief Get specified number of entries from specified feed
*/
void GoogleReader::getNumberOfEntriesFromFeed(QString feedId, int count) {
qDebug() << "GoogleReader::getAllEntriesFromFeed(" << feedId << "," << count << ")";
if (_googleCookie == NULL) {
emit signalConnected(false);
this->login();
}
QNetworkAccessManager* managerGetAllEntriesFromFeed = new QNetworkAccessManager(this);
QString feedUrl = QString("http://www.google.com/reader/atom/feed/%1?n=%2").arg(feedId, QString::number(count));
QNetworkRequest getRequest= QNetworkRequest(QUrl(feedUrl));
getRequest.setRawHeader("Authorization", _googleCookie->value());
connect(managerGetAllEntriesFromFeed, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseEntries(QNetworkReply*)));
managerGetAllEntriesFromFeed->get(getRequest);
}
/*!
\brief Parse entries
*/
void GoogleReader::parseEntries(QNetworkReply* reply) {
qDebug() << "GoogleReader::parseEntries()";
reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) {
qDebug() << reply->readAll();
emit signalStatusGetEntries(false);
}
emit shareRawEntries(reply->readAll());
emit signalStatusGetEntries(true);
}
/******************************************************************************
MANIPULATING WITH FEEDS AND ENTRIES
******************************************************************************/
/*!
\brief Mark feeds as read
*/
void GoogleReader::markFeedAsRead(QString feedId) {
qDebug() << "GoogleReader::markFeedAsRead(" << feedId << ")";
if (_googleCookie == NULL) {
emit signalConnected(false);
this->login();
}
QNetworkAccessManager* managerMarkAllAsRead = new QNetworkAccessManager(this);
QString url = "http://www.google.com/reader/api/0/mark-all-as-read";
QString unixTimeMS = QString::number((getUnixTime()));
QString postData = QString("s=feed/%1&ts=%2&T=%3").arg(QUrl::toPercentEncoding(feedId), unixTimeMS, _googleToken);
QNetworkRequest postRequest = QNetworkRequest(QUrl(url));
postRequest.setRawHeader("Authorization", _googleCookie->value());
postRequest.setRawHeader("Id", feedId.toAscii());
postRequest.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
connect(managerMarkAllAsRead, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseRespondeMarkFeedAsRead(QNetworkReply*)));
managerMarkAllAsRead->post(postRequest, postData.toAscii());
}
/*!
\brief Parses respond for marking feed as read
*/
void GoogleReader::parseRespondeMarkFeedAsRead(QNetworkReply* reply) {
qDebug() << "GoogleReader::parseRespondeMarkFeedAsRead()";
reply->deleteLater();
QString strReply = reply->readAll();
if (strReply != "OK") {
qDebug() << "Error during marking feed as read";
qDebug() << strReply;
refreshSID();
markFeedAsRead(reply->request().rawHeader("Id"));
return;
}
emit signalStatusMarkFeedAsRead(true);
}
/*!
\brief Marks entry as read
*/
void GoogleReader::markEntryAsRead(QString entryId) {
qDebug() << "GoogleReader::markEntryAsRead(" << entryId << ")";
if (_googleCookie == NULL) {
emit signalConnected(false);
this->login();
}
QNetworkAccessManager* managerMarkEntryAsRead = new QNetworkAccessManager(this);
QString url = "http://www.google.com/reader/api/0/edit-tag?client=GooDer";
QString postData = QString("i=%1&a=user/-/state/com.google/read&ac=edit&T=%2").arg(entryId, _googleToken);
QNetworkRequest postRequest = QNetworkRequest(QUrl(url));
postRequest.setRawHeader("Authorization", _googleCookie->value());
postRequest.setRawHeader("Id", entryId.toAscii());
postRequest.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
connect(managerMarkEntryAsRead, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseMarkEntryAsRead(QNetworkReply *)));
managerMarkEntryAsRead->post(postRequest, postData.toAscii());
}
/*!
\brief Parses responde for marking entry as read
*/
void GoogleReader::parseMarkEntryAsRead(QNetworkReply* reply) {
qDebug() << "GoogleReader::parseMarkEntryAsRead()";
reply->deleteLater();
QString strReply = reply->readAll();
if (strReply != "OK") {
qDebug() << "Error during marking entry as read";
qDebug() << strReply;
refreshSID();
markEntryAsRead(reply->request().rawHeader("Id"));
return;
}
emit signalStatusMarkEntryAsRead(true);
}
/*!
\brief Adds label for feed
*/
void GoogleReader::addFeedLabel(QString feedId, QString feedLabel) {
qDebug() << "GoogleReader::addFeedLabel(" << feedId << "," << feedLabel << ")";
if (_googleCookie == NULL) {
emit signalConnected(false);
this->login();
}
QNetworkAccessManager* managerAddLabel = new QNetworkAccessManager(this);
QString url = "http://www.google.com/reader/api/0/subscription/edit?client=GooDer";
QString postData = QString("s=feed/%1&ac=edit&a=user/-/label/%2&T=%3").arg(feedId, feedLabel, _googleToken);
QNetworkRequest postRequest = QNetworkRequest(QUrl(url));
postRequest.setRawHeader("Authorization", _googleCookie->value());
postRequest.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
connect(managerAddLabel, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseAddFeedLabel(QNetworkReply *)));
managerAddLabel->post(postRequest, postData.toAscii());
}
/*!
\brief Parses responde for adding feed label request
*/
void GoogleReader::parseAddFeedLabel(QNetworkReply* reply) {
qDebug() << "GoogleReader::parseAddFeedLabel()";
reply->deleteLater();
QString strReply = reply->readAll();
if (strReply != "OK") {
qDebug() << "Error during adding label to feed";
qDebug() << strReply;
emit signalStatusAddFeedLabel(false);
return;
}
emit signalStatusAddFeedLabel(true);
}
/*!
\brief Remove label from feed
*/
void GoogleReader::removeFeedLabel(QString feedId, QString feedLabel) {
qDebug() << "GoogleReader::removeFeedLabel(" << feedId << "," << feedLabel << ")";
if (_googleCookie == NULL) {
emit signalConnected(false);
this->login();
}
QNetworkAccessManager* managerRemoveFeedLabel = new QNetworkAccessManager(this);
QString url = "http://www.google.com/reader/api/0/subscription/edit?client=GooDer";
QString postData = QString("s=feed/%1&ac=edit&r=user/-/label/%2&T=%3").arg(feedId, feedLabel, _googleToken);
QNetworkRequest postRequest = QNetworkRequest(QUrl(url));
postRequest.setRawHeader("Authorization", _googleCookie->value());
postRequest.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
connect(managerRemoveFeedLabel, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseRemoveFeedLabel(QNetworkReply *)));
managerRemoveFeedLabel->post(postRequest, postData.toAscii());
}
/*!
\brief Parses responde for removing feed label request
*/
void GoogleReader::parseRemoveFeedLabel(QNetworkReply* reply) {
qDebug() << "GoogleReader::parseRemoveFeedLabel()";
reply->deleteLater();
QString strReply = reply->readAll();
if (strReply != "OK") {
qDebug() << "Error during removing label from feed";
qDebug() << strReply;
emit signalStatusRemoveFeedLabel(false);
return;
}
emit signalStatusRemoveFeedLabel(true);
}
/*!
\brief Subscribe new feed
*/
void GoogleReader::addFeed(QString feedAddress, QString feedName, QString feedLabel) {
qDebug() << "GoogleReader::addFeed(" << feedAddress << "," << feedName << "," << feedLabel << ")";
if (_googleCookie == NULL) {
emit signalConnected(false);
this->login();
}
if (feedAddress.left(7) != "http://")
feedAddress.insert(0, "http://");
QString url = "http://www.google.com/reader/api/0/subscription/edit?client=GooDer";
QString postData = "";
if (feedName.isEmpty() && feedLabel.isEmpty()) {
postData = QString("s=feed/%1&ac=subscribe&T=%2").arg(feedAddress, _googleToken);
} else if (!feedName.isEmpty() && feedLabel.isEmpty()) {
postData = QString("s=feed/%1&ac=subscribe&t=%2&T=%3").arg(feedAddress, feedName, _googleToken);
} else if (!feedName.isEmpty() && !feedLabel.isEmpty()) {
postData = QString("s=feed/%1&ac=subscribe&t=%2&a=user/-/label/%3&T=%4").arg(feedAddress, feedName, feedLabel, _googleToken);
} else if (feedName.isEmpty() && !feedLabel.isEmpty()) {
postData = QString("s=feed/%1&ac=subscribe&a=user/-/label/%2&T=%3").arg(feedAddress, feedLabel, _googleToken);
}
QNetworkAccessManager* feedConnection = new QNetworkAccessManager(this);
QNetworkRequest postRequest = QNetworkRequest(QUrl(url));
postRequest.setRawHeader("Authorization", _googleCookie->value());
postRequest.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
connect(feedConnection, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseAddFeed(QNetworkReply *)));
feedConnection->post(postRequest, postData.toAscii());
}
/*!
\brief Parses responde for subscribing new feed
*/
void GoogleReader::parseAddFeed(QNetworkReply* reply) {
qDebug() << "GoogleReader::parseAddFeed()";
reply->deleteLater();
QString strReply = reply->readAll();
if (strReply != "OK") {
qDebug() << "Error during add feed";
qDebug() << strReply;
emit signalStatusAddFeed(false);
return;
}
emit signalStatusAddFeed(true);
}
/*!
\brief Remove feed from subscribe list
*/
void GoogleReader::removeFeed(QString feedId) {
qDebug() << "GoogleReader::removeFeed(" << feedId << ")";
if (_googleCookie == NULL) {
emit signalConnected(false);
this->login();
}
QString url = "http://www.google.com/reader/api/0/subscription/edit?client=GooDer";
QString postData = QString("s=feed/%1&ac=unsubscribe&T=%2").arg(feedId, _googleToken);
QNetworkRequest postRequest = QNetworkRequest(QUrl(url));
postRequest.setRawHeader("Authorization", _googleCookie->value());
postRequest.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
QNetworkAccessManager* managerRemoveFeed = new QNetworkAccessManager(this);
connect(managerRemoveFeed, SIGNAL(finished(QNetworkReply*)),
this, SLOT(parseRemoveFeed(QNetworkReply *)));
managerRemoveFeed->post(postRequest, postData.toAscii());
}
/*!
\brief Parses responde for removing feed from subscribe list
*/
void GoogleReader::parseRemoveFeed(QNetworkReply* reply) {
qDebug() << "GoogleReader::parseRemoveFeed()";
reply->deleteLater();
QString strReply = reply->readAll();
if (strReply != "OK") {
qDebug() << "Error during removing feed";
qDebug() << strReply;
emit signalStatusRemoveFeed(false);
return;
}
emit signalStatusRemoveFeed(true);
}