85 lines
2.7 KiB
Java

package update;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.Optional;
import guis.MainGui;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import main.Start;
import manager.SettingManager;
public class Updater {
public void checkForUpdate(){
UpdateChecker uc = new UpdateChecker();
uc.requestServerData();
System.out.println("got version: " + uc.getLatestVersion());
if(uc.getLatestVersion() > Start.VERSION){
Platform.runLater(new Runnable() {
@Override
public void run() {
String dl = "https://cookiestudios.org/software/QuickLaunch/QuickLaunch.jar";
Alert updateAlert = new Alert(AlertType.INFORMATION,
"There is a newer version of QuickLaunch available\nDownload now?",
ButtonType.YES,
ButtonType.NO);
updateAlert.setTitle("Update available!");
Optional<ButtonType> result = updateAlert.showAndWait();
if(result.isPresent() && result.get() == ButtonType.YES){
try {
System.out.println("here");
cleanDirectory();
URL website = new URL(dl);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(SettingManager.getJarDirectory()+File.separator+"Squirrel.jar");
fos.getChannel().transferFrom(rbc, 0, Integer.MAX_VALUE);
fos.close();
try {
ProcessBuilder pb = new ProcessBuilder("java","-jar",SettingManager.getJarDirectory()+File.separator+"Squirrel.jar");
pb.directory(new File(SettingManager.getJarDirectory()));
pb.redirectErrorStream(true);
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.start();
}catch(Exception e) {
e.printStackTrace();
}
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
MainGui.addNotification("Couldn't download update", 2);
}
}
}
});
} else{
if(uc.getLatestVersion() == Start.VERSION){
MainGui.addNotification("QuickLaunch is up to date", 2);
}
else {
MainGui.addNotification("Update server unavailable", 2);
}
new UpdateFileHandler().launch();
}
}
private void cleanDirectory(){
File squirrel = new File(SettingManager.getJarDirectory()+File.separator+"Squirrel.jar");
if(squirrel.exists()){
squirrel.delete();
}
File fMark = new File(SettingManager.getJarDirectory()+File.separator+"f.MARK");
if(fMark.exists()){
fMark.delete();
}
}
}