Expose network errors to qml

This commit is contained in:
Tobias Fella 2020-07-02 21:57:09 +02:00
parent 237a6d67ed
commit d59a1ef1ec
4 changed files with 51 additions and 6 deletions

View File

@ -68,6 +68,14 @@ Feed::Feed(int index)
setRefreshing(false);
Q_EMIT entryCountChanged();
Q_EMIT unreadEntryCountChanged();
setErrorId(0);
setErrorString(QLatin1String(""));
}
});
connect(&Fetcher::instance(), &Fetcher::error, this, [this](QString url, int errorId, QString errorString) {
if(url == m_url) {
setErrorId(errorId);
setErrorString(errorString);
}
});
}
@ -168,6 +176,16 @@ bool Feed::refreshing() const
return m_refreshing;
}
int Feed::errorId() const
{
return m_errorId;
}
QString Feed::errorString() const
{
return m_errorString;
}
void Feed::setName(QString name)
{
m_name = name;
@ -240,6 +258,18 @@ void Feed::setRefreshing(bool refreshing)
Q_EMIT refreshingChanged(m_refreshing);
}
void Feed::setErrorId(int errorId)
{
m_errorId = errorId;
Q_EMIT errorIdChanged(m_errorId);
}
void Feed::setErrorString(QString errorString)
{
m_errorString = errorString;
Q_EMIT errorStringChanged(m_errorString);
}
void Feed::refresh()
{
Fetcher::instance().fetch(m_url);

View File

@ -46,6 +46,8 @@ class Feed : public QObject
Q_PROPERTY(bool notify READ notify WRITE setNotify NOTIFY notifyChanged)
Q_PROPERTY(int entryCount READ entryCount NOTIFY entryCountChanged)
Q_PROPERTY(int unreadEntryCount READ unreadEntryCount NOTIFY unreadEntryCountChanged)
Q_PROPERTY(int errorId READ errorId WRITE setErrorId NOTIFY errorIdChanged)
Q_PROPERTY(QString errorString READ errorString WRITE setErrorString NOTIFY errorStringChanged)
public:
Feed(int index);
@ -68,6 +70,8 @@ public:
int entryCount() const;
int unreadEntryCount() const;
bool read() const;
int errorId() const;
QString errorString() const;
bool refreshing() const;
@ -83,6 +87,8 @@ public:
void setAutoUpdateType(int type);
void setNotify(bool notify);
void setRefreshing(bool refreshing);
void setErrorId(int errorId);
void setErrorString(QString errorString);
Q_INVOKABLE void refresh();
void remove();
@ -101,6 +107,8 @@ Q_SIGNALS:
void notifyChanged(bool notify);
void entryCountChanged();
void unreadEntryCountChanged();
void errorIdChanged(int &errorId);
void errorStringChanged(QString &errorString);
void refreshingChanged(bool refreshing);
@ -118,6 +126,8 @@ private:
int m_autoUpdateCount;
int m_autoUpdateType;
bool m_notify;
int m_errorId;
QString m_errorString;
bool m_refreshing = false;
};

View File

@ -48,12 +48,16 @@ void Fetcher::fetch(QString url)
QNetworkRequest request((QUrl(url)));
QNetworkReply *reply = get(request);
connect(reply, &QNetworkReply::finished, this, [this, url, reply]() {
QByteArray data = reply->readAll();
Syndication::DocumentSource *document = new Syndication::DocumentSource(data, url);
Syndication::FeedPtr feed = Syndication::parserCollection()->parse(*document, QStringLiteral("Atom"));
processFeed(feed, url);
if(reply->error()) {
qWarning() << "Error fetching feed";
qWarning() << reply->errorString();
Q_EMIT error(url, reply->error(), reply->errorString());
} else {
QByteArray data = reply->readAll();
Syndication::DocumentSource *document = new Syndication::DocumentSource(data, url);
Syndication::FeedPtr feed = Syndication::parserCollection()->parse(*document, QStringLiteral("Atom"));
processFeed(feed, url);
}
delete reply;
});
}

View File

@ -56,4 +56,5 @@ Q_SIGNALS:
void startedFetchingFeed(QString url);
void feedUpdated(QString url);
void feedDetailsUpdated(QString url, QString name, QString image, QString link, QString description, QDateTime lastUpdated);
void error(QString url, int errorId, QString errorString);
};