Improve coding style

This commit is contained in:
Vladimir Golovnev (Glassez) 2020-11-16 10:02:11 +03:00
parent acad35c5bc
commit c41df9ffbd
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
147 changed files with 4454 additions and 2227 deletions

View file

@ -53,7 +53,8 @@ namespace
// http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent
// http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent
// http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent
static const QHash<QString, QString> HTMLEntities {
static const QHash<QString, QString> HTMLEntities
{
{"nbsp", "&#160;"}, // no-break space = non-breaking space, U+00A0 ISOnum
{"iexcl", "&#161;"}, // inverted exclamation mark, U+00A1 ISOnum
{"cent", "&#162;"}, // cent sign, U+00A2 ISOnum
@ -359,17 +360,20 @@ namespace
// Ported to Qt from KDElibs4
QDateTime parseDate(const QString &string)
{
const char shortDay[][4] = {
const char shortDay[][4] =
{
"Mon", "Tue", "Wed",
"Thu", "Fri", "Sat",
"Sun"
};
const char longDay[][10] = {
const char longDay[][10] =
{
"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday",
"Sunday"
};
const char shortMonth[][4] = {
const char shortMonth[][4] =
{
"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"
@ -389,7 +393,8 @@ namespace
// Also accept obsolete form "Weekday, DD-Mon-YY HH:MM:SS ±hhmm"
QRegExp rx("^(?:([A-Z][a-z]+),\\s*)?(\\d{1,2})(\\s+|-)([^-\\s]+)(\\s+|-)(\\d{2,4})\\s+(\\d\\d):(\\d\\d)(?::(\\d\\d))?\\s+(\\S+)$");
QStringList parts;
if (!str.indexOf(rx)) {
if (!str.indexOf(rx))
{
// Check that if date has '-' separators, both separators are '-'.
parts = rx.capturedTexts();
const bool h1 = (parts[3] == QLatin1String("-"));
@ -397,7 +402,8 @@ namespace
if (h1 != h2)
return QDateTime::currentDateTime();
}
else {
else
{
// Check for the obsolete form "Wdy Mon DD HH:MM:SS YYYY"
rx = QRegExp("^([A-Z][a-z]+)\\s+(\\S+)\\s+(\\d\\d)\\s+(\\d\\d):(\\d\\d):(\\d\\d)\\s+(\\d\\d\\d\\d)$");
if (str.indexOf(rx))
@ -421,7 +427,8 @@ namespace
return QDateTime::currentDateTime();
int second = 0;
if (!parts[nsec].isEmpty()) {
if (!parts[nsec].isEmpty())
{
second = parts[nsec].toInt(&ok[0]);
if (!ok[0])
return QDateTime::currentDateTime();
@ -433,7 +440,8 @@ namespace
int month = 0;
for ( ; (month < 12) && (parts[nmonth] != shortMonth[month]); ++month);
int dayOfWeek = -1;
if (!parts[nwday].isEmpty()) {
if (!parts[nwday].isEmpty())
{
// Look up the weekday name
while ((++dayOfWeek < 7) && (shortDay[dayOfWeek] != parts[nwday]));
if (dayOfWeek >= 7)
@ -444,7 +452,8 @@ namespace
// || (dayOfWeek < 0 && format == RFCDateDay))
// return QDateTime;
const int i = parts[nyear].size();
if (i < 4) {
if (i < 4)
{
// It's an obsolete year specification with less than 4 digits
year += ((i == 2) && (year < 50)) ? 2000 : 1900;
}
@ -452,9 +461,11 @@ namespace
// Parse the UTC offset part
int offset = 0; // set default to '-0000'
bool negOffset = false;
if (parts.count() > 10) {
if (parts.count() > 10)
{
rx = QRegExp("^([+-])(\\d\\d)(\\d\\d)$");
if (!parts[10].indexOf(rx)) {
if (!parts[10].indexOf(rx))
{
// It's a UTC offset ±hhmm
parts = rx.capturedTexts();
offset = parts[2].toInt(&ok[0]) * 3600;
@ -466,13 +477,16 @@ namespace
if (negOffset)
offset = -offset;
}
else {
else
{
// Check for an obsolete time zone name
const QByteArray zone = parts[10].toLatin1();
if ((zone.length() == 1) && (isalpha(zone[0])) && (toupper(zone[0]) != 'J')) {
if ((zone.length() == 1) && (isalpha(zone[0])) && (toupper(zone[0]) != 'J'))
{
negOffset = true; // military zone: RFC 2822 treats as '-0000'
}
else if ((zone != "UT") && (zone != "GMT")) { // treated as '+0000'
else if ((zone != "UT") && (zone != "GMT"))
{ // treated as '+0000'
offset = (zone == "EDT")
? -4 * 3600
: ((zone == "EST") || (zone == "CDT"))
@ -484,7 +498,8 @@ namespace
: (zone == "PST")
? -8 * 3600
: 0;
if (!offset) {
if (!offset)
{
// Check for any other alphabetic time zone
bool nonalpha = false;
for (int i = 0, end = zone.size(); (i < end) && !nonalpha; ++i)
@ -509,7 +524,8 @@ namespace
if (!result.isValid())
return QDateTime::currentDateTime(); // invalid date/time
if (leapSecond) {
if (leapSecond)
{
// Validate a leap second time. Leap seconds are inserted after 23:59:59 UTC.
// Convert the time to UTC and check that it is 00:00:00.
if ((hour*3600 + minute*60 + 60 - offset + 86400*5) % 86400) // (max abs(offset) is 100 hours)
@ -548,11 +564,15 @@ void Parser::parse_impl(const QByteArray &feedData)
xml.setEntityResolver(&resolver);
bool foundChannel = false;
while (xml.readNextStartElement()) {
if (xml.name() == "rss") {
while (xml.readNextStartElement())
{
if (xml.name() == "rss")
{
// Find channels
while (xml.readNextStartElement()) {
if (xml.name() == "channel") {
while (xml.readNextStartElement())
{
if (xml.name() == "channel")
{
parseRSSChannel(xml);
foundChannel = true;
break;
@ -563,7 +583,8 @@ void Parser::parse_impl(const QByteArray &feedData)
}
break;
}
if (xml.name() == "feed") { // Atom feed
if (xml.name() == "feed")
{ // Atom feed
parseAtomChannel(xml);
foundChannel = true;
break;
@ -573,10 +594,12 @@ void Parser::parse_impl(const QByteArray &feedData)
xml.skipCurrentElement();
}
if (!foundChannel) {
if (!foundChannel)
{
m_result.error = tr("Invalid RSS feed.");
}
else if (xml.hasError()) {
else if (xml.hasError())
{
m_result.error = tr("%1 (line: %2, column: %3, offset: %4).")
.arg(xml.errorString()).arg(xml.lineNumber())
.arg(xml.columnNumber()).arg(xml.characterOffset());
@ -592,43 +615,53 @@ void Parser::parseRssArticle(QXmlStreamReader &xml)
QVariantHash article;
QString altTorrentUrl;
while (!xml.atEnd()) {
while (!xml.atEnd())
{
xml.readNext();
const QString name(xml.name().toString());
if (xml.isEndElement() && (name == QLatin1String("item")))
break;
if (xml.isStartElement()) {
if (name == QLatin1String("title")) {
if (xml.isStartElement())
{
if (name == QLatin1String("title"))
{
article[Article::KeyTitle] = xml.readElementText().trimmed();
}
else if (name == QLatin1String("enclosure")) {
else if (name == QLatin1String("enclosure"))
{
if (xml.attributes().value("type") == QLatin1String("application/x-bittorrent"))
article[Article::KeyTorrentURL] = xml.attributes().value(QLatin1String("url")).toString();
else if (xml.attributes().value("type").isEmpty())
altTorrentUrl = xml.attributes().value(QLatin1String("url")).toString();
}
else if (name == QLatin1String("link")) {
else if (name == QLatin1String("link"))
{
const QString text {xml.readElementText().trimmed()};
if (text.startsWith(QLatin1String("magnet:"), Qt::CaseInsensitive))
article[Article::KeyTorrentURL] = text; // magnet link instead of a news URL
else
article[Article::KeyLink] = text;
}
else if (name == QLatin1String("description")) {
else if (name == QLatin1String("description"))
{
article[Article::KeyDescription] = xml.readElementText(QXmlStreamReader::IncludeChildElements);
}
else if (name == QLatin1String("pubDate")) {
else if (name == QLatin1String("pubDate"))
{
article[Article::KeyDate] = parseDate(xml.readElementText().trimmed());
}
else if (name == QLatin1String("author")) {
else if (name == QLatin1String("author"))
{
article[Article::KeyAuthor] = xml.readElementText().trimmed();
}
else if (name == QLatin1String("guid")) {
else if (name == QLatin1String("guid"))
{
article[Article::KeyId] = xml.readElementText().trimmed();
}
else {
else
{
article[name] = xml.readElementText(QXmlStreamReader::IncludeChildElements);
}
}
@ -642,24 +675,31 @@ void Parser::parseRssArticle(QXmlStreamReader &xml)
void Parser::parseRSSChannel(QXmlStreamReader &xml)
{
while (!xml.atEnd()) {
while (!xml.atEnd())
{
xml.readNext();
if (xml.isStartElement()) {
if (xml.name() == QLatin1String("title")) {
if (xml.isStartElement())
{
if (xml.name() == QLatin1String("title"))
{
m_result.title = xml.readElementText();
}
else if (xml.name() == QLatin1String("lastBuildDate")) {
else if (xml.name() == QLatin1String("lastBuildDate"))
{
const QString lastBuildDate = xml.readElementText();
if (!lastBuildDate.isEmpty()) {
if (m_result.lastBuildDate == lastBuildDate) {
if (!lastBuildDate.isEmpty())
{
if (m_result.lastBuildDate == lastBuildDate)
{
qDebug() << "The RSS feed has not changed since last time, aborting parsing.";
return;
}
m_result.lastBuildDate = lastBuildDate;
}
}
else if (xml.name() == QLatin1String("item")) {
else if (xml.name() == QLatin1String("item"))
{
parseRssArticle(xml);
}
}
@ -671,18 +711,22 @@ void Parser::parseAtomArticle(QXmlStreamReader &xml)
QVariantHash article;
bool doubleContent = false;
while (!xml.atEnd()) {
while (!xml.atEnd())
{
xml.readNext();
const QString name(xml.name().toString());
if (xml.isEndElement() && (name == QLatin1String("entry")))
break;
if (xml.isStartElement()) {
if (name == QLatin1String("title")) {
if (xml.isStartElement())
{
if (name == QLatin1String("title"))
{
article[Article::KeyTitle] = xml.readElementText().trimmed();
}
else if (name == QLatin1String("link")) {
else if (name == QLatin1String("link"))
{
const QString link = (xml.attributes().isEmpty()
? xml.readElementText().trimmed()
: xml.attributes().value(QLatin1String("href")).toString());
@ -696,8 +740,10 @@ void Parser::parseAtomArticle(QXmlStreamReader &xml)
article[Article::KeyLink] = (m_baseUrl.isEmpty() ? link : m_baseUrl + link);
}
else if ((name == QLatin1String("summary")) || (name == QLatin1String("content"))) {
if (doubleContent) { // Duplicate content -> ignore
else if ((name == QLatin1String("summary")) || (name == QLatin1String("content")))
{
if (doubleContent)
{ // Duplicate content -> ignore
xml.skipCurrentElement();
continue;
}
@ -705,28 +751,34 @@ void Parser::parseAtomArticle(QXmlStreamReader &xml)
// Try to also parse broken articles, which don't use html '&' escapes
// Actually works great for non-broken content too
const QString feedText = xml.readElementText(QXmlStreamReader::IncludeChildElements).trimmed();
if (!feedText.isEmpty()) {
if (!feedText.isEmpty())
{
article[Article::KeyDescription] = feedText;
doubleContent = true;
}
}
else if (name == QLatin1String("updated")) {
else if (name == QLatin1String("updated"))
{
// ATOM uses standard compliant date, don't do fancy stuff
const QDateTime articleDate = QDateTime::fromString(xml.readElementText().trimmed(), Qt::ISODate);
article[Article::KeyDate] = (articleDate.isValid() ? articleDate : QDateTime::currentDateTime());
}
else if (name == QLatin1String("author")) {
while (xml.readNextStartElement()) {
else if (name == QLatin1String("author"))
{
while (xml.readNextStartElement())
{
if (xml.name() == QLatin1String("name"))
article[Article::KeyAuthor] = xml.readElementText().trimmed();
else
xml.skipCurrentElement();
}
}
else if (name == QLatin1String("id")) {
else if (name == QLatin1String("id"))
{
article[Article::KeyId] = xml.readElementText().trimmed();
}
else {
else
{
article[name] = xml.readElementText(QXmlStreamReader::IncludeChildElements);
}
}
@ -739,24 +791,31 @@ void Parser::parseAtomChannel(QXmlStreamReader &xml)
{
m_baseUrl = xml.attributes().value("xml:base").toString();
while (!xml.atEnd()) {
while (!xml.atEnd())
{
xml.readNext();
if (xml.isStartElement()) {
if (xml.name() == QLatin1String("title")) {
if (xml.isStartElement())
{
if (xml.name() == QLatin1String("title"))
{
m_result.title = xml.readElementText();
}
else if (xml.name() == QLatin1String("updated")) {
else if (xml.name() == QLatin1String("updated"))
{
const QString lastBuildDate = xml.readElementText();
if (!lastBuildDate.isEmpty()) {
if (m_result.lastBuildDate == lastBuildDate) {
if (!lastBuildDate.isEmpty())
{
if (m_result.lastBuildDate == lastBuildDate)
{
qDebug() << "The RSS feed has not changed since last time, aborting parsing.";
return;
}
m_result.lastBuildDate = lastBuildDate;
}
}
else if (xml.name() == QLatin1String("entry")) {
else if (xml.name() == QLatin1String("entry"))
{
parseAtomArticle(xml);
}
}
@ -776,14 +835,16 @@ void Parser::addArticle(QVariantHash article)
if (localId.toString().isEmpty())
localId = article.value(Article::KeyTitle);
if (localId.toString().isEmpty()) {
if (localId.toString().isEmpty())
{
// The article could not be uniquely identified
// since it has no appropriate data.
// Just ignore it.
return;
}
if (m_articleIDs.contains(localId.toString())) {
if (m_articleIDs.contains(localId.toString()))
{
// The article could not be uniquely identified
// since the Feed has duplicate identifiers.
// Just ignore it.