Line data Source code
1 : #include "gui/update_checker.h"
2 :
3 : #include <array>
4 : #include <cstdio>
5 : #include <iostream>
6 : #include <memory>
7 :
8 : #include "common.h"
9 :
10 : namespace Amplitron {
11 :
12 20 : UpdateChecker::UpdateChecker() = default;
13 :
14 20 : UpdateChecker::~UpdateChecker() { shutdown(); }
15 :
16 0 : void UpdateChecker::start_check() {
17 0 : if (!check_thread_.joinable()) {
18 0 : check_thread_ = std::thread([this]() { this->check_for_updates(); });
19 0 : }
20 0 : }
21 :
22 15 : void UpdateChecker::shutdown() {
23 15 : shutdown_requested_ = true;
24 15 : if (check_thread_.joinable()) {
25 0 : check_thread_.join();
26 0 : }
27 15 : }
28 :
29 6 : bool UpdateChecker::has_new_release() const {
30 6 : std::lock_guard<std::mutex> lock(mutex_);
31 6 : return has_new_release_;
32 6 : }
33 :
34 0 : std::string UpdateChecker::new_release_version() const {
35 0 : std::lock_guard<std::mutex> lock(mutex_);
36 0 : return new_release_version_;
37 0 : }
38 :
39 0 : std::string UpdateChecker::new_release_url() const {
40 0 : std::lock_guard<std::mutex> lock(mutex_);
41 0 : return new_release_url_;
42 0 : }
43 :
44 0 : void UpdateChecker::check_for_updates() {
45 : #ifndef AMPLITRON_NO_DESKTOP_SHELL
46 0 : const char* cmd =
47 : "curl -s https://api.github.com/repos/amplitron-dsp/Amplitron/releases/latest | grep "
48 : "'\"tag_name\":' | sed -E 's/.*\"([^\"]+)\".*/\\1/'";
49 0 : std::array<char, 128> buffer;
50 0 : std::string result;
51 :
52 0 : std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
53 0 : if (!pipe) {
54 0 : return;
55 : }
56 :
57 0 : while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
58 0 : if (shutdown_requested_) return;
59 0 : result += buffer.data();
60 : }
61 :
62 0 : if (!result.empty()) {
63 0 : result.erase(result.find_last_not_of(" \n\r\t") + 1);
64 :
65 0 : std::lock_guard<std::mutex> lock(mutex_);
66 0 : if (result != AMPLITRON_VERSION && !result.empty() && result[0] == 'v') {
67 0 : has_new_release_ = true;
68 0 : new_release_version_ = result;
69 0 : new_release_url_ = "https://github.com/amplitron-dsp/Amplitron/releases/tag/" + result;
70 0 : }
71 0 : }
72 : #endif
73 0 : }
74 :
75 : } // namespace Amplitron
|