From 44816892e597decc1a3283bd92305164573e22b4 Mon Sep 17 00:00:00 2001 From: josephpei Date: Wed, 6 Jan 2021 16:55:20 +0800 Subject: [PATCH] check new release --- lang/en_US.ts | 50 +++++++++++++++++++++--------------------- lang/zh_CN.ts | 50 +++++++++++++++++++++--------------------- src/mainwindow.cpp | 20 ++++++++++++++--- src/mainwindow.h | 2 ++ src/utils/utility.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++ src/utils/utility.h | 15 +++++++++++++ 6 files changed, 135 insertions(+), 53 deletions(-) create mode 100644 src/utils/utility.cpp create mode 100644 src/utils/utility.h diff --git a/lang/en_US.ts b/lang/en_US.ts index 1ad0d32..b0a328f 100644 --- a/lang/en_US.ts +++ b/lang/en_US.ts @@ -10,13 +10,13 @@ - + Overview - + Proxies @@ -76,87 +76,87 @@ - + Show MainWindow - + &Quit - + Global - + Rule - + Direct - + Set as system proxy - + Copy shell command - + Start at login - + Allow connect from lan - + Manage - + Update - + Auto Update - + About - + Check Update - + Proxy Mode - + Config - + Help @@ -164,32 +164,32 @@ SubscribeDialog - + New - + Delete - + Update - + Name - + Url - + Time diff --git a/lang/zh_CN.ts b/lang/zh_CN.ts index e6873dd..38017fb 100644 --- a/lang/zh_CN.ts +++ b/lang/zh_CN.ts @@ -10,13 +10,13 @@ - + Overview - + Proxies @@ -76,87 +76,87 @@ - + Show MainWindow 显示主窗口 - + &Quit &退出 - + Global 全局连接 - + Rule 规则判断 - + Direct 直接连接 - + Set as system proxy 设置为系统代理 - + Copy shell command 复制终端代理命令 - + Start at login 开机启动 - + Allow connect from lan 允许局域网连接 - + Manage 管理 - + Update 更新 - + Auto Update 自动更新 - + About 关于 - + Check Update 检查更新 - + Proxy Mode 代理模式 - + Config 配置 - + Help 帮助 @@ -164,32 +164,32 @@ SubscribeDialog - + New 新建 - + Delete 删除 - + Update 更新 - + Name 名称 - + Url 链接 - + Time 时间 diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 70d7be5..cebd78a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1,8 +1,10 @@ #include "mainwindow.h" +#include "BuildConfig.h" #include "./ui_mainwindow.h" #include "./core/configurator.h" #include "./core/clashApi.h" #include "./utils/iconsForkAwesome.h" +#include "./utils/utility.h" #include #include @@ -43,11 +45,11 @@ MainWindow::MainWindow(QWidget *parent) ui->logoLabel->setPixmap(logo); ui->overviewButton->setFont(font); - QString overviewStr = QString("%1 %2").arg(QChar(0xf0e4)).arg(tr("Overview")); + QString overviewStr = QString("%1 %2").arg(ICON_FK_TACHOMETER).arg(tr("Overview")); ui->overviewButton->setText(overviewStr); pageButtons->addButton(ui->overviewButton); ui->proxiesButton->setFont(font); - QString proxiesStr = QString("%1 %2").arg(QChar(0xf1d8)).arg(tr("Proxies")); + QString proxiesStr = QString("%1 %2").arg(ICON_FK_PAPER_PLANE).arg(tr("Proxies")); ui->proxiesButton->setText(proxiesStr); pageButtons->addButton(ui->proxiesButton); @@ -179,8 +181,10 @@ void MainWindow::createActions() connect(autoUpdateSubConfig, &QAction::triggered, this, &MainWindow::autoUpdateSubConfigChange); about = new QAction(tr("About"), this); - checkUpdate = new QAction(tr("Check Update"), this); connect(about, &QAction::triggered, this, &MainWindow::showAboutDialog); + + checkUpdate = new QAction(tr("Check Update"), this); + connect(checkUpdate, &QAction::triggered, this, &MainWindow::checkLatestRelease); } void MainWindow::proxyGroupMenusChange() @@ -429,6 +433,16 @@ void MainWindow::showAboutDialog() } } +void MainWindow::checkLatestRelease() +{ + QString version = Utility::getLatestVersion(); + if (Utility::isVersionNewer(QCLASH_VERSION, version)) { + QMessageBox::information(this, "Latest Version", QString("Found newer version %1").arg(version)); + } else { + qDebug() << "You use the latest version, enjoy youself!"; + } +} + void MainWindow::showSubscribeDialog() { if (subscribeDialog) diff --git a/src/mainwindow.h b/src/mainwindow.h index 56f2a6e..410a296 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -37,6 +37,8 @@ private slots: void showMainWindow(); void showAboutDialog(); + void checkLatestRelease(); + void showSubscribeDialog(); void updateSubActions(); diff --git a/src/utils/utility.cpp b/src/utils/utility.cpp new file mode 100644 index 0000000..d558cf0 --- /dev/null +++ b/src/utils/utility.cpp @@ -0,0 +1,51 @@ +#include "utility.h" + +#include +#include +#include + +HttpUtil &Utility::http = HttpUtil::instance(); + +const QString Utility::getLatestVersion() +{ + QString url("https://api.github.com/repos/josephpei/qClash/releases"); + QByteArray releaseJsonStr = http.get(url); + if (releaseJsonStr.isEmpty()) + return QString(); + QJsonObject latestRelease; + QJsonDocument releaseJsonDoc = QJsonDocument::fromJson(releaseJsonStr); + QJsonArray releases = releaseJsonDoc.array(); + for (int i = 0; i < releases.size(); ++i) { + QJsonObject release = releases[i].toObject(); + latestRelease = release; + break; + } + return latestRelease.empty() ? "" : latestRelease["name"].toString(); +} + +bool Utility::isVersionNewer(const QString& currentVersion, const QString& version) +{ + QList _currentVersion = getVersion(currentVersion); + QList _version = getVersion(version); + + for (int i = 0; i < _version.size() && i < _currentVersion.size(); ++i) { + if (_version.at(i) > _currentVersion.at(i)) { + return true; + } else if (_version.at(i) < _currentVersion.at(i)) { + return false; + } + } + return false; +} + +QList Utility::getVersion(QString version) +{ + QList _version; + if (version.startsWith('v') || version.startsWith('v')) { + version = version.mid(1); + } + for (QString v : version.split('.')) { + _version.append(v.toInt()); + } + return _version; +} diff --git a/src/utils/utility.h b/src/utils/utility.h new file mode 100644 index 0000000..77e3002 --- /dev/null +++ b/src/utils/utility.h @@ -0,0 +1,15 @@ +#pragma once + +#include "httputil.h" + +class Utility { +private: + static HttpUtil &http; + +private: + static QList getVersion(QString version); + +public: + static const QString getLatestVersion(); + static bool isVersionNewer(const QString& currentVersion, const QString& version); +}; \ No newline at end of file