Repository navigation
Add QfNewsParser to expose opt-in QField blog news items #8082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
aea51a0
Add QfNewsParser to expose opt-in QField blog news items
kaustuvpokharel 4a03fa3
Address review feedback
kaustuvpokharel b5a4743
Use QDomDocument to parse RSS feeds
kaustuvpokharel 52cb9d0
Only reject news items older than 45 days
kaustuvpokharel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,238 @@ | ||
| /*************************************************************************** | ||
| qfnewsparser.cpp | ||
| ---------------- | ||
| begin : October 2026 | ||
| copyright : (C) 2026 by Kaustuv Pokharel | ||
| email : kaustuv@opengis.ch | ||
| ***************************************************************************/ | ||
|
|
||
| /*************************************************************************** | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify * | ||
| * it under the terms of the GNU General Public License as published by * | ||
| * the Free Software Foundation; either version 2 of the License, or * | ||
| * (at your option) any later version. * | ||
| * * | ||
| ***************************************************************************/ | ||
|
|
||
| #include "qfnewsparser.h" | ||
| #include "qfstringutils.h" | ||
|
|
||
| #include <QCryptographicHash> | ||
| #include <QDir> | ||
| #include <QDomDocument> | ||
| #include <QFile> | ||
| #include <QFileInfo> | ||
| #include <QNetworkReply> | ||
| #include <QNetworkRequest> | ||
| #include <QSettings> | ||
| #include <QStandardPaths> | ||
| #include <qgsnetworkaccessmanager.h> | ||
|
|
||
| #include <algorithm> | ||
|
|
||
|
|
||
| QfNewsParser::QfNewsParser( QObject *parent ) | ||
| : QObject( parent ) | ||
| { | ||
| } | ||
|
|
||
| void QfNewsParser::setFeedUrl( const QUrl &url ) | ||
| { | ||
| if ( mFeedUrl == url ) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| mFeedUrl = url; | ||
| emit feedUrlChanged(); | ||
|
|
||
| clearItems(); | ||
|
|
||
| if ( enabled() && !mFeedUrl.isEmpty() ) | ||
| { | ||
| QFile cacheFile( cacheFilePath() ); | ||
| if ( cacheFile.exists() && cacheFile.open( QIODevice::ReadOnly ) ) | ||
| { | ||
| const QList<QfNewsItem> feedItems = parseFeed( cacheFile.readAll() ); | ||
| if ( !feedItems.isEmpty() ) | ||
| { | ||
| updateItems( feedItems ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bool QfNewsParser::enabled() const | ||
| { | ||
| return QSettings().value( ENABLED_SETTINGS_KEY, false ).toBool(); | ||
| } | ||
|
|
||
| void QfNewsParser::setEnabled( bool enabled ) | ||
| { | ||
| if ( this->enabled() == enabled ) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| QSettings settings; | ||
| settings.setValue( ENABLED_SETTINGS_KEY, enabled ); | ||
| settings.remove( DISMISSED_UUIDS_SETTINGS_KEY ); | ||
|
|
||
| emit enabledChanged(); | ||
|
|
||
| clearItems(); | ||
| } | ||
|
|
||
| void QfNewsParser::fetch() | ||
| { | ||
| if ( !enabled() || mFeedUrl.isEmpty() || mIsFetching ) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| mIsFetching = true; | ||
| emit isFetchingChanged(); | ||
|
|
||
| const QUrl url = mFeedUrl; | ||
| QNetworkReply *reply = QgsNetworkAccessManager::instance()->get( QNetworkRequest( url ) ); | ||
| connect( reply, &QNetworkReply::finished, this, [this, reply, url]() { | ||
| reply->deleteLater(); | ||
|
|
||
| if ( reply->error() == QNetworkReply::NoError && enabled() && mFeedUrl == url ) | ||
| { | ||
| const QByteArray data = reply->readAll(); | ||
| const QList<QfNewsItem> feedItems = parseFeed( data ); | ||
| if ( !feedItems.isEmpty() ) | ||
| { | ||
| QDir().mkpath( QFileInfo( cacheFilePath() ).absolutePath() ); | ||
| QFile cacheFile( cacheFilePath() ); | ||
| if ( cacheFile.open( QIODevice::WriteOnly ) ) | ||
| { | ||
| cacheFile.write( data ); | ||
| } | ||
|
|
||
| updateItems( feedItems ); | ||
| } | ||
| } | ||
|
|
||
| mIsFetching = false; | ||
| emit isFetchingChanged(); | ||
| } ); | ||
| } | ||
|
|
||
| void QfNewsParser::dismiss( const QString &uuid ) | ||
| { | ||
| QSettings settings; | ||
| QStringList dismissedUuids = settings.value( DISMISSED_UUIDS_SETTINGS_KEY ).toStringList(); | ||
| if ( !dismissedUuids.contains( uuid ) ) | ||
| { | ||
| dismissedUuids << uuid; | ||
| settings.setValue( DISMISSED_UUIDS_SETTINGS_KEY, dismissedUuids ); | ||
| } | ||
|
|
||
| const qsizetype removedCount = mItems.removeIf( [&uuid]( const QfNewsItem &item ) { return item.uuid == uuid; } ); | ||
| if ( removedCount > 0 ) | ||
| { | ||
| emit itemsChanged(); | ||
| } | ||
| } | ||
|
|
||
| QList<QfNewsItem> QfNewsParser::parseFeed( const QByteArray &data ) | ||
| { | ||
| QList<QfNewsItem> items; | ||
|
|
||
| QDomDocument document; | ||
| if ( !document.setContent( data, QDomDocument::ParseOption::UseNamespaceProcessing ) ) | ||
| { | ||
| return items; | ||
| } | ||
|
|
||
| const QDomNodeList itemNodes = document.elementsByTagName( QStringLiteral( "item" ) ); | ||
| for ( int i = 0; i < itemNodes.size(); ++i ) | ||
| { | ||
| const QDomElement itemElement = itemNodes.at( i ).toElement(); | ||
| if ( itemElement.parentNode().toElement().tagName() != QLatin1String( "channel" ) ) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| QfNewsItem item; | ||
| item.uuid = itemElement.firstChildElement( QStringLiteral( "guid" ) ).text().trimmed(); | ||
| item.title = itemElement.firstChildElement( QStringLiteral( "title" ) ).text().trimmed(); | ||
| item.url = QUrl( itemElement.firstChildElement( QStringLiteral( "link" ) ).text().trimmed() ); | ||
| item.publicationDate = QDateTime::fromString( itemElement.firstChildElement( QStringLiteral( "pubDate" ) ).text().trimmed(), Qt::RFC2822Date ); | ||
| item.description = QfStringUtils::htmlToPlainText( itemElement.firstChildElement( QStringLiteral( "description" ) ).text() ); | ||
|
|
||
| for ( QDomElement child = itemElement.firstChildElement(); !child.isNull(); child = child.nextSiblingElement() ) | ||
| { | ||
| if ( child.localName() == QLatin1String( "content" ) && child.hasAttribute( QStringLiteral( "url" ) ) ) | ||
| { | ||
| item.imageUrl = QUrl( child.attribute( QStringLiteral( "url" ) ) ); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| // The guid element is optional in RSS feeds, fall back to the item URL | ||
| if ( item.uuid.isEmpty() ) | ||
| { | ||
| item.uuid = item.url.toString(); | ||
| } | ||
|
|
||
| if ( !item.uuid.isEmpty() && !item.title.isEmpty() && item.publicationDate.isValid() ) | ||
| { | ||
| items << item; | ||
| } | ||
| } | ||
|
|
||
| return items; | ||
| } | ||
|
|
||
| QString QfNewsParser::cacheFilePath() const | ||
| { | ||
| const QString urlHash = QCryptographicHash::hash( mFeedUrl.toString().toUtf8(), QCryptographicHash::Sha256 ).toHex(); | ||
| return QStringLiteral( "%1/news/%2.xml" ).arg( QStandardPaths::writableLocation( QStandardPaths::AppConfigLocation ), urlHash ); | ||
| } | ||
|
|
||
| void QfNewsParser::clearItems() | ||
| { | ||
| if ( !mItems.isEmpty() ) | ||
| { | ||
| mItems.clear(); | ||
| emit itemsChanged(); | ||
| } | ||
| } | ||
|
|
||
| void QfNewsParser::updateItems( const QList<QfNewsItem> &feedItems ) | ||
| { | ||
| QSettings settings; | ||
| const QStringList dismissedUuids = settings.value( DISMISSED_UUIDS_SETTINGS_KEY ).toStringList(); | ||
|
|
||
| QList<QfNewsItem> items; | ||
| QStringList feedDismissedUuids; | ||
| for ( const QfNewsItem &item : feedItems ) | ||
| { | ||
| if ( dismissedUuids.contains( item.uuid ) ) | ||
| { | ||
| feedDismissedUuids << item.uuid; | ||
| } | ||
| else if ( item.publicationDate.daysTo( QDateTime::currentDateTime() ) <= MAXIMUM_AGE_DAYS ) | ||
| { | ||
| items << item; | ||
| } | ||
| } | ||
|
|
||
| // Forget dismissed items that are no longer part of the feed | ||
| if ( feedDismissedUuids.size() != dismissedUuids.size() ) | ||
| { | ||
| settings.setValue( DISMISSED_UUIDS_SETTINGS_KEY, feedDismissedUuids ); | ||
| } | ||
|
kaustuvpokharel marked this conversation as resolved.
|
||
|
|
||
| std::sort( items.begin(), items.end(), []( const QfNewsItem &a, const QfNewsItem &b ) { return a.publicationDate > b.publicationDate; } ); | ||
|
|
||
| if ( mItems != items ) | ||
| { | ||
| mItems = items; | ||
| emit itemsChanged(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.