ns-usbloader/src/main/java/nsusbloader/NSLMain.java

153 lines
5.8 KiB
Java
Raw Normal View History

/*
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 <https://www.gnu.org/licenses/>.
*/
2019-02-10 04:59:54 +03:00
package nsusbloader;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
2019-02-18 03:06:49 +03:00
import nsusbloader.Controllers.NSLMainController;
import nsusbloader.Utilities.Rcm;
2019-02-10 04:59:54 +03:00
import java.io.File;
2019-02-10 04:59:54 +03:00
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;
2019-02-10 04:59:54 +03:00
public class NSLMain extends Application {
2020-01-24 01:08:16 +03:00
public static final String appVersion = "v4.0";
public static boolean isCli;
2020-01-24 01:08:16 +03:00
2019-02-10 04:59:54 +03:00
@Override
public void start(Stage primaryStage) throws Exception{
2019-02-18 03:06:49 +03:00
FXMLLoader loader = new FXMLLoader(getClass().getResource("/NSLMain.fxml"));
Locale userLocale = new Locale(AppPreferences.getInstance().getLanguage()); // NOTE: user locale based on ISO3 Language codes
2019-03-14 23:41:17 +03:00
ResourceBundle rb = ResourceBundle.getBundle("locale", userLocale);
2019-02-10 04:59:54 +03:00
2019-02-11 05:13:31 +03:00
loader.setResources(rb);
Parent root = loader.load();
2019-02-10 04:59:54 +03:00
2019-02-11 05:13:31 +03:00
primaryStage.getIcons().addAll(
new Image(getClass().getResourceAsStream("/res/app_icon32x32.png")),
new Image(getClass().getResourceAsStream("/res/app_icon48x48.png")),
new Image(getClass().getResourceAsStream("/res/app_icon64x64.png")),
new Image(getClass().getResourceAsStream("/res/app_icon128x128.png"))
);
2019-02-10 04:59:54 +03:00
2019-03-13 06:45:07 +03:00
primaryStage.setTitle("NS-USBloader "+appVersion);
primaryStage.setMinWidth(650);
primaryStage.setMinHeight(450);
Scene mainScene = new Scene(root,
AppPreferences.getInstance().getSceneWidth(),
AppPreferences.getInstance().getSceneHeight()
);
2019-02-18 03:06:49 +03:00
mainScene.getStylesheets().add(AppPreferences.getInstance().getTheme());
2019-02-11 05:13:31 +03:00
primaryStage.setScene(mainScene);
primaryStage.show();
2019-02-10 04:59:54 +03:00
2019-02-11 05:13:31 +03:00
primaryStage.setOnCloseRequest(e->{
if (MediatorControl.getInstance().getTransferActive())
if(! ServiceWindow.getConfirmationWindow(rb.getString("windowTitleConfirmExit"), rb.getString("windowBodyConfirmExit")))
e.consume();
});
2019-02-18 03:06:49 +03:00
NSLMainController controller = loader.getController();
controller.setHostServices(getHostServices());
primaryStage.setOnHidden(e-> {
AppPreferences.getInstance().setSceneHeight(mainScene.getHeight());
AppPreferences.getInstance().setSceneWidth(mainScene.getWidth());
controller.exit();
});
2019-02-10 04:59:54 +03:00
}
public static void main(String[] args) {
if (handleCli(args))
return;
launch(args);
}
private static boolean handleCli(String[] args){
if (args.length == 0)
return false;
NSLMain.isCli = true;
try {
switch (args[0]) {
case "-v":
case "--version":
System.out.println("NS-USBloader " + NSLMain.appVersion);
return true;
case "-c":
case "--clean":
if (Preferences.userRoot().nodeExists("NS-USBloader")) {
Preferences.userRoot().node("NS-USBloader").removeNode();
System.out.println("Settings removed");
}
else
System.out.println("Nothing to remove");
return true;
case "--rcm": // TODO: rewrite
if (args.length < 2){
System.out.println("No payload file specified. Expected:\n"
+ "... file.jar --rcm payload.bin\n" + "or\n"
+ "... file.jar --rcm /home/user/payload.bin\n");
return true;
}
boolean isWindows = false;
if (System.getProperty("os.name").toLowerCase().replace(" ", "").contains("windows"))
isWindows = true;
if (isWindows) {
if (! args[1].matches("^.:\\\\.*$"))
args[1] = System.getProperty("user.dir") + File.separator + args[1];
}
else {
if (! args[1].startsWith("/"))
args[1] = System.getProperty("user.dir") + File.separator + args[1];
}
Rcm rcm = new Rcm(args[1]);
Thread rcmThread = new Thread(rcm);
rcmThread.start();
return true;
case "--help":
default:
System.out.println("CLI Usage:\n"
+ "\t --rcm payload.bin\tSend payload\n"
+ "\t-c, --clean\tRemove/reset settings and exit\n"
+ "\t-v, --version \tShow application version\n"
+ "\t-h, --help\t\tShow this message");
return true;
}
}
catch (Exception e){
e.printStackTrace();
return true;
}
2019-02-10 04:59:54 +03:00
}
}