|
|
|
@ -1,4 +1,5 @@
|
|
|
|
|
package tools;
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.lang.Runtime;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
@ -13,12 +14,14 @@ import java.io.IOException;
|
|
|
|
|
public class Directories {
|
|
|
|
|
private String projectName;
|
|
|
|
|
private String dataHomeDirectory;
|
|
|
|
|
private String os;
|
|
|
|
|
|
|
|
|
|
/** Constructor with projectName parameter.
|
|
|
|
|
* @param projectName name of the project
|
|
|
|
|
*/
|
|
|
|
|
public Directories(String projectName) {
|
|
|
|
|
this.projectName = projectName;
|
|
|
|
|
os = System.getProperty("os.name");
|
|
|
|
|
setDataHomeDirectory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -28,24 +31,21 @@ public class Directories {
|
|
|
|
|
/* Follow XDG Base Directory Specification
|
|
|
|
|
* https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
|
|
|
|
*/
|
|
|
|
|
String d;
|
|
|
|
|
String os = System.getProperty("os.name");
|
|
|
|
|
if (os.equals("Linux")) {
|
|
|
|
|
d = System.getenv().get("XDG_DATA_HOME");
|
|
|
|
|
if (d == null || d.equals("")) {
|
|
|
|
|
d = System.getProperty("user.home") + "/.local/share";
|
|
|
|
|
dataHomeDirectory = System.getenv().get("XDG_DATA_HOME");
|
|
|
|
|
if (dataHomeDirectory == null || dataHomeDirectory.equals("")) {
|
|
|
|
|
dataHomeDirectory = System.getProperty("user.home") + "/.local/share";
|
|
|
|
|
}
|
|
|
|
|
} else if (os.equals("Mac")||os.equals("Mac OS X")) {
|
|
|
|
|
/* Apple MacOS X User Data Directory
|
|
|
|
|
* https://developer.apple.com/library/archive/qa/qa1170/_index.html */
|
|
|
|
|
d = System.getProperty("user.home") + "/Library";
|
|
|
|
|
dataHomeDirectory = System.getProperty("user.home") + "/Library";
|
|
|
|
|
} else {
|
|
|
|
|
d = ".";
|
|
|
|
|
dataHomeDirectory = ".";
|
|
|
|
|
}
|
|
|
|
|
d += "/" + projectName + "/";
|
|
|
|
|
dataHomeDirectory += "/" + projectName + "/";
|
|
|
|
|
// create directory if not already exists
|
|
|
|
|
new File(d).mkdirs();
|
|
|
|
|
dataHomeDirectory = d;
|
|
|
|
|
new File(dataHomeDirectory).mkdirs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Getter for dataHomeDirectory.
|
|
|
|
@ -55,10 +55,10 @@ public class Directories {
|
|
|
|
|
return dataHomeDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void openDataHomeDirectory() {
|
|
|
|
|
/** Opens dataHomeDirectory if supported.
|
|
|
|
|
*/
|
|
|
|
|
private void openDataHomeDirectory() {
|
|
|
|
|
try {
|
|
|
|
|
String os = System.getProperty("os.name");
|
|
|
|
|
|
|
|
|
|
if (os.equals("Linux")) {
|
|
|
|
|
Runtime runtime = Runtime.getRuntime();
|
|
|
|
|
runtime.exec(new String[] { "xdg-open", dataHomeDirectory });
|
|
|
|
@ -66,8 +66,23 @@ public class Directories {
|
|
|
|
|
Runtime runtime = Runtime.getRuntime();
|
|
|
|
|
runtime.exec(new String[] { "open", dataHomeDirectory });
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.out.println("Error encountered while trying to open directory");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Asks the user to choose opening dataHomeDirectory or not.
|
|
|
|
|
*/
|
|
|
|
|
public void askOpenDataHomeDirectory() {
|
|
|
|
|
if (os.equals("Linux") || os.equals("Mac") || os.equals("Mac OS X")) {
|
|
|
|
|
System.out.println("Do you want to open this directory? (y/N)");
|
|
|
|
|
Scanner scanner = new Scanner(System.in);
|
|
|
|
|
String resp = scanner.nextLine();
|
|
|
|
|
if (resp.equals("y") || resp.equals("Y")) {
|
|
|
|
|
System.out.println("Openning");
|
|
|
|
|
openDataHomeDirectory();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|