2017-04-17 00:58:49 +02:00
/ * *
2017-04-19 02:15:44 +02:00
* cemu_UI
*
* Copyright 2017 < @Seil0 >
*
2017-10-15 13:37:45 +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 .
2017-04-19 02:15:44 +02:00
*
2017-10-15 13:37:45 +02:00
* 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-17 00:58:49 +02:00
* /
2017-10-15 13:37:45 +02:00
2017-11-13 16:44:39 +01:00
package com.cemu_UI.controller ;
2017-04-17 00:58:49 +02:00
import java.io.File ;
import java.io.IOException ;
2017-09-12 15:04:21 +02:00
import org.apache.logging.log4j.LogManager ;
import org.apache.logging.log4j.Logger ;
2017-11-13 16:44:39 +01:00
import com.cemu_UI.application.MainWindowController ;
import com.cemu_UI.vendorCloudController.GoogleDriveController ;
2017-05-07 19:57:55 +02:00
import javafx.application.Platform ;
2017-04-17 00:58:49 +02:00
public class CloudController {
2017-11-13 16:44:39 +01:00
public CloudController ( MainWindowController mwc ) {
this . mwc = mwc ;
2017-04-17 00:58:49 +02:00
}
2017-11-13 16:44:39 +01:00
private MainWindowController mwc ;
2017-04-17 00:58:49 +02:00
private GoogleDriveController googleDriveController = new GoogleDriveController ( ) ;
2017-09-12 15:04:21 +02:00
private static final Logger LOGGER = LogManager . getLogger ( CloudController . class . getName ( ) ) ;
2017-04-17 00:58:49 +02:00
2017-11-13 16:44:39 +01:00
public boolean initializeConnection ( String cloudService , String cemuDirectory ) {
2017-10-29 23:27:36 +01:00
boolean success = false ;
2017-09-12 15:04:21 +02:00
LOGGER . info ( " sartting cloud initialisation ... " ) ;
2017-10-29 23:27:36 +01:00
2017-05-25 19:07:16 +02:00
if ( cloudService . equals ( " GoogleDrive " ) ) {
2017-09-12 15:04:21 +02:00
LOGGER . info ( " selected service is Google Drive " ) ;
2017-04-17 00:58:49 +02:00
try {
googleDriveController . main ( cemuDirectory ) ;
} catch ( IOException e ) {
2017-09-12 15:04:21 +02:00
LOGGER . error ( " error while initialize connection " , e ) ;
2017-10-29 23:27:36 +01:00
return success ;
2017-04-17 00:58:49 +02:00
}
2017-12-07 22:52:32 +01:00
success = true ;
2017-04-17 00:58:49 +02:00
}
2017-10-29 23:27:36 +01:00
2017-05-25 19:07:16 +02:00
if ( cloudService . equals ( " Dropbox " ) ) {
2017-09-12 15:04:21 +02:00
LOGGER . info ( " selected service is Dropbox " ) ;
2017-04-17 00:58:49 +02:00
}
2017-09-12 15:04:21 +02:00
LOGGER . info ( " cloud initialisation done! " ) ;
2017-10-29 23:27:36 +01:00
return success ;
2017-04-17 00:58:49 +02:00
}
2017-11-13 16:44:39 +01:00
public void stratupCheck ( String cloudService , String cemuDirectory ) {
2017-05-25 19:07:16 +02:00
if ( cloudService . equals ( " GoogleDrive " ) ) {
2017-09-12 15:04:21 +02:00
LOGGER . info ( " starting startup check google drive ... " ) ;
2017-05-04 22:07:50 +02:00
try {
if ( ! googleDriveController . checkFolder ( ) ) {
googleDriveController . creatFolder ( ) ;
2017-11-13 16:44:39 +01:00
mwc . saveSettings ( ) ;
2017-05-07 19:57:55 +02:00
Thread thread = new Thread ( new Runnable ( ) {
2017-08-30 11:29:58 +02:00
@Override
public void run ( ) {
2017-05-07 19:57:55 +02:00
Platform . runLater ( ( ) - > {
2017-11-13 16:44:39 +01:00
mwc . getPlayBtn ( ) . setText ( " syncing... " ) ;
2017-05-07 19:57:55 +02:00
} ) ;
googleDriveController . uploadAllFiles ( ) ;
Platform . runLater ( ( ) - > {
2017-11-13 16:44:39 +01:00
mwc . getPlayBtn ( ) . setText ( " play " ) ;
2017-05-07 19:57:55 +02:00
} ) ;
}
} ) ;
thread . start ( ) ;
2017-05-04 22:07:50 +02:00
} else {
sync ( cloudService , cemuDirectory ) ;
}
} catch ( IOException e ) {
2017-09-12 15:04:21 +02:00
LOGGER . error ( " google drive startup check failed " , e ) ;
2017-05-04 22:07:50 +02:00
}
}
2017-05-25 19:07:16 +02:00
if ( cloudService . equals ( " Dropbox " ) ) {
2017-05-04 22:07:50 +02:00
}
}
2017-11-13 16:44:39 +01:00
public void sync ( String cloudService , String cemuDirectory ) {
2017-05-01 16:46:00 +02:00
//running sync in a new thread, instead of blocking the main thread
2017-05-07 19:57:55 +02:00
Thread thread = new Thread ( new Runnable ( ) {
2017-08-30 11:29:58 +02:00
@Override
2017-05-07 19:57:55 +02:00
public void run ( ) {
Platform . runLater ( ( ) - > {
2017-12-07 22:52:32 +01:00
mwc . getPlayBtn ( ) . setText ( " syncing... " ) ;
2017-05-07 19:57:55 +02:00
} ) ;
2017-09-12 15:04:21 +02:00
LOGGER . info ( " starting synchronization in new thread ... " ) ;
2017-05-01 16:46:00 +02:00
2017-05-25 19:07:16 +02:00
if ( cloudService . equals ( " GoogleDrive " ) ) {
2017-05-01 16:46:00 +02:00
try {
googleDriveController . sync ( cemuDirectory ) ;
} catch ( IOException e ) {
2017-09-12 15:04:21 +02:00
LOGGER . error ( " google drive synchronization failed " , e ) ;
2017-05-01 16:46:00 +02:00
}
}
2017-05-25 19:07:16 +02:00
if ( cloudService . equals ( " Dropbox " ) ) {
2017-05-01 16:46:00 +02:00
}
2017-05-07 19:57:55 +02:00
Platform . runLater ( ( ) - > {
2017-12-07 22:52:32 +01:00
mwc . getPlayBtn ( ) . setText ( " play " ) ;
2017-11-27 21:57:59 +01:00
mwc . saveSettings ( ) ;
2017-05-07 19:57:55 +02:00
} ) ;
2017-09-12 15:04:21 +02:00
LOGGER . info ( " synchronization successful! " ) ;
2017-05-01 16:46:00 +02:00
}
2017-05-07 19:57:55 +02:00
} ) ;
thread . start ( ) ;
2017-05-01 16:46:00 +02:00
2017-04-17 00:58:49 +02:00
}
void uploadFile ( String cloudService , File file ) {
2017-05-01 16:46:00 +02:00
//running uploadFile in a new thread, instead of blocking the main thread
new Thread ( ) {
2017-08-30 11:29:58 +02:00
@Override
public void run ( ) {
2017-09-12 15:04:21 +02:00
LOGGER . info ( " starting uploadFile in new thread ... " ) ;
2017-05-01 16:46:00 +02:00
2017-05-25 19:07:16 +02:00
if ( cloudService . equals ( " GoogleDrive " ) ) {
2017-05-01 16:46:00 +02:00
try {
googleDriveController . uploadFile ( file ) ;
} catch ( IOException e ) {
2017-09-12 15:04:21 +02:00
LOGGER . error ( " google drive uploadFile failed " , e ) ;
2017-05-01 16:46:00 +02:00
}
}
2017-05-25 19:07:16 +02:00
if ( cloudService . equals ( " Dropbox " ) ) {
2017-04-17 00:58:49 +02:00
2017-05-01 16:46:00 +02:00
}
}
} . start ( ) ;
2017-04-17 00:58:49 +02:00
}
public String getFolderID ( String cloudService ) {
String folderID = " " ;
2017-08-11 20:01:43 +02:00
if ( cloudService ! = null ) {
if ( cloudService . equals ( " GoogleDrive " ) ) {
folderID = googleDriveController . getFolderID ( ) ;
}
if ( cloudService . equals ( " Dropbox " ) ) {
}
2017-04-17 00:58:49 +02:00
}
return folderID ;
}
public void setFolderID ( String folderID , String cloudService ) {
2017-08-11 20:01:43 +02:00
if ( cloudService ! = null ) {
if ( cloudService . equals ( " GoogleDrive " ) ) {
googleDriveController . setFolderID ( folderID ) ;
}
if ( cloudService . equals ( " Dropbox " ) ) {
}
2017-04-17 00:58:49 +02:00
}
}
}