Skip to content
Snippets Groups Projects
Commit 82d2126b authored by Robert Goldmann's avatar Robert Goldmann
Browse files

#132 - implemented download of latest updater and latest version

parent 01a8a8cb
Branches
Tags
No related merge requests found
package de.deadlocker8.budgetmaster.logic; package de.deadlocker8.budgetmaster.logic.updater;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList; import java.util.ArrayList;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import de.deadlocker8.budgetmaster.logic.updater.VersionInformation; import de.deadlocker8.budgetmaster.logic.utils.Strings;
import logger.Logger;
import tools.Localization;
import tools.PathUtils;
public class Updater public class Updater
{ {
private VersionInformation latestVersion; private VersionInformation latestVersion;
private static final String LATEST_VERSION_INFO_URL = "https://raw.githubusercontent.com/deadlocker8/BudgetMaster/master/src/de/deadlocker8/budgetmaster/resources/languages/_de.properties"; private static final String LATEST_VERSION_INFO_URL = "https://raw.githubusercontent.com/deadlocker8/BudgetMaster/master/src/de/deadlocker8/budgetmaster/resources/languages/_de.properties";
private static final String CHANGELOG_URL = "https://raw.githubusercontent.com/deadlocker8/BudgetMaster/master/src/de/deadlocker8/budgetmaster/resources/changelog.json"; private static final String CHANGELOG_URL = "https://raw.githubusercontent.com/deadlocker8/BudgetMaster/master/src/de/deadlocker8/budgetmaster/resources/changelog.json";
private static final String BUILD_FOLDER = "https://github.com/deadlocker8/BudgetMaster/raw/master/build/";
public Updater() public Updater()
{ {
...@@ -102,4 +112,64 @@ public class Updater ...@@ -102,4 +112,64 @@ public class Updater
} }
return null; return null;
} }
private void downloadLatestUpdater() throws IOException
{
//download into temp directory and file
Path target = Paths.get(PathUtils.getOSindependentPath() + Localization.getString(Strings.FOLDER) + "/Updater.jar");
download(BUILD_FOLDER + "Updater.jar", target);
}
private File getCurrentExecutableName()
{
return new File(Updater.class.getProtectionDomain().getCodeSource().getLocation().getPath());
}
public void downloadLatestVersion() throws IOException
{
File currentExecutable = getCurrentExecutableName();
File currentFolder = currentExecutable.getParentFile();
String currentFileName = currentExecutable.getName();
String fileEnding;
//check if BudgetMaster is running from executable
//no updating procedure if running from source
if(currentFileName.contains("."))
{
fileEnding = currentExecutable.getAbsolutePath().substring(currentExecutable.getAbsolutePath().indexOf("."), currentExecutable.getAbsolutePath().length());
}
else
{
Logger.debug("Update procedure will be skipped because BudgetMaster is running from source");
return;
}
PathUtils.checkFolder(new File(PathUtils.getOSindependentPath() + Localization.getString(Strings.FOLDER)));
//download latest updater.jar
downloadLatestUpdater();
//download into temp directory and file
if(fileEnding.equalsIgnoreCase("exe"))
{
Path target = Paths.get(PathUtils.getOSindependentPath() + Localization.getString(Strings.FOLDER) + "/update_BudgetMaster.exe");
download(BUILD_FOLDER + "BudgetMaster.exe", target);
}
else
{
Path target = Paths.get(PathUtils.getOSindependentPath() + Localization.getString(Strings.FOLDER) + "/update_BudgetMasterClient.jar");
download(BUILD_FOLDER + "BudgetMasterClient.jar", target);
}
//TODO start upater with params
//--> move temp jar/exe to currentFolder with currentFileName
}
public void download(String url, Path target) throws IOException
{
URL website = new URL(url);
InputStream in = website.openStream();
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
}
} }
\ No newline at end of file
...@@ -41,6 +41,7 @@ public class Strings ...@@ -41,6 +41,7 @@ public class Strings
public static final String LOAD_DATABASE_EXPORT = "load.database.export"; public static final String LOAD_DATABASE_EXPORT = "load.database.export";
public static final String LOAD_DATABASE_IMPORT = "load.database.import"; public static final String LOAD_DATABASE_IMPORT = "load.database.import";
public static final String LOAD_DATABASE_DELETE = "load.database.delete"; public static final String LOAD_DATABASE_DELETE = "load.database.delete";
public static final String LOAD_UPDATE = "load.update";
//MISC //MISC
public static final String CATEGORY_NONE = "category.none"; public static final String CATEGORY_NONE = "category.none";
...@@ -183,4 +184,5 @@ public class Strings ...@@ -183,4 +184,5 @@ public class Strings
public static final String ERROR_DATABASE_IMPORT_WRONG_FILE = "error.database.import.wrong.file"; public static final String ERROR_DATABASE_IMPORT_WRONG_FILE = "error.database.import.wrong.file";
public static final String ERROR_PASSWORD_SAVE = "error.password.save"; public static final String ERROR_PASSWORD_SAVE = "error.password.save";
public static final String ERROR_UPDATER_GET_LATEST_VERSION = "error.updater.get.latest.version"; public static final String ERROR_UPDATER_GET_LATEST_VERSION = "error.updater.get.latest.version";
public static final String ERROR_UPDATER_DOWNLOAD_LATEST_VERSION = "error.updater.download.latest.version";
} }
\ No newline at end of file
...@@ -39,6 +39,7 @@ load.report=Der Monatsbericht wird erstellt, bitte warten... ...@@ -39,6 +39,7 @@ load.report=Der Monatsbericht wird erstellt, bitte warten...
load.database.export=Die Datenbank wird exportiert, bitte warten... load.database.export=Die Datenbank wird exportiert, bitte warten...
load.database.import=Die Datenbank wird importiert, bitte warten... load.database.import=Die Datenbank wird importiert, bitte warten...
load.database.delete=Die Datenbank wird gelöscht, bitte warten... load.database.delete=Die Datenbank wird gelöscht, bitte warten...
load.update=Update wird heruntergeladen, bitte warten...
# MISC # MISC
category.none=Keine Kategorie category.none=Keine Kategorie
...@@ -180,6 +181,7 @@ error.database.import=Beim Einlesen der Datei ist ein Fehler aufgetreten. ...@@ -180,6 +181,7 @@ error.database.import=Beim Einlesen der Datei ist ein Fehler aufgetreten.
error.database.import.wrong.file=Die angegebene Datei enthält kein gültiges BudgetMaster-Datenformat und kann daher nicht importiert werden. error.database.import.wrong.file=Die angegebene Datei enthält kein gültiges BudgetMaster-Datenformat und kann daher nicht importiert werden.
error.password.save=Beim Speichern des Passworts ist ein Fehler aufgetreten. error.password.save=Beim Speichern des Passworts ist ein Fehler aufgetreten.
error.updater.get.latest.version=Beim Überprüfen auf Updates ist ein Fehler aufgetreten. Informationen zu neuen Versionen konnten nicht abgerufen werden. Versuche es später noch einmal. error.updater.get.latest.version=Beim Überprüfen auf Updates ist ein Fehler aufgetreten. Informationen zu neuen Versionen konnten nicht abgerufen werden. Versuche es später noch einmal.
error.updater.download.latest.version=Beim Herunterladen des Updates ist ein Fehler aufgetreten.\n\n{0}
# UI # UI
categorytab.button.category.new=\ Neue Kategorie categorytab.button.category.new=\ Neue Kategorie
......
...@@ -39,6 +39,7 @@ load.report=Please wait while the report is being generated... ...@@ -39,6 +39,7 @@ load.report=Please wait while the report is being generated...
load.database.export=Please wait while the database is being exported... load.database.export=Please wait while the database is being exported...
load.database.import=Please wait while the database is being imported... load.database.import=Please wait while the database is being imported...
load.database.delete=Please wait while the database is being deleted... load.database.delete=Please wait while the database is being deleted...
load.update=Please wait while the update is being downloaded...
# MISC # MISC
category.none=No Category category.none=No Category
...@@ -180,6 +181,7 @@ error.database.import=An error occurred while reading the file. ...@@ -180,6 +181,7 @@ error.database.import=An error occurred while reading the file.
error.database.import.wrong.file=The specified file does not contain a valid BudgetMaster data format thus can not be imported. error.database.import.wrong.file=The specified file does not contain a valid BudgetMaster data format thus can not be imported.
error.password.save=An error occurred while saving the password. error.password.save=An error occurred while saving the password.
error.updater.get.latest.version=An error occurred while checking for updates. Information about latest versions could not be retrieved. Please try again later. error.updater.get.latest.version=An error occurred while checking for updates. Information about latest versions could not be retrieved. Please try again later.
error.updater.download.latest.version=An error occurred while downloading the update.\n\n{0}
# UI # UI
categorytab.button.category.new=\ New Category categorytab.button.category.new=\ New Category
......
...@@ -12,9 +12,9 @@ import de.deadlocker8.budgetmaster.logic.FilterSettings; ...@@ -12,9 +12,9 @@ import de.deadlocker8.budgetmaster.logic.FilterSettings;
import de.deadlocker8.budgetmaster.logic.NormalPayment; import de.deadlocker8.budgetmaster.logic.NormalPayment;
import de.deadlocker8.budgetmaster.logic.PaymentHandler; import de.deadlocker8.budgetmaster.logic.PaymentHandler;
import de.deadlocker8.budgetmaster.logic.Settings; import de.deadlocker8.budgetmaster.logic.Settings;
import de.deadlocker8.budgetmaster.logic.Updater;
import de.deadlocker8.budgetmaster.logic.serverconnection.ExceptionHandler; import de.deadlocker8.budgetmaster.logic.serverconnection.ExceptionHandler;
import de.deadlocker8.budgetmaster.logic.serverconnection.ServerConnection; import de.deadlocker8.budgetmaster.logic.serverconnection.ServerConnection;
import de.deadlocker8.budgetmaster.logic.updater.Updater;
import de.deadlocker8.budgetmaster.logic.updater.VersionInformation; import de.deadlocker8.budgetmaster.logic.updater.VersionInformation;
import de.deadlocker8.budgetmaster.logic.utils.Colors; import de.deadlocker8.budgetmaster.logic.utils.Colors;
import de.deadlocker8.budgetmaster.logic.utils.Helpers; import de.deadlocker8.budgetmaster.logic.utils.Helpers;
...@@ -101,6 +101,11 @@ public class Controller ...@@ -101,6 +101,11 @@ public class Controller
checkForUpdates(); checkForUpdates();
} }
initUI();
}
private void initUI()
{
try try
{ {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/de/deadlocker8/budgetmaster/ui/fxml/HomeTab.fxml")); FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/de/deadlocker8/budgetmaster/ui/fxml/HomeTab.fxml"));
...@@ -347,11 +352,14 @@ public class Controller ...@@ -347,11 +352,14 @@ public class Controller
try try
{ {
boolean updateAvailable = updater.isUpdateAvailable(Integer.parseInt(Localization.getString(Strings.VERSION_CODE))); boolean updateAvailable = updater.isUpdateAvailable(Integer.parseInt(Localization.getString(Strings.VERSION_CODE)));
String changes = updater.getChangelog(updater.getLatestVersion().getVersionCode()); //DEBUG
//String changes = updater.getChangelog(updater.getLatestVersion().getVersionCode());
String changes = "";
if(!updateAvailable) if(!updateAvailable)
return; return;
Platform.runLater(()->{
Alert alert = new Alert(AlertType.INFORMATION); Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(Localization.getString(Strings.INFO_TITLE_UPDATE_AVAILABLE)); alert.setTitle(Localization.getString(Strings.INFO_TITLE_UPDATE_AVAILABLE));
alert.setHeaderText(""); alert.setHeaderText("");
...@@ -368,14 +376,45 @@ public class Controller ...@@ -368,14 +376,45 @@ public class Controller
Optional<ButtonType> result = alert.showAndWait(); Optional<ButtonType> result = alert.showAndWait();
if (result.get() == buttonTypeOne) if (result.get() == buttonTypeOne)
{ {
//TODO update //TODO download latest updater first
Stage modalStage = Helpers.showModal(Localization.getString(Strings.TITLE_MODAL), Localization.getString(Strings.LOAD_UPDATE), stage, icon);
Worker.runLater(() -> {
try
{
updater.downloadLatestVersion();
Platform.runLater(() -> {
if(modalStage != null)
{
modalStage.close();
}
});
}
catch(IOException ex)
{
Logger.error(ex);
Platform.runLater(() -> {
if(modalStage != null)
{
modalStage.close();
AlertGenerator.showAlert(AlertType.ERROR,
Localization.getString(Strings.TITLE_ERROR),
"",
Localization.getString(Strings.ERROR_UPDATER_DOWNLOAD_LATEST_VERSION, ex.getMessage()),
icon, null, null, true);
}
});
}
});
} }
else else
{ {
alert.close(); alert.close();
} }
});
} }
catch(NumberFormatException | IOException e) catch(IOException e)
{ {
Logger.error(e); Logger.error(e);
AlertGenerator.showAlert(AlertType.ERROR, AlertGenerator.showAlert(AlertType.ERROR,
...@@ -450,7 +489,6 @@ public class Controller ...@@ -450,7 +489,6 @@ public class Controller
categoryBudgets = connection.getCategoryBudgets(currentDate.getYear(), currentDate.getMonthOfYear()); categoryBudgets = connection.getCategoryBudgets(currentDate.getYear(), currentDate.getMonthOfYear());
paymentHandler.filter(newFilterSettings); paymentHandler.filter(newFilterSettings);
Platform.runLater(() -> { Platform.runLater(() -> {
if(modalStage != null) if(modalStage != null)
{ {
......
...@@ -6,9 +6,9 @@ import java.util.ArrayList; ...@@ -6,9 +6,9 @@ import java.util.ArrayList;
import java.util.Optional; import java.util.Optional;
import de.deadlocker8.budgetmaster.logic.Settings; import de.deadlocker8.budgetmaster.logic.Settings;
import de.deadlocker8.budgetmaster.logic.Updater;
import de.deadlocker8.budgetmaster.logic.serverconnection.ExceptionHandler; import de.deadlocker8.budgetmaster.logic.serverconnection.ExceptionHandler;
import de.deadlocker8.budgetmaster.logic.serverconnection.ServerConnection; import de.deadlocker8.budgetmaster.logic.serverconnection.ServerConnection;
import de.deadlocker8.budgetmaster.logic.updater.Updater;
import de.deadlocker8.budgetmaster.logic.utils.Colors; import de.deadlocker8.budgetmaster.logic.utils.Colors;
import de.deadlocker8.budgetmaster.logic.utils.FileHelper; import de.deadlocker8.budgetmaster.logic.utils.FileHelper;
import de.deadlocker8.budgetmaster.logic.utils.Helpers; import de.deadlocker8.budgetmaster.logic.utils.Helpers;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment