Skip to content

Commit

Permalink
feat: toast & check new version
Browse files Browse the repository at this point in the history
  • Loading branch information
arucil committed Jan 15, 2022
1 parent 4be6c47 commit e6df668
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 83 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
[package]
name = "wqxtools"
version = "0.0.1"
authors = ["amlo <[email protected]>"]
edition = "2021"

[dependencies]
semver = "1.0.4"

[workspace]
members = [
"bin_dasm",
Expand Down
3 changes: 2 additions & 1 deletion api_cpp_binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ crate-type = ["staticlib"]

[dependencies]
config = { version = "0.1.0", path = "../config" }
gvb_interp = { version = "0.1.0", path = "../gvb_interp" }
gvb_interp = { version = "0.1.0", path = "../gvb_interp" }
wqxtools = { path = ".." }
2 changes: 2 additions & 0 deletions api_cpp_binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ pub mod config;
pub mod gvb;
pub mod string;
pub mod types;
pub mod version;

pub use self::array::*;
pub use self::config::*;
pub use self::gvb::*;
pub use self::string::*;
pub use self::types::*;
pub use self::version::*;

#[repr(C)]
pub struct Unit(pub i32);
Expand Down
7 changes: 7 additions & 0 deletions api_cpp_binding/src/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::{Maybe, Utf8Str};

#[no_mangle]
pub extern "C" fn is_new_version(ver: Utf8Str) -> Maybe<bool> {
wqxtools::is_new_version(unsafe { ver.as_str() })
.map_or(Maybe::Nothing, Maybe::Just)
}
2 changes: 1 addition & 1 deletion gui/bas_files/3d迷宫.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
95 IF B%(I,J,1)THEN 92 ELSE B%(I,J,1)=1:98
96 IF B%(I+1,J,0)THEN 92 ELSE B%(I+1,J,0)=1:98
97 IF B%(I,J+1,1)THEN 92 ELSE B%(I,J+1,1)=1
98 NEXT:NEXT
98 NEXT:NEXT
100 FOR K=1 TO L:B%(K,0,0)=1:B%(K,L,0)=1:B%(0,K,1)=1:B%(L,K,1)=1:NEXT
104 LB=(6-L/2)*5:LBB=(6+L/2)*5
110 cls:X=1:Y=INT(RND(1)*L)+1:P=L:Q=INT(RND(1)*L)+1:YX=(GB-1)*4+L/2-2
Expand Down
9 changes: 3 additions & 6 deletions gui/src/gvb/gvbsim_window.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "gvbsim_window.h"

#include <QHeaderView>
#include <QStatusBar>
#include <QKeyEvent>
#include <QMessageBox>
#include <QStatusBar>
#include <QTableView>
#include <QTimer>
#include <QToolBar>
Expand Down Expand Up @@ -240,11 +240,8 @@ void GvbSimWindow::stop() {

auto result = api::gvb_vm_stop(m_vm);
if (result.tag == api::GvbStopVmResult::Tag::Left) {
QMessageBox::critical(
this,
"错误",
QString("运行时错误:%1")
.arg(QString::fromUtf8(result.left._0.data, result.left._0.len)));
auto msg = QString::fromUtf8(result.left._0.data, result.left._0.len);
m_message.setValue(QString("程序运行出错:") + msg);
destroy_string(result.left._0);
}
api::gvb_reset_exec_result(&m_execResult);
Expand Down
153 changes: 117 additions & 36 deletions gui/src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include <QFileDialog>
#include <QFileInfo>
#include <QFrame>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLabel>
#include <QMenu>
#include <QMenuBar>
Expand All @@ -20,7 +22,6 @@
#include <QScreen>
#include <QTimer>

#include "toast.h"
#include "about_dialog.h"
#include "action.h"
#include "api.h"
Expand All @@ -39,14 +40,8 @@

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
m_networkMan(new QNetworkAccessManager(this)),
m_manualUpdate(false) {
m_networkMan(new QNetworkAccessManager(this)) {
m_networkMan->setTransferTimeout(3000);
connect(
m_networkMan,
&QNetworkAccessManager::finished,
this,
&MainWindow::versionRequestFinished);

initUi();

Expand Down Expand Up @@ -76,8 +71,7 @@ MainWindow::MainWindow(QWidget *parent) :
openFileByPath(args.at(1), qApp->primaryScreen());
}

m_manualUpdate = false;
requestVersions();
checkNewVersion(false);
});
}

Expand Down Expand Up @@ -192,8 +186,15 @@ void MainWindow::initMenu() {

auto mnuHelp = menuBar()->addMenu("帮助");

auto actAbout = mnuHelp->addAction("关于");
auto actCheckVer = mnuHelp->addAction("检查新版本");
connect(actCheckVer, &QAction::triggered, this, [this] {
showMessage("正在检查版本更新", 1000, MessageType::Info);
checkNewVersion(true);
});

mnuHelp->addSeparator();

auto actAbout = mnuHelp->addAction("关于");
connect(actAbout, &QAction::triggered, this, [this] {
AboutDialog(this).exec();
});
Expand Down Expand Up @@ -654,36 +655,116 @@ void MainWindow::dropEvent(QDropEvent *ev) {
}
}

void MainWindow::requestVersions() {
m_networkMan->get(QNetworkRequest(QUrl(VERSION_API_ENDPOINT)));
}
void MainWindow::checkNewVersion(bool manual) {
auto reply = m_networkMan->get(QNetworkRequest(QUrl(VERSION_API_ENDPOINT)));
connect(reply, &QNetworkReply::finished, [=] {
reply->deleteLater();

if (reply->error()) {
if (manual) {
QString msg;
switch (reply->error()) {
case QNetworkReply::TimeoutError:
msg = "连接超时";
break;
case QNetworkReply::TemporaryNetworkFailureError:
msg = "网络断开";
break;
default:
msg = reply->errorString();
break;
}
QMessageBox::critical(this, "错误", QString("检查版本失败:") + msg);
}

void MainWindow::versionRequestFinished(QNetworkReply *reply) {
reply->deleteLater();
if (reply->error()) {
if (!m_manualUpdate) {
return;
}
auto resp = reply->readAll();
QJsonParseError error;
auto json = QJsonDocument::fromJson(resp, &error);
if (error.error != QJsonParseError::NoError) {
if (manual) {
QMessageBox::critical(this, "错误", "检查版本失败:JSON parse error");
}
return;
}

QString msg;
switch (reply->error()) {
case QNetworkReply::TimeoutError:
msg = "连接超时";
break;
case QNetworkReply::TemporaryNetworkFailureError:
msg = "网络断开";
break;
default:
msg = reply->errorString();
break;
auto tag = json.array().at(0).toObject()["tag_name"].toString().toUtf8();
auto result =
api::is_new_version({tag.data(), static_cast<size_t>(tag.size())});
if (result.tag == api::Maybe<bool>::Tag::Nothing) {
if (manual) {
QMessageBox::critical(
this,
"错误",
"检查版本失败:release tag_name is not semver");
}
return;
}
QMessageBox::critical(this, "错误", QString("检查版本失败:") + msg);
return;
}
auto resp = reply->readAll();
QJsonParseError error;
auto json = QJsonDocument::fromJson(resp, &error);
// TODO

if (manual) {
if (result.just._0) {
notifyNewVersion(tag);
} else {
showMessage("已经是最新版本", 700, MessageType::Info);
}
} else if (result.just._0) {
showMessage(
"有新版本,请点击菜单 [帮助] -> [检查新版本] 查看新版本",
1500,
MessageType::Info);
}
});
}

void MainWindow::notifyNewVersion(const QString &tag) {
auto reply = m_networkMan->get(
QNetworkRequest(QUrl(QString(VERSION_API_ENDPOINT "/%1?include_html_description=true").arg(tag))));
connect(reply, &QNetworkReply::finished, [=] {
reply->deleteLater();
if (reply->error()) {
QString msg;
switch (reply->error()) {
case QNetworkReply::TimeoutError:
msg = "连接超时";
break;
case QNetworkReply::TemporaryNetworkFailureError:
msg = "网络断开";
break;
default:
msg = reply->errorString();
break;
}
QMessageBox::critical(
this,
"错误",
QString("获取新版本信息失败:") + msg);
}
auto resp = reply->readAll();
QJsonParseError error;
auto json = QJsonDocument::fromJson(resp, &error);
if (error.error != QJsonParseError::NoError) {
QMessageBox::critical(
this,
"错误",
"获取新版本信息失败:JSON parse error");
return;
}

auto release = json.object();
auto description = release["description_html"].toString();
auto url = release["_links"].toObject()["self"].toString();

m_toast->hide();

QMessageBox::information(
this,
"新版本",
QString("<h3>%1</h3><p>%2</p><a href=\"%3\">点击链接下载新版本</a>")
.arg(tag)
.arg(description)
.arg(url));
});
}

void MainWindow::showMessage(const QString &text, int ms, MessageType type) {
Expand Down
6 changes: 2 additions & 4 deletions gui/src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ class MainWindow: public QMainWindow {
void initUi();
void initMenu();
ActionResult confirmSaveIfDirty(ToolWidget *);
void requestVersions();
void checkNewVersion(bool manual);
void notifyNewVersion(const QString &tag);

private slots:
void openFile();
void createFile(const Tool &);
ActionResult saveFile();
ActionResult saveFileAs(bool save = false);
void setTitle();
void versionRequestFinished(QNetworkReply *);
void showMessage(const QString &, int ms, MessageType);

private:
Expand Down Expand Up @@ -83,6 +83,4 @@ private slots:
BoolValue m_loaded;

QVector<QAction *> m_extraEditActions;

bool m_manualUpdate;
};
Loading

0 comments on commit e6df668

Please sign in to comment.