about summary refs log tree commit diff stats
path: root/src/tracker_frame.cpp
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2023-05-06 12:24:01 -0400
committerStar Rauchenberger <fefferburbia@gmail.com>2023-05-06 12:24:01 -0400
commitd20664f90a84da691baeae5fb55bb28f409ed577 (patch)
tree6a259fb645cda39aa479849e6881e78f91924fad /src/tracker_frame.cpp
parentd7212d755dca7f4fd99cf4b775cd0d372d7bcbb2 (diff)
downloadlingo-ap-tracker-d20664f90a84da691baeae5fb55bb28f409ed577.tar.gz
lingo-ap-tracker-d20664f90a84da691baeae5fb55bb28f409ed577.tar.bz2
lingo-ap-tracker-d20664f90a84da691baeae5fb55bb28f409ed577.zip
Automatically check for updates
Diffstat (limited to 'src/tracker_frame.cpp')
-rw-r--r--src/tracker_frame.cpp101
1 files changed, 98 insertions, 3 deletions
diff --git a/src/tracker_frame.cpp b/src/tracker_frame.cpp index 2a862a5..0308886 100644 --- a/src/tracker_frame.cpp +++ b/src/tracker_frame.cpp
@@ -1,11 +1,17 @@
1#include "tracker_frame.h" 1#include "tracker_frame.h"
2 2
3#include <wx/webrequest.h>
4
5#include <nlohmann/json.hpp>
6#include <sstream>
7
3#include "ap_state.h" 8#include "ap_state.h"
4#include "connection_dialog.h" 9#include "connection_dialog.h"
5#include "tracker_config.h" 10#include "tracker_config.h"
6#include "tracker_panel.h" 11#include "tracker_panel.h"
12#include "version.h"
7 13
8enum TrackerFrameIds { ID_CONNECT = 1 }; 14enum TrackerFrameIds { ID_CONNECT = 1, ID_CHECK_FOR_UPDATES = 2 };
9 15
10wxDEFINE_EVENT(STATE_CHANGED, wxCommandEvent); 16wxDEFINE_EVENT(STATE_CHANGED, wxCommandEvent);
11wxDEFINE_EVENT(STATUS_CHANGED, wxCommandEvent); 17wxDEFINE_EVENT(STATUS_CHANGED, wxCommandEvent);
@@ -25,6 +31,7 @@ TrackerFrame::TrackerFrame()
25 31
26 wxMenu *menuHelp = new wxMenu(); 32 wxMenu *menuHelp = new wxMenu();
27 menuHelp->Append(wxID_ABOUT); 33 menuHelp->Append(wxID_ABOUT);
34 menuHelp->Append(ID_CHECK_FOR_UPDATES, "Check for Updates");
28 35
29 wxMenuBar *menuBar = new wxMenuBar(); 36 wxMenuBar *menuBar = new wxMenuBar();
30 menuBar->Append(menuFile, "&File"); 37 menuBar->Append(menuFile, "&File");
@@ -38,10 +45,30 @@ TrackerFrame::TrackerFrame()
38 Bind(wxEVT_MENU, &TrackerFrame::OnAbout, this, wxID_ABOUT); 45 Bind(wxEVT_MENU, &TrackerFrame::OnAbout, this, wxID_ABOUT);
39 Bind(wxEVT_MENU, &TrackerFrame::OnExit, this, wxID_EXIT); 46 Bind(wxEVT_MENU, &TrackerFrame::OnExit, this, wxID_EXIT);
40 Bind(wxEVT_MENU, &TrackerFrame::OnConnect, this, ID_CONNECT); 47 Bind(wxEVT_MENU, &TrackerFrame::OnConnect, this, ID_CONNECT);
48 Bind(wxEVT_MENU, &TrackerFrame::OnCheckForUpdates, this,
49 ID_CHECK_FOR_UPDATES);
41 Bind(STATE_CHANGED, &TrackerFrame::OnStateChanged, this); 50 Bind(STATE_CHANGED, &TrackerFrame::OnStateChanged, this);
42 Bind(STATUS_CHANGED, &TrackerFrame::OnStatusChanged, this); 51 Bind(STATUS_CHANGED, &TrackerFrame::OnStatusChanged, this);
43 52
44 tracker_panel_ = new TrackerPanel(this); 53 tracker_panel_ = new TrackerPanel(this);
54
55 if (!GetTrackerConfig().asked_to_check_for_updates) {
56 GetTrackerConfig().asked_to_check_for_updates = true;
57
58 if (wxMessageBox(
59 "Check for updates automatically when the tracker is opened?",
60 "Lingo AP Tracker", wxYES_NO) == wxYES) {
61 GetTrackerConfig().should_check_for_updates = true;
62 } else {
63 GetTrackerConfig().should_check_for_updates = false;
64 }
65
66 GetTrackerConfig().Save();
67 }
68
69 if (GetTrackerConfig().should_check_for_updates) {
70 CheckForUpdates(/*manual=*/false);
71 }
45} 72}
46 73
47void TrackerFrame::SetStatusMessage(std::string message) { 74void TrackerFrame::SetStatusMessage(std::string message) {
@@ -56,8 +83,12 @@ void TrackerFrame::UpdateIndicators() {
56} 83}
57 84
58void TrackerFrame::OnAbout(wxCommandEvent &event) { 85void TrackerFrame::OnAbout(wxCommandEvent &event) {
59 wxMessageBox("Lingo Archipelago Tracker by hatkirby", 86 std::ostringstream message_text;
60 "About lingo-ap-tracker", wxOK | wxICON_INFORMATION); 87 message_text << "Lingo Archipelago Tracker " << kTrackerVersion
88 << " by hatkirby";
89
90 wxMessageBox(message_text.str(), "About lingo-ap-tracker",
91 wxOK | wxICON_INFORMATION);
61} 92}
62 93
63void TrackerFrame::OnExit(wxCommandEvent &event) { Close(true); } 94void TrackerFrame::OnExit(wxCommandEvent &event) { Close(true); }
@@ -76,6 +107,10 @@ void TrackerFrame::OnConnect(wxCommandEvent &event) {
76 } 107 }
77} 108}
78 109
110void TrackerFrame::OnCheckForUpdates(wxCommandEvent &event) {
111 CheckForUpdates(/*manual=*/true);
112}
113
79void TrackerFrame::OnStateChanged(wxCommandEvent &event) { 114void TrackerFrame::OnStateChanged(wxCommandEvent &event) {
80 tracker_panel_->UpdateIndicators(); 115 tracker_panel_->UpdateIndicators();
81 Refresh(); 116 Refresh();
@@ -84,3 +119,63 @@ void TrackerFrame::OnStateChanged(wxCommandEvent &event) {
84void TrackerFrame::OnStatusChanged(wxCommandEvent &event) { 119void TrackerFrame::OnStatusChanged(wxCommandEvent &event) {
85 SetStatusText(event.GetString()); 120 SetStatusText(event.GetString());
86} 121}
122
123void TrackerFrame::CheckForUpdates(bool manual) {
124 wxWebRequest request = wxWebSession::GetDefault().CreateRequest(
125 this,
126 "https://api.github.com/repos/hatkirby/lingo-ap-tracker/"
127 "releases?per_page=8");
128
129 if (!request.IsOk()) {
130 if (manual) {
131 wxMessageBox("Could not check for updates.", "Error",
132 wxOK | wxICON_ERROR);
133 } else {
134 SetStatusText("Could not check for updates.");
135 }
136
137 return;
138 }
139
140 Bind(wxEVT_WEBREQUEST_STATE, [this, manual](wxWebRequestEvent &evt) {
141 if (evt.GetState() == wxWebRequest::State_Completed) {
142 std::string response = evt.GetResponse().AsString().ToStdString();
143 nlohmann::json parsed_response = nlohmann::json::parse(response);
144
145 if (parsed_response.is_array() && !parsed_response.empty()) {
146 // This will parse to 0.0.0 if it's invalid, which will always be older
147 // than our current version.
148 Version latest_version(
149 parsed_response[0]["tag_name"].get<std::string>());
150 if (kTrackerVersion < latest_version) {
151 std::ostringstream message_text;
152 message_text << "There is a newer version of Lingo AP Tracker "
153 "available. You have "
154 << kTrackerVersion << ", and the latest version is "
155 << latest_version << ". Would you like to update?";
156
157 if (wxMessageBox(message_text.str(), "Update available", wxYES_NO) ==
158 wxYES) {
159 wxLaunchDefaultBrowser(
160 parsed_response[0]["html_url"].get<std::string>());
161 }
162 } else if (manual) {
163 wxMessageBox("Lingo AP Tracker is up to date!", "Lingo AP Tracker",
164 wxOK);
165 }
166 } else if (manual) {
167 wxMessageBox("Lingo AP Tracker is up to date!", "Lingo AP Tracker",
168 wxOK);
169 }
170 } else if (evt.GetState() == wxWebRequest::State_Failed) {
171 if (manual) {
172 wxMessageBox("Could not check for updates.", "Error",
173 wxOK | wxICON_ERROR);
174 } else {
175 SetStatusText("Could not check for updates.");
176 }
177 }
178 });
179
180 request.Start();
181}