check for new version rework, support "check for stable updates only" mode

This commit is contained in:
SoftFever 2023-12-29 12:04:40 +08:00
parent f89932f63e
commit 63f9b3b1ba
6 changed files with 98 additions and 76 deletions

View file

@ -4,6 +4,7 @@
#include "GUI_ObjectList.hpp"
#include "GUI_Factories.hpp"
#include "format.hpp"
#include "libslic3r_version.h"
// Localization headers: include libslic3r version first so everything in this file
// uses the slic3r/GUI version (the macros will take precedence over the functions).
@ -4290,7 +4291,8 @@ Semver get_version(const std::string& str, const std::regex& regexp) {
void GUI_App::check_new_version_sf(bool show_tips, int by_user)
{
AppConfig* app_config = wxGetApp().app_config;
auto version_check_url = app_config->version_check_url();
bool check_stable_only = app_config->get_bool("check_stable_update_only");
auto version_check_url = app_config->version_check_url(check_stable_only);
Http::get(version_check_url)
.on_error([&](std::string body, std::string error, unsigned http_status) {
(void)body;
@ -4298,25 +4300,22 @@ void GUI_App::check_new_version_sf(bool show_tips, int by_user)
error);
})
.timeout_connect(1)
.on_complete([this,by_user](std::string body, unsigned http_status) {
.on_complete([this,by_user, check_stable_only](std::string body, unsigned http_status) {
// Http response OK
if (http_status != 200)
return;
try {
boost::trim(body);
// SoftFever: parse github release, ported from SS
// Orca: parse github release, inspired by SS
boost::property_tree::ptree root;
std::stringstream json_stream(body);
boost::property_tree::read_json(json_stream, root);
bool i_am_pre = false;
// at least two number, use '.' as separator. can be followed by -Az23 for prereleased and +Az42 for
// metadata
std::regex matcher("[0-9]+\\.[0-9]+(\\.[0-9]+)*(-[A-Za-z0-9]+)?(\\+[A-Za-z0-9]+)?");
Semver current_version = get_version(SoftFever_VERSION, matcher);
Semver current_version = get_version(SoftFever_VERSION, matcher);
Semver best_pre(1, 0, 0);
Semver best_release(1, 0, 0);
std::string best_pre_url;
@ -4324,56 +4323,73 @@ void GUI_App::check_new_version_sf(bool show_tips, int by_user)
std::string best_release_content;
std::string best_pre_content;
const std::regex reg_num("([0-9]+)");
std::string tag = root.get<std::string>("tag_name");
if (tag[0] == 'v')
tag.erase(0, 1);
for (std::regex_iterator it = std::sregex_iterator(tag.begin(), tag.end(), reg_num);
it != std::sregex_iterator(); ++it) {
}
Semver tag_version = get_version(tag, matcher);
if (current_version == tag_version)
i_am_pre = root.get<bool>("prerelease");
if (root.get<bool>("prerelease")) {
if (best_pre < tag_version) {
best_pre = tag_version;
best_pre_url = root.get<std::string>("html_url");
best_pre_content = root.get<std::string>("body");
best_pre.set_prerelease("Preview");
}
if (check_stable_only) {
std::string tag = root.get<std::string>("tag_name");
if (tag[0] == 'v')
tag.erase(0, 1);
for (std::regex_iterator it = std::sregex_iterator(tag.begin(), tag.end(), reg_num); it != std::sregex_iterator(); ++it) {}
Semver tag_version = get_version(tag, matcher);
if (root.get<bool>("prerelease")) {
if (best_pre < tag_version) {
best_pre = tag_version;
best_pre_url = root.get<std::string>("html_url");
best_pre_content = root.get<std::string>("body");
best_pre.set_prerelease("Preview");
}
} else {
if (best_release < tag_version) {
best_release = tag_version;
best_release_url = root.get<std::string>("html_url");
best_release_content = root.get<std::string>("body");
}
}
} else {
if (best_release < tag_version) {
best_release = tag_version;
best_release_url = root.get<std::string>("html_url");
best_release_content = root.get<std::string>("body");
}
for (auto json_version : root) {
std::string tag = json_version.second.get<std::string>("tag_name");
if (tag[0] == 'v')
tag.erase(0, 1);
for (std::regex_iterator it = std::sregex_iterator(tag.begin(), tag.end(), reg_num); it != std::sregex_iterator();
++it) {}
Semver tag_version = get_version(tag, matcher);
if (json_version.second.get<bool>("prerelease")) {
if (best_pre < tag_version) {
best_pre = tag_version;
best_pre_url = json_version.second.get<std::string>("html_url");
best_pre_content = json_version.second.get<std::string>("body");
best_pre.set_prerelease("Preview");
}
} else {
if (best_release < tag_version) {
best_release = tag_version;
best_release_url = json_version.second.get<std::string>("html_url");
best_release_content = json_version.second.get<std::string>("body");
}
}
}
}
// if release is more recent than beta, use release anyway
if (best_pre < best_release) {
best_pre = best_release;
best_pre_url = best_release_url;
best_pre_content = best_release_content;
best_pre = best_release;
best_pre_url = best_release_url;
best_pre_content = best_release_content;
}
// if we're the most recent, don't do anything
if ((i_am_pre ? best_pre : best_release) <= current_version) {
if (by_user != 0)
this->no_new_version();
return;
if ((check_stable_only ? best_release : best_pre) <= current_version) {
if (by_user != 0)
this->no_new_version();
return;
}
// BOOST_LOG_TRIVIAL(info) << format("Got %1% online version: `%2%`. Sending to GUI thread...",
// SLIC3R_APP_NAME, i_am_pre ? best_pre.to_string(): best_release.to_string());
version_info.url = i_am_pre ? best_pre_url : best_release_url;
version_info.version_str = i_am_pre ? best_pre.to_string() : best_release.to_string_sf();
version_info.description = i_am_pre ? best_pre_content : best_release_content;
version_info.url = check_stable_only ? best_release_url : best_pre_url;
version_info.version_str = check_stable_only ? best_release.to_string_sf() : best_pre.to_string();
version_info.description = check_stable_only ? best_release_content : best_pre_content;
version_info.force_upgrade = false;
wxCommandEvent *evt = new wxCommandEvent(EVT_SLIC3R_VERSION_ONLINE);
evt->SetString((i_am_pre ? best_pre : best_release).to_string());
wxCommandEvent* evt = new wxCommandEvent(EVT_SLIC3R_VERSION_ONLINE);
evt->SetString((check_stable_only ? best_release : best_pre).to_string());
GUI::wxGetApp().QueueEvent(evt);
} catch (...) {
}
} catch (...) {}
})
.perform();
}
@ -4591,8 +4607,7 @@ void GUI_App::sync_preset(Preset* preset)
if (http_code >= 400) {
result = 0;
updated_info = "hold";
}
else
} else
result = -1;
}
}
@ -4614,8 +4629,7 @@ void GUI_App::sync_preset(Preset* preset)
auto update_time_str = values_map[BBL_JSON_KEY_UPDATE_TIME];
if (!update_time_str.empty())
update_time = std::atoll(update_time_str.c_str());
}
else {
} else {
BOOST_LOG_TRIVIAL(trace) << "[sync_preset]create: request_setting_id failed, http code "<<http_code;
// do not post new preset this time if http code >= 400
if (http_code >= 400) {
@ -4625,12 +4639,10 @@ void GUI_App::sync_preset(Preset* preset)
else
result = -1;
}
}
else {
} else {
BOOST_LOG_TRIVIAL(trace) << "[sync_preset]create: can not generate differed preset";
}
}
else if (preset->sync_info.compare("update") == 0) {
} else if (preset->sync_info.compare("update") == 0) {
if (!setting_id.empty()) {
int ret = preset_bundle->get_differed_values_to_update(*preset, values_map);
if (!ret) {
@ -5028,7 +5040,7 @@ bool GUI_App::select_language()
// 3) new_language_info->CanonicalName is a safe bet. It points to a valid dictionary name.
app_config->set("language", new_language_info->CanonicalName.ToUTF8().data());
return true;
}
}
}
return false;
@ -5936,18 +5948,15 @@ void GUI_App::MacOpenFiles(const wxArrayString &fileNames)
input_files.push_back(non_gcode_files[i]);
}
this->plater()->load_files(input_files);
}
else {
} else {
for (size_t i = 0; i < files.size(); ++i) {
this->init_params->input_files.emplace_back(files[i]);
}
}
}
else {
} else {
if (m_post_initialized) {
this->plater()->load_gcode(gcode_files.front());
}
else {
} else {
this->init_params->input_gcode = true;
this->init_params->input_files = { into_u8(gcode_files.front()) };
}
@ -6296,8 +6305,7 @@ void GUI_App::gcode_thumbnails_debug()
width = 0;
height = 0;
rows.clear();
}
else if (reading_image)
} else if (reading_image)
row += gcode_line.substr(2);
}
}