Changeset View
Changeset View
Standalone View
Standalone View
src/widgets/welcomewidget.cpp
Show First 20 Lines • Show All 131 Lines • ▼ Show 20 Line(s) | 129 | connect(manager, &QNetworkAccessManager::finished, this, [&](QNetworkReply * reply) { | |||
---|---|---|---|---|---|
132 | } else { | 132 | } else { | ||
133 | QDomDocument document; | 133 | QDomDocument document; | ||
134 | if (document.setContent(reply->readAll())) { | 134 | if (document.setContent(reply->readAll())) { | ||
135 | parseRss(document); | 135 | parseRss(document); | ||
136 | } | 136 | } | ||
137 | } | 137 | } | ||
138 | }); | 138 | }); | ||
139 | } | 139 | } | ||
140 | // Since the calls above are async, I need to wait a little time | | |||
141 | // to have the responses processed before building the widget feed | | |||
142 | QTimer::singleShot(500, this, &WelcomeWidget::setupRssFeed); | | |||
143 | } | 140 | } | ||
144 | 141 | | |||
145 | void WelcomeWidget::parseRss(const QDomDocument &document) | 142 | void WelcomeWidget::parseRss(const QDomDocument &document) | ||
146 | { | 143 | { | ||
147 | auto itemList = document.elementsByTagName("item"); | 144 | auto itemList = document.elementsByTagName("item"); | ||
148 | QRegularExpression dateRegex("(?<date>\\d{2} \\w{3} \\d{4})"); | 145 | QRegularExpression dateRegex("(?<date>\\d{2} \\w{3} \\d{4})"); | ||
149 | 146 | | |||
150 | for (int i = 0; i < itemList.count(); ++i) { | 147 | for (int i = 0; i < itemList.count(); ++i) { | ||
151 | auto node = itemList.at(i); | 148 | auto node = itemList.at(i); | ||
152 | if (node.isElement()) { | 149 | if (node.isElement()) { | ||
153 | //Sample of date format Wed, 24 May 2017 13:46:07 +0000 | 150 | //Sample of date format Wed, 24 May 2017 13:46:07 +0000 | ||
154 | QString pDate = node.firstChildElement("pubDate").toElement().text(); | 151 | QString pDate = node.firstChildElement("pubDate").toElement().text(); | ||
155 | QRegularExpressionMatch match = dateRegex.match(pDate); | 152 | QRegularExpressionMatch match = dateRegex.match(pDate); | ||
156 | if (match.hasMatch()) { | 153 | if (match.hasMatch()) { | ||
157 | Post p; | 154 | Post p; | ||
158 | p.title = node.firstChildElement("title").toElement().text(); | 155 | p.title = node.firstChildElement("title").toElement().text(); | ||
159 | p.url = node.firstChildElement("link").toElement().text(); | 156 | p.url = node.firstChildElement("link").toElement().text(); | ||
160 | p.date = match.captured("date"); | 157 | p.date = match.captured("date"); | ||
161 | m_postList.append(p); | 158 | m_postList.append(p); | ||
162 | } | 159 | } | ||
163 | } | 160 | } | ||
164 | } | 161 | } | ||
162 | setupRssFeed(); | ||||
165 | } | 163 | } | ||
166 | 164 | | |||
167 | void WelcomeWidget::setupRssFeed() | 165 | void WelcomeWidget::setupRssFeed() | ||
168 | { | 166 | { | ||
169 | if (m_postList.empty()) { | 167 | if (m_postList.empty()) { | ||
170 | fallback(); | 168 | fallback(); | ||
171 | return; | 169 | return; | ||
172 | } | 170 | } | ||
Show All 37 Lines |