google drive integration v2/cloud integration clean up

* Clean up the cloud part
* Added an alert to verify activation of cloud sync
This commit is contained in:
Seil0 2017-05-07 19:57:55 +02:00
parent e796b582fa
commit ef2d913ace
11 changed files with 70 additions and 82 deletions

Binary file not shown.

View File

@ -17,6 +17,7 @@ import java.io.File;
import java.io.IOException;
import cloudControllerInstances.GoogleDriveController;
import javafx.application.Platform;
public class CloudController {
@ -24,12 +25,11 @@ public class CloudController {
main = ma;
}
@SuppressWarnings("unused")//TODO
private Main main;
Main main;
private GoogleDriveController googleDriveController = new GoogleDriveController();
void initializeConnection(String cloudService, String cemuDirectory) {
System.out.println("sartting initialisation... ");
System.out.println("sartting cloud initialisation... ");
if(cloudService == "GoogleDrive") {
try {
googleDriveController.main(cemuDirectory);
@ -40,7 +40,7 @@ public class CloudController {
if(cloudService == "Dropbox") {
}
System.out.println("done!");
System.out.println("cloud initialisation done!");
}
void stratupCheck(String cloudService, String cemuDirectory) {
@ -50,7 +50,19 @@ public class CloudController {
if (!googleDriveController.checkFolder()) {
googleDriveController.creatFolder();
main.mainWindowController.saveSettings();
googleDriveController.uploadAllFiles();
Thread thread = new Thread(new Runnable() {
public void run() {
Platform.runLater(() -> {
main.mainWindowController.getPlayBtn().setText("syncing...");
});
googleDriveController.uploadAllFiles();
Platform.runLater(() -> {
main.mainWindowController.getPlayBtn().setText("play");
});
}
});
thread.start();
} else {
sync(cloudService, cemuDirectory);
}
@ -68,8 +80,11 @@ public class CloudController {
void sync(String cloudService, String cemuDirectory) {
//running sync in a new thread, instead of blocking the main thread
new Thread() {
public void run() {
Thread thread = new Thread(new Runnable() {
public void run() {
Platform.runLater(() -> {
main.mainWindowController.getPlayBtn().setText("syncing...");
});
System.out.println("starting sync in new thread...");
if(cloudService == "GoogleDrive") {
@ -82,8 +97,13 @@ public class CloudController {
if(cloudService == "Dropbox") {
}
Platform.runLater(() -> {
main.mainWindowController.getPlayBtn().setText("play");
});
System.out.println("sync finished!");
}
}.start();
});
thread.start();
}

View File

@ -35,7 +35,7 @@ import javafx.scene.layout.AnchorPane;
public class Main extends Application {
Stage primaryStage;
MainWindowController mainWindowController;
public MainWindowController mainWindowController; //TODO find a better way
CloudController cloudController;
private String dirWin = System.getProperty("user.home") + "/Documents/cemu_UI"; //Windows: C:/Users/"User"/Documents/HomeFlix
private String dirLinux = System.getProperty("user.home") + "/cemu_UI"; //Linux: /home/"User"/HomeFlix
@ -120,12 +120,11 @@ public class Main extends Application {
//loading settings and initialize UI, dbController.main() loads all databases
mainWindowController.loadSettings();
mainWindowController.dbController.main();
if(mainWindowController.isCloudSync()) {
cloudController.initializeConnection(mainWindowController.getCloudService(), mainWindowController.getCemuPath());
cloudController.stratupCheck(mainWindowController.getCloudService(), mainWindowController.getCemuPath());
// mainWindowController.saveSettings();//TODO find a better way
}
mainWindowController.dbController.main();
mainWindowController.addUIData();
mainWindowController.initActions();
mainWindowController.initUI();

View File

@ -146,7 +146,7 @@ public class MainWindowController {
private String selectedGameTitle;
private String color;
private String version = "0.1.5";
private String buildNumber = "010";
private String buildNumber = "011";
private String versionName = "Gusty Garden";
private int xPos = -200;
private int yPos = 17;
@ -194,7 +194,7 @@ public class MainWindowController {
}
void initActions() {
System.out.print("initializing Actions... ");
System.out.println("initializing Actions... ");
HamburgerBackArrowBasicTransition burgerTask = new HamburgerBackArrowBasicTransition(menuHam);
menuHam.addEventHandler(MouseEvent.MOUSE_PRESSED, (e)->{
@ -419,7 +419,7 @@ public class MainWindowController {
}
}
});
System.out.println("done!");
System.out.println("initializing Actions done!");
}
@FXML
@ -540,9 +540,20 @@ public class MainWindowController {
if(cloudSync) {
cloudSync = false;
} else {
cloudSync = true;
main.cloudController.initializeConnection(getCloudService(), getCemuPath());
main.cloudController.sync(getCloudService(), getCemuPath());
Alert cloudWarningAlert = new Alert(AlertType.CONFIRMATION); //new alert with file-chooser
cloudWarningAlert.setTitle("cemu_UI");
cloudWarningAlert.setHeaderText("activate cloud savegame sync (beta)");
cloudWarningAlert.setContentText("You just activate the cloud savegame sync function of cemu_UI which is currently in beta. Are you sure you want to do this?");
cloudWarningAlert.initOwner(main.primaryStage);
Optional<ButtonType> coverResult = cloudWarningAlert.showAndWait();
if (coverResult.get() == ButtonType.OK){
cloudSync = true;
main.cloudController.initializeConnection(getCloudService(), getCemuPath());
main.cloudController.sync(getCloudService(), getCemuPath());
} else {
cloudSyncToggleBtn.setSelected(false);
}
}
saveSettings();//TODO remove (only save on exit settings)
}
@ -1029,4 +1040,12 @@ public class MainWindowController {
this.cloudService = cloudService;
}
public JFXButton getPlayBtn() {
return playBtn;
}
public void setPlayBtn(JFXButton playBtn) {
this.playBtn = playBtn;
}
}

View File

@ -8,7 +8,6 @@ import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.apache.commons.io.FileUtils;
@ -112,49 +111,18 @@ public class GoogleDriveController {
public void sync(String cemuDirectory) throws IOException {
getLocalSavegames();
//
// if (!checkFolder()) {
// creatFolder();
//
// for (int i = 0; i < localSavegames.size(); i++) {
// uploadFile(localSavegames.get(i));
// }
// } else {
getCloudSavegames();
// System.out.println(cloudSavegames.size() + "; " + localSavegames.size() + "; " + localSavegamesName.size());
// download files from cloud which don't exist locally
for (int i = 0; i < cloudSavegames.size(); i++) {
// System.out.println(localSavegamesName.get(i)+";"+cloudSavegames.get(i).getName());
// System.out.println(localSavegames.get(a).getName()+";"+cloudSavegames.get(i).getName().substring(9,cloudSavegames.get(i).getName().length()));
// if the file exists locally, check which one is newer
if (localSavegamesName.contains(cloudSavegames.get(i).getName())) {
int localSavegamesNumber = localSavegamesName.indexOf(cloudSavegames.get(i).getName());
long localModified = new DateTime(localSavegames.get(localSavegamesNumber).lastModified()).getValue();
long cloudModified = cloudSavegames.get(i).getModifiedTime().getValue();
FileInputStream fis = new FileInputStream(localSavegames.get(localSavegamesNumber));
// // System.out.println(localSavegamesNumber);
//// System.out.println(localSavegames.get(localSavegamesNumber).getName() + "; " + cloudSavegames.get(i).getName());
// System.out.println(localModified + "; " + cloudModified);
//// System.out.println(new Date(localModified) + "; " + new Date(cloudModified));
// if (localModified == cloudModified) {
// System.out.println("both files are the same, nothing to do \n");
// } else if (localModified >= cloudModified) {
// System.out.println("\nlocal is newer, going to upload local file");
// System.out.println("uploading "+ localSavegames.get(localSavegamesNumber).getName()+"("+new Date(localModified)+")");
//// updateFile(cloudSavegames.get(i), localSavegames.get(localSavegamesNumber));
// } else {
// System.out.println("\ncloud is newer, going to download cloud file");
// System.out.println("downloading "+ cloudSavegames.get(i).getName() + "(" + new Date(cloudModified) + ")");
//// downloadFile(cloudSavegames.get(i));
// }
FileInputStream fis = new FileInputStream(localSavegames.get(localSavegamesNumber));
if (cloudSavegames.get(i).getMd5Checksum().equals(org.apache.commons.codec.digest.DigestUtils.md5Hex(fis))) {
System.out.println("both files are the same, nothing to do");
@ -181,8 +149,6 @@ public class GoogleDriveController {
uploadFile(localSavegames.get(j));
}
}
// }
}
//create a folder in google drive
@ -217,16 +183,11 @@ public class GoogleDriveController {
private void getLocalSavegames() throws IOException {
java.io.File dir = new java.io.File(cemuDirectory+"/mlc01/emulatorSave");
String[] extensions = new String[] { "dat" };
System.out.println("Getting all .dat files in " + dir.getCanonicalPath()+" including those in subdirectories \n");
System.out.println("Getting all .dat files in " + dir.getCanonicalPath()+" including those in subdirectories");
List<java.io.File> files = (List<java.io.File>) FileUtils.listFiles(dir, extensions, true);
for (java.io.File file : files) {
localSavegamesName.add(file.getParentFile().getName()+"_"+file.getName());
localSavegames.add(file);
// System.out.println(file.getAbsolutePath());
// System.out.println(file.getParentFile().getName());
// System.out.println(file.lastModified());
// System.out.println(file.getName()+"\n");
}
}
@ -239,8 +200,6 @@ public class GoogleDriveController {
for (File file : files.getFiles()) {
cloudSavegamesName.add(file.getName());
cloudSavegames.add(file);
// System.out.println(file.getName());
// System.out.println(file.getModifiedTime()+"\n");
}
}
@ -250,29 +209,23 @@ public class GoogleDriveController {
File fileMetadata = new File();
fileMetadata.setName(uploadFile.getParentFile().getName()+"_"+uploadFile.getName());
fileMetadata.setParents(Collections.singletonList(folderID));
// System.out.println(new DateTime(uploadFile.lastModified())+"; "+ new DateTime(uploadFile.lastModified()).getTimeZoneShift());
fileMetadata.setModifiedTime(new DateTime(uploadFile.lastModified()));
// System.out.println(fileMetadata.getModifiedTime()+"; "+fileMetadata.getModifiedTime().getTimeZoneShift());
FileContent mediaContent = new FileContent("", uploadFile);
File file = service.files().create(fileMetadata, mediaContent).setFields("id, parents").execute();
System.out.println("File ID: " + file.getId());
System.out.println("upload successfull, File ID: " + file.getId());
}
//download a file from the cloud to the local savegames folder
private void downloadFile(File downloadFile) throws IOException{
System.out.print("downloading "+downloadFile.getName()+"... ");
// String directory = downloadFile.getName().substring(0,8);
java.io.File directoryFile = new java.io.File(cemuDirectory+"/mlc01/emulatorSave/"+ downloadFile.getName().substring(0,8));
String file = downloadFile.getName().substring(9,downloadFile.getName().length());
// System.out.println(cemuDirectory+"/mlc01/emulatorSave/"+ directory +"/"+ file);
// System.out.println("DownloadfileID: " + downloadFile.getId());
if(!directoryFile.exists()) {
System.out.println("dir dosent exist");
directoryFile.mkdir();
}
OutputStream outputStream = new FileOutputStream(directoryFile +"/"+ file); //TODO needs to be tested
OutputStream outputStream = new FileOutputStream(directoryFile +"/"+ file);
service.files().get(downloadFile.getId()).executeMediaAndDownloadTo(outputStream);
System.out.println("done");
@ -295,20 +248,17 @@ public class GoogleDriveController {
}
public void uploadAllFiles() {
new Thread() {
public void run() {
try {
getLocalSavegames();
for (int i = 0; i < localSavegames.size(); i++) {
uploadFile(localSavegames.get(i));
}
} catch (IOException e) {
//Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
try {
getLocalSavegames();
System.out.println("uploading " + localSavegames.size() + " files...");
for (int i = 0; i < localSavegames.size(); i++) {
uploadFile(localSavegames.get(i));
}
} catch (IOException e) {
//Auto-generated catch block
e.printStackTrace();
}
}
public String getFolderID() {