Skip to content

Commit

Permalink
Allow entries to be dismissed
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 15, 2019
1 parent af365c6 commit da2e8fe
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
15 changes: 15 additions & 0 deletions python/core/auto_generated/qgsnewsfeedparser.sip.in
Expand Up @@ -59,6 +59,13 @@ configuration to use when connecting to the feed.
QList< QgsNewsFeedParser::Entry > entries() const;
%Docstring
Returns a list of existing entries in the feed.
%End

void dismissEntry( int key );
%Docstring
Dismisses an entry with matching ``key``.

This removes the entry from the local store, ensuring it will never be present again.
%End

public slots:
Expand All @@ -76,6 +83,14 @@ Fetches new entries from the feed's URL.
%Docstring
Emitted when ``entries`` have fetched from the feed.

.. seealso:: :py:func:`fetch`
%End

void entryAdded( const QgsNewsFeedParser::Entry &entry );
%Docstring
Emitted whenever a new entry is available from the feed (as a result
of a call to fetch()).

.. seealso:: :py:func:`fetch`
%End

Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsapplication.cpp
Expand Up @@ -54,6 +54,7 @@
#include "qgsstyle.h"
#include "qgsprojutils.h"
#include "qgsvaliditycheckregistry.h"
#include "qgsnewsfeedparser.h"

#include "gps/qgsgpsconnectionregistry.h"
#include "processing/qgsprocessingregistry.h"
Expand Down Expand Up @@ -219,6 +220,7 @@ void QgsApplication::init( QString profileFolder )
qRegisterMetaType<QgsGeometry>( "QgsGeometry" );
qRegisterMetaType<QgsDatumTransform::GridDetails>( "QgsDatumTransform::GridDetails" );
qRegisterMetaType<QgsDatumTransform::TransformDetails>( "QgsDatumTransform::TransformDetails" );
qRegisterMetaType<QgsNewsFeedParser::Entry>( "QgsNewsFeedParser::Entry" );

( void ) resolvePkgPath();

Expand Down
11 changes: 11 additions & 0 deletions src/core/qgsnewsfeedparser.cpp
Expand Up @@ -56,6 +56,16 @@ QList<QgsNewsFeedParser::Entry> QgsNewsFeedParser::entries() const
return mEntries;
}

void QgsNewsFeedParser::dismissEntry( int key )
{
mEntries.erase( std::remove_if( mEntries.begin(), mEntries.end(),
[key]( const Entry & entry )
{
return entry.key == key;
} ), mEntries.end() );
QgsSettings().remove( QStringLiteral( "%1/%2" ).arg( mSettingsKey ).arg( key ), QgsSettings::Core );
}

void QgsNewsFeedParser::fetch()
{
QNetworkRequest req( mFeedUrl );
Expand Down Expand Up @@ -109,6 +119,7 @@ void QgsNewsFeedParser::onFetch( const QString &content )
newEntries.append( newEntry );
mEntries.append( newEntry );
storeEntryInSettings( newEntry );
emit entryAdded( newEntry );
}

emit fetched( newEntries );
Expand Down
15 changes: 15 additions & 0 deletions src/core/qgsnewsfeedparser.h
Expand Up @@ -74,6 +74,13 @@ class CORE_EXPORT QgsNewsFeedParser : public QObject
*/
QList< QgsNewsFeedParser::Entry > entries() const;

/**
* Dismisses an entry with matching \a key.
*
* This removes the entry from the local store, ensuring it will never be present again.
*/
void dismissEntry( int key );

public slots:

/**
Expand All @@ -91,6 +98,14 @@ class CORE_EXPORT QgsNewsFeedParser : public QObject
*/
void fetched( const QList< QgsNewsFeedParser::Entry > &entries );

/**
* Emitted whenever a new entry is available from the feed (as a result
* of a call to fetch()).
*
* \see fetch()
*/
void entryAdded( const QgsNewsFeedParser::Entry &entry );

private slots:

void onFetch( const QString &content );
Expand Down
29 changes: 28 additions & 1 deletion tests/src/core/testqgsnewsfeedparser.cpp
Expand Up @@ -16,10 +16,11 @@

#include "qgstest.h"
#include <QObject>

#include <QSignalSpy>
#include "qgsnewsfeedparser.h"
#include "qgssettings.h"


class TestQgsNewsFeedParser: public QObject
{
Q_OBJECT
Expand Down Expand Up @@ -71,6 +72,7 @@ void TestQgsNewsFeedParser::testFetch()
uint beforeTime = QDateTime::currentDateTimeUtc().toTime_t();

QgsNewsFeedParser parser( url );
QSignalSpy spy( &parser, &QgsNewsFeedParser::entryAdded );
QVERIFY( parser.entries().isEmpty() );
QEventLoop loop;
connect( &parser, &QgsNewsFeedParser::fetched, this, [ =, &loop, &entries ]( const QList< QgsNewsFeedParser::Entry > &e )
Expand All @@ -82,6 +84,7 @@ void TestQgsNewsFeedParser::testFetch()
loop.exec();

// check result
QCOMPARE( spy.count(), 5 );
QCOMPARE( entries.count(), 5 );
QCOMPARE( entries.at( 0 ).title, QStringLiteral( "Next Microsoft Windows code name revealed" ) );
QCOMPARE( entries.at( 1 ).title, QStringLiteral( "QGIS core will be rewritten in Rust" ) );
Expand Down Expand Up @@ -143,6 +146,30 @@ void TestQgsNewsFeedParser::testFetch()
QCOMPARE( parser3.entries().at( 2 ).title, QStringLiteral( "Null Island QGIS Meeting" ) );
QCOMPARE( parser3.entries().at( 3 ).title, QStringLiteral( "QGIS Italian Meeting" ) );

// dismiss imaginary entry
parser3.dismissEntry( -1 );
QCOMPARE( parser3.entries().count(), 4 );

// dismiss valid entry
parser3.dismissEntry( 4 );
QCOMPARE( parser3.entries().count(), 3 );
QCOMPARE( parser3.entries().at( 0 ).title, QStringLiteral( "QGIS acquired by ESRI" ) );
QCOMPARE( parser3.entries().at( 1 ).title, QStringLiteral( "Null Island QGIS Meeting" ) );
QCOMPARE( parser3.entries().at( 2 ).title, QStringLiteral( "QGIS Italian Meeting" ) );

// craft a new parser, should not have dismissed entry
QgsNewsFeedParser parser4( url );
QCOMPARE( parser4.entries().count(), 3 );
QCOMPARE( parser4.entries().at( 0 ).title, QStringLiteral( "QGIS acquired by ESRI" ) );
QCOMPARE( parser4.entries().at( 1 ).title, QStringLiteral( "Null Island QGIS Meeting" ) );
QCOMPARE( parser4.entries().at( 2 ).title, QStringLiteral( "QGIS Italian Meeting" ) );
// even if we re-fetch, the dismissed entry should not come back
parser4.fetch();
QCOMPARE( parser4.entries().count(), 3 );
QCOMPARE( parser4.entries().at( 0 ).title, QStringLiteral( "QGIS acquired by ESRI" ) );
QCOMPARE( parser4.entries().at( 1 ).title, QStringLiteral( "Null Island QGIS Meeting" ) );
QCOMPARE( parser4.entries().at( 2 ).title, QStringLiteral( "QGIS Italian Meeting" ) );

}


Expand Down

0 comments on commit da2e8fe

Please sign in to comment.