Skip to content

Commit

Permalink
Merge pull request #687 from jdpurcell/updatechecker
Browse files Browse the repository at this point in the history
Update check fixes/improvements
  • Loading branch information
jurplel authored Jun 4, 2024
2 parents d1e0f01 + 4a70c69 commit 3b96bd3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 deletions.
4 changes: 2 additions & 2 deletions qView.pro
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ DEFINES += QT_DEPRECATED_WARNINGS
DEFINES += QT_NO_FOREACH

# To disables both manual and automatic checking for updates either uncomment line below or
# add config flag while building from the commad line.
CONFIG += qv_disable_online_version_check
# add config flag while building from the command line.
#CONFIG += qv_disable_online_version_check

qv_disable_online_version_check {
DEFINES += QV_DISABLE_ONLINE_VERSION_CHECK
Expand Down
2 changes: 1 addition & 1 deletion src/qvaboutdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ QVAboutDialog::QVAboutDialog(double givenLatestVersionNum, QWidget *parent) :
#ifndef QV_DISABLE_ONLINE_VERSION_CHECK
if (latestVersionNum < 0.0)
{
qvApp->checkUpdates();
qvApp->checkUpdates(false);
latestVersionNum = 0.0;
}
#endif //QV_DISABLE_ONLINE_VERSION_CHECK
Expand Down
6 changes: 3 additions & 3 deletions src/qvapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ QVApplication::QVApplication(int &argc, char **argv) : QApplication(argc, argv)
// Check for updates
// TODO: move this to after first window show event
if (getSettingsManager().getBoolean("updatenotifications")) {
checkUpdates();
checkUpdates(true);
}

// Setup macOS dock menu
Expand Down Expand Up @@ -196,10 +196,10 @@ MainWindow *QVApplication::getMainWindow(bool shouldBeEmpty)
return window;
}

void QVApplication::checkUpdates()
void QVApplication::checkUpdates(bool isStartupCheck)
{
#ifndef QV_DISABLE_ONLINE_VERSION_CHECK
updateChecker.check();
updateChecker.check(isStartupCheck);
#endif // QV_DISABLE_ONLINE_VERSION_CHECK
}

Expand Down
2 changes: 1 addition & 1 deletion src/qvapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class QVApplication : public QApplication

MainWindow *getMainWindow(bool shouldBeEmpty);

void checkUpdates();
void checkUpdates(bool isStartupCheck);

void checkedUpdates();

Expand Down
37 changes: 31 additions & 6 deletions src/updatechecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ UpdateChecker::UpdateChecker(QObject *parent) : QObject(parent)
connect(&netAccessManager, &QNetworkAccessManager::finished, this, &UpdateChecker::readReply);
}

void UpdateChecker::check()
void UpdateChecker::check(bool isStartupCheck)
{
sendRequest(UPDATE_URL);
if (isStartupCheck)
{
QDateTime lastCheckTime = getLastCheckTime();
if (lastCheckTime.isValid() && QDateTime::currentDateTimeUtc() < lastCheckTime.addSecs(STARTUP_CHECK_INTERVAL_HOURS * 3600))
return;
}

sendRequest(UPDATE_URL + "/latest");
}

void UpdateChecker::sendRequest(const QUrl &url)
Expand Down Expand Up @@ -49,20 +56,38 @@ void UpdateChecker::readReply(QNetworkReply *reply)
return;
}

QJsonObject object = json.array().first().toObject();
QJsonObject object = json.object();

latestVersionNum = object.value("tag_name").toString("0.0").toDouble();

QStringList changelogList = object.value("body").toString().split("\n");
static const QRegularExpression newLineRegEx("\r?\n");
QStringList changelogList = object.value("body").toString().split(newLineRegEx);
// remove "changelog" heading
changelogList.removeFirst();
changelog = changelogList.join("");
// remove additional newline if present
if (!changelogList.isEmpty() && changelogList.first().isEmpty())
changelogList.removeFirst();
changelog = changelogList.join("\n");

releaseDate = QDateTime::fromString(object.value("published_at").toString(), Qt::ISODate);
releaseDate = releaseDate.toTimeSpec(Qt::LocalTime);

setLastCheckTime(QDateTime::currentDateTimeUtc());

emit checkedUpdates();
}

QDateTime UpdateChecker::getLastCheckTime() const
{
qint64 secsSinceEpoch = QSettings().value("lastupdatecheck").toLongLong();
return secsSinceEpoch == 0 ? QDateTime() : QDateTime::fromSecsSinceEpoch(secsSinceEpoch, Qt::UTC);
}

void UpdateChecker::setLastCheckTime(QDateTime value)
{
QSettings().setValue("lastupdatecheck", value.toSecsSinceEpoch());
}

void UpdateChecker::openDialog()
{
QLocale locale;
Expand All @@ -71,7 +96,7 @@ void UpdateChecker::openDialog()
auto *msgBox = new QMessageBox();
msgBox->setWindowTitle(tr("qView Update Available"));
msgBox->setText(tr("qView %1 is available to download.").arg(QString::number(latestVersionNum, 'f', 1))
+ "\n\n" + releaseDate.toString(locale.dateFormat()) + "\n" + changelog);
+ "\n\n" + releaseDate.toString(locale.dateFormat()) + "\n\n" + changelog);
msgBox->setWindowModality(Qt::ApplicationModal);
msgBox->setStandardButtons(QMessageBox::Close | QMessageBox::Reset);
msgBox->button(QMessageBox::Reset)->setText(tr("&Disable Update Checking"));
Expand Down
8 changes: 6 additions & 2 deletions src/updatechecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class UpdateChecker : public QObject
public:
explicit UpdateChecker(QObject *parent = nullptr);

void check();
void check(bool isStartupCheck);

void openDialog();

Expand All @@ -23,11 +23,15 @@ class UpdateChecker : public QObject

void readReply(QNetworkReply *reply);

bool showSystemNotification();
QDateTime getLastCheckTime() const;

void setLastCheckTime(QDateTime value);

private:
const QString UPDATE_URL = "https://api.github.com/repos/jurplel/qview/releases";
const QString DOWNLOAD_URL = "https://interversehq.com/qview/download/";
// If update checking is enabled, this rate limits the auto-check that happens at startup
const int STARTUP_CHECK_INTERVAL_HOURS = 4;

double latestVersionNum;

Expand Down

0 comments on commit 3b96bd3

Please sign in to comment.