2019-02-18 03:06:49 +03:00
|
|
|
package nsusbloader.Controllers;
|
2019-02-10 04:59:54 +03:00
|
|
|
|
2019-03-23 20:05:48 +03:00
|
|
|
import javafx.application.HostServices;
|
2019-03-19 17:44:39 +03:00
|
|
|
import javafx.concurrent.Task;
|
2019-02-10 04:59:54 +03:00
|
|
|
import javafx.fxml.FXML;
|
|
|
|
import javafx.fxml.Initializable;
|
2019-02-16 04:34:30 +03:00
|
|
|
import javafx.scene.control.*;
|
2019-03-14 23:41:17 +03:00
|
|
|
import javafx.scene.input.DragEvent;
|
|
|
|
import javafx.scene.input.TransferMode;
|
2019-02-10 04:59:54 +03:00
|
|
|
import javafx.scene.layout.Region;
|
|
|
|
import javafx.stage.FileChooser;
|
2019-03-23 20:05:48 +03:00
|
|
|
import nsusbloader.*;
|
|
|
|
import nsusbloader.ModelControllers.UpdatesChecker;
|
2019-03-17 19:45:51 +03:00
|
|
|
import nsusbloader.NET.NETCommunications;
|
2019-03-14 21:28:20 +03:00
|
|
|
import nsusbloader.USB.UsbCommunications;
|
2019-02-10 04:59:54 +03:00
|
|
|
|
|
|
|
import java.io.File;
|
2019-03-19 07:01:30 +03:00
|
|
|
import java.net.*;
|
2019-03-14 23:41:17 +03:00
|
|
|
import java.util.ArrayList;
|
2019-02-10 04:59:54 +03:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.ResourceBundle;
|
|
|
|
|
|
|
|
public class NSLMainController implements Initializable {
|
|
|
|
|
|
|
|
private ResourceBundle resourceBundle;
|
|
|
|
|
|
|
|
@FXML
|
2019-02-17 18:38:07 +03:00
|
|
|
public TextArea logArea; // Accessible from Mediator
|
2019-02-10 04:59:54 +03:00
|
|
|
@FXML
|
|
|
|
private Button selectNspBtn;
|
|
|
|
@FXML
|
|
|
|
private Button uploadStopBtn;
|
|
|
|
private Region btnUpStopImage;
|
|
|
|
@FXML
|
2019-02-17 18:38:07 +03:00
|
|
|
public ProgressBar progressBar; // Accessible from Mediator
|
2019-02-10 04:59:54 +03:00
|
|
|
|
2019-03-17 19:45:51 +03:00
|
|
|
@FXML
|
2019-03-18 19:12:11 +03:00
|
|
|
private SettingsController SettingsTabController;
|
2019-03-17 19:45:51 +03:00
|
|
|
@FXML
|
2019-04-10 13:07:28 +03:00
|
|
|
public FrontController FrontTabController; // Accessible from Mediator | todo: incapsulate
|
2019-02-16 04:34:30 +03:00
|
|
|
|
2019-03-19 17:44:39 +03:00
|
|
|
private Task<Void> usbNetCommunications;
|
2019-03-18 19:12:11 +03:00
|
|
|
private Thread workThread;
|
2019-02-10 04:59:54 +03:00
|
|
|
|
2019-02-15 05:44:39 +03:00
|
|
|
private String previouslyOpenedPath;
|
|
|
|
|
2019-02-10 04:59:54 +03:00
|
|
|
@Override
|
|
|
|
public void initialize(URL url, ResourceBundle rb) {
|
|
|
|
this.resourceBundle = rb;
|
2019-02-18 03:06:49 +03:00
|
|
|
logArea.setText(rb.getString("logsGreetingsMessage")+" "+ NSLMain.appVersion+"!\n");
|
2019-02-10 04:59:54 +03:00
|
|
|
if (System.getProperty("os.name").toLowerCase().startsWith("lin"))
|
|
|
|
if (!System.getProperty("user.name").equals("root"))
|
|
|
|
logArea.appendText(rb.getString("logsEnteredAsMsg1")+System.getProperty("user.name")+"\n"+rb.getString("logsEnteredAsMsg2") + "\n");
|
|
|
|
|
|
|
|
logArea.appendText(rb.getString("logsGreetingsMessage2")+"\n");
|
|
|
|
|
2019-02-17 18:38:07 +03:00
|
|
|
MediatorControl.getInstance().setController(this);
|
2019-02-10 04:59:54 +03:00
|
|
|
|
|
|
|
uploadStopBtn.setDisable(true);
|
|
|
|
selectNspBtn.setOnAction(e->{ selectFilesBtnAction(); });
|
2019-02-11 17:33:53 +03:00
|
|
|
uploadStopBtn.setOnAction(e->{ uploadBtnAction(); });
|
2019-02-10 04:59:54 +03:00
|
|
|
|
2019-02-17 18:38:07 +03:00
|
|
|
selectNspBtn.getStyleClass().add("buttonSelect");
|
|
|
|
|
2019-02-10 04:59:54 +03:00
|
|
|
this.btnUpStopImage = new Region();
|
|
|
|
btnUpStopImage.getStyleClass().add("regionUpload");
|
2019-04-10 13:07:28 +03:00
|
|
|
|
2019-02-10 04:59:54 +03:00
|
|
|
uploadStopBtn.getStyleClass().add("buttonUp");
|
|
|
|
uploadStopBtn.setGraphic(btnUpStopImage);
|
2019-02-15 05:44:39 +03:00
|
|
|
|
2019-04-10 13:07:28 +03:00
|
|
|
this.previouslyOpenedPath = AppPreferences.getInstance().getRecent();
|
2019-03-23 20:05:48 +03:00
|
|
|
|
|
|
|
if (AppPreferences.getInstance().getAutoCheckUpdates()){
|
|
|
|
Task<List<String>> updTask = new UpdatesChecker();
|
|
|
|
updTask.setOnSucceeded(event->{
|
|
|
|
List<String> result = updTask.getValue();
|
|
|
|
if (result != null){
|
2019-03-28 00:38:27 +03:00
|
|
|
if (!result.get(0).isEmpty()) {
|
2019-03-23 20:05:48 +03:00
|
|
|
SettingsTabController.setNewVersionLink(result.get(0));
|
2019-03-28 00:38:27 +03:00
|
|
|
ServiceWindow.getInfoNotification(resourceBundle.getString("windowTitleNewVersionAval"), resourceBundle.getString("windowTitleNewVersionAval") + ": " + result.get(0) + "\n\n" + result.get(1));
|
|
|
|
}
|
2019-03-23 20:05:48 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ServiceWindow.getInfoNotification(resourceBundle.getString("windowTitleNewVersionUnknown"), resourceBundle.getString("windowBodyNewVersionUnknown"));
|
|
|
|
});
|
|
|
|
Thread updates = new Thread(updTask);
|
|
|
|
updates.setDaemon(true);
|
|
|
|
updates.start();
|
|
|
|
}
|
2019-02-16 04:34:30 +03:00
|
|
|
}
|
2019-03-23 20:05:48 +03:00
|
|
|
/**
|
|
|
|
* Provide hostServices to Settings tab
|
|
|
|
* */
|
|
|
|
public void setHostServices(HostServices hs ){ SettingsTabController.registerHostServices(hs);}
|
2019-04-10 13:07:28 +03:00
|
|
|
|
2019-02-10 04:59:54 +03:00
|
|
|
/**
|
|
|
|
* Functionality for selecting NSP button.
|
|
|
|
* Uses setReady and setNotReady to simplify code readability.
|
|
|
|
* */
|
|
|
|
private void selectFilesBtnAction(){
|
|
|
|
List<File> filesList;
|
|
|
|
FileChooser fileChooser = new FileChooser();
|
2019-02-11 15:09:23 +03:00
|
|
|
fileChooser.setTitle(resourceBundle.getString("btnFileOpen"));
|
2019-02-18 03:06:49 +03:00
|
|
|
|
|
|
|
File validator = new File(previouslyOpenedPath);
|
|
|
|
if (validator.exists())
|
2019-04-10 13:07:28 +03:00
|
|
|
fileChooser.setInitialDirectory(validator);
|
2019-02-18 03:06:49 +03:00
|
|
|
else
|
2019-04-10 13:07:28 +03:00
|
|
|
fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
|
2019-02-18 03:06:49 +03:00
|
|
|
|
2019-04-23 02:31:39 +03:00
|
|
|
if (SettingsTabController.getTfXCISupport() && FrontTabController.getSelectedProtocol().equals("TinFoil")){
|
|
|
|
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("NSP/XCI", "*.nsp", "*.xci"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("NSP ROM", "*.nsp"));
|
|
|
|
|
2019-02-10 04:59:54 +03:00
|
|
|
filesList = fileChooser.showOpenMultipleDialog(logArea.getScene().getWindow());
|
2019-02-15 05:44:39 +03:00
|
|
|
if (filesList != null && !filesList.isEmpty()) {
|
2019-04-10 13:07:28 +03:00
|
|
|
FrontTabController.tableFilesListController.setFiles(filesList);
|
2019-02-16 17:58:26 +03:00
|
|
|
uploadStopBtn.setDisable(false);
|
2019-02-15 05:44:39 +03:00
|
|
|
previouslyOpenedPath = filesList.get(0).getParent();
|
|
|
|
}
|
2019-02-10 04:59:54 +03:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* It's button listener when no transmission executes
|
|
|
|
* */
|
2019-02-11 17:33:53 +03:00
|
|
|
private void uploadBtnAction(){
|
2019-03-18 19:12:11 +03:00
|
|
|
if ((workThread == null || !workThread.isAlive())){
|
2019-03-20 15:51:18 +03:00
|
|
|
// Collect files
|
|
|
|
List<File> nspToUpload;
|
2019-04-10 13:07:28 +03:00
|
|
|
if ((nspToUpload = FrontTabController.tableFilesListController.getFilesForUpload()) == null) {
|
2019-03-20 15:51:18 +03:00
|
|
|
logArea.setText(resourceBundle.getString("logsNoFolderFileSelected"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logArea.setText(resourceBundle.getString("logsFilesToUploadTitle")+"\n");
|
|
|
|
for (File item: nspToUpload)
|
|
|
|
logArea.appendText(" "+item.getAbsolutePath()+"\n");
|
|
|
|
}
|
|
|
|
// If USB selected
|
2019-04-10 13:07:28 +03:00
|
|
|
if (FrontTabController.getSelectedProtocol().equals("GoldLeaf") ||
|
2019-03-17 19:45:51 +03:00
|
|
|
(
|
2019-04-10 13:07:28 +03:00
|
|
|
FrontTabController.getSelectedProtocol().equals("TinFoil")
|
|
|
|
&& FrontTabController.getSelectedNetUsb().equals("USB")
|
2019-03-17 19:45:51 +03:00
|
|
|
)
|
|
|
|
){
|
2019-04-10 13:07:28 +03:00
|
|
|
usbNetCommunications = new UsbCommunications(nspToUpload, FrontTabController.getSelectedProtocol());
|
2019-03-19 17:44:39 +03:00
|
|
|
workThread = new Thread(usbNetCommunications);
|
2019-03-18 19:12:11 +03:00
|
|
|
workThread.setDaemon(true);
|
|
|
|
workThread.start();
|
2019-03-17 19:45:51 +03:00
|
|
|
}
|
|
|
|
else { // NET INSTALL OVER TINFOIL
|
2019-04-10 13:07:28 +03:00
|
|
|
if (SettingsTabController.isNsIpValidate() && ! FrontTabController.getNsIp().matches("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"))
|
2019-03-17 19:45:51 +03:00
|
|
|
if (!ServiceWindow.getConfirmationWindow(resourceBundle.getString("windowTitleBadIp"),resourceBundle.getString("windowBodyBadIp")))
|
|
|
|
return;
|
2019-03-20 15:51:18 +03:00
|
|
|
|
2019-04-10 13:07:28 +03:00
|
|
|
String nsIP = FrontTabController.getNsIp();
|
2019-03-18 19:12:11 +03:00
|
|
|
|
2019-03-19 17:44:39 +03:00
|
|
|
if (!SettingsTabController.getExpertModeSelected())
|
|
|
|
usbNetCommunications = new NETCommunications(nspToUpload, nsIP, false, "", "", "");
|
2019-03-17 19:45:51 +03:00
|
|
|
else {
|
2019-03-19 17:44:39 +03:00
|
|
|
usbNetCommunications = new NETCommunications(
|
|
|
|
nspToUpload,
|
|
|
|
nsIP,
|
|
|
|
SettingsTabController.getNotServeSelected(),
|
|
|
|
SettingsTabController.getAutoIpSelected()?"":SettingsTabController.getHostIp(),
|
|
|
|
SettingsTabController.getRandPortSelected()?"":SettingsTabController.getHostPort(),
|
|
|
|
SettingsTabController.getNotServeSelected()?SettingsTabController.getHostExtra():""
|
|
|
|
);
|
2019-03-17 19:45:51 +03:00
|
|
|
}
|
2019-03-19 17:44:39 +03:00
|
|
|
|
|
|
|
workThread = new Thread(usbNetCommunications);
|
|
|
|
workThread.setDaemon(true);
|
|
|
|
workThread.start();
|
2019-02-16 17:58:26 +03:00
|
|
|
}
|
2019-02-10 04:59:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* It's button listener when transmission in progress
|
|
|
|
* */
|
|
|
|
private void stopBtnAction(){
|
2019-03-18 19:12:11 +03:00
|
|
|
if (workThread != null && workThread.isAlive()){
|
2019-04-10 13:07:28 +03:00
|
|
|
usbNetCommunications.cancel(false);
|
2019-02-10 04:59:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* This thing modify UI for reusing 'Upload to NS' button and make functionality set for "Stop transmission"
|
|
|
|
* Called from mediator
|
|
|
|
* */
|
2019-02-18 03:06:49 +03:00
|
|
|
public void notifyTransmissionStarted(boolean isTransmissionStarted){
|
2019-02-10 04:59:54 +03:00
|
|
|
if (isTransmissionStarted) {
|
|
|
|
selectNspBtn.setDisable(true);
|
|
|
|
uploadStopBtn.setOnAction(e->{ stopBtnAction(); });
|
|
|
|
|
|
|
|
uploadStopBtn.setText(resourceBundle.getString("btnStop"));
|
|
|
|
|
|
|
|
btnUpStopImage.getStyleClass().remove("regionUpload");
|
|
|
|
btnUpStopImage.getStyleClass().add("regionStop");
|
|
|
|
|
|
|
|
uploadStopBtn.getStyleClass().remove("buttonUp");
|
|
|
|
uploadStopBtn.getStyleClass().add("buttonStop");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
selectNspBtn.setDisable(false);
|
2019-02-11 17:33:53 +03:00
|
|
|
uploadStopBtn.setOnAction(e->{ uploadBtnAction(); });
|
2019-02-10 04:59:54 +03:00
|
|
|
|
|
|
|
uploadStopBtn.setText(resourceBundle.getString("btnUpload"));
|
|
|
|
|
|
|
|
btnUpStopImage.getStyleClass().remove("regionStop");
|
|
|
|
btnUpStopImage.getStyleClass().add("regionUpload");
|
|
|
|
|
|
|
|
uploadStopBtn.getStyleClass().remove("buttonStop");
|
|
|
|
uploadStopBtn.getStyleClass().add("buttonUp");
|
|
|
|
}
|
|
|
|
}
|
2019-03-15 04:09:04 +03:00
|
|
|
/**
|
|
|
|
* Crunch. Now you see that I'm not a programmer.. This function called from NSTableViewController
|
|
|
|
* */
|
2019-03-16 02:43:14 +03:00
|
|
|
public void disableUploadStopBtn(boolean disable){
|
|
|
|
uploadStopBtn.setDisable(disable);
|
2019-03-15 04:09:04 +03:00
|
|
|
}
|
2019-03-14 23:41:17 +03:00
|
|
|
/**
|
|
|
|
* Drag-n-drop support (dragOver consumer)
|
|
|
|
* */
|
|
|
|
@FXML
|
|
|
|
private void handleDragOver(DragEvent event){
|
|
|
|
if (event.getDragboard().hasFiles())
|
|
|
|
event.acceptTransferModes(TransferMode.ANY);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Drag-n-drop support (drop consumer)
|
|
|
|
* */
|
|
|
|
@FXML
|
|
|
|
private void handleDrop(DragEvent event){
|
2019-03-16 02:43:14 +03:00
|
|
|
if (MediatorControl.getInstance().getTransferActive()) {
|
|
|
|
event.setDropCompleted(true);
|
|
|
|
return;
|
|
|
|
}
|
2019-03-14 23:41:17 +03:00
|
|
|
List<File> filesDropped = new ArrayList<>();
|
|
|
|
try {
|
2019-04-23 02:31:39 +03:00
|
|
|
if (SettingsTabController.getTfXCISupport() && FrontTabController.getSelectedProtocol().equals("TinFoil")){
|
|
|
|
for (File fileOrDir : event.getDragboard().getFiles()) {
|
|
|
|
if (fileOrDir.getName().toLowerCase().endsWith(".nsp") || fileOrDir.getName().toLowerCase().endsWith(".xci"))
|
|
|
|
filesDropped.add(fileOrDir);
|
|
|
|
else if (fileOrDir.isDirectory())
|
|
|
|
for (File file : fileOrDir.listFiles())
|
|
|
|
if (file.getName().toLowerCase().endsWith(".nsp") || file.getName().toLowerCase().endsWith(".xci"))
|
|
|
|
filesDropped.add(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (File fileOrDir : event.getDragboard().getFiles()) {
|
|
|
|
if (fileOrDir.getName().toLowerCase().endsWith(".nsp"))
|
|
|
|
filesDropped.add(fileOrDir);
|
|
|
|
else if (fileOrDir.isDirectory())
|
|
|
|
for (File file : fileOrDir.listFiles())
|
|
|
|
if (file.getName().toLowerCase().endsWith(".nsp"))
|
|
|
|
filesDropped.add(file);
|
|
|
|
}
|
2019-03-14 23:41:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (SecurityException se){
|
|
|
|
se.printStackTrace();
|
|
|
|
}
|
2019-03-16 02:43:14 +03:00
|
|
|
if (!filesDropped.isEmpty())
|
2019-04-10 13:07:28 +03:00
|
|
|
FrontTabController.tableFilesListController.setFiles(filesDropped);
|
2019-03-14 23:41:17 +03:00
|
|
|
|
|
|
|
event.setDropCompleted(true);
|
|
|
|
}
|
2019-02-18 03:06:49 +03:00
|
|
|
/**
|
|
|
|
* Save preferences before exit
|
|
|
|
* */
|
2019-04-10 13:07:28 +03:00
|
|
|
public void exit(){
|
2019-03-19 05:06:18 +03:00
|
|
|
AppPreferences.getInstance().setAll(
|
2019-04-10 13:07:28 +03:00
|
|
|
FrontTabController.getSelectedProtocol(),
|
2019-03-19 05:06:18 +03:00
|
|
|
previouslyOpenedPath,
|
2019-04-10 13:07:28 +03:00
|
|
|
FrontTabController.getSelectedNetUsb(),
|
|
|
|
FrontTabController.getNsIp(),
|
2019-03-19 05:06:18 +03:00
|
|
|
SettingsTabController.isNsIpValidate(),
|
|
|
|
SettingsTabController.getExpertModeSelected(),
|
|
|
|
SettingsTabController.getAutoIpSelected(),
|
|
|
|
SettingsTabController.getRandPortSelected(),
|
|
|
|
SettingsTabController.getNotServeSelected(),
|
|
|
|
SettingsTabController.getHostIp(),
|
|
|
|
SettingsTabController.getHostPort(),
|
2019-03-23 20:05:48 +03:00
|
|
|
SettingsTabController.getHostExtra(),
|
2019-04-23 02:31:39 +03:00
|
|
|
SettingsTabController.getAutoCheckForUpdates(),
|
|
|
|
SettingsTabController.getTfXCISupport()
|
2019-03-19 05:06:18 +03:00
|
|
|
);
|
2019-02-18 03:06:49 +03:00
|
|
|
}
|
2019-02-10 04:59:54 +03:00
|
|
|
}
|