Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expand manual in the application data directory to allow users to keep bookmark on the browser #1277

Merged
merged 9 commits into from
Feb 27, 2025
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,7 @@ tasks.jacocoTestCoverageVerification {

tasks.getByName("run") {
jvmArgs(["--add-opens", "java.desktop/sun.awt.X11=ALL-UNNAMED"])
dependsOn manualZips
}

tasks.register('debug', JavaExec) {
Expand All @@ -1545,6 +1546,7 @@ tasks.register('runOnJava17', JavaExec) {
mainClass.set application.mainClass
classpath = sourceSets.main.runtimeClasspath
jvmArgs(["--add-opens", "java.desktop/sun.awt.X11=ALL-UNNAMED"])
dependsOn manualZips
group = 'application'
}

Expand All @@ -1557,6 +1559,7 @@ tasks.register('runOnJava21', JavaExec) {
mainClass.set application.mainClass
classpath = sourceSets.main.runtimeClasspath
jvmArgs(["--add-opens", "java.desktop/sun.awt.X11=ALL-UNNAMED"])
dependsOn manualZips
group = 'application'
}

Expand Down
1 change: 1 addition & 0 deletions src/org/omegat/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,7 @@ SU_CONFIG_DIR_CREATE_ERROR=Your operating system refused to let OmegaT\n\
SU_SCRIPT_DIR_CREATE_ERROR=It was not possible to create the script folder\n\
inside the configuration folder. The configuration folder will be used\n\
instead.
SU_DATA_DIR_CREATE_ERROR=Your operating system refused to let OmegaT create the application data folder.

# HttpConnectionUtils

Expand Down
34 changes: 9 additions & 25 deletions src/org/omegat/help/Help.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
2007 Didier Briel
2009 Alex Buloichik
2015 Aaron Madlon-Kay
2023 Hiroshi Miura
2023-2025 Hiroshi Miura
Home page: https://www.omegat.org/
Support center: https://omegat.org/support

Expand Down Expand Up @@ -40,10 +40,8 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Comparator;
import java.util.Properties;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

Expand Down Expand Up @@ -89,6 +87,7 @@ public static void showJavadoc() throws IOException {
* Shows help in the system browser.
*
* @throws IOException
* when URI creation failed.
*/
public static void showHelp() throws IOException {
String lang = detectHelpLanguage();
Expand Down Expand Up @@ -125,7 +124,12 @@ private static URI getHelpZipFileURI(String lang) {
return null;
}
try {
Path destinationDir = Files.createTempDirectory("omegat-" + OStrings.VERSION + "-help-" + lang);
Path destinationDir = Paths.get(StaticUtils.getApplicationDataDir(), "manual", OStrings.VERSION, lang);
Path indexPath = destinationDir.resolve("index.html");
if (indexPath.toFile().exists()) {
// already have manual
return indexPath.toUri();
}
return extractZip(zipFile, destinationDir).toURI();
} catch (IOException ignored) {
}
Expand All @@ -144,29 +148,9 @@ private static File extractZip(File file, Path destinationDir) throws IOExceptio
zipInputStream.closeEntry();
}
}
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
cleanUp(destinationDir);
} catch (IOException e) {
e.printStackTrace();
}
}));
return destinationDir.resolve(OConsts.HELP_HOME).toFile();
}

private static void cleanUp(Path destinationDir) throws IOException {
if (Files.exists(destinationDir)) {
try (Stream<Path> walk = Files.walk(destinationDir)) {
walk.sorted(Comparator.reverseOrder()).forEachOrdered(file -> {
try {
Files.delete(file);
} catch (IOException ignored) {
}
});
}
}
}

public static URI getHelpFileURI(String filename) {
return getHelpFileURI(null, filename);
}
Expand Down Expand Up @@ -223,7 +207,7 @@ public static String errorHaiku() {

/**
* Detects the documentation language to use.
*
* <p>
* If the latest manual is not available in the system locale language, it
* returns null, i.e. show a language selection screen.
*/
Expand Down
75 changes: 71 additions & 4 deletions src/org/omegat/util/StaticUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,37 @@ private StaticUtils() {
}

/**
* Configuration directory on Windows platforms
* Configuration directory on Windows platforms.
*/
private static final String WINDOWS_ROAMING_DATA_DIR = "AppData\\Roaming";
private static final String WINDOWS_CONFIG_DIR = "\\OmegaT\\";

/**
* Configuration directory on UNIX platforms
* Configuration directory on UNIX platforms.
*/
private static final String UNIX_CONFIG_DIR = "/.omegat/";

/**
* Configuration directory on Mac OS X
* Configuration directory on macOS.
*/
private static final String OSX_CONFIG_DIR = "/Library/Preferences/OmegaT/";

/**
* Application data directory on Windows platforms.
*/
private static final String WINDOWS_LOCAL_DATA_DIR = "AppData\\Local";
private static final String WINDOWS_DATA_DIR = "\\OmegaT\\";

/**
* Application data directory on UNIX platforms.
*/
private static final String UNIX_DATA_DIR = "/.local/share/OmegaT/";

/**
* Application data directory on macOS.
*/
private static final String OSX_DATA_DIR = "/Library/Application Support/OmegaT/";

/**
* Script directory
*/
Expand Down Expand Up @@ -258,7 +275,7 @@ public static String getConfigDir() {
// Trying first Vista/7, because "Application Data" exists also as
// virtual folder,
// so we would not be able to differentiate with 2000/XP otherwise
File appDataFile = new File(home, "AppData\\Roaming");
File appDataFile = new File(home, WINDOWS_ROAMING_DATA_DIR);
if (appDataFile.exists()) {
appData = appDataFile.getAbsolutePath();
} else {
Expand Down Expand Up @@ -329,6 +346,56 @@ public static String getConfigDir() {
return configDir;
}

/**
* Get application data directory.
* @return directory path to store application data.
*/
public static String getApplicationDataDir() {
String dataDir = null;
String home = getHomeDir();
// if os or user home is null or empty, we cannot reliably determine
// the data dir, so we use the current working dir (= empty string)
if (StringUtil.isEmpty(home)) {
dataDir = new File(".").getAbsolutePath() + File.separator;
return dataDir;
}

if (Platform.isWindows) {
File appDataFile = new File(home, WINDOWS_LOCAL_DATA_DIR);
if (appDataFile.exists()) {
dataDir = appDataFile.getAbsolutePath() + WINDOWS_DATA_DIR;
}
} else if (Platform.isUnixLike()) {
dataDir = home + UNIX_DATA_DIR;
} else if (Platform.isMacOSX()) {
// "~/Library/Application Suppport/OmegaT/"
dataDir = home + OSX_DATA_DIR;
} else {
// use the user's home directory by default
dataDir = home + File.separator;
}
if (dataDir == null || dataDir.isEmpty()) {
return new File(".").getAbsolutePath() + File.separator;
}
try {
// check if the dir exists
File dir = new File(dataDir);
if (!dir.exists()) {
// create the dir
boolean created = dir.mkdirs();
if (!created) {
Log.logErrorRB("SU_DATA_DIR_CREATE_ERROR");
dataDir = new File(".").getAbsolutePath() + File.separator;
}
}
} catch (SecurityException e) {
// the system doesn't want us to write where we want to write
dataDir = new File(".").getAbsolutePath() + File.separator;
Log.log(e.toString());
}
return dataDir;
}

public static String getHomeDir() {
String home; // user home directory
// get os and user home properties
Expand Down
Loading