-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.cpp
490 lines (435 loc) · 15.9 KB
/
database.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
#include "database.h"
#include <QXmlStreamReader>
#include <QDebug>
Database::Database(QObject *parent) :
QObject(parent)
{
}
/*!
\brief
*/
int Database::getUnreadCountInLabel(QString label) {
bool flagLabel = false;
int sum = 0;
foreach (Feed* feed, _storage) {
QList<QString> labelList = feed->getLabel();
foreach (QString feedLabel, labelList) {
if (feedLabel == label) {
flagLabel = true;
sum += feed->getUnreadCount();
}
}
}
return (flagLabel) ? sum : -1;
}
void Database::removeFeed(QString feedId) {
for (int i = 0; i < _storage.count(); i++) {
if (_storage.at(i)->getId() == feedId) {
_storage.removeAt(i);
}
}
}
void Database::markFeedAsRead(QString feedId) {
foreach (Feed* feed, _storage) {
if (feed->getId() == feedId) {
foreach (Entry* entry, feed->getEntriesList()) {
if (!entry->isRead()) {
entry->setAsRead();
feed->decrementUnreadCount();
_numberOfNewEntries--;
}
}
break;
}
}
}
void Database::markEntryAsRead(QString entryId) {
foreach (Feed* feed, _storage) {
foreach (Entry* entry, feed->getEntriesList()) {
if (entry->getId() == entryId) {
entry->setAsRead();
feed->decrementUnreadCount();
_numberOfNewEntries--;
break;
}
}
}
}
void Database::addFeedLabel(QString feedId, QString labelName) {
foreach (Feed* feed, _storage) {
if (feed->getId() == feedId) {
feed->setLabel(labelName);
break;
}
}
}
void Database::removeFeedLabel(QString feedId, QString labelName) {
foreach (Feed* feed, _storage) {
if (feed->getId() == feedId) {
feed->removeLabel(labelName);
break;
}
}
}
/*!
\brief
*/
int Database::getUnreadCountInFeed(QString feedId) {
foreach (Feed* feed, _storage) {
if (feed->getId() == feedId) {
return feed->getUnreadCount();
}
}
return 0;
}
/*!
\brief
*/
QList<Feed*> Database::getFeedsInLabel(QString label) {
QList<Feed*> feedsList;
foreach (Feed* feed, _storage) {
QList<QString> tempLabelList = feed->getLabel();
foreach (QString tempLabel, tempLabelList) {
if (label == tempLabel) {
feedsList.append(feed);
}
}
}
return feedsList;
}
/*!
\brief
*/
void Database::setNumberOfNewEntries(QString feedId, int numberOfNewEntries) {
foreach (Feed* feed, _storage) {
if (feed->getTitle() == feedId) {
feed->setUnreadCount(numberOfNewEntries);
break;
}
}
}
QList<QString> Database::getLabelsList() {
QList<QString> labelList;
foreach (Feed* feed, _storage) {
QList<QString> templabelList = feed->getLabel();
foreach (QString label, templabelList) {
if (label != "" && !labelList.contains(label)) {
labelList.append(label);
}
}
}
return labelList;
}
QString Database::getIdForFeed(QString name) {
foreach (Feed* feed, _storage) {
if (name == feed->getTitle()) {
return feed->getId();
}
}
return NULL;
}
QString Database::getEntryLink(QString id) {
foreach (Feed* feed, _storage) {
foreach (Entry* entry, feed->getEntriesList()) {
if (entry->getId() == id) {
return entry->getLink();
}
}
}
return NULL;
}
Feed* Database::getFeed(QString id) {
foreach (Feed* feed, _storage)
if (feed->getId() == id)
return feed;
return NULL;
}
Entry* Database::getEntry(QString id) {
foreach (Feed* feed, _storage) {
foreach (Entry* entry, feed->getEntriesList()) {
if (entry->getId() == id) {
return entry;
}
}
}
return NULL;
}
QString Database::getFeedIdForEntry(QString id) {
foreach (Feed* feed, _storage) {
foreach (Entry* entry, feed->getEntriesList()) {
if (entry->getId() == id) {
return feed->getId();
}
}
}
return NULL;
}
void Database::addFeeds(QByteArray source) {
QXmlStreamReader sourceReader(source);
QString id = "";
QString title = "";
QList<QString> label;
QString firstitemmsec = "";
bool inList = false;
bool feedExists = false;
//dokud nejsem na konci
while (!sourceReader.atEnd()) {
//prectu dalsi pocatecni element
if (!sourceReader.readNextStartElement())
continue;
//pokud jsem v seznamu zdroju
if (inList) {
//hledam tagy string a number
if (sourceReader.name() != "string" && sourceReader.name() != "number")
continue;
//nactu si atributy prislusneho tagu
QXmlStreamAttributes attributes = sourceReader.attributes();
sourceReader.readNext();
//pokud jsem na polozce id zdroje, ulozim ji
if (attributes.value((QString("name"))) == "id")
if (sourceReader.text().toString().left(4) == "feed")
id = sourceReader.text().toString().mid(5);
//pokud jsem na polozce udavajici pocet neprectenych polozek
if (attributes.value((QString("name"))) == "title")
title = sourceReader.text().toString();
if (attributes.value((QString("name"))) == "label")
label.append(sourceReader.text().toString());
//pokud jsem na polozce udavajici cas nejnovejsi polozky
if (attributes.value((QString("name"))) == "firstitemmsec") {
firstitemmsec = sourceReader.text().toString();
//pokud mam vsechny potrebne informace o zdroji
if (!id.isEmpty() && !title.isEmpty() && !firstitemmsec.isEmpty()) {
//prochazim seznamem zdroju
foreach (Feed* feed, _storage) {
//pokud jsem stejny zdroj nasel
if (feed->getId() == id) {
feedExists = true;
break;
}
}
if (!feedExists) {
//vytvorim novy zdroj
Feed* newFeed = new Feed(id, title, label, firstitemmsec);
id.clear();
title.clear();
label.clear();
//a vlozim do seznamu
_storage.append(newFeed);
}
}
feedExists = false;
}
}
//pokud jsem v seznamu zdroju
if (sourceReader.name() == "list")
inList = true;
}
}
void Database::addUnreadFeeds(QByteArray source) {
QXmlStreamReader sourceReader(source);
QString id = "";
int unreadCount = 0;
QString newestItemTimestampUsec = "";
bool inFeeds = false;
bool inList = false;
bool notInUser = false;
_numberOfNewEntries = 0;
//dokud nejsem na konci
while (!sourceReader.atEnd()) {
//prectu dalsi pocatecni element
if (!sourceReader.readNextStartElement())
continue;
//pokud jsem v seznamu zdroju
if (inList) {
//hledam tagy string a number
if (sourceReader.name() != "string" && sourceReader.name() != "number")
continue;
//nactu si atributy prislusneho tagu
QXmlStreamAttributes attributes = sourceReader.attributes();
sourceReader.readNext();
//pokud jsem opravdu u adresy zdroje (a ne napr. labelu)
if (sourceReader.text().toString().left(4) == "feed")
inFeeds = true;
else
inFeeds = false;
//pokud jsem na polozce id zdroje, ulozim ji
if (attributes.value((QString("name"))) == "id" && inFeeds) {
id = sourceReader.text().toString().mid(5);
notInUser = true;
}
//pokud jsem na polozce udavajici pocet neprectenych polozek
if (attributes.value((QString("name"))) == "count" && notInUser)
unreadCount = sourceReader.text().toString().toInt();
//pokud jsem na polozce udavajici cas nejnovejsi polozky
if (attributes.value((QString("name"))) == "newestItemTimestampUsec") {
if (notInUser) {
newestItemTimestampUsec = sourceReader.text().toString();
//pokud mam vsechny potrebne informace o zdroji
if (!id.isEmpty() && !newestItemTimestampUsec.isEmpty()) {
//prochazim seznamem zdroju
foreach (Feed* feed, _storage) {
//pokud jsem stejny zdroj nasel
if (feed->getId() == id) {
//nastavim novy pocet neprectenych polozek
feed->setUnreadCount(unreadCount);
feed->setNewestItemTimestamp(newestItemTimestampUsec.toInt());
_numberOfNewEntries += unreadCount;
break;
}
}
}
}
notInUser = false;
inFeeds = false;
}
}
//pokud jsem v seznamu zdroju
if (sourceReader.name() == "list")
inList = true;
}
}
void Database::addEntries(QByteArray source) {
QXmlStreamReader sourceReader(source);
QXmlStreamAttributes attributes;
QString title = "";
QString published = "";
QString link = "";
QString feedId = "";
QString feedTitle = "";
QString entryId = "";
QString label = "";
QString author = "";
QString summary = "";
bool inEvent = false;
bool inSource = false;
bool feedExists = false;
bool entryExists = false;
bool markedAsRead = false;
Entry* newEntry;
//dokud nejsem na konci XML
while (!sourceReader.atEnd()) {
//prectu dalsi pocatecni element
if (!sourceReader.readNextStartElement())
continue;
//pokud jsem v casti tykajici se udalost
if (inEvent) {
//pokud jsem v casti popisujici zdroj
if (sourceReader.name() == "title" && inSource) {
//ziskam atributy tagu
attributes = sourceReader.attributes();
sourceReader.readNext();
//ulozim nazev zdroje
feedTitle = sourceReader.text().toString();
//prochazim seznam zdroju, zda je tento zdroj jiz vlozen
foreach (Feed* feed, _storage) {
if (feed->getId() == feedId) {
if (feed->getLabel().isEmpty())
feed->setLabel(label);
feedExists = true;
break;
}
}
//pokud zdroj neni v seznamu
if (!feedExists) {
//vytvorim jej
Feed *newFeed = new Feed(feedId, feedTitle);
newFeed->setLabel(label);
//a vlozim do seznamu
_storage.append(newFeed);
}
//prochazim seznam zdroju
foreach (Feed* feed, _storage) {
//a hledam zdroj do ktereho patri dana polozka
if (feed->getId() == feedId) {
foreach (Entry* entry, feed->getEntriesList()) {
//pokud jsem nasel stejnou polozku nebudu ji vytvaret znovu
if (entry->getId() == entryId) {
entryExists = true;
break;
}
}
if (!entryExists) {
//vytvorim novou polozku
newEntry = new Entry(title, published, link, entryId, author, summary, markedAsRead);
//vlozim polozku do zdroje
feed->entries.append(newEntry);
//pokud neni vyplnenen nazev zdroje vyplnim jej
if (feed->getTitle().isEmpty())
feed->setTitle(feedTitle);
}
}
}
inSource = false;
inEvent = false;
feedExists = false;
entryExists = false;
markedAsRead = false;
label.clear();
}
//pokud jsem na tagu, ktery obsahuje id polozky
if (sourceReader.name() == "id") {
attributes = sourceReader.attributes();
sourceReader.readNext();
entryId = sourceReader.text().toString();
}
//pokud jsem na tagu, ktery obsahuje kategorii
if (sourceReader.name() == "category") {
attributes = sourceReader.attributes();
sourceReader.readNext();
//vyhledam si zda je polozka oznacena jako prectena
//tuto hodnotu pak vkladam pri vytvareni polozky
if (attributes.value(QString("label")) == "read")
markedAsRead = true;
//pokud je zdroj prirazen do nejake slozky
if (attributes.value(QString("term")).toString().contains("label", Qt::CaseInsensitive))
label = attributes.value(QString("label")).toString();
}
//pokud jsem na tagu, ktery obsahuje nazev polozky
else if (sourceReader.name() == "title") {
attributes = sourceReader.attributes();
sourceReader.readNext();
title = sourceReader.text().toString();
}
//pokud jsem na tagu, ktery obsahuje datum a cas publikovani
else if (sourceReader.name() == "published") {
attributes = sourceReader.attributes();
sourceReader.readNext();
published = sourceReader.text().toString();
}
//pokud jsem na tagu, ktery obsahuje shrnuti
else if (sourceReader.name() == "summary") {
attributes = sourceReader.attributes();
sourceReader.readNext();
summary = sourceReader.text().toString();
}
//pokud jsem na tagu, ktery obsahuje odkaz na internetove stranky, kde
//je polozka publikovana
else if (sourceReader.name() == "link") {
attributes = sourceReader.attributes();
if (attributes.value(QString("rel")) == "alternate")
link = attributes.value(QString("href")).toString();
}
//pokud jsem na tagu, ktery obsahuje id zdroje
else if (sourceReader.name() == "source") {
inSource = true;
attributes = sourceReader.attributes();
sourceReader.readNext();
feedId = attributes.value(QString("gr:stream-id")).toString().mid(5);
}
else if (sourceReader.name() == "author") {
inSource = true;
attributes = sourceReader.attributes();
sourceReader.readNext();
if (attributes.value(QString("gr:unknown-author")) == "true")
author = trUtf8("Unknown author");
else {
sourceReader.readNext();
author = sourceReader.text().toString();
}
}
}
//pokud jsem v casti oznacujici polozku
if (sourceReader.name() == "entry")
inEvent = true;
}
}