From 4cea480bb7e02bacb73410d794589c7014bbb299 Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Fri, 12 Jun 2020 17:40:17 +0300 Subject: [PATCH] Add Windows drivers installer function --- .../Controllers/SettingsController.java | 44 +++-- src/main/java/nsusbloader/ServiceWindow.java | 3 +- .../WindowsDrivers/DownloadDriversTask.java | 88 ++++++++++ .../WindowsDrivers/DriversInstall.java | 151 ++++++++++++++++++ src/main/resources/SettingsTab.fxml | 5 + src/main/resources/locale.properties | 5 + src/main/resources/locale_deu.properties | 2 + src/main/resources/locale_fra.properties | 2 + src/main/resources/locale_ita.properties | 2 + src/main/resources/locale_por.properties | 2 + src/main/resources/locale_rus.properties | 5 + src/main/resources/locale_spa.properties | 2 + src/main/resources/locale_ukr.properties | 7 +- src/main/resources/locale_vie.properties | 2 + src/main/resources/locale_zho.properties | 2 + src/main/resources/res/app_dark.css | 8 + src/main/resources/res/app_light.css | 7 + src/main/resources/res/dwnload_ico128x128.png | Bin 0 -> 9934 bytes src/main/resources/res/dwnload_ico32x32.png | Bin 0 -> 4394 bytes src/main/resources/res/dwnload_ico48x48.png | Bin 0 -> 8062 bytes src/main/resources/res/dwnload_ico64x64.png | Bin 0 -> 7552 bytes 21 files changed, 317 insertions(+), 20 deletions(-) create mode 100644 src/main/java/nsusbloader/Utilities/WindowsDrivers/DownloadDriversTask.java create mode 100644 src/main/java/nsusbloader/Utilities/WindowsDrivers/DriversInstall.java create mode 100644 src/main/resources/res/dwnload_ico128x128.png create mode 100644 src/main/resources/res/dwnload_ico32x32.png create mode 100644 src/main/resources/res/dwnload_ico48x48.png create mode 100644 src/main/resources/res/dwnload_ico64x64.png diff --git a/src/main/java/nsusbloader/Controllers/SettingsController.java b/src/main/java/nsusbloader/Controllers/SettingsController.java index 8e776a9..5f23072 100644 --- a/src/main/java/nsusbloader/Controllers/SettingsController.java +++ b/src/main/java/nsusbloader/Controllers/SettingsController.java @@ -30,6 +30,7 @@ import javafx.scene.layout.VBox; import nsusbloader.AppPreferences; import nsusbloader.ServiceWindow; import nsusbloader.ModelControllers.UpdatesChecker; +import nsusbloader.Utilities.WindowsDrivers.DriversInstall; import java.io.File; import java.io.IOException; @@ -42,22 +43,16 @@ import java.util.jar.JarFile; public class SettingsController implements Initializable { @FXML - private CheckBox nspFilesFilterForGLCB; - @FXML - private CheckBox validateNSHostNameCb; - @FXML - private CheckBox expertModeCb; - @FXML - private CheckBox autoDetectIpCb; - @FXML - private CheckBox randPortCb; + private CheckBox nspFilesFilterForGLCB, + validateNSHostNameCb, + expertModeCb, + autoDetectIpCb, + randPortCb; @FXML - private TextField pcIpTextField; - @FXML - private TextField pcPortTextField; - @FXML - private TextField pcExtraTextField; + private TextField pcIpTextField, + pcPortTextField, + pcExtraTextField; @FXML private CheckBox dontServeCb; @@ -69,13 +64,14 @@ public class SettingsController implements Initializable { private CheckBox autoCheckUpdCb; @FXML private Hyperlink newVersionLink; - @FXML - private Button checkForUpdBtn; + @FXML private CheckBox tfXciSpprtCb; @FXML - private Button langBtn; + private Button langBtn, + checkForUpdBtn, + drvInstBtn; @FXML private ChoiceBox langCB; @@ -215,6 +211,15 @@ public class SettingsController implements Initializable { updates.setDaemon(true); updates.start(); }); + + if (isWindows()){ + Region btnDrvImage = new Region(); + btnDrvImage.getStyleClass().add("regionWindows"); + drvInstBtn.setGraphic(btnDrvImage); + drvInstBtn.setVisible(true); + drvInstBtn.setOnAction(actionEvent -> new DriversInstall(resourceBundle)); + } + tfXciSpprtCb.setSelected(AppPreferences.getInstance().getTfXCI()); // Language settings area @@ -282,6 +287,11 @@ public class SettingsController implements Initializable { } glOldVerCheck.setOnAction(e-> glOldVerChoice.setDisable(! glOldVerCheck.isSelected()) ); } + + private boolean isWindows(){ + return System.getProperty("os.name").toLowerCase().replace(" ", "").contains("windows"); + } + public boolean getNSPFileFilterForGL(){return nspFilesFilterForGLCB.isSelected(); } public boolean getExpertModeSelected(){ return expertModeCb.isSelected(); } public boolean getAutoIpSelected(){ return autoDetectIpCb.isSelected(); } diff --git a/src/main/java/nsusbloader/ServiceWindow.java b/src/main/java/nsusbloader/ServiceWindow.java index 2461196..0565448 100644 --- a/src/main/java/nsusbloader/ServiceWindow.java +++ b/src/main/java/nsusbloader/ServiceWindow.java @@ -54,9 +54,8 @@ public class ServiceWindow { new Image("/res/warn_ico64x64.png"), new Image("/res/warn_ico128x128.png") ); - dialogStage.toFront(); - alertBox.show(); + dialogStage.toFront(); } /** * Create notification window with confirm/deny diff --git a/src/main/java/nsusbloader/Utilities/WindowsDrivers/DownloadDriversTask.java b/src/main/java/nsusbloader/Utilities/WindowsDrivers/DownloadDriversTask.java new file mode 100644 index 0000000..7239c67 --- /dev/null +++ b/src/main/java/nsusbloader/Utilities/WindowsDrivers/DownloadDriversTask.java @@ -0,0 +1,88 @@ +/* + Copyright 2019-2020 Dmitry Isaenko + + This file is part of NS-USBloader. + + NS-USBloader is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + NS-USBloader is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with NS-USBloader. If not, see . +*/ +package nsusbloader.Utilities.WindowsDrivers; + +import javafx.concurrent.Task; + +import java.io.*; +import java.net.URL; + +public class DownloadDriversTask extends Task { + + private static final String driverFileLocationURL = "https://github.com/developersu/NS-Drivers/releases/download/v1.0/Drivers_set.exe"; + private static final long driversFileSize = 3857375; + + private static File driversInstallerFile; + + @Override + protected String call() { + if (isDriversDownloaded()) + return driversInstallerFile.getAbsolutePath(); + if (downloadDrivers()) + return driversInstallerFile.getAbsolutePath(); + return null; + } + + private boolean isDriversDownloaded(){ + return driversInstallerFile != null && driversInstallerFile.length() == driversFileSize; + } + + private boolean downloadDrivers(){ + try { + File tmpDirectory = File.createTempFile("nsul", null); + if (! tmpDirectory.delete()) + return false; + if (! tmpDirectory.mkdirs()) + return false; + + tmpDirectory.deleteOnExit(); + + URL url = new URL(driverFileLocationURL); + BufferedInputStream bis = new BufferedInputStream(url.openStream()); + + driversInstallerFile = new File(tmpDirectory, "drivers.exe"); + FileOutputStream fos = new FileOutputStream(driversInstallerFile); + + byte[] dataBuffer = new byte[1024]; + int bytesRead; + double totalRead = 0; + + while ((bytesRead = bis.read(dataBuffer, 0, 1024)) != -1) { + fos.write(dataBuffer, 0, bytesRead); + totalRead += bytesRead; + updateProgress(totalRead, driversFileSize); + if (this.isCancelled()) { + bis.close(); + fos.close(); + updateProgress(0, 0); + return false; + } + } + bis.close(); + fos.close(); + + return true; + } + catch (IOException | SecurityException e){ + updateMessage("Error: "+e.toString().replaceAll(":.*$", "")); + e.printStackTrace(); + return false; + } + } +} diff --git a/src/main/java/nsusbloader/Utilities/WindowsDrivers/DriversInstall.java b/src/main/java/nsusbloader/Utilities/WindowsDrivers/DriversInstall.java new file mode 100644 index 0000000..0526134 --- /dev/null +++ b/src/main/java/nsusbloader/Utilities/WindowsDrivers/DriversInstall.java @@ -0,0 +1,151 @@ +/* + Copyright 2019-2020 Dmitry Isaenko + + This file is part of NS-USBloader. + + NS-USBloader is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + NS-USBloader is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with NS-USBloader. If not, see . +*/ +package nsusbloader.Utilities.WindowsDrivers; + +import javafx.geometry.Insets; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.control.ProgressBar; +import javafx.scene.image.Image; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Pane; +import javafx.scene.layout.Priority; +import javafx.scene.layout.VBox; +import javafx.stage.Stage; +import nsusbloader.AppPreferences; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ResourceBundle; + +public class DriversInstall { + + private static volatile boolean isRunning; + + private Label runInstallerStatusLabel; + + public DriversInstall(ResourceBundle rb){ + + if (DriversInstall.isRunning) + return; + + DriversInstall.isRunning = true; + + DownloadDriversTask downloadTask = new DownloadDriversTask(); + + Button cancelButton = new Button(rb.getString("btn_Cancel")); + + HBox hBoxInformation = new HBox(); + hBoxInformation.setAlignment(Pos.TOP_LEFT); + hBoxInformation.getChildren().add(new Label(rb.getString("windowBodyDownloadDrivers"))); + + ProgressBar progressBar = new ProgressBar(); + progressBar.setPrefWidth(Double.MAX_VALUE); + progressBar.progressProperty().bind(downloadTask.progressProperty()); + + Label downloadStatusLabel = new Label(); + downloadStatusLabel.setWrapText(true); + downloadStatusLabel.textProperty().bind(downloadTask.messageProperty()); + + runInstallerStatusLabel = new Label(); + runInstallerStatusLabel.setWrapText(true); + + Pane fillerPane1 = new Pane(); + Pane fillerPane2 = new Pane(); + + VBox parentVBox = new VBox(); + parentVBox.setAlignment(Pos.TOP_CENTER); + parentVBox.setFillWidth(true); + parentVBox.setSpacing(5.0); + parentVBox.setPadding(new Insets(5.0)); + parentVBox.setFillWidth(true); + parentVBox.getChildren().addAll( + hBoxInformation, + fillerPane1, + downloadStatusLabel, + runInstallerStatusLabel, + fillerPane2, + progressBar, + cancelButton + ); // TODO:FIX + + VBox.setVgrow(fillerPane1, Priority.ALWAYS); + VBox.setVgrow(fillerPane2, Priority.ALWAYS); + + Stage stage = new Stage(); + + stage.setTitle(rb.getString("windowTitleDownloadDrivers")); + stage.getIcons().addAll( + new Image("/res/dwnload_ico32x32.png"), //TODO: REDRAW + new Image("/res/dwnload_ico48x48.png"), + new Image("/res/dwnload_ico64x64.png"), + new Image("/res/dwnload_ico128x128.png") + ); + stage.setMinWidth(400); + stage.setMinHeight(150); + + Scene mainScene = new Scene(parentVBox, 405, 155); + + mainScene.getStylesheets().add(AppPreferences.getInstance().getTheme()); + + stage.setOnHidden(windowEvent -> { + downloadTask.cancel(true ); + DriversInstall.isRunning = false; + }); + + stage.setScene(mainScene); + stage.show(); + stage.toFront(); + + downloadTask.setOnSucceeded(event -> { + cancelButton.setText(rb.getString("btn_Close")); + + String returnedValue = downloadTask.getValue(); + + if (returnedValue == null) + return; + + if (runInstaller(returnedValue)) + stage.close(); + }); + + Thread downloadThread = new Thread(downloadTask); + downloadThread.start(); + + cancelButton.setOnAction(actionEvent -> { + downloadTask.cancel(true ); + stage.close(); + }); + } + + private boolean runInstaller(String pathToFile) { + try { + Runtime.getRuntime().exec("cmd /c "+pathToFile); + return true; + } + catch (Exception e){ + runInstallerStatusLabel.setText("Error: "+e.toString()); + e.printStackTrace(); + return false; + } + } +} diff --git a/src/main/resources/SettingsTab.fxml b/src/main/resources/SettingsTab.fxml index 76c8c7b..016f194 100644 --- a/src/main/resources/SettingsTab.fxml +++ b/src/main/resources/SettingsTab.fxml @@ -22,6 +22,11 @@