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 .
*
* /
2017-04-15 17:54:47 +02:00
2018-03-01 15:42:47 +01:00
package kellerkinder.HomeFlix.application ;
2017-02-09 16:27:52 +01:00
2016-08-14 15:17:14 +02:00
import java.io.File ;
import java.io.IOException ;
2017-02-09 16:27:52 +01:00
import java.util.Locale ;
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
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 " ) ;
private String dirWin = userHome + " /Documents/HomeFlix " ; // Windows: C:/Users/"User"/Documents/HomeFlix
private String dirLinux = userHome + " /HomeFlix " ; // Linux: /home/"User"/HomeFlix
2018-03-01 15:42:47 +01:00
private File directory ;
private File configFile ;
private File posterCache ;
2018-03-29 18:15:57 +02:00
2017-04-06 12:07:34 +02:00
private String FONT_FAMILY = " System " ;
2018-03-29 18:15:57 +02:00
private String local = System . getProperty ( " user.language " ) + " _ " + System . getProperty ( " user.country " ) ;
2017-04-06 12:07:34 +02:00
private double FONT_SIZE = 17 ;
2017-02-09 20:39:43 +01:00
private ResourceBundle bundle ;
2017-09-16 18:22:46 +02:00
private static Logger LOGGER ;
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 ;
2016-08-14 15:17:14 +02:00
mainWindow ( ) ;
}
2018-03-29 18:15:57 +02:00
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 ( ) ;
loader . setLocation ( ClassLoader . getSystemResource ( " fxml/MainWindow.fxml " ) ) ;
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
2017-09-16 18:22:46 +02:00
mainWindowController = loader . getController ( ) ; //Link of FXMLController and controller class
mainWindowController . setMain ( this ) ; //call setMain
2018-03-01 15:42:47 +01:00
2017-09-16 18:22:46 +02:00
2018-03-01 15:42:47 +01:00
// get OS and the specific paths
2018-03-28 21:58:01 +02:00
if ( osName . contains ( " Windows " ) ) {
2017-09-16 18:22:46 +02:00
directory = new File ( dirWin ) ;
2018-03-01 15:42:47 +01:00
configFile = new File ( dirWin + " /config.xml " ) ;
posterCache = new File ( dirWin + " /posterCache " ) ;
2018-03-01 16:10:37 +01:00
} else {
directory = new File ( dirLinux ) ;
configFile = new File ( dirLinux + " /config.xml " ) ;
posterCache = new File ( dirLinux + " /posterCache " ) ;
2017-09-16 18:22:46 +02: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-03-01 15:42:47 +01:00
// startup checks
if ( ! configFile . exists ( ) ) {
2018-03-29 11:26:20 +02:00
directory . mkdir ( ) ;
getFirstSource ( ) ;
2018-03-01 15:42:47 +01:00
mainWindowController . setColor ( " ee3523 " ) ;
2017-09-16 18:22:46 +02:00
mainWindowController . setSize ( FONT_SIZE ) ;
2018-03-01 15:42:47 +01:00
mainWindowController . setAutoUpdate ( false ) ;
2017-09-16 18:22:46 +02:00
mainWindowController . setLocal ( local ) ;
mainWindowController . saveSettings ( ) ;
}
2018-03-01 15:42:47 +01:00
if ( ! posterCache . exists ( ) ) {
2017-09-16 18:22:46 +02:00
posterCache . mkdir ( ) ;
}
2018-03-01 15:42:47 +01:00
// init here as it loads the games to the mwc and the gui, therefore the window must exist
mainWindowController . init ( ) ;
2018-03-07 23:51:02 +01:00
mainWindowController . getDbController ( ) . 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
/ * *
* TODO add option to add streaming as first source when there i no config . xml
* we need to get the path for the first source from the user
2018-03-29 11:26:20 +02:00
* /
2018-03-29 18:15:57 +02:00
private void getFirstSource ( ) {
2018-03-04 20:07:43 +01:00
switch ( System . getProperty ( " user.language " ) + " _ " + System . getProperty ( " user.country " ) ) {
case " en_US " :
bundle = ResourceBundle . getBundle ( " locals.HomeFlix-Local " , Locale . US ) ; // us_english
break ;
case " de_DE " :
bundle = ResourceBundle . getBundle ( " locals.HomeFlix-Local " , Locale . GERMAN ) ; // German
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
} ;
selectFirstSource . setBtn1Action ( btn1Action ) ;
selectFirstSource . setBtn2Action ( btn2Action ) ;
selectFirstSource . showAndWait ( ) ;
2016-08-14 15:17:14 +02:00
}
2017-02-09 20:39:43 +01:00
2016-08-14 15:17:14 +02:00
public static void main ( String [ ] args ) {
2018-03-29 18:15:57 +02:00
if ( System . getProperty ( " os.name " ) . equals ( " Windows " ) ) {
2018-03-01 16:10:37 +01:00
System . setProperty ( " logFilename " , userHome + " /Documents/HomeFlix/app.log " ) ;
File logFile = new File ( userHome + " /Documents/HomeFlix/app.log " ) ;
2017-09-16 18:22:46 +02:00
logFile . delete ( ) ;
2018-03-29 18:15:57 +02:00
} else {
2018-03-01 16:10:37 +01:00
System . setProperty ( " logFilename " , userHome + " /HomeFlix/app.log " ) ;
File logFile = new File ( userHome + " /HomeFlix/app.log " ) ;
2017-09-16 18:22:46 +02:00
logFile . delete ( ) ;
}
LOGGER = LogManager . getLogger ( Main . class . getName ( ) ) ;
2016-08-14 15:17:14 +02:00
launch ( args ) ;
}
2017-04-06 12:07:34 +02:00
2018-03-01 15:42:47 +01:00
public Stage getPrimaryStage ( ) {
return primaryStage ;
}
public void setPrimaryStage ( Stage primaryStage ) {
this . primaryStage = 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
2017-04-06 12:07:34 +02:00
public String getFONT_FAMILY ( ) {
return FONT_FAMILY ;
}
public void setFONT_FAMILY ( String FONT_FAMILY ) {
this . FONT_FAMILY = FONT_FAMILY ;
}
2018-03-29 18:15:57 +02: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
}
2016-08-14 15:17:14 +02:00
}