add update check

pull/14094/head
Steven Powell 2022-05-03 16:24:51 -07:00
parent 6703066c1b
commit 352acecb40
4 changed files with 77 additions and 5 deletions

5
gui/releases.json Normal file
View File

@ -0,0 +1,5 @@
[
{
"name":"0.0.0"
}
]

View File

@ -6,7 +6,7 @@ SOURCES = main.cpp \
RESOURCES = systray.qrc RESOURCES = systray.qrc
ICON = images/minikube.icns ICON = images/minikube.icns
QT += widgets QT += widgets network
requires(qtConfig(combobox)) requires(qtConfig(combobox))
DISTFILES += \ DISTFILES += \

View File

@ -83,6 +83,7 @@
#include <QFontDialog> #include <QFontDialog>
#include <QStackedWidget> #include <QStackedWidget>
#include <QProcessEnvironment> #include <QProcessEnvironment>
#include <QNetworkAccessManager>
#ifndef QT_NO_TERMWIDGET #ifndef QT_NO_TERMWIDGET
#include <QApplication> #include <QApplication>
@ -90,6 +91,8 @@
#include "qtermwidget.h" #include "qtermwidget.h"
#endif #endif
const QVersionNumber version = QVersionNumber::fromString("0.0.1");
Window::Window() Window::Window()
{ {
trayIconIcon = new QIcon(":/images/minikube.png"); trayIconIcon = new QIcon(":/images/minikube.png");
@ -115,6 +118,7 @@ Window::Window()
setWindowTitle(tr("minikube")); setWindowTitle(tr("minikube"));
setWindowIcon(*trayIconIcon); setWindowIcon(*trayIconIcon);
checkForUpdates();
} }
QProcessEnvironment Window::setMacEnv() QProcessEnvironment Window::setMacEnv()
@ -910,10 +914,8 @@ void Window::sshConsole()
mainWindow->show(); mainWindow->show();
#elif __APPLE__ #elif __APPLE__
QString command = program + " ssh -p " + selectedClusterName(); QString command = program + " ssh -p " + selectedClusterName();
QStringList arguments = { "-e", "tell app \"Terminal\"", QStringList arguments = { "-e", "tell app \"Terminal\"", "-e", "activate",
"-e", "activate", "-e", "do script \"" + command + "\"", "-e", "end tell" };
"-e", "do script \"" + command + "\"",
"-e", "end tell" };
QProcess *process = new QProcess(this); QProcess *process = new QProcess(this);
process->start("/usr/bin/osascript", arguments); process->start("/usr/bin/osascript", arguments);
#else #else
@ -982,4 +984,63 @@ void Window::checkForMinikube()
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
void Window::checkForUpdates()
{
QString releases = getRequest("https://storage.googleapis.com/minikube-gui/releases.json");
QJsonObject latestRelease =
QJsonDocument::fromJson(releases.toUtf8()).array().first().toObject();
QString latestReleaseVersion = latestRelease["name"].toString();
QVersionNumber latestReleaseVersionNumber = QVersionNumber::fromString(latestReleaseVersion);
if (version >= latestReleaseVersionNumber) {
return;
}
QJsonObject links = latestRelease["links"].toObject();
QString link;
#if __linux__
link = links["linux"].toString();
#elif __APPLE__
link = links["darwin"].toString();
#else
link = links["windows"].toString();
#endif
notifyUpdate(latestReleaseVersion, link);
}
void Window::notifyUpdate(QString latest, QString link)
{
QDialog dialog;
dialog.setWindowTitle(tr("minikube GUI Update Available"));
dialog.setWindowIcon(*trayIconIcon);
dialog.setModal(true);
QFormLayout form(&dialog);
QLabel *msgLabel = new QLabel(this);
msgLabel->setText("Version " + latest
+ " of minikube GUI is now available!\n\nDownload the update from:");
form.addWidget(msgLabel);
QLabel *linkLabel = new QLabel(this);
linkLabel->setOpenExternalLinks(true);
linkLabel->setText("<a href=\"" + link + "\">" + link + "</a>");
form.addWidget(linkLabel);
QDialogButtonBox buttonBox(Qt::Horizontal, &dialog);
buttonBox.addButton(QString(tr("OK")), QDialogButtonBox::AcceptRole);
connect(&buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
form.addRow(&buttonBox);
dialog.exec();
}
QString Window::getRequest(QString url)
{
QNetworkAccessManager *manager = new QNetworkAccessManager();
QObject::connect(manager, &QNetworkAccessManager::finished, this, [=](QNetworkReply *reply) {
if (reply->error()) {
qDebug() << reply->errorString();
}
});
QNetworkReply *resp = manager->get(QNetworkRequest(QUrl(url)));
QEventLoop loop;
connect(resp, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
return resp->readAll();
}
#endif #endif

View File

@ -58,6 +58,9 @@
#include <QFormLayout> #include <QFormLayout>
#include <QStackedWidget> #include <QStackedWidget>
#include <QProcessEnvironment> #include <QProcessEnvironment>
#include <QVersionNumber>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#ifndef QT_NO_SYSTEMTRAYICON #ifndef QT_NO_SYSTEMTRAYICON
@ -193,6 +196,9 @@ private:
bool isBasicView; bool isBasicView;
void delay(); void delay();
int getCenter(int widgetSize, int parentSize); int getCenter(int widgetSize, int parentSize);
void checkForUpdates();
QString getRequest(QString url);
void notifyUpdate(QString latest, QString link);
}; };
#endif // QT_NO_SYSTEMTRAYICON #endif // QT_NO_SYSTEMTRAYICON