moved static vars and save & load code to XMLController

* moved static vars and save & load code to XMLController
* code clean up
* happy new year
This commit is contained in:
2019-01-08 17:10:33 +01:00
parent 060527ae03
commit d3d22db7a8
17 changed files with 392 additions and 296 deletions

View File

@ -1,7 +1,7 @@
/**
* Project-HomeFlix
*
* Copyright 2016-2018 <@Seil0>
* Copyright 2016-2019 <@Seil0>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -21,25 +21,15 @@
*/
package kellerkinder.HomeFlix.application;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.kellerkinder.Alerts.JFX2BtnCancelAlert;
import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
@ -50,6 +40,7 @@ import javafx.scene.layout.AnchorPane;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import kellerkinder.HomeFlix.controller.XMLController;
public class Main extends Application {
@ -57,27 +48,15 @@ public class Main extends Application {
private Scene scene;
private AnchorPane pane;
private MainWindowController mainWindowController;
private static String userHome = System.getProperty("user.home");
private static String userName = System.getProperty("user.name");
private static String osName = System.getProperty("os.name");
private static String osArch = System.getProperty("os.arch");
private static String osVers = System.getProperty("os.version");
private static String javaVers = System.getProperty("java.version");
private static String javaVend = System.getProperty("java.vendor");
private static String local = System.getProperty("user.language") + "_" + System.getProperty("user.country");
private static String dirHomeFlix;
private static File directory;
private static File configFile;
private static File posterCache;
private static XMLController xmlController;
private ResourceBundle bundle;
private static Logger LOGGER;
private Properties props = new Properties();
@Override
public void start(Stage primaryStage) throws IOException {
LOGGER.info("OS: " + osName + " " + osVers + " " + osArch);
LOGGER.info("Java: " + javaVend + " " + javaVers);
LOGGER.info("User: " + userName + " " + userHome);
LOGGER.info("OS: " + XMLController.getOsName() + " " + XMLController.getOsVers() + " " + XMLController.getOsVers());
LOGGER.info("Java: " + XMLController.getJavaVend() + " " + XMLController.getJavaVers());
LOGGER.info("User: " + XMLController.getUserName() + " " + XMLController.getUserHome());
this.primaryStage = primaryStage;
mainWindowController = new MainWindowController(this);
@ -109,20 +88,16 @@ public class Main extends Application {
primaryStage.show(); // show stage
// startup checks
if (!configFile.exists()) {
directory.mkdir();
if (!XMLController.getConfigFile().exists()) {
XMLController.getDirHomeFlix().mkdir();
System.out.println("config not found");
addFirstSource();
mainWindowController.setColor("ee3523");
mainWindowController.setFontSize(17.0);
mainWindowController.setAutoUpdate(false);
mainWindowController.setLocal(local);
saveSettings();
xmlController.saveSettings();
}
if (!posterCache.exists()) {
posterCache.mkdir();
if (!XMLController.getPosterCache().exists()) {
XMLController.getPosterCache().mkdir();
}
mainWindowController.init();
@ -136,31 +111,30 @@ public class Main extends Application {
* @param args arguments given at the start
*/
public static void main(String[] args) {
if (osName.contains("Windows")) {
dirHomeFlix = userHome + "/Documents/HomeFlix";
// Logger initialization
String logPath = "";
if (System.getProperty("os.name").contains("Windows")) {
logPath = System.getProperty("user.home") + "/Documents/HomeFlix/app.log";
} else {
dirHomeFlix = userHome + "/HomeFlix";
logPath = System.getProperty("user.home") + "/HomeFlix/app.log";
}
// set the concrete files
directory = new File(dirHomeFlix);
configFile = new File(dirHomeFlix + "/config.xml");
posterCache = new File(dirHomeFlix + "/posterCache");
System.setProperty("logFilename", dirHomeFlix + "/app.log");
File logFile = new File(dirHomeFlix + "/app.log");
System.setProperty("logFilename", logPath);
File logFile = new File(logPath);
logFile.delete();
LOGGER = LogManager.getLogger(Main.class.getName());
xmlController = new XMLController();
launch(args);
}
/**
/** TODO this should move
* we need to get the path for the first source from the user and add it to
* sources.json, if the user ends the file-/directory-chooser the program will exit
*/
void addFirstSource() {
switch (local) {
switch (XMLController.getSysLocal()) {
case "en_US":
bundle = ResourceBundle.getBundle("locals.HomeFlix-Local", Locale.US); // us_english
break;
@ -217,103 +191,6 @@ public class Main extends Application {
selectFirstSource.showAndWait();
}
/**
* save the configuration to the config.xml file
*/
public void saveSettings() {
LOGGER.info("saving settings ...");
try {
props.setProperty("color", mainWindowController.getColor());
props.setProperty("autoUpdate", String.valueOf(mainWindowController.isAutoUpdate()));
props.setProperty("useBeta", String.valueOf(mainWindowController.isUseBeta()));
props.setProperty("autoplay", String.valueOf(mainWindowController.isAutoplay()));
props.setProperty("size", mainWindowController.getFontSize().toString());
props.setProperty("local", mainWindowController.getLocal());
OutputStream outputStream = new FileOutputStream(getConfigFile()); // new output-stream
props.storeToXML(outputStream, "Project HomeFlix settings"); // write new .xml
outputStream.close();
} catch (IOException e) {
LOGGER.error("An error occurred while saving the settings!", e);
}
}
/**
* load the configuration from the config.xml file
* and try to load the API keys from apiKeys.json
*/
public void loadSettings() {
LOGGER.info("loading settings ...");
try {
InputStream inputStream = new FileInputStream(getConfigFile());
props.loadFromXML(inputStream); // new input-stream from .xml
try {
mainWindowController.setColor(props.getProperty("color"));
} catch (Exception e) {
LOGGER.error("cloud not load color", e);
mainWindowController.setColor("00a8cc");
}
try {
mainWindowController.setFontSize(Double.parseDouble(props.getProperty("size")));
} catch (Exception e) {
LOGGER.error("cloud not load fontsize", e);
mainWindowController.setFontSize(17.0);
}
try {
mainWindowController.setAutoUpdate(Boolean.parseBoolean(props.getProperty("autoUpdate")));
} catch (Exception e) {
LOGGER.error("cloud not load autoUpdate", e);
mainWindowController.setAutoUpdate(false);
}
try {
mainWindowController.setUseBeta(Boolean.parseBoolean(props.getProperty("useBeta")));
} catch (Exception e) {
LOGGER.error("cloud not load autoUpdate", e);
mainWindowController.setUseBeta(false);
}
try {
mainWindowController.setAutoplay(Boolean.parseBoolean(props.getProperty("autoplay")));
} catch (Exception e) {
LOGGER.error("cloud not load autoplay", e);
mainWindowController.setAutoplay(false);
}
try {
mainWindowController.setLocal(props.getProperty("local"));
} catch (Exception e) {
LOGGER.error("cloud not load local", e);
mainWindowController.setLocal(System.getProperty("user.language") + "_" + System.getProperty("user.country"));
}
inputStream.close();
} catch (IOException e) {
LOGGER.error("An error occurred while loading the settings!", e);
}
// try loading the omdbAPI key
try {
InputStream in = getClass().getClassLoader().getResourceAsStream("apiKeys.json");
if (in != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
JsonObject apiKeys = Json.parse(reader).asObject();
mainWindowController.setOmdbAPIKey(apiKeys.getString("omdbAPIKey", ""));
reader.close();
in.close();
} else {
LOGGER.warn("Cloud not load apiKeys.json. No such file");
}
} catch (Exception e) {
LOGGER.error("Cloud not load the omdbAPI key. Please contact the developer!", e);
}
}
public Stage getPrimaryStage() {
return primaryStage;
}
@ -321,16 +198,4 @@ public class Main extends Application {
public AnchorPane getPane() {
return pane;
}
public File getDirectory() {
return directory;
}
public File getConfigFile() {
return configFile;
}
public File getPosterCache() {
return posterCache;
}
}

View File

@ -1,7 +1,7 @@
/**
* Project-HomeFlix
*
* Copyright 2016-2018 <@Seil0>
* Copyright 2016-2019 <@Seil0>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -86,6 +86,7 @@ import javafx.util.Duration;
import kellerkinder.HomeFlix.controller.DBController;
import kellerkinder.HomeFlix.controller.OMDbAPIController;
import kellerkinder.HomeFlix.controller.UpdateController;
import kellerkinder.HomeFlix.controller.XMLController;
import kellerkinder.HomeFlix.datatypes.SourceDataType;
import kellerkinder.HomeFlix.player.Player;
import kellerkinder.HomeFlix.datatypes.FilmTabelDataType;
@ -153,29 +154,22 @@ public class MainWindowController {
// poster-mode
// @FXML private AnchorPane posterModeAnchorPane;
private Main main;
private MainWindowController mainWindowController;
private UpdateController updateController;
private DBController dbController;
private XMLController xmlController;
private static final Logger LOGGER = LogManager.getLogger(MainWindowController.class.getName());
private boolean menuTrue = false;
private boolean settingsTrue = false;
private boolean autoUpdate = false;
private boolean useBeta = false;
private boolean autoplay = false;
private final String version = "0.7.0";
private final String buildNumber = "165";
private final String buildNumber = "167";
private final String versionName = "toothless dragon";
private String btnStyle;
private String color;
private String local;
private String omdbAPIKey;
private double fontSize;
private final int hashA = -647380320;
private int last;
private int indexTable;
@ -200,14 +194,15 @@ public class MainWindowController {
public MainWindowController(Main main) {
this.main = main;
mainWindowController = this;
dbController = new DBController(this.main, this);
xmlController = new XMLController(); // TODO pass it over from main?
dbController = new DBController(this);
}
public void init() {
LOGGER.info("Initializing Project-HomeFlix build " + buildNumber);
main.loadSettings(); // load settings
xmlController.loadSettings(); // load settings
checkAutoUpdate();
// initialize the GUI and the DBController
@ -222,16 +217,16 @@ public class MainWindowController {
// Initialize general UI elements
private void initUI() {
versionLbl.setText("Version: " + version + " (Build: " + buildNumber + ")");
fontsizeSlider.setValue(getFontSize());
colorPicker.setValue(Color.valueOf(getColor()));
fontsizeSlider.setValue(XMLController.getFontSize());
colorPicker.setValue(Color.valueOf(XMLController.getColor()));
updateBtn.setFont(Font.font("System", 12));
autoUpdateToggleBtn.setSelected(isAutoUpdate());
autoplayToggleBtn.setSelected(isAutoplay());
autoUpdateToggleBtn.setSelected(XMLController.isAutoUpdate());
autoplayToggleBtn.setSelected(XMLController.isAutoplay());
languageChoisBox.setItems(languages);
branchChoisBox.setItems(branches);
if (isUseBeta()) {
if (XMLController.isUseBeta()) {
branchChoisBox.getSelectionModel().select(1);
} else {
branchChoisBox.getSelectionModel().select(0);
@ -303,7 +298,7 @@ public class MainWindowController {
}
if (settingsTrue) {
settingsScrollPane.setVisible(false);
main.saveSettings();
xmlController.saveSettings();
settingsTrue = false;
}
});
@ -313,9 +308,9 @@ public class MainWindowController {
public void changed(ObservableValue<? extends Number> ov, Number value, Number new_value) {
String local = languageChoisBox.getItems().get((int) new_value).toString();
local = local.substring(local.length() - 6, local.length() - 1); // reading only en_US from English (en_US)
setLocal(local);
XMLController.setUsrLocal(local);
setLocalUI();
main.saveSettings();
xmlController.saveSettings();
}
});
@ -323,22 +318,22 @@ public class MainWindowController {
@Override
public void changed(ObservableValue<? extends Number> ov, Number value, Number new_value) {
if (branchChoisBox.getItems().get((int) new_value).toString() == "beta") {
setUseBeta(true);
XMLController.setUseBeta(true);
} else {
setUseBeta(false);
XMLController.setUseBeta(false);
}
main.saveSettings();
xmlController.saveSettings();
}
});
fontsizeSlider.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> ov, Number old_val, Number new_val) {
setFontSize(fontsizeSlider.getValue());
XMLController.setFontSize(fontsizeSlider.getValue());
if (!getCurrentTitle().isEmpty()) {
dbController.readCache(getCurrentStreamUrl());
}
main.saveSettings();
xmlController.saveSettings();
}
});
@ -362,7 +357,7 @@ public class MainWindowController {
filmRoot.getChildren().add(new TreeItem<FilmTabelDataType>(filterData.get(i))); // add filtered data to root node after search
}
if (searchTextField.getText().hashCode() == hashA) {
setColor("000000");
XMLController.setColor("000000");
colorPicker.setValue(new Color(0, 0, 0, 1));
applyColor();
}
@ -443,7 +438,7 @@ public class MainWindowController {
LOGGER.info("loading from cache: " + getCurrentTitle());
dbController.readCache(getCurrentStreamUrl());
} else {
Thread omdbAPIThread = new Thread(new OMDbAPIController(main, dbController, currentTableFilm, omdbAPIKey, true));
Thread omdbAPIThread = new Thread(new OMDbAPIController(dbController, currentTableFilm, XMLController.getOmdbAPIKey(), true));
omdbAPIThread.setName("OMDbAPI");
omdbAPIThread.start();
}
@ -538,7 +533,7 @@ public class MainWindowController {
private void settingsBtnclicked() {
if (settingsTrue) {
settingsScrollPane.setVisible(false);
main.saveSettings();
xmlController.saveSettings();
settingsTrue = false;
} else {
settingsScrollPane.setVisible(true);
@ -572,29 +567,29 @@ public class MainWindowController {
@FXML
private void colorPickerAction() {
setColor(colorPicker.getValue().toString().substring(2, 10));
main.saveSettings();
XMLController.setColor(colorPicker.getValue().toString().substring(2, 10));
xmlController.saveSettings();
applyColor();
}
@FXML
private void updateBtnAction() {
updateController = new UpdateController(this, buildNumber, useBeta);
updateController = new UpdateController(this, buildNumber, XMLController.isUseBeta());
Thread updateThread = new Thread(updateController);
updateThread.setName("Updater");
updateThread.start();
}
@FXML
private void autoUpdateToggleBtnAction(){
autoUpdate = isAutoUpdate() ? false : true;
main.saveSettings();
private void autoUpdateToggleBtnAction() {
XMLController.setAutoUpdate(!XMLController.isAutoUpdate());
xmlController.saveSettings();
}
@FXML
private void autoplayToggleBtnAction(){
autoplay = isAutoplay() ? false : true;
main.saveSettings();
XMLController.setAutoplay(!XMLController.isAutoplay());
xmlController.saveSettings();
}
// refresh the selected child of the root node
@ -652,9 +647,9 @@ public class MainWindowController {
try {
// read old array
File oldSources = new File(main.getDirectory() + "/sources.json");
File oldSources = new File(XMLController.getDirHomeFlix() + "/sources.json");
if (oldSources.exists()) {
newsources = Json.parse(new FileReader(main.getDirectory() + "/sources.json")).asArray();
newsources = Json.parse(new FileReader(XMLController.getDirHomeFlix() + "/sources.json")).asArray();
} else {
newsources = Json.array();
}
@ -662,7 +657,7 @@ public class MainWindowController {
// add new source
source = Json.object().add("path", path).add("mode", mode);
newsources.add(source);
Writer writer = new FileWriter(main.getDirectory() + "/sources.json");
Writer writer = new FileWriter(XMLController.getDirHomeFlix() + "/sources.json");
newsources.writeTo(writer);
writer.close();
} catch (IOException e) {
@ -682,11 +677,11 @@ public class MainWindowController {
*/
private void applyColor() {
String menuBtnStyle;
BigInteger usedColor = new BigInteger(getColor(), 16);
BigInteger usedColor = new BigInteger(XMLController.getColor(), 16);
BigInteger checkColor = new BigInteger("78909cff", 16);
if (usedColor.compareTo(checkColor) == -1) {
btnStyle = "-fx-button-type: RAISED; -fx-background-color: #" + getColor() + "; -fx-text-fill: WHITE;";
btnStyle = "-fx-button-type: RAISED; -fx-background-color: #" + XMLController.getColor() + "; -fx-text-fill: WHITE;";
menuBtnStyle = "-fx-text-fill: WHITE;";
playbtn.setGraphic(new ImageView(new Image("icons/ic_play_arrow_white_18dp_1x.png")));
@ -696,7 +691,7 @@ public class MainWindowController {
menuHam.getStyleClass().clear();
menuHam.getStyleClass().add("jfx-hamburgerW");
} else {
btnStyle = "-fx-button-type: RAISED; -fx-background-color: #" + getColor() + "; -fx-text-fill: BLACK;";
btnStyle = "-fx-button-type: RAISED; -fx-background-color: #" + XMLController.getColor() + "; -fx-text-fill: BLACK;";
menuBtnStyle = "-fx-text-fill: BLACK;";
playbtn.setGraphic(new ImageView(new Image("icons/ic_play_arrow_black_18dp_1x.png")));
@ -708,9 +703,9 @@ public class MainWindowController {
}
// boxes and TextFields
sideMenuVBox.setStyle("-fx-background-color: #" + getColor() + ";");
topHBox.setStyle("-fx-background-color: #" + getColor() + ";");
searchTextField.setFocusColor(Color.valueOf(getColor()));
sideMenuVBox.setStyle("-fx-background-color: #" + XMLController.getColor() + ";");
topHBox.setStyle("-fx-background-color: #" + XMLController.getColor() + ";");
searchTextField.setFocusColor(Color.valueOf(XMLController.getColor()));
// normal buttons
addDirectoryBtn.setStyle(btnStyle);
@ -747,7 +742,7 @@ public class MainWindowController {
* set the local based on the languageChoisBox selection
*/
void setLocalUI() {
switch (getLocal()) {
switch (XMLController.getUsrLocal()) {
case "en_US":
setBundle(ResourceBundle.getBundle("locals.HomeFlix-Local", Locale.US)); // us_English
languageChoisBox.getSelectionModel().select(0);
@ -785,10 +780,10 @@ public class MainWindowController {
// if AutoUpdate, then check for updates
private void checkAutoUpdate() {
if (isAutoUpdate()) {
if (XMLController.isAutoUpdate()) {
try {
LOGGER.info("AutoUpdate: looking for updates on startup ...");
updateController = new UpdateController(this, buildNumber, useBeta);
updateController = new UpdateController(this, buildNumber, XMLController.isUseBeta());
Thread updateThread = new Thread(updateController);
updateThread.setName("Updater");
updateThread.start();
@ -824,7 +819,7 @@ public class MainWindowController {
for (FilmTabelDataType entry : dbController.getAllNotCachedEntries()) {
System.out.println(entry.getStreamUrl() + " is NOT cached!");
Runnable OMDbAPIWorker = new OMDbAPIController(main, dbController, entry, omdbAPIKey, false);
Runnable OMDbAPIWorker = new OMDbAPIController(dbController, entry, XMLController.getOmdbAPIKey(), false);
executor.execute(OMDbAPIWorker);
}
executor.shutdown();
@ -836,14 +831,6 @@ public class MainWindowController {
public DBController getDbController() {
return dbController;
}
public String getColor() {
return color;
}
public void setColor(String input) {
this.color = input;
}
public FilmTabelDataType getCurrentTableFilm() {
return currentTableFilm;
@ -857,14 +844,6 @@ public class MainWindowController {
return currentTableFilm.getStreamUrl();
}
public Double getFontSize() {
return fontSize;
}
public void setFontSize(Double input) {
this.fontSize = input;
}
public int getIndexTable() {
return indexTable;
}
@ -873,46 +852,6 @@ public class MainWindowController {
return indexList;
}
public boolean isAutoUpdate() {
return autoUpdate;
}
public void setAutoUpdate(boolean input) {
this.autoUpdate = input;
}
public boolean isUseBeta() {
return useBeta;
}
public void setUseBeta(boolean useBeta) {
this.useBeta = useBeta;
}
public boolean isAutoplay() {
return autoplay;
}
public void setAutoplay(boolean autoplay) {
this.autoplay = autoplay;
}
public String getLocal() {
return local;
}
public void setLocal(String input) {
this.local = input;
}
public String getOmdbAPIKey() {
return omdbAPIKey;
}
public void setOmdbAPIKey(String omdbAPIKey) {
this.omdbAPIKey = omdbAPIKey;
}
public ObservableList<FilmTabelDataType> getFilmsList() {
return filmsList;
}