From fc124e128151a2d78ed5b81bc6b9dd7b64159dae Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Sun, 23 Feb 2025 12:14:50 +0900 Subject: [PATCH 1/9] fix: expand manual in the configuration directory - allow users to make bookmark in the browser Signed-off-by: Hiroshi Miura --- build.gradle | 3 +++ src/org/omegat/help/Help.java | 13 +++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 066ff4ef4c..bb26cea58f 100644 --- a/build.gradle +++ b/build.gradle @@ -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) { @@ -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' } @@ -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' } diff --git a/src/org/omegat/help/Help.java b/src/org/omegat/help/Help.java index 626116a297..f64b29b32b 100644 --- a/src/org/omegat/help/Help.java +++ b/src/org/omegat/help/Help.java @@ -48,6 +48,7 @@ import java.util.zip.ZipInputStream; import org.omegat.util.Language; +import org.omegat.util.Log; import org.omegat.util.OConsts; import org.omegat.util.OStrings; import org.omegat.util.StaticUtils; @@ -88,7 +89,7 @@ public static void showJavadoc() throws IOException { /** * Shows help in the system browser. * - * @throws IOException + * @throws IOException when URI creation failed. */ public static void showHelp() throws IOException { String lang = detectHelpLanguage(); @@ -125,7 +126,11 @@ private static URI getHelpZipFileURI(String lang) { return null; } try { - Path destinationDir = Files.createTempDirectory("omegat-" + OStrings.VERSION + "-help-" + lang); + Path destinationDir = Paths.get(StaticUtils.getConfigDir(), "manual", OStrings.VERSION, lang); + if (destinationDir.resolve("index.html").toFile().exists()) { + // already have manual + return destinationDir.toUri(); + } return extractZip(zipFile, destinationDir).toURI(); } catch (IOException ignored) { } @@ -148,7 +153,7 @@ private static File extractZip(File file, Path destinationDir) throws IOExceptio try { cleanUp(destinationDir); } catch (IOException e) { - e.printStackTrace(); + Log.log(e); } })); return destinationDir.resolve(OConsts.HELP_HOME).toFile(); @@ -223,7 +228,7 @@ public static String errorHaiku() { /** * Detects the documentation language to use. - * + *

* If the latest manual is not available in the system locale language, it * returns null, i.e. show a language selection screen. */ From 22a7f709b4c9a7169004f447e7fc5b5eb002aab6 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Sun, 23 Feb 2025 13:11:28 +0900 Subject: [PATCH 2/9] style: apply spotless Signed-off-by: Hiroshi Miura --- src/org/omegat/help/Help.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/org/omegat/help/Help.java b/src/org/omegat/help/Help.java index f64b29b32b..4d6a968cbd 100644 --- a/src/org/omegat/help/Help.java +++ b/src/org/omegat/help/Help.java @@ -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 @@ -89,7 +89,8 @@ public static void showJavadoc() throws IOException { /** * Shows help in the system browser. * - * @throws IOException when URI creation failed. + * @throws IOException + * when URI creation failed. */ public static void showHelp() throws IOException { String lang = detectHelpLanguage(); From 151a1df88b22810a1c58439fbf388e117a038622 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Mon, 24 Feb 2025 21:40:29 +0900 Subject: [PATCH 3/9] fix: manual expand to users application data folder Signed-off-by: Hiroshi Miura --- src/org/omegat/Bundle.properties | 1 + src/org/omegat/help/Help.java | 7 +-- src/org/omegat/util/StaticUtils.java | 72 ++++++++++++++++++++++++++-- 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/org/omegat/Bundle.properties b/src/org/omegat/Bundle.properties index 7cf4afacd8..5c4090bc4a 100644 --- a/src/org/omegat/Bundle.properties +++ b/src/org/omegat/Bundle.properties @@ -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 operationg system refused to let OmegaT create the application data folder. # HttpConnectionUtils diff --git a/src/org/omegat/help/Help.java b/src/org/omegat/help/Help.java index 4d6a968cbd..0d575acce9 100644 --- a/src/org/omegat/help/Help.java +++ b/src/org/omegat/help/Help.java @@ -127,10 +127,11 @@ private static URI getHelpZipFileURI(String lang) { return null; } try { - Path destinationDir = Paths.get(StaticUtils.getConfigDir(), "manual", OStrings.VERSION, lang); - if (destinationDir.resolve("index.html").toFile().exists()) { + Path destinationDir = Paths.get(StaticUtils.getApplicationDataDir(), "manual", OStrings.VERSION, lang); + Path indexPath = destinationDir.resolve("index.html"); + if (indexPath.toFile().exists()) { // already have manual - return destinationDir.toUri(); + return indexPath.toUri(); } return extractZip(zipFile, destinationDir).toURI(); } catch (IOException ignored) { diff --git a/src/org/omegat/util/StaticUtils.java b/src/org/omegat/util/StaticUtils.java index f244fd11c6..ea4e2aa682 100644 --- a/src/org/omegat/util/StaticUtils.java +++ b/src/org/omegat/util/StaticUtils.java @@ -72,20 +72,35 @@ private StaticUtils() { } /** - * Configuration directory on Windows platforms + * Configuration directory on Windows platforms. */ 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_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 */ @@ -329,6 +344,57 @@ public static String getConfigDir() { return configDir; } + /** + * Get application data directory. + * @return directory path to store application data. + */ + public static String getApplicationDataDir() { + String dataDir; + 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) { + String appData = null; + File appDataFile = new File(home, "AppData\\Roaming"); + if (appDataFile.exists()) { + appData = appDataFile.getAbsolutePath(); + } + dataDir = appData + 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.isEmpty()) { + 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 From 8919d81267199aaf51bbb2130b9621998db3b770 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Mon, 24 Feb 2025 23:43:33 +0900 Subject: [PATCH 4/9] fix: drop clean up of expanded manuals Signed-off-by: Hiroshi Miura --- src/org/omegat/help/Help.java | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/src/org/omegat/help/Help.java b/src/org/omegat/help/Help.java index 0d575acce9..63b2a85f73 100644 --- a/src/org/omegat/help/Help.java +++ b/src/org/omegat/help/Help.java @@ -151,29 +151,9 @@ private static File extractZip(File file, Path destinationDir) throws IOExceptio zipInputStream.closeEntry(); } } - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - try { - cleanUp(destinationDir); - } catch (IOException e) { - Log.log(e); - } - })); return destinationDir.resolve(OConsts.HELP_HOME).toFile(); } - private static void cleanUp(Path destinationDir) throws IOException { - if (Files.exists(destinationDir)) { - try (Stream 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); } From 0b1982b3c321edaca22aef996ac9c5f84a366fc3 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Mon, 24 Feb 2025 23:53:25 +0900 Subject: [PATCH 5/9] style: remove unused imports Signed-off-by: Hiroshi Miura --- src/org/omegat/help/Help.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/org/omegat/help/Help.java b/src/org/omegat/help/Help.java index 63b2a85f73..b520b703f4 100644 --- a/src/org/omegat/help/Help.java +++ b/src/org/omegat/help/Help.java @@ -40,15 +40,12 @@ 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; import org.omegat.util.Language; -import org.omegat.util.Log; import org.omegat.util.OConsts; import org.omegat.util.OStrings; import org.omegat.util.StaticUtils; From 5dc28835c13189ecf595cb77ad4761dd71869c27 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Mon, 24 Feb 2025 23:55:59 +0900 Subject: [PATCH 6/9] Store app data in %APPDATA%/Local on Windows Signed-off-by: Hiroshi Miura --- src/org/omegat/util/StaticUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/omegat/util/StaticUtils.java b/src/org/omegat/util/StaticUtils.java index ea4e2aa682..4b391ebfc1 100644 --- a/src/org/omegat/util/StaticUtils.java +++ b/src/org/omegat/util/StaticUtils.java @@ -360,7 +360,7 @@ public static String getApplicationDataDir() { if (Platform.isWindows) { String appData = null; - File appDataFile = new File(home, "AppData\\Roaming"); + File appDataFile = new File(home, "AppData\\Local"); if (appDataFile.exists()) { appData = appDataFile.getAbsolutePath(); } From 8c0c34cb449e5f082c07958dd3a0c62c2d7cdb0c Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Tue, 25 Feb 2025 17:33:29 +0900 Subject: [PATCH 7/9] style: define Windows PATH as variable Signed-off-by: Hiroshi Miura --- src/org/omegat/util/StaticUtils.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/org/omegat/util/StaticUtils.java b/src/org/omegat/util/StaticUtils.java index 4b391ebfc1..ef49ef1c01 100644 --- a/src/org/omegat/util/StaticUtils.java +++ b/src/org/omegat/util/StaticUtils.java @@ -74,6 +74,7 @@ private StaticUtils() { /** * Configuration directory on Windows platforms. */ + private static final String WINDOWS_ROAMING_DATA_DIR = "AppData\\Roaming"; private static final String WINDOWS_CONFIG_DIR = "\\OmegaT\\"; /** @@ -89,6 +90,7 @@ private StaticUtils() { /** * Application data directory on Windows platforms. */ + private static final String WINDOWS_LOCAL_DATA_DIR = "AppData\\Local"; private static final String WINDOWS_DATA_DIR = "\\OmegaT\\"; /** @@ -273,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 { @@ -359,8 +361,7 @@ public static String getApplicationDataDir() { } if (Platform.isWindows) { - String appData = null; - File appDataFile = new File(home, "AppData\\Local"); + File appDataFile = new File(home, WINDOWS_LOCAL_DATA_DIR); if (appDataFile.exists()) { appData = appDataFile.getAbsolutePath(); } From ff0b2516e4d06adfeee4b7176b3a2a04f9da8cfe Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Tue, 25 Feb 2025 17:34:44 +0900 Subject: [PATCH 8/9] fix: avoid NPE when Windows AppData folder not exist Signed-off-by: Hiroshi Miura --- src/org/omegat/util/StaticUtils.java | 36 ++++++++++++++-------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/org/omegat/util/StaticUtils.java b/src/org/omegat/util/StaticUtils.java index ef49ef1c01..d47ebd5a7a 100644 --- a/src/org/omegat/util/StaticUtils.java +++ b/src/org/omegat/util/StaticUtils.java @@ -351,7 +351,7 @@ public static String getConfigDir() { * @return directory path to store application data. */ public static String getApplicationDataDir() { - String dataDir; + 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) @@ -363,9 +363,8 @@ public static String getApplicationDataDir() { if (Platform.isWindows) { File appDataFile = new File(home, WINDOWS_LOCAL_DATA_DIR); if (appDataFile.exists()) { - appData = appDataFile.getAbsolutePath(); + dataDir = appDataFile.getAbsolutePath() + WINDOWS_DATA_DIR; } - dataDir = appData + WINDOWS_DATA_DIR; } else if (Platform.isUnixLike()) { dataDir = home + UNIX_DATA_DIR; } else if (Platform.isMacOSX()) { @@ -375,23 +374,24 @@ public static String getApplicationDataDir() { // use the user's home directory by default dataDir = home + File.separator; } - if (!dataDir.isEmpty()) { - 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; - } + 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()); } + } 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; } From 04d8cd67a7b7a1aab85cb409fd6bbfa6aef09a87 Mon Sep 17 00:00:00 2001 From: kazephil Date: Tue, 25 Feb 2025 20:11:03 +0900 Subject: [PATCH 9/9] Fix typo in SU_DATA_DIR_CREATE_ERROR --- src/org/omegat/Bundle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/omegat/Bundle.properties b/src/org/omegat/Bundle.properties index 5c4090bc4a..b9ccb43b94 100644 --- a/src/org/omegat/Bundle.properties +++ b/src/org/omegat/Bundle.properties @@ -1962,7 +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 operationg system refused to let OmegaT create the application data folder. +SU_DATA_DIR_CREATE_ERROR=Your operating system refused to let OmegaT create the application data folder. # HttpConnectionUtils