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 ;
2016-08-14 15:17:14 +02:00
import java.util.Optional ;
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 12:27:44 +02:00
import com.jfoenix.controls.JFXAlert ;
import com.jfoenix.controls.JFXButton ;
import com.jfoenix.controls.JFXDialogLayout ;
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.control.Alert ;
import javafx.scene.control.Alert.AlertType ;
2018-03-29 11:26:20 +02:00
import javafx.scene.control.ButtonBar.ButtonData ;
2016-08-14 15:17:14 +02:00
import javafx.scene.control.ButtonType ;
import javafx.scene.image.Image ;
import javafx.scene.layout.AnchorPane ;
2018-03-29 12:27:44 +02:00
import javafx.scene.text.Text ;
2016-08-14 15:17:14 +02:00
import javafx.stage.DirectoryChooser ;
2018-03-29 11:26:20 +02:00
import javafx.stage.FileChooser ;
2018-03-29 12:27:44 +02:00
import javafx.stage.Modality ;
2016-08-14 15:17:14 +02:00
import javafx.stage.Stage ;
public class Main extends Application {
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 " ) ;
private static String javaVend = System . getProperty ( " java.vendor " ) ;
2018-03-01 16:10:37 +01:00
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 ;
2017-04-06 12:07:34 +02:00
private String FONT_FAMILY = " System " ;
2017-02-09 20:39:43 +01: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 ;
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 ) ;
2017-06-04 20:13:04 +02:00
this . primaryStage = primaryStage ;
2016-08-14 15:17:14 +02:00
mainWindow ( ) ;
}
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-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
// 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 11:26:20 +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
* /
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 11:26:20 +02:00
// // 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");
// } else {
// LOGGER.error("The selected folder dosen't exist!");
// }
// }
// };
//
// // 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");
// } else {
// LOGGER.error("The selected file dosen't exist!");
// }
// }
// };
//
// JFXDirStrmCancelDialog selectFirstSource = new JFXDirStrmCancelDialog(bundle.getString("addSourceHeader"),
// bundle.getString("addSourceBody"), "", 200, 100, btn1Action, btn2Action, pane, bundle);
// selectFirstSource.show();
2018-03-13 18:08:30 +01:00
Alert alert = new Alert ( AlertType . CONFIRMATION ) ; //new alert with DirectoryChooser
2016-08-14 15:17:14 +02:00
alert . setTitle ( " Project HomeFlix " ) ;
2018-03-29 11:26:20 +02:00
alert . setHeaderText ( bundle . getString ( " addSourceHeader " ) ) ;
alert . setContentText ( bundle . getString ( " addSourceBody " ) ) ;
2018-03-13 18:08:30 +01:00
alert . setResizable ( true ) ;
2018-03-29 11:26:20 +02:00
ButtonType buttonDirectory = new ButtonType ( bundle . getString ( " addDirectory " ) ) ;
ButtonType buttonStreaming = new ButtonType ( bundle . getString ( " addStreamSource " ) ) ;
ButtonType buttonCancel = new ButtonType ( " Cancel " , ButtonData . CANCEL_CLOSE ) ;
alert . getButtonTypes ( ) . setAll ( buttonDirectory , buttonStreaming , buttonCancel ) ;
2016-08-14 15:17:14 +02:00
Optional < ButtonType > result = alert . showAndWait ( ) ;
2018-03-29 11:26:20 +02:00
if ( result . get ( ) = = buttonDirectory ) {
2016-08-14 15:17:14 +02:00
DirectoryChooser directoryChooser = new DirectoryChooser ( ) ;
2018-03-29 11:26:20 +02:00
directoryChooser . setTitle ( bundle . getString ( " addDirectory " ) ) ;
File selectedFolder = directoryChooser . showDialog ( primaryStage ) ;
if ( selectedFolder ! = null & & selectedFolder . exists ( ) ) {
mainWindowController . addSource ( selectedFolder . getPath ( ) , " local " ) ;
} else {
LOGGER . error ( " The selected folder dosen't exist! " ) ;
System . exit ( 1 ) ;
}
} else if ( result . get ( ) = = buttonStreaming ) {
FileChooser fileChooser = new FileChooser ( ) ;
fileChooser . setTitle ( " addStreamSource " ) ;
File selectedFile = fileChooser . showOpenDialog ( getPrimaryStage ( ) ) ;
if ( selectedFile ! = null & & selectedFile . exists ( ) ) {
mainWindowController . addSource ( selectedFile . getPath ( ) , " stream " ) ;
} else {
LOGGER . error ( " The selected file dosen't exist! " ) ;
System . exit ( 1 ) ;
}
2016-08-14 15:17:14 +02:00
} else {
2018-03-29 11:26:20 +02:00
LOGGER . warn ( " No source selected! " ) ;
2018-03-13 18:08:30 +01:00
System . exit ( 1 ) ;
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-01 16:10:37 +01:00
if ( System . getProperty ( " os.name " ) . equals ( " Windows " ) ) {
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 ( ) ;
} 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-02 13:50:21 +01:00
public AnchorPane getPane ( ) {
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-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
}