Skip to content

Commit

Permalink
Merge pull request #61 from roboalex2/feature/only-ask-once
Browse files Browse the repository at this point in the history
update dialog offers to be reminded in 24h
  • Loading branch information
patschuh authored Mar 27, 2024
2 parents ceffece + 3df668f commit 75225df
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 34 deletions.
47 changes: 47 additions & 0 deletions src/main/java/at/esque/kafka/alerts/UpdateAlert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package at.esque.kafka.alerts;

import at.esque.kafka.Main;
import at.esque.kafka.alerts.model.UpdateDialogResult;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.stage.Window;

import java.util.Optional;

public class UpdateAlert {
public static UpdateDialogResult show(String title, String header, String content) {
return show(title, header, content, null);
}

public static UpdateDialogResult show(String title, String header, String content, Window owner) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle(title);
alert.setHeaderText(header);
alert.setContentText(content);
Main.applyIcon(alert);

ButtonType open = new ButtonType("Open", ButtonBar.ButtonData.FINISH);
ButtonType askAgain = new ButtonType("Ask in 24h again", ButtonBar.ButtonData.OK_DONE);
ButtonType cancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);

alert.getButtonTypes().setAll(open, askAgain, cancel);


Main.applyStylesheet(alert.getDialogPane().getScene());

Node okButton = alert.getDialogPane().lookupButton(open);
okButton.getStyleClass().add("primary");

if (owner != null) {
alert.initOwner(owner);
}

Optional<ButtonType> result = alert.showAndWait();
return switch (result.get().getButtonData()) {
case FINISH -> UpdateDialogResult.OPEN;
case OK_DONE -> UpdateDialogResult.REMIND_LATER;
case CANCEL_CLOSE -> UpdateDialogResult.CANCEL;
default -> UpdateDialogResult.CANCEL;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package at.esque.kafka.alerts.model;

public enum UpdateDialogResult {
OPEN,
CANCEL,
REMIND_LATER
}
79 changes: 45 additions & 34 deletions src/main/java/at/esque/kafka/handlers/VersionInfoHandler.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package at.esque.kafka.handlers;

import at.esque.kafka.alerts.ConfirmationAlert;
import at.esque.kafka.alerts.ErrorAlert;
import at.esque.kafka.alerts.*;
import at.esque.kafka.alerts.model.UpdateDialogResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.Inject;
import com.google.inject.Singleton;
Expand All @@ -19,9 +19,8 @@
import java.io.InputStream;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.time.temporal.ChronoUnit;
import java.util.*;

import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;

Expand Down Expand Up @@ -69,37 +68,37 @@ private Map<String, Object> checkLatestVersion() {
final Map<String, Object> versionCheckContent = configHandler.getVersionCheckContent();
if (versionCheckContent == null || Duration.between(Instant.parse((String) versionCheckContent.get("checkTime")), Instant.now()).toHours() > Long.parseLong(configHandler.getSettingsProperties().get(Settings.CHECK_FOR_UPDATES_DURATION_BETWEEN_HOURS))) {
final ConnectionSpec connectionSpec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.cipherSuites(
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
CipherSuite.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_RSA_WITH_AES_256_GCM_SHA384,
CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA256,
CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA256,
CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA
).build();
.tlsVersions(TlsVersion.TLS_1_2)
.cipherSuites(
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
CipherSuite.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_RSA_WITH_AES_256_GCM_SHA384,
CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA256,
CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA256,
CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_RSA_WITH_AES_256_CBC_SHA
).build();

OkHttpClient client = new OkHttpClient.Builder()
.connectionSpecs(List.of(connectionSpec))
.build();
.connectionSpecs(List.of(connectionSpec))
.build();

Request request = new Request.Builder()
.url(GITHUB_LATEST_RELEASE_URL)
.addHeader("User-Agent", "patschuh/KafkaEsque")
.method("GET", null)
.build();
.url(GITHUB_LATEST_RELEASE_URL)
.addHeader("User-Agent", "patschuh/KafkaEsque")
.method("GET", null)
.build();


Call call = client.newCall(request);
Expand Down Expand Up @@ -127,15 +126,27 @@ private Map<String, Object> checkLatestVersion() {
}

public void showDialogIfUpdateIsAvailable(HostServices hostServices) {
final String askLaterFieldName = "showUpdateDialogAgainTimestamp";
int showUpdateDialogTimestamp = Optional.ofNullable(configHandler.getVersionCheckContent())
.map(el -> (Integer) el.get(askLaterFieldName))
.orElse(0);
if (showUpdateDialogTimestamp > Instant.now().getEpochSecond()) {
return;
}

final UpdateInfo updateInfo = availableUpdate();
if (updateInfo != null) {
final boolean openInBrowser = ConfirmationAlert.show("Update Available", "Version " + updateInfo.getTag() + " is available", "Do you want to open the release page?");
if (openInBrowser) {
final UpdateDialogResult action = UpdateAlert.show("Update Available", "Version " + updateInfo.getTag() + " is available", "Do you want to open the release page?");
if (UpdateDialogResult.OPEN.equals(action)) {
try {
hostServices.showDocument(updateInfo.getReleasePage());
} catch (Exception e) {
ErrorAlert.show(e);
}
} else if (UpdateDialogResult.REMIND_LATER.equals(action)) {
Map<String, Object> versionCheckContent = Optional.ofNullable(configHandler.getVersionCheckContent()).orElse(new HashMap<>());
versionCheckContent.put(askLaterFieldName, Instant.now().plus(1, ChronoUnit.DAYS).getEpochSecond());
configHandler.writeVersionCheckContent(versionCheckContent);
}
}
}
Expand Down

0 comments on commit 75225df

Please sign in to comment.