Skip to content

Commit

Permalink
バージョンスキップの挙動を改善 (#87)
Browse files Browse the repository at this point in the history
* Fix

* Fix

* Fix leak

* Update update-checker.cpp

* Update update-checker.cpp

* Update update-checker.cpp
  • Loading branch information
umireon authored Jul 25, 2023
1 parent 966f11c commit 115e02f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 44 deletions.
1 change: 1 addition & 0 deletions src/plugin-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ bool obs_module_load(void)

void obs_module_unload()
{
update_checker_close();
obs_log(LOG_INFO, "plugin unloaded");
}
34 changes: 15 additions & 19 deletions src/update-checker/UpdateDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,23 @@ static QString dialogContent =
"<p><a href=\"https://github.com/umireon/obs-pokemon-sv-screen-builder/releases\">https://github.com/umireon/obs-pokemon-sv-screen-builder/releases</a></p>"
"<h2>更新履歴</h2>";

UpdateDialog::UpdateDialog(const char *_pluginName, const char *_pluginVersion,
const char *latestVersion,
const char *latestChangelog, config_t *_config,
UpdateDialog::UpdateDialog(std::string latestVersion,
std::string latestChangelog, config_t *config,
QWidget *parent = nullptr)
: QDialog(parent),
layout(new QVBoxLayout),
config(_config),
pluginName(_pluginName),
pluginVersion(_pluginVersion)
: QDialog(parent), layout(new QVBoxLayout)
{
setWindowTitle("ポケモンSVスクリーンビルダー - 更新が利用可能!");
setLayout(layout);
QLabel *label = new QLabel(
dialogContent.replace(QString("{version}"), latestVersion));
QLabel *label = new QLabel(dialogContent.replace(
QString("{version}"), latestVersion.c_str()));
label->setOpenExternalLinks(true);
label->setTextInteractionFlags(Qt::TextBrowserInteraction);
label->setTextFormat(Qt::RichText);
label->setWordWrap(true);
layout->addWidget(label);

QScrollArea *scrollArea = new QScrollArea;
QLabel *scrollAreaLabel = new QLabel(latestChangelog);
QLabel *scrollAreaLabel = new QLabel(latestChangelog.c_str());
scrollAreaLabel->setOpenExternalLinks(true);
scrollAreaLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
scrollAreaLabel->setTextFormat(Qt::MarkdownText);
Expand All @@ -45,14 +40,15 @@ UpdateDialog::UpdateDialog(const char *_pluginName, const char *_pluginVersion,
// Add a checkbox to disable update checks
QCheckBox *disableCheckbox = new QCheckBox("更新通知をオフにする");
layout->addWidget(disableCheckbox);
connect(disableCheckbox, &QCheckBox::stateChanged, [this](int state) {
config_set_bool(config, pluginName.c_str(), "check_update_skip",
state != Qt::Unchecked);
config_set_string(config, pluginName.c_str(),
"check_update_skip_version",
pluginVersion.c_str());
config_save_safe(config, "tmp", nullptr);
});
connect(disableCheckbox, &QCheckBox::stateChanged,
[this, latestVersion, config](int state) {
config_set_bool(config, "check-update", "skip",
state != Qt::Unchecked);
config_set_string(config, "check-update",
"skip-version",
latestVersion.c_str());
config_save_safe(config, "tmp", nullptr);
});

// Add a button to close the dialog
QPushButton *closeButton = new QPushButton("Close");
Expand Down
8 changes: 2 additions & 6 deletions src/update-checker/UpdateDialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
class UpdateDialog : public QDialog {
Q_OBJECT
public:
UpdateDialog(const char *_pluginName, const char *_pluginVersion,
const char *latestVersion, const char *latestChangelog,
config_t *_config, QWidget *parent);
UpdateDialog(std::string latestVersion, std::string latestChangelog,
config_t *config, QWidget *parent);

private:
QVBoxLayout *layout;
config_t *config;
std::string pluginName;
std::string pluginVersion;
};
58 changes: 39 additions & 19 deletions src/update-checker/update-checker.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
#include <filesystem>

#include <QTimer>

#include <obs-frontend-api.h>
#include <obs-module.h>
#include <util/config-file.h>

#include "UpdateDialog.hpp"
#include "GitHubClient.hpp"

#include "update-checker.h"

config_t *checkUpdateConfig;
UpdateDialog *updateDialog;

static bool getIsSkipping(config_t *config, const char *pluginName,
const char *pluginVersion)
static bool getIsSkipping(config_t *config, std::string latestVersion)
{
bool skip = config_get_bool(config, pluginName, "check_update_skip");
const char *skipVersion = config_get_string(
config, pluginName, "check_update_skip_version");
bool skip = config_get_bool(config, "check-update", "skip");
const char *skipVersion =
config_get_string(config, "check-update", "skip-version");
if (skip) {
if (skipVersion != nullptr &&
std::strcmp(skipVersion, pluginVersion) == 0) {
if (skipVersion != nullptr && skipVersion == latestVersion) {
return true;
} else {
config_set_bool(config, pluginName, "check_update_skip",
false);
config_set_bool(config, "check-update", "skip", false);
config_save_safe(config, "tmp", nullptr);
return false;
}
Expand All @@ -35,14 +36,10 @@ void update_checker_check_update(const char *latest_release_url,
const char *plugin_name,
const char *plugin_version)
{
config_t *config = obs_frontend_get_global_config();
if (getIsSkipping(config, plugin_name, plugin_version)) {
blog(LOG_INFO, "[%s] Checking update skipped!", plugin_name);
return;
}

GitHubClient client(plugin_name, plugin_version);
auto result = client.getLatestRelease(latest_release_url);
GitHubClient::LatestRelease result =
client.getLatestRelease(latest_release_url);
if (result.error) {
blog(LOG_INFO, "[%s] Failed to fetch latest release info!",
plugin_name);
Expand All @@ -53,9 +50,32 @@ void update_checker_check_update(const char *latest_release_url,
return;
}

updateDialog = new UpdateDialog(
plugin_name, plugin_version, result.version.c_str(),
result.body.c_str(), config,
(QWidget *)obs_frontend_get_main_window());
char *configDirDstr = obs_module_config_path("");
std::filesystem::create_directories(configDirDstr);
bfree(configDirDstr);

char *configDstr = obs_module_config_path("update-checker.ini");
int configResult =
config_open(&checkUpdateConfig, configDstr, CONFIG_OPEN_ALWAYS);
bfree(configDstr);
if (configResult != CONFIG_SUCCESS) {
blog(LOG_ERROR, "[%s] Update checker config cennot be opened!",
plugin_name);
return;
}
if (getIsSkipping(checkUpdateConfig, result.version.c_str())) {
blog(LOG_INFO, "[%s] Checking update skipped!", plugin_name);
return;
}

updateDialog =
new UpdateDialog(result.version, result.body, checkUpdateConfig,
(QWidget *)obs_frontend_get_main_window());
QTimer::singleShot(2000, updateDialog, &UpdateDialog::exec);
}

void update_checker_close(void)
{
config_close(checkUpdateConfig);
delete updateDialog;
}
2 changes: 2 additions & 0 deletions src/update-checker/update-checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ void update_checker_check_update(const char *latest_release_url,
const char *plugin_name,
const char *plugin_version);

void update_checker_close(void);

#ifdef __cplusplus
}
#endif

0 comments on commit 115e02f

Please sign in to comment.