QGIS API Documentation 4.1.0-Master (376402f9aeb)
Loading...
Searching...
No Matches
qgsnewsfeedparser.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsnewsfeedparser.h
3 -------------------
4 begin : July 2019
5 copyright : (C) 2019 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15#ifndef QGSNEWSFEEDPARSER_H
16#define QGSNEWSFEEDPARSER_H
17
18#include "qgis_core.h"
19#include "qgis_sip.h"
20#include "qgssettingstree.h"
21
22#include <QDateTime>
23#include <QObject>
24#include <QPixmap>
25#include <QString>
26#include <QUrl>
27
28using namespace Qt::StringLiterals;
29
36
46class CORE_EXPORT QgsNewsFeedParser : public QObject
47{
48 Q_OBJECT
49
50 Q_PROPERTY( bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged );
51 Q_PROPERTY( bool isFetching READ isFetching NOTIFY isFetchingChanged );
52
53 public:
54#ifndef SIP_RUN
55 static inline QgsSettingsTreeNamedListNode *sTreeNewsFeed = QgsSettingsTree::sTreeApp->createNamedListNode( u"news-feed"_s );
61
62 static inline QgsSettingsTreeNamedListNode *sTreeNewsFeedEntries = sTreeNewsFeed->createNamedListNode( u"entries"_s );
70#endif
71
77 class Entry
78 {
79 public:
81 int key = 0;
82
84 QString title;
85
87 QString imageUrl;
88
90 QPixmap image;
91
93 QString content;
94
96 QUrl link;
97
99 bool sticky = false;
100
102 QDateTime published;
103
105 QDateTime expiry;
106 };
107
114 QgsNewsFeedParser( const QUrl &feedUrl, const QString &authcfg = QString(), QObject *parent SIP_TRANSFERTHIS = nullptr );
115
121 bool enabled() const SIP_SKIP { return mEnabled; }
122
128 void setEnabled( bool enabled ) SIP_SKIP;
129
135 bool isFetching() const { return mIsFetching; }
136
140 QList< QgsNewsFeedParser::Entry > entries() const;
141
149 Q_INVOKABLE void dismissEntry( int key );
150
155 Q_INVOKABLE void dismissAll();
156
160 QString authcfg() const;
161
165 static QString keyForFeed( const QString &baseUrl );
166
167 public slots:
168
173 void fetch();
174
175 signals:
176
182 void fetched( const QList< QgsNewsFeedParser::Entry > &entries );
183
191
201
209
214 void imageFetched( int key, const QPixmap &pixmap );
215
221
227
228 private slots:
229
230 void onFetch( const QString &content );
231
232 private:
233 QString mBaseUrl;
234 QUrl mFeedUrl;
235 QString mAuthCfg;
236 qint64 mFetchStartTime = 0;
237 QString mFeedKey;
238
239 QList< Entry > mEntries;
240 bool mBlockSignals = false;
241
242 bool mEnabled = true;
243 bool mIsFetching = false;
244
245 void readStoredEntries();
246 Entry readEntryFromSettings( int key );
247 void storeEntryInSettings( const Entry &entry );
248 void fetchImageForEntry( const Entry &entry );
249
251};
252
253#endif // QGSNEWSFEEDPARSER_H
HTTP network content fetcher.
Represents a single entry from a news feed.
QString content
HTML content of news entry.
QDateTime published
Entry publication date.
bool sticky
true if entry is "sticky" and should always be shown at the top
QUrl link
Optional URL link for entry.
QString imageUrl
Optional URL for image associated with entry.
QPixmap image
Optional image data.
QDateTime expiry
Optional auto-expiry time for entry.
int key
Unique entry identifier.
QString title
Entry title.
void entryDismissed(const QgsNewsFeedParser::Entry &entry)
Emitted whenever an entry is dismissed (as a result of a call to dismissEntry()).
static QgsSettingsTreeNamedListNode * sTreeNewsFeedEntries
void enabledChanged()
Emitted when the enabled/disabled state of the feed URL associated to the news parser changes.
static QgsSettingsTreeNamedListNode * sTreeNewsFeed
static const QgsSettingsEntryString * settingsFeedEntryTitle
static const QgsSettingsEntryBool * settingsFeedDisabled
static const QgsSettingsEntryString * settingsFeedEntryLink
static const QgsSettingsEntryVariant * settingsFeedEntryPublished
void fetched(const QList< QgsNewsFeedParser::Entry > &entries)
Emitted when entries have been fetched from the feed.
void isFetchingChanged()
Emitted when the news parser's fetching state changes.
friend class TestQgsNewsFeedParser
static const QgsSettingsEntryString * settingsFeedEntryImageUrl
static const QgsSettingsEntryDouble * settingsFeedLatitude
QString authcfg() const
Returns the authentication configuration for the parser.
static const QgsSettingsEntryInteger64 * settingsFeedLastFetchTime
QgsNewsFeedParser(const QUrl &feedUrl, const QString &authcfg=QString(), QObject *parent=nullptr)
Constructor for QgsNewsFeedParser, parsing the specified feedUrl.
static const QgsSettingsEntryBool * settingsFeedEntrySticky
static const QgsSettingsEntryDouble * settingsFeedLongitude
static const QgsSettingsEntryString * settingsFeedLanguage
bool enabled() const
Returns true if the feed URL associated with the news parser is enabled.
static const QgsSettingsEntryString * settingsFeedEntryContent
void entryUpdated(const QgsNewsFeedParser::Entry &entry)
Emitted whenever an existing entry is available from the feed (as a result of a call to fetch()).
static const QgsSettingsEntryVariant * settingsFeedEntryExpiry
void entryAdded(const QgsNewsFeedParser::Entry &entry)
Emitted whenever a new entry is available from the feed (as a result of a call to fetch()).
bool isFetching() const
Returns true if the news parser is fetching items.
void imageFetched(int key, const QPixmap &pixmap)
Emitted when the image attached to the entry with the specified key has been fetched and is now avail...
QList< QgsNewsFeedParser::Entry > entries() const
Returns a list of existing entries in the feed.
void setEnabled(bool enabled)
Sets whether the feed URL associated with the news parser is enabled.
A boolean settings entry.
A double settings entry.
A 64 bits integer (long long) settings entry.
A string settings entry.
A variant settings entry.
A named list tree node for the settings tree to help organizing and introspecting the tree.
static QgsSettingsTreeNode * sTreeApp
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_SKIP
Definition qgis_sip.h:133