Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update version check and dialog #370

Merged
merged 6 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions data/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"check_for_updates": true
}
8 changes: 6 additions & 2 deletions src/plugin-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>

#include <obs-module.h>

#include <plugin-support.h>
#include "plugin-support.h"

#include "update-checker/github-utils.h"
#include "update-checker/update-checker.h"
Expand All @@ -40,7 +40,11 @@ bool obs_module_load(void)
obs_register_source(&enhance_filter_info);
obs_log(LOG_INFO, "plugin loaded successfully (version %s)",
PLUGIN_VERSION);
github_utils_get_release();
const char *latestRelease = github_utils_get_release();
if (latestRelease != NULL) {
obs_log(LOG_INFO, "latest release is %s", latestRelease);
check_update(latestRelease);
}
return true;
}

Expand Down
66 changes: 63 additions & 3 deletions src/update-checker/UpdateDialog.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,70 @@
#include "UpdateDialog.hpp"

UpdateDialog::UpdateDialog(QWidget *parent)
#include <obs.h>
#include <obs-module.h>

#include <QLabel>
#include <QVBoxLayout>
#include <QString>

static QString dialogContent =
"<p>A new version of the Background Removal plugin (<a "
"href=\"https://github.com/royshil/obs-backgroundremoval/releases\">v{version}</a>) is "
"now available for download. We've made some exciting updates and improvements that we think "
"you'll love. To get the latest features and enhancements, please follow the link below:</p>"
"<p>Download the latest version from GitHub: <a "
"href=\"https://github.com/royshil/obs-backgroundremoval/releases\">v{version}</a></p>"
"<p>Once you've downloaded the new version, install the update as usual, there's no need to "
"uninstall the previous version.</p>"
"<p>If you have any questions or need assistance during the update process, feel free to reach out"
" to our <a href=\"https://github.com/royshil/obs-backgroundremoval/issues\">support team</a>.</p>"
"<p>Thank you for using our plugin and we hope you enjoy the latest release! 🙏</p>";

UpdateDialog::UpdateDialog(const char *latestVersion, QWidget *parent)
: QDialog(parent), layout(new QVBoxLayout)
{
setWindowTitle("Update available!");
setWindowTitle("Background Removal - Update available! 🚀");
setLayout(layout);
QLabel *label = new QLabel("OBS Background Removal: Update available!");
QLabel *label = new QLabel(dialogContent.replace(
QString("{version}"), QString(latestVersion)));
label->setOpenExternalLinks(true);
label->setTextInteractionFlags(Qt::TextBrowserInteraction);
label->setTextFormat(Qt::RichText);
label->setWordWrap(true);
layout->addWidget(label);
// Add a checkbox to disable update checks
QCheckBox *disableCheckbox = new QCheckBox("Disable update checks");
layout->addWidget(disableCheckbox);
connect(disableCheckbox, &QCheckBox::stateChanged, this,
&UpdateDialog::disableUpdateChecks);
// Add a button to close the dialog
QPushButton *closeButton = new QPushButton("Close");
layout->addWidget(closeButton);
connect(closeButton, &QPushButton::clicked, this, &QDialog::close);
}

void UpdateDialog::disableUpdateChecks(int state)
{
UNUSED_PARAMETER(state);

// Get the config file
char *config_file = obs_module_file("config.json");
if (!config_file) {
blog(LOG_INFO, "Unable to find config file");
return;
}

// Parse the config file
obs_data_t *json_data = obs_data_create_from_json_file(config_file);
if (!json_data) {
blog(LOG_INFO, "Failed to parse config file");
return;
}

// Update the config
obs_data_set_bool(json_data, "check_for_updates",
state == Qt::Unchecked);
obs_data_save_json(json_data, config_file);

obs_data_release(json_data);
}
3 changes: 2 additions & 1 deletion src/update-checker/UpdateDialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
class UpdateDialog : public QDialog {
Q_OBJECT
public:
UpdateDialog(QWidget *parent = nullptr);
UpdateDialog(const char *latestVersion, QWidget *parent = nullptr);

private:
QVBoxLayout *layout;
void disableUpdateChecks(int state);
};
50 changes: 36 additions & 14 deletions src/update-checker/github-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,43 @@ std::size_t writeFunctionStdString(void *ptr, std::size_t size, size_t nmemb,
return size * nmemb;
}

void github_utils_get_release(void)
const char *github_utils_get_release(void)
{
CURL *curl = curl_easy_init();
if (curl) {
std::string str;
CURLcode code;
curl_easy_setopt(curl, CURLOPT_URL,
GITHUB_LATEST_RELEASE_URL.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, USER_AGENT.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
writeFunctionStdString);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str);
code = curl_easy_perform(curl);
curl_easy_cleanup(curl);
blog(LOG_INFO, "%s\n", str.c_str());
blog(LOG_INFO, "%s\n", curl_easy_strerror(code));
if (!curl) {
blog(LOG_INFO, "Failed to initialize curl");
return NULL;
}
CURLcode code;
curl_easy_setopt(curl, CURLOPT_URL, GITHUB_LATEST_RELEASE_URL.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, USER_AGENT.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunctionStdString);
std::string responseBody;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseBody);
code = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (code != CURLE_OK) {
blog(LOG_INFO, "Failed to get latest release info");
return NULL;
}

// Parse the JSON response
obs_data_t *data = obs_data_create_from_json(responseBody.c_str());
if (!data) {
blog(LOG_INFO, "Failed to parse latest release info");
return NULL;
}

// The version is in the "tag_name" property
char *version = strdup(obs_data_get_string(data, "tag_name"));
obs_data_release(data);

// remove the "v" prefix, if it exists
if (version[0] == 'v') {
char *newVersion = strdup(version + 1);
free(version);
version = newVersion;
}

return version;
}
2 changes: 1 addition & 1 deletion src/update-checker/github-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
extern "C" {
#endif

void github_utils_get_release(void);
const char *github_utils_get_release(void);

#ifdef __cplusplus
}
Expand Down
39 changes: 34 additions & 5 deletions src/update-checker/update-checker.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
#include <QTimer>

#include "update-checker.h"
#include "UpdateDialog.hpp"

#include <obs-frontend-api.h>
#include <obs-module.h>

#include <QTimer>

UpdateDialog *update_dialog;

void check_update(void)
extern "C" const char *PLUGIN_VERSION;

void check_update(const char *latestRelease)
{
update_dialog =
new UpdateDialog((QWidget *)obs_frontend_get_main_window());
// Check configuration to see if update checks are disabled
char *config_file = obs_module_file("config.json");
if (!config_file) {
blog(LOG_INFO, "Unable to find config file");
return;
}

obs_data_t *data = obs_data_create_from_json_file(config_file);
if (!data) {
blog(LOG_INFO, "Failed to parse config file");
return;
}

bool shouldCheckForUpdates =
obs_data_get_bool(data, "check_for_updates");
obs_data_release(data);
if (!shouldCheckForUpdates) {
// Update checks are disabled
return;
}

if (strcmp(latestRelease, PLUGIN_VERSION) == 0) {
// No update available, latest version is the same as the current version
return;
}

update_dialog = new UpdateDialog(
latestRelease, (QWidget *)obs_frontend_get_main_window());
QTimer::singleShot(2000, update_dialog, &UpdateDialog::exec);
}
2 changes: 1 addition & 1 deletion src/update-checker/update-checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
extern "C" {
#endif

void check_update(void);
void check_update(const char *latestRelease);

#ifdef __cplusplus
}
Expand Down