Skip to content

Commit 3616eb4

Browse files
committed
* Actually check for updates
1 parent a1ee1a9 commit 3616eb4

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

src/main/java/com/codedead/opal/OpalApplication.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,13 @@ public void start(final Stage primaryStage) {
9393
logger.info("Creating the MainWindowController");
9494

9595
final MainWindowController mainWindowController = loader.getController();
96-
mainWindowController.setControllers(
97-
settingsController,
98-
new UpdateController(properties.getProperty("updateApi", "https://codedead.com/Software/Opal/version.json"))
96+
mainWindowController.setControllers(settingsController, new UpdateController(properties.getProperty("updateApi", "https://codedead.com/Software/Opal/version.json"), properties.getProperty("currentVersion", "1.0.0.0"))
9997
);
10098

10199
final Scene scene = new Scene(root);
102100

103101
primaryStage.setTitle(translationBundle.getString("MainWindowTitle"));
104-
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream(SharedVariables.ICON_URL)));
102+
primaryStage.getIcons().add(new Image(Objects.requireNonNull(getClass().getResourceAsStream(SharedVariables.ICON_URL))));
105103
primaryStage.setScene(scene);
106104

107105
logger.info("Showing the MainWindow");

src/main/java/com/codedead/opal/controller/UpdateController.java

+71-3
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,26 @@
2424
public final class UpdateController {
2525

2626
private String updateUrl;
27+
private String currentVersion;
2728

2829
private final Logger logger;
2930
private final ObjectMapper objectMapper;
3031

3132
/**
3233
* Initialize a new UpdateController
3334
*
34-
* @param updateUrl The URL that can be used to check for updates
35+
* @param updateUrl The URL that can be used to check for updates
36+
* @param currentVersion The current application version
3537
*/
36-
public UpdateController(final String updateUrl) {
38+
public UpdateController(final String updateUrl, final String currentVersion) {
3739
logger = LogManager.getLogger(UpdateController.class);
3840
logger.info("Initializing new UpdateController object");
3941

4042
objectMapper = new ObjectMapper();
4143
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
4244

4345
setUpdateUrl(updateUrl);
46+
setCurrentVersion(currentVersion);
4447
}
4548

4649
/**
@@ -63,10 +66,52 @@ public Optional<PlatformUpdate> checkForUpdates(final String currentPlatform, fi
6366

6467
final List<PlatformUpdate> updates = getUpdates();
6568

66-
return updates
69+
final Optional<PlatformUpdate> update = updates
6770
.stream()
6871
.filter(e -> e.getPlatformName().equalsIgnoreCase(currentPlatform) && e.isPortable() == isPortable)
6972
.findFirst();
73+
74+
if (update.isPresent()) {
75+
final PlatformUpdate platformUpdate = update.get();
76+
if (versionCompare(currentVersion, platformUpdate.getMajorVersion() + "." + platformUpdate.getMinorVersion() + "." + platformUpdate.getBuildVersion() + "." + platformUpdate.getRevisionVersion()) < 0) {
77+
return update;
78+
}
79+
return Optional.empty();
80+
}
81+
return Optional.empty();
82+
}
83+
84+
/**
85+
* Compare to strings to check which version is larger
86+
*
87+
* @param v1 The first {@link String} object that contains a version
88+
* @param v2 The second {@link String} object that contains a version
89+
* @return 1 if v1 is larger than v2, -1 if v2 is larger than v1 and 0 if both are equal
90+
*/
91+
private int versionCompare(String v1, String v2) {
92+
int vnum1 = 0;
93+
int vnum2 = 0;
94+
95+
for (int i = 0, j = 0; (i < v1.length() || j < v2.length()); ) {
96+
while (i < v1.length() && v1.charAt(i) != '.') {
97+
vnum1 = vnum1 * 10 + (v1.charAt(i) - '0');
98+
i++;
99+
}
100+
while (j < v2.length() && v2.charAt(j) != '.') {
101+
vnum2 = vnum2 * 10 + (v2.charAt(j) - '0');
102+
j++;
103+
}
104+
105+
if (vnum1 > vnum2)
106+
return 1;
107+
if (vnum2 > vnum1)
108+
return -1;
109+
110+
vnum1 = vnum2 = 0;
111+
i++;
112+
j++;
113+
}
114+
return 0;
70115
}
71116

72117
/**
@@ -144,4 +189,27 @@ public void setUpdateUrl(final String updateUrl) {
144189

145190
this.updateUrl = updateUrl;
146191
}
192+
193+
/**
194+
* Get the current version
195+
*
196+
* @return The current version
197+
*/
198+
public String getCurrentVersion() {
199+
return currentVersion;
200+
}
201+
202+
/**
203+
* Set the current version
204+
*
205+
* @param currentVersion The current version
206+
*/
207+
public void setCurrentVersion(final String currentVersion) {
208+
if (currentVersion == null)
209+
throw new NullPointerException("currentVersion cannot be null!");
210+
if (currentVersion.isEmpty())
211+
throw new IllegalArgumentException("currentVersion cannot be empty!");
212+
213+
this.currentVersion = currentVersion;
214+
}
147215
}

src/main/resources/default.properties

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ portable=false
55
loglevel=INFO
66
timerDelay=3600000
77
timerDelayType=2
8+
currentVersion=1.0.0.0

0 commit comments

Comments
 (0)