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
|
#include "atomchannel.h"
#include <QSqlError>
#include <QSqlQuery>
#include <QXmlStreamReader>
#include "constants.h"
#include "macros.h"
#include "rsshit_db.h"
AtomChannel::AtomChannel(QXmlStreamReader *xmlReader)
{
Q_ASSERT(xmlReader != nullptr);
const QString titleTag{"title"};
const QString linkTag{"link"};
const QString descriptionTag{"description"};
const QString lastBuildDateTag{"lastBuildDate"};
const QString languageTag{"language"};
while (!xmlReader->atEnd() && !xmlReader->hasError()) {
const auto itemNext = xmlReader->readNext();
switch (itemNext) {
case QXmlStreamReader::TokenType::StartElement: {
const auto name = xmlReader->name();
// qDebug() << __func__ << ": StartElement" << name;
// qDebug() << __func__ << "namespaceUri:" << xmlReader->namespaceUri();
// qDebug() << __func__ << "prefix:" << xmlReader->prefix();
// qDebug() << __func__ << "qualifiedName:" << xmlReader->qualifiedName();
// const auto elementText = xmlReader->readElementText();
if (name == titleTag)
title = xmlReader->readElementText();
else if (name == linkTag)
link = xmlReader->readElementText();
else if (name == descriptionTag)
description = xmlReader->readElementText();
else if (name == lastBuildDateTag)
lastBuildDate = QDateTime::fromString(xmlReader->readElementText(),
Qt::DateFormat::RFC2822Date);
else if (name == languageTag)
language = QLocale{xmlReader->readElementText()}.language();
else if (name == AtomChannelImage::tag) {
// qDebug() << "got image tag";
image = AtomChannelImage{xmlReader};
// qDebug() << image;
} else if (name == AtomItem::tag) {
items << AtomItem{xmlReader};
// qDebug() << items.constLast();
// qDebug() << __func__ << "got feed item";
} else {
// qDebug() << "exit" << __func__;
// qDebug() << __func__ << "unknown tag:" << name;
continue;
}
break;
}
case QXmlStreamReader::TokenType::EndElement: {
// qDebug() << "EndElement: " << xmlReader->name();
if (xmlReader->name() == AtomChannel::xmlTag)
return;
}
case QXmlStreamReader::TokenType::Characters: {
const auto characters = xmlReader->text().toString().simplified();
if (characters.isEmpty())
break;
qDebug() << "channel: characters: " << characters;
break;
}
}
}
qDebug() << "exit " << __func__;
}
// TODO: move to a separate function, code is almost identical in all methods called `getDbId`
int AtomChannel::getDbId()
{
if (m_id != rsshit::db::IdNotFound)
return m_id;
const auto db = rsshit::db::open();
if (!db)
return rsshit::db::IdNotFound;
QSqlQuery selectQ{"select id from feeds where link=?"};
selectQ.addBindValue(link);
if (!selectQ.exec()) {
qWarning() << "cannot exec query" << selectQ.lastQuery() << ":"
<< selectQ.lastError().text();
return rsshit::db::IdNotFound;
}
if (!selectQ.next())
return rsshit::db::IdNotFound;
const auto idVariant = selectQ.value(rsshit::db::idTag);
if (!idVariant.isValid() || !idVariant.canConvert<int>())
return rsshit::db::IdNotFound;
bool ok{false};
const auto result = idVariant.toInt(&ok);
if (!ok) {
qWarning() << "got invalid id from db:" << idVariant;
return rsshit::db::IdNotFound;
}
return result;
}
// TODO: move to a separate function, code is almost identical in all methods called `createInDb`
int AtomChannel::createInDb()
{
if (m_id != rsshit::db::IdNotFound)
return m_id;
const auto db = rsshit::db::open();
if (!db)
return rsshit::db::IdNotFound;
QSqlQuery insertQ{"insert into feeds(link, title, image_url) values(?, ?, ?)"};
insertQ.addBindValue(link);
insertQ.addBindValue(title);
insertQ.addBindValue(image.url);
if (!insertQ.exec()) {
qWarning() << "cannot exec query" << insertQ.lastQuery() << ":"
<< insertQ.lastError().text();
return rsshit::db::IdNotFound;
}
m_id = insertQ.lastInsertId().toInt();
return m_id;
}
// TODO: can be moved to IDbObject
int AtomChannel::getOrInsertDbId()
{
const auto id = getDbId();
if (id != rsshit::db::IdNotFound)
return id;
return createInDb();
}
QList<int> AtomChannel::syncDbItems()
{
if (m_id == rsshit::db::IdNotFound)
m_id = getOrInsertDbId();
if (m_id == rsshit::db::IdNotFound)
return {};
QList<int> result;
for (auto &item : items) {
auto id = item.getOrInsertDbId(this->m_id);
if (id != rsshit::db::IdNotFound)
result << id;
}
return result;
}
QDebug operator<<(QDebug debug, const AtomChannel &channel)
{
QDebugStateSaver saver{debug};
debug.nospace() << typeid(AtomChannel).name() << " {" << Qt::endl;
PRINT_ATOM_FIELD(channel, title);
PRINT_ATOM_FIELD(channel, link);
PRINT_ATOM_FIELD(channel, description);
PRINT_ATOM_FIELD(channel, lastBuildDate);
PRINT_ATOM_FIELD(channel, language);
debug << "\timage:\n" << channel.image << Qt::endl;
debug << "\titems count:" << channel.items.size() << Qt::endl;
for (const auto &item : channel.items)
debug << item << Qt::endl;
debug.nospace() << "}";
return debug;
}
|