Skip to content

Commit 1819d04

Browse files
committed
Improved update nag.
1 parent f6c3fc3 commit 1819d04

File tree

2 files changed

+158
-59
lines changed

2 files changed

+158
-59
lines changed

SerialPrograms/Source/CommonFramework/Options/CheckForUpdatesOption.h

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "Common/Cpp/Options/GroupOption.h"
1111
#include "Common/Cpp/Options/BooleanCheckBoxOption.h"
12+
#include "Common/Cpp/Options/StringOption.h"
1213
#include "CommonFramework/Globals.h"
1314

1415
namespace PokemonAutomation{
@@ -21,35 +22,44 @@ class CheckForUpdatesOption : public GroupOption{
2122
"Check for Updates",
2223
LockMode::UNLOCK_WHILE_RUNNING,
2324
EnableMode::ALWAYS_ENABLED,
24-
false
25+
true
2526
)
26-
, RELEASE(
27+
, RELEASE0(
2728
"<b>New Releases:</b><br>Automatically check for new stable releases.",
2829
LockMode::UNLOCK_WHILE_RUNNING,
2930
true
3031
)
31-
, BETA(
32+
, PUBLIC_BETA0(
3233
"<b>Public Betas:</b><br>Automatically check for new public betas.",
3334
LockMode::UNLOCK_WHILE_RUNNING,
3435
IS_BETA_VERSION
3536
)
36-
, PRIVATE_BETA(
37+
, PRIVATE_BETA0(
3738
"<b>Private Betas:</b><br>Automatically check for new private betas.",
3839
LockMode::UNLOCK_WHILE_RUNNING,
3940
IS_BETA_VERSION
4041
)
42+
,SKIP_VERSION(
43+
false,
44+
"<b>Skip this Version:</b><br>Skip this version and don't notify until the next update.",
45+
LockMode::UNLOCK_WHILE_RUNNING,
46+
"",
47+
"0.50.0"
48+
)
4149
{
42-
PA_ADD_OPTION(RELEASE);
43-
PA_ADD_OPTION(BETA);
50+
PA_ADD_OPTION(RELEASE0);
51+
PA_ADD_OPTION(PUBLIC_BETA0);
4452
if (IS_BETA_VERSION){
45-
PA_ADD_OPTION(PRIVATE_BETA);
53+
PA_ADD_OPTION(PRIVATE_BETA0);
4654
}
55+
PA_ADD_OPTION(SKIP_VERSION);
4756
}
4857

4958
public:
50-
BooleanCheckBoxOption RELEASE;
51-
BooleanCheckBoxOption BETA;
52-
BooleanCheckBoxOption PRIVATE_BETA;
59+
BooleanCheckBoxOption RELEASE0;
60+
BooleanCheckBoxOption PUBLIC_BETA0;
61+
BooleanCheckBoxOption PRIVATE_BETA0;
62+
StringOption SKIP_VERSION;
5363
};
5464

5565

SerialPrograms/Source/CommonFramework/Startup/NewVersionCheck.cpp

Lines changed: 138 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
*/
66

7+
#include <QPushButton>
78
#include <QMessageBox>
89
#include "Common/Cpp/Time.h"
910
#include "Common/Cpp/Json/JsonObject.h"
@@ -25,59 +26,161 @@ namespace PokemonAutomation{
2526
static auto CHECK_FOR_UPDATES_PERIOD = std::chrono::days(1);
2627

2728

29+
struct ProgramVersion{
30+
int major;
31+
int minor;
32+
int patch;
33+
std::string version_text;
34+
35+
ProgramVersion(Logger& logger, const JsonObject* json){
36+
if (!json->read_integer(major, "VersionMajor")){
37+
throw_and_log<ParseException>(logger, "Invalid version JSON: Missing major version.");
38+
}
39+
if (!json->read_integer(minor, "VersionMinor")){
40+
throw_and_log<ParseException>(logger, "Invalid version JSON: Missing minor version.");
41+
}
42+
if (!json->read_integer(patch, "VersionPatch")){
43+
throw_and_log<ParseException>(logger, "Invalid version JSON: Missing patch #.");
44+
}
45+
version_text = std::to_string(major)
46+
+ "." + std::to_string(minor)
47+
+ "." + std::to_string(patch);
48+
}
49+
50+
bool is_newer() const{
51+
if (major > PROGRAM_VERSION_MAJOR){
52+
return true;
53+
}
54+
if (major < PROGRAM_VERSION_MAJOR){
55+
return false;
56+
}
57+
58+
if (minor > PROGRAM_VERSION_MINOR){
59+
return true;
60+
}
61+
if (minor < PROGRAM_VERSION_MINOR){
62+
return false;
63+
}
64+
65+
if (patch > PROGRAM_VERSION_PATCH){
66+
return true;
67+
}
68+
69+
return false;
70+
}
71+
72+
bool show_update_nag() const{
73+
if (!is_newer()){
74+
return false;
75+
}
76+
std::string skip_version = GlobalSettings::instance().CHECK_FOR_UPDATES->SKIP_VERSION;
77+
return version_text != skip_version;
78+
}
79+
};
80+
2881
bool compare_version(Logger& logger, const JsonObject* json){
2982
if (json == nullptr){
3083
logger.log("Invalid version JSON.", COLOR_RED);
3184
return false;
3285
}
3386

3487
int major;
88+
int minor;
89+
int patch;
3590
if (!json->read_integer(major, "VersionMajor")){
3691
logger.log("Invalid version JSON: Missing major version.", COLOR_RED);
3792
return false;
3893
}
94+
if (!json->read_integer(minor, "VersionMinor")){
95+
logger.log("Invalid version JSON: Missing minor version.", COLOR_RED);
96+
return false;
97+
}
98+
if (!json->read_integer(patch, "VersionPatch")){
99+
logger.log("Invalid version JSON: Missing patch #.", COLOR_RED);
100+
return false;
101+
}
102+
std::string full_version = std::to_string(major)
103+
+ "." + std::to_string(minor)
104+
+ "." + std::to_string(patch);
105+
std::string skip_version = GlobalSettings::instance().CHECK_FOR_UPDATES->SKIP_VERSION;
106+
if (full_version == skip_version){
107+
return false;
108+
}
109+
39110
if (major > PROGRAM_VERSION_MAJOR){
40111
return true;
41112
}
42113
if (major < PROGRAM_VERSION_MAJOR){
43114
return false;
44115
}
45116

46-
int minor;
47-
if (!json->read_integer(minor, "VersionMinor")){
48-
logger.log("Invalid version JSON: Missing minor version.", COLOR_RED);
49-
return false;
50-
}
51117
if (minor > PROGRAM_VERSION_MINOR){
52118
return true;
53119
}
54120
if (minor < PROGRAM_VERSION_MINOR){
55121
return false;
56122
}
57123

58-
int patch;
59-
if (!json->read_integer(patch, "VersionPatch")){
60-
logger.log("Invalid version JSON: Missing patch #.", COLOR_RED);
61-
return false;
62-
}
63124
if (patch > PROGRAM_VERSION_PATCH){
64125
return true;
65126
}
66127

67128
return false;
68129
}
69-
std::string get_changes(const JsonObject* json){
70-
const std::string* changes = json->get_string("Changes");
130+
std::string get_changes(const JsonObject& json){
131+
const std::string* changes = json.get_string("Changes");
71132
if (changes == nullptr){
72133
return "";
73134
}
74135
return "<br><br>" + *changes;
75136
}
137+
void show_update_box(
138+
const std::string& title,
139+
const std::string& link_text,
140+
const std::string& link_url,
141+
const std::string& header,
142+
const ProgramVersion& version,
143+
const JsonObject& node
144+
){
145+
QMessageBox box;
146+
QPushButton* ok = box.addButton(QMessageBox::Ok);
147+
QPushButton* skip = box.addButton("Skip this version.", QMessageBox::NoRole);
148+
149+
box.setTextFormat(Qt::RichText);
150+
std::string text = header + "<br>";
151+
text += make_text_url(link_url, link_text);
152+
text += get_changes(node);
153+
154+
155+
box.setWindowTitle(QString::fromStdString(title));
156+
box.setText(QString::fromStdString(text));
157+
158+
// box.open();
159+
160+
box.exec();
161+
162+
QAbstractButton* clicked = box.clickedButton();
163+
if (clicked == ok){
164+
return;
165+
}
166+
if (clicked == skip){
167+
GlobalSettings::instance().CHECK_FOR_UPDATES->SKIP_VERSION.set(version.version_text);
168+
return;
169+
}
170+
171+
172+
// box.information(
173+
// nullptr,
174+
// QString::fromStdString(title),
175+
// QString::fromStdString(text)
176+
// );
177+
}
178+
76179

77180
void check_new_version(Logger& logger){
78-
bool check_release = GlobalSettings::instance().CHECK_FOR_UPDATES->RELEASE;
79-
bool check_beta = GlobalSettings::instance().CHECK_FOR_UPDATES->BETA;
80-
bool check_private_beta = GlobalSettings::instance().CHECK_FOR_UPDATES->PRIVATE_BETA;
181+
bool check_release = GlobalSettings::instance().CHECK_FOR_UPDATES->RELEASE0;
182+
bool check_beta = GlobalSettings::instance().CHECK_FOR_UPDATES->PUBLIC_BETA0;
183+
bool check_private_beta = GlobalSettings::instance().CHECK_FOR_UPDATES->PRIVATE_BETA0;
81184
bool updates_enabled = false;
82185
updates_enabled |= check_release;
83186
updates_enabled |= check_beta;
@@ -110,61 +213,47 @@ void check_new_version(Logger& logger){
110213
return;
111214
}
112215

216+
113217
// cout << json.dump() << endl;
114218

115219
if (check_private_beta){
116220
const JsonObject* node = obj->get_object("PrivateBeta");
117-
if (compare_version(logger, node)){
118-
QMessageBox box;
119-
box.setTextFormat(Qt::RichText);
120-
std::string text = "A new private beta is available!<br>";
121-
text += make_text_url(
221+
ProgramVersion latest(logger, node);
222+
if (latest.show_update_nag()){
223+
show_update_box(
224+
"New Private Beta Available!",
225+
"Download from our Discord!",
122226
"https://discord.com/channels/695809740428673034/732736538965704726",
123-
"Download from our Discord!"
124-
);
125-
text += get_changes(node);
126-
box.information(
127-
nullptr,
128227
"A new private beta is available!",
129-
QString::fromStdString(text)
228+
latest, *node
130229
);
131230
return;
132231
}
133232
}
134233
if (check_beta){
135234
const JsonObject* node = obj->get_object("Beta");
136-
if (compare_version(logger, node)){
137-
QMessageBox box;
138-
box.setTextFormat(Qt::RichText);
139-
std::string text = "A new beta is available!<br>";
140-
text += make_text_url(
235+
ProgramVersion latest(logger, node);
236+
if (latest.show_update_nag()){
237+
show_update_box(
238+
"New Beta Available!",
239+
"Download here.",
141240
"https://github.com/PokemonAutomation/ComputerControl/releases",
142-
"Download here."
143-
);
144-
text += get_changes(node);
145-
box.information(
146-
nullptr,
147241
"A new beta is available!",
148-
QString::fromStdString(text)
242+
latest, *node
149243
);
150244
return;
151245
}
152246
}
153247
if (check_release){
154248
const JsonObject* node = obj->get_object("Release");
155-
if (compare_version(logger, node)){
156-
QMessageBox box;
157-
box.setTextFormat(Qt::RichText);
158-
std::string text = "A new version is available!<br>";
159-
text += make_text_url(
160-
"https://github.com/PokemonAutomation/ComputerControl/releases",
161-
"Download here."
162-
);
163-
text += get_changes(node);
164-
box.information(
165-
nullptr,
249+
ProgramVersion latest(logger, node);
250+
if (latest.show_update_nag()){
251+
show_update_box(
166252
"New Version Available!",
167-
QString::fromStdString(text)
253+
"Download here.",
254+
"https://github.com/PokemonAutomation/ComputerControl/releases",
255+
"A new version is available!",
256+
latest, *node
168257
);
169258
return;
170259
}

0 commit comments

Comments
 (0)