2016-08-14 15:17:14 +02:00
/ * *
2018-03-01 15:42:47 +01:00
* Project - HomeFlix
*
* Copyright 2016 - 2018 < @Seil0 >
2016-08-14 15:17:14 +02:00
*
* 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
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program 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 this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
*
* /
2018-03-01 15:42:47 +01:00
package kellerkinder.HomeFlix.application ;
2017-02-09 16:27:52 +01:00
2018-05-17 18:58:54 +02:00
import java.io.BufferedReader ;
2016-08-14 15:17:14 +02:00
import java.io.File ;
2018-05-17 18:58:54 +02:00
import java.io.FileInputStream ;
import java.io.FileOutputStream ;
2016-08-14 15:17:14 +02:00
import java.io.IOException ;
2018-05-17 18:58:54 +02:00
import java.io.InputStream ;
import java.io.InputStreamReader ;
import java.io.OutputStream ;
2017-02-09 16:27:52 +01:00
import java.util.Locale ;
2018-05-17 18:58:54 +02:00
import java.util.Properties ;
2017-02-09 16:27:52 +01:00
import java.util.ResourceBundle ;
2017-09-16 18:22:46 +02:00
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2018-03-29 18:15:57 +02:00
import org.kellerkinder.Alerts.JFX2BtnCancelAlert ;
2018-03-29 12:27:44 +02:00
2018-05-17 18:58:54 +02:00
import com.eclipsesource.json.Json ;
import com.eclipsesource.json.JsonObject ;
2016-08-14 15:17:14 +02:00
import javafx.application.Application ;
2018-03-29 12:27:44 +02:00
import javafx.event.ActionEvent ;
import javafx.event.EventHandler ;
2016-08-14 15:17:14 +02:00
import javafx.fxml.FXMLLoader ;
import javafx.scene.Scene ;
import javafx.scene.image.Image ;
import javafx.scene.layout.AnchorPane ;
import javafx.stage.DirectoryChooser ;
2018-03-29 11:26:20 +02:00
import javafx.stage.FileChooser ;
2016-08-14 15:17:14 +02:00
import javafx.stage.Stage ;
public class Main extends Application {
2018-03-29 18:15:57 +02:00
2018-03-01 15:42:47 +01:00
private Stage primaryStage ;
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 " ) ;
2018-03-29 18:15:57 +02:00
private static String javaVend = System . getProperty ( " java.vendor " ) ;
2018-04-07 17:26:56 +02:00
private static String local = System . getProperty ( " user.language " ) + " _ " + System . getProperty ( " user.country " ) ;
2018-04-28 18:02:02 +02:00
private static String dirHomeFlix ;
2018-05-17 18:58:54 +02:00
private static File directory ;
private static File configFile ;
private static File posterCache ;
2017-02-09 20:39:43 +01:00
private ResourceBundle bundle ;
2017-09-16 18:22:46 +02:00
private static Logger LOGGER ;
2018-05-17 18:58:54 +02:00
private Properties props = new Properties ( ) ;
2018-03-29 18:15:57 +02:00
2016-08-14 15:17:14 +02:00
@Override
2017-04-25 23:35:20 +02:00
public void start ( Stage primaryStage ) throws IOException {
2018-03-01 15:42:47 +01:00
LOGGER . info ( " OS: " + osName + " " + osVers + " " + osArch ) ;
LOGGER . info ( " Java: " + javaVend + " " + javaVers ) ;
LOGGER . info ( " User: " + userName + " " + userHome ) ;
2018-03-29 18:15:57 +02:00
this . primaryStage = primaryStage ;
2018-05-17 18:58:54 +02:00
mainWindowController = new MainWindowController ( this ) ;
2016-08-14 15:17:14 +02:00
mainWindow ( ) ;
}
2018-03-29 18:15:57 +02:00
2018-04-02 18:29:59 +02:00
/ * *
* initialize the mainWindowController , GUI and load the saved settings or call addFirstSource
* initialize the primaryStage and set the file / directory paths
* /
2017-03-05 18:29:24 +01:00
private void mainWindow ( ) {
2016-08-14 15:17:14 +02:00
try {
2017-09-16 18:22:46 +02:00
FXMLLoader loader = new FXMLLoader ( ) ;
2018-09-23 14:33:16 +02:00
loader . setLocation ( getClass ( ) . getResource ( " /fxml/MainWindow.fxml " ) ) ;
2018-05-17 18:58:54 +02:00
loader . setController ( mainWindowController ) ;
2018-03-01 15:42:47 +01:00
pane = ( AnchorPane ) loader . load ( ) ;
2017-09-16 18:22:46 +02:00
primaryStage . setMinHeight ( 600 . 00 ) ;
2018-03-11 15:36:55 +01:00
primaryStage . setMinWidth ( 1000 . 00 ) ;
2017-09-16 18:22:46 +02:00
primaryStage . setResizable ( false ) ;
primaryStage . setTitle ( " Project HomeFlix " ) ;
2018-03-01 15:42:47 +01:00
primaryStage . getIcons ( ) . add ( new Image ( Main . class . getResourceAsStream ( " /icons/Homeflix_Icon_64x64.png " ) ) ) ; //adds application icon
2018-04-28 18:02:02 +02:00
primaryStage . setOnCloseRequest ( event - > System . exit ( 1 ) ) ;
2018-03-01 15:42:47 +01:00
2018-03-29 12:27:44 +02:00
// generate window
scene = new Scene ( pane ) ; // create new scene, append pane to scene
scene . getStylesheets ( ) . add ( getClass ( ) . getResource ( " /css/MainWindow.css " ) . toExternalForm ( ) ) ;
primaryStage . setScene ( scene ) ; // append scene to stage
primaryStage . show ( ) ; // show stage
2018-10-11 12:29:26 +02:00
System . out . println ( " test " ) ;
2018-05-17 18:58:54 +02:00
// startup checks TODO move to mwc
2018-03-01 15:42:47 +01:00
if ( ! configFile . exists ( ) ) {
2018-04-28 18:02:02 +02:00
directory . mkdir ( ) ;
2018-10-11 12:29:26 +02:00
System . out . println ( " config not found " ) ;
2018-04-28 18:02:02 +02:00
2018-04-02 18:29:59 +02:00
addFirstSource ( ) ;
2018-03-01 15:42:47 +01:00
mainWindowController . setColor ( " ee3523 " ) ;
2018-04-07 17:26:56 +02:00
mainWindowController . setFontSize ( 17 . 0 ) ;
2018-03-01 15:42:47 +01:00
mainWindowController . setAutoUpdate ( false ) ;
2017-09-16 18:22:46 +02:00
mainWindowController . setLocal ( local ) ;
2018-05-17 18:58:54 +02:00
saveSettings ( ) ;
2017-09-16 18:22:46 +02:00
}
2018-03-01 15:42:47 +01:00
if ( ! posterCache . exists ( ) ) {
2017-09-16 18:22:46 +02:00
posterCache . mkdir ( ) ;
}
2018-10-11 12:29:26 +02:00
mainWindowController . init ( ) ;
2016-08-14 15:17:14 +02:00
} catch ( IOException e ) {
2018-03-01 15:42:47 +01:00
LOGGER . error ( e ) ;
2016-08-14 15:17:14 +02:00
}
}
2018-03-29 18:15:57 +02:00
2018-05-17 18:58:54 +02:00
/ * *
* set the log file location and initialize the logger launch the GUI
* @param args arguments given at the start
* /
public static void main ( String [ ] args ) {
if ( osName . contains ( " Windows " ) ) {
dirHomeFlix = userHome + " /Documents/HomeFlix " ;
} else {
dirHomeFlix = userHome + " /HomeFlix " ;
}
// 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 " ) ;
logFile . delete ( ) ;
LOGGER = LogManager . getLogger ( Main . class . getName ( ) ) ;
launch ( args ) ;
}
2018-03-29 18:15:57 +02:00
/ * *
2018-04-02 18:29:59 +02:00
* 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
2018-03-29 11:26:20 +02:00
* /
2018-10-11 12:29:26 +02:00
void addFirstSource ( ) {
2018-04-07 17:26:56 +02:00
switch ( local ) {
2018-03-04 20:07:43 +01:00
case " en_US " :
bundle = ResourceBundle . getBundle ( " locals.HomeFlix-Local " , Locale . US ) ; // us_english
break ;
case " de_DE " :
2018-04-28 18:02:02 +02:00
bundle = ResourceBundle . getBundle ( " locals.HomeFlix-Local " , Locale . GERMAN ) ; // de_german
2018-03-04 20:07:43 +01:00
break ;
default :
bundle = ResourceBundle . getBundle ( " locals.HomeFlix-Local " , Locale . US ) ; // default local
break ;
}
2017-02-09 16:27:52 +01:00
2018-03-29 18:15:57 +02:00
JFX2BtnCancelAlert selectFirstSource = new JFX2BtnCancelAlert ( bundle . getString ( " addSourceHeader " ) ,
bundle . getString ( " addSourceBody " ) ,
" -fx-button-type: RAISED; -fx-background-color: #ee3523; -fx-text-fill: BLACK; " ,
bundle . getString ( " addDirectory " ) , bundle . getString ( " addStreamSource " ) ,
bundle . getString ( " cancelBtnText " ) , primaryStage ) ;
// directory action
EventHandler < ActionEvent > btn1Action = new EventHandler < ActionEvent > ( ) {
@Override
public void handle ( ActionEvent event ) {
DirectoryChooser directoryChooser = new DirectoryChooser ( ) ;
directoryChooser . setTitle ( bundle . getString ( " addDirectory " ) ) ;
File selectedFolder = directoryChooser . showDialog ( primaryStage ) ;
if ( selectedFolder ! = null & & selectedFolder . exists ( ) ) {
mainWindowController . addSource ( selectedFolder . getPath ( ) , " local " ) ;
selectFirstSource . getAlert ( ) . close ( ) ;
} else {
LOGGER . error ( " The selected folder dosen't exist! " ) ;
System . exit ( 1 ) ;
}
2018-03-29 11:26:20 +02:00
}
2018-03-29 18:15:57 +02:00
} ;
// streaming action
EventHandler < ActionEvent > btn2Action = new EventHandler < ActionEvent > ( ) {
@Override
public void handle ( ActionEvent event ) {
FileChooser fileChooser = new FileChooser ( ) ;
fileChooser . setTitle ( " addStreamSource " ) ;
File selectedFile = fileChooser . showOpenDialog ( getPrimaryStage ( ) ) ;
if ( selectedFile ! = null & & selectedFile . exists ( ) ) {
mainWindowController . addSource ( selectedFile . getPath ( ) , " stream " ) ;
selectFirstSource . getAlert ( ) . close ( ) ;
} else {
LOGGER . error ( " The selected file dosen't exist! " ) ;
System . exit ( 1 ) ;
}
2018-03-29 11:26:20 +02:00
}
2018-03-29 18:15:57 +02:00
} ;
2018-10-11 12:29:26 +02:00
2018-03-29 18:15:57 +02:00
selectFirstSource . setBtn1Action ( btn1Action ) ;
selectFirstSource . setBtn2Action ( btn2Action ) ;
selectFirstSource . showAndWait ( ) ;
2016-08-14 15:17:14 +02:00
}
2017-02-09 20:39:43 +01:00
2018-04-02 18:29:59 +02:00
/ * *
2018-05-17 18:58:54 +02:00
* save the configuration to the config . xml file
2018-04-02 18:29:59 +02:00
* /
2018-05-17 18:58:54 +02:00
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 ( ) ) ;
2018-04-28 18:02:02 +02:00
2018-05-17 18:58:54 +02:00
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 ) ;
2017-09-16 18:22:46 +02:00
}
2018-05-17 18:58:54 +02:00
}
/ * *
* 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
2018-04-28 18:02:02 +02:00
2018-05-17 18:58:54 +02:00
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 ) ;
}
2016-08-14 15:17:14 +02:00
}
2018-05-17 18:58:54 +02:00
2017-04-06 12:07:34 +02:00
2018-03-01 15:42:47 +01:00
public Stage getPrimaryStage ( ) {
return primaryStage ;
}
2018-03-29 18:15:57 +02:00
public AnchorPane getPane ( ) {
2018-03-02 13:50:21 +01:00
return pane ;
}
2018-03-01 15:42:47 +01:00
2018-03-01 16:10:37 +01:00
public File getDirectory ( ) {
return directory ;
}
2017-04-09 15:07:56 +02:00
2018-03-01 16:10:37 +01:00
public File getConfigFile ( ) {
return configFile ;
2017-04-09 15:07:56 +02:00
}
2018-03-01 16:10:37 +01:00
public File getPosterCache ( ) {
return posterCache ;
2017-04-09 15:07:56 +02:00
}
2018-09-21 18:57:39 +02:00
}