Skip to content

Commit

Permalink
read news url from current.json file
Browse files Browse the repository at this point in the history
  • Loading branch information
illwieckz committed Jan 19, 2021
1 parent 6e6430f commit 5fdaf2f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion News.qml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Item {
}

Component.onCompleted: {
fetchNews('https://unvanquished.net/api/get_recent_posts/');
fetchNews(downloader.newsUrl);
}
}

Expand Down
10 changes: 7 additions & 3 deletions currentversionfetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,20 @@ void CurrentVersionFetcher::reply(QNetworkReply* reply)
QString updaterUrl;
QString productVersion;
QString productUrl;
QString newsVersion;
QString newsUrl;

if (reply->error() != QNetworkReply::NoError) {
qDebug() << "CurrentVersionFetcher: network error";
emit onCurrentVersions(updaterVersion, updaterUrl, productVersion, productUrl);
emit onCurrentVersions(updaterVersion, updaterUrl, productVersion, productUrl, newsUrl);
return;
}

QJsonParseError error;
QJsonDocument json = QJsonDocument::fromJson(reply->readAll(), &error);
if (error.error != QJsonParseError::NoError) {
qDebug() << "CurrentVersionFetcher: JSON parsing error";
emit onCurrentVersions(updaterVersion, updaterUrl, productVersion, productUrl);
emit onCurrentVersions(updaterVersion, updaterUrl, productVersion, productUrl, newsUrl);
return;
}

Expand All @@ -101,6 +103,8 @@ void CurrentVersionFetcher::reply(QNetworkReply* reply)

ComponentVersionFetcher(jsonObject, "product", "all-all", &productVersion, &productUrl);

emit onCurrentVersions(updaterVersion, updaterUrl, productVersion, productUrl);
ComponentVersionFetcher(jsonObject, "news", "all-all", &newsVersion, &newsUrl);

emit onCurrentVersions(updaterVersion, updaterUrl, productVersion, productUrl, newsUrl);
}

2 changes: 1 addition & 1 deletion currentversionfetcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CurrentVersionFetcher : public QObject
void fetchCurrentVersion(QString url);

signals:
void onCurrentVersions(QString updaterVersion, QString updaterUrl, QString productVersion, QString productUrl);
void onCurrentVersions(QString updaterVersion, QString updaterUrl, QString productVersion, QString productUrl, QString newsUrl);

private slots:
void reply(QNetworkReply* reply);
Expand Down
14 changes: 12 additions & 2 deletions qmldownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ QmlDownloader::~QmlDownloader()
stopAria();
}

QString QmlDownloader::newsUrl() const {
return latestNewsUrl_;
}

int QmlDownloader::downloadSpeed() const {
return downloadSpeed_;
}
Expand All @@ -44,6 +48,10 @@ int QmlDownloader::completedSize() const {
return completedSize_;
}

void QmlDownloader::setNewsUrl(QString newsUrl) {
latestNewsUrl_ = newsUrl;
}

void QmlDownloader::setDownloadSpeed(int speed) {
downloadSpeed_ = speed;
downloadTime_.addSpeed(speed);
Expand Down Expand Up @@ -198,17 +206,19 @@ void QmlDownloader::stopAria()
// Initiates an asynchronous request for the latest available versions.
void QmlDownloader::checkForUpdate()
{
connect(&fetcher_, SIGNAL(onCurrentVersions(QString, QString, QString, QString)), this, SLOT(onCurrentVersions(QString, QString, QString, QString)));
connect(&fetcher_, SIGNAL(onCurrentVersions(QString, QString, QString, QString, QString)), this, SLOT(onCurrentVersions(QString, QString, QString, QString, QString)));
fetcher_.fetchCurrentVersion("https://cdn.unvanquished.net/current.json");
}

// Receives the results of the checkForUpdate request.
void QmlDownloader::onCurrentVersions(QString updaterVersion, QString updaterUrl, QString productVersion, QString productUrl)
void QmlDownloader::onCurrentVersions(QString updaterVersion, QString updaterUrl, QString productVersion, QString productUrl, QString newsUrl)
{
latestUpdaterVersion_ = updaterVersion;
latestUpdaterUrl_ = updaterUrl;
latestProductVersion_ = productVersion;
latestProductUrl_ = productUrl;

setNewsUrl(newsUrl);
}

// This runs after the splash screen has been displayed for the programmed amount of time (and the
Expand Down
6 changes: 5 additions & 1 deletion qmldownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class QmlDownloader : public QObject
{
Q_OBJECT
Q_PROPERTY(QString newsUrl READ newsUrl)
Q_PROPERTY(int downloadSpeed READ downloadSpeed NOTIFY downloadSpeedChanged)
Q_PROPERTY(int uploadSpeed READ uploadSpeed NOTIFY uploadSpeedChanged)
Q_PROPERTY(int eta READ eta NOTIFY etaChanged)
Expand All @@ -37,6 +38,7 @@ class QmlDownloader : public QObject

QmlDownloader();
~QmlDownloader();
QString newsUrl() const;
int downloadSpeed() const;
int uploadSpeed() const;
int eta() const;
Expand All @@ -59,12 +61,13 @@ class QmlDownloader : public QObject
void stateChanged(DownloadState state);

public slots:
void setNewsUrl(QString newsUrl);
void setDownloadSpeed(int speed);
void setUploadSpeed(int speed);
void setTotalSize(int size);
void setCompletedSize(int size);
void onDownloadEvent(int event);
void onCurrentVersions(QString updaterVersion, QString updaterUrl, QString productVersion, QString productUrl);
void onCurrentVersions(QString updaterVersion, QString updaterUrl, QString productVersion, QString productUrl, QString newsUrl);

Q_INVOKABLE void startUpdate();
Q_INVOKABLE void toggleDownload();
Expand All @@ -91,6 +94,7 @@ public slots:
QString latestUpdaterUrl_;
QString latestProductVersion_;
QString latestProductUrl_;
QString latestNewsUrl_;
DownloadState state_;
std::unique_ptr<QTemporaryDir> temp_dir_;

Expand Down

0 comments on commit 5fdaf2f

Please sign in to comment.