diff --git a/data/config.json b/data/config.json new file mode 100644 index 00000000..6bc45b15 --- /dev/null +++ b/data/config.json @@ -0,0 +1,3 @@ +{ + "check_for_updates": true +} diff --git a/src/plugin-main.c b/src/plugin-main.c index 7cf597e2..0d287c32 100644 --- a/src/plugin-main.c +++ b/src/plugin-main.c @@ -18,7 +18,7 @@ with this program. If not, see #include -#include +#include "plugin-support.h" #include "update-checker/github-utils.h" #include "update-checker/update-checker.h" @@ -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; } diff --git a/src/update-checker/UpdateDialog.cpp b/src/update-checker/UpdateDialog.cpp index a1d863fd..125a4b92 100644 --- a/src/update-checker/UpdateDialog.cpp +++ b/src/update-checker/UpdateDialog.cpp @@ -1,10 +1,70 @@ #include "UpdateDialog.hpp" -UpdateDialog::UpdateDialog(QWidget *parent) +#include +#include + +#include +#include +#include + +static QString dialogContent = + "

A new version of the Background Removal plugin (v{version}) 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:

" + "

Download the latest version from GitHub: v{version}

" + "

Once you've downloaded the new version, install the update as usual, there's no need to " + "uninstall the previous version.

" + "

If you have any questions or need assistance during the update process, feel free to reach out" + " to our support team.

" + "

Thank you for using our plugin and we hope you enjoy the latest release! 🙏

"; + +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); } diff --git a/src/update-checker/UpdateDialog.hpp b/src/update-checker/UpdateDialog.hpp index 5011d3df..1c7a5871 100644 --- a/src/update-checker/UpdateDialog.hpp +++ b/src/update-checker/UpdateDialog.hpp @@ -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); }; diff --git a/src/update-checker/github-utils.cpp b/src/update-checker/github-utils.cpp index 45179eeb..22cc2aed 100644 --- a/src/update-checker/github-utils.cpp +++ b/src/update-checker/github-utils.cpp @@ -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; } diff --git a/src/update-checker/github-utils.h b/src/update-checker/github-utils.h index b767204b..bba6dae6 100644 --- a/src/update-checker/github-utils.h +++ b/src/update-checker/github-utils.h @@ -4,7 +4,7 @@ extern "C" { #endif -void github_utils_get_release(void); +const char *github_utils_get_release(void); #ifdef __cplusplus } diff --git a/src/update-checker/update-checker.cpp b/src/update-checker/update-checker.cpp index fefd4547..b4941947 100644 --- a/src/update-checker/update-checker.cpp +++ b/src/update-checker/update-checker.cpp @@ -1,15 +1,44 @@ -#include - #include "update-checker.h" #include "UpdateDialog.hpp" #include +#include + +#include 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); } diff --git a/src/update-checker/update-checker.h b/src/update-checker/update-checker.h index c42ff6dd..44311d1a 100644 --- a/src/update-checker/update-checker.h +++ b/src/update-checker/update-checker.h @@ -4,7 +4,7 @@ extern "C" { #endif -void check_update(void); +void check_update(const char *latestRelease); #ifdef __cplusplus }