offline help fix

This commit is contained in:
Greg DiCristofaro 2023-08-08 15:09:50 -04:00
parent 5662187b2d
commit 854a2bae8a
2 changed files with 59 additions and 39 deletions

View File

@ -21,9 +21,10 @@ package org.sleuthkit.autopsy.corecomponents;
import org.sleuthkit.autopsy.coreutils.Desktop; import org.sleuthkit.autopsy.coreutils.Desktop;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI; import java.nio.file.Paths;
import org.netbeans.core.actions.HTMLViewAction; import org.netbeans.core.actions.HTMLViewAction;
import org.openide.awt.ActionID; import org.openide.awt.ActionID;
import org.openide.awt.ActionReference; import org.openide.awt.ActionReference;
@ -33,6 +34,7 @@ import org.openide.awt.HtmlBrowser;
import org.openide.util.NbBundle; import org.openide.util.NbBundle;
import org.openide.util.NbBundle.Messages; import org.openide.util.NbBundle.Messages;
import java.util.logging.Level; import java.util.logging.Level;
import org.openide.modules.InstalledFileLocator;
import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.coreutils.Logger;
/** /**
@ -52,6 +54,9 @@ import org.sleuthkit.autopsy.coreutils.Logger;
@Messages("CTL_OfflineHelpAction=Offline Autopsy Documentation") @Messages("CTL_OfflineHelpAction=Offline Autopsy Documentation")
public final class OfflineHelpAction implements ActionListener { public final class OfflineHelpAction implements ActionListener {
private static final String DOCS_FOLDER = "docs";
private static final String HELP_HTML_FILE = "index.html";
private static final Logger logger private static final Logger logger
= org.sleuthkit.autopsy.coreutils.Logger.getLogger(AboutWindowPanel.class.getName()); = org.sleuthkit.autopsy.coreutils.Logger.getLogger(AboutWindowPanel.class.getName());
@ -67,41 +72,40 @@ public final class OfflineHelpAction implements ActionListener {
* Tested and working: Chrome, Firefox, IE Not tested: Opera, Safari * Tested and working: Chrome, Firefox, IE Not tested: Opera, Safari
*/ */
private void viewOfflineHelp() { private void viewOfflineHelp() {
String fileForHelp = "";
String indexForHelp = ""; File systemHelpFile = getOfflineHelpFile();
String currentDirectory = ""; if (systemHelpFile == null) {
URI uri = null; logger.log(Level.SEVERE, "Unable to load Offline Documentation file");
return;
try {
// Match the form: file:///C:/some/directory/AutopsyXYZ/docs/index.html
fileForHelp = NbBundle.getMessage(OfflineHelpAction.class, "FILE_FOR_LOCAL_HELP");
indexForHelp = NbBundle.getMessage(OfflineHelpAction.class, "INDEX_FOR_LOCAL_HELP");
currentDirectory = System.getProperty("user.dir").replace("\\", "/").replace(" ", "%20"); //NON-NLS
uri = new URI(fileForHelp + currentDirectory + indexForHelp);
} catch (Exception ex) {
logger.log(Level.SEVERE, "Unable to load Offline Documentation: "
+ fileForHelp + currentDirectory + indexForHelp, ex); //NON-NLS
} }
if (uri != null) {
// Display URL in the System browser // Display URL in the System browser
if (Desktop.isDesktopSupported()) { if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop(); Desktop desktop = Desktop.getDesktop();
try { try {
desktop.browse(uri); desktop.open(systemHelpFile);
} catch (IOException ex) { return;
logger.log(Level.SEVERE, "Unable to launch the system browser: " } catch (IOException ex) {
+ fileForHelp + currentDirectory + indexForHelp, ex); //NON-NLS logger.log(Level.SEVERE, "Unable to launch the system browser: "
} + systemHelpFile, ex); //NON-NLS
} else {
org.openide.awt.StatusDisplayer.getDefault().setStatusText(
NbBundle.getMessage(HTMLViewAction.class, "CTL_OpeningBrowser")); //NON-NLS
try {
HtmlBrowser.URLDisplayer.getDefault().showURL(uri.toURL());
} catch (MalformedURLException ex) {
logger.log(Level.SEVERE, "Unable to launch the built-in browser: "
+ fileForHelp + currentDirectory + indexForHelp, ex); //NON-NLS
}
} }
} }
org.openide.awt.StatusDisplayer.getDefault().setStatusText(
NbBundle.getMessage(HTMLViewAction.class, "CTL_OpeningBrowser")); //NON-NLS
try {
HtmlBrowser.URLDisplayer.getDefault().showURL(systemHelpFile.toURI().toURL());
} catch (MalformedURLException ex) {
logger.log(Level.SEVERE, "Unable to launch the built-in browser: "
+ systemHelpFile, ex); //NON-NLS
}
}
private File getOfflineHelpFile() {
return InstalledFileLocator.getDefault().getDefault().locate(
Paths.get(DOCS_FOLDER, HELP_HTML_FILE).toString(),
OfflineHelpAction.class.getPackage().getName(),
false);
} }
} }

View File

@ -21,6 +21,7 @@ package org.sleuthkit.autopsy.coreutils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level; import java.util.logging.Level;
import org.apache.commons.lang3.SystemUtils; import org.apache.commons.lang3.SystemUtils;
@ -31,6 +32,7 @@ import org.apache.commons.lang3.SystemUtils;
public class Desktop { public class Desktop {
private static final Logger LOGGER = Logger.getLogger(Desktop.class.getName()); private static final Logger LOGGER = Logger.getLogger(Desktop.class.getName());
private static final long XDG_TIMEOUT_SECS = 30;
private static Boolean xdgSupported = null; private static Boolean xdgSupported = null;
@ -74,6 +76,7 @@ public class Desktop {
/** /**
* Private constructor for this wrapper. * Private constructor for this wrapper.
*
* @param awtDesktop The delegate java.awt.Desktop. * @param awtDesktop The delegate java.awt.Desktop.
*/ */
private Desktop(java.awt.Desktop awtDesktop) { private Desktop(java.awt.Desktop awtDesktop) {
@ -82,17 +85,28 @@ public class Desktop {
/** /**
* Opens a given path using `xdg-open` on linux. * Opens a given path using `xdg-open` on linux.
*
* @param path The path. * @param path The path.
* @throws IOException * @throws IOException
*/ */
private void xdgOpen(String path) throws IOException { private void xdgOpen(String path) throws IOException {
Runtime.getRuntime().exec(new String[]{"xdg-open", path}); Process process = Runtime.getRuntime().exec(new String[]{"xdg-open", path});
try {
process.waitFor(XDG_TIMEOUT_SECS, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
throw new IOException("xdg-open timed out", ex);
}
int exitCode = process.exitValue();
if (exitCode != 0) {
throw new IOException("Received non-zero exit code from xdg-open: " + exitCode);
}
} }
/** /**
* Triggers the OS to navigate to the given uri. * Triggers the OS to navigate to the given uri.
*
* @param uri The uri. * @param uri The uri.
* @throws IOException * @throws IOException
*/ */
public void browse(URI uri) throws IOException { public void browse(URI uri) throws IOException {
if (!awtDesktop.isSupported(java.awt.Desktop.Action.BROWSE) && isXdgSupported()) { if (!awtDesktop.isSupported(java.awt.Desktop.Action.BROWSE) && isXdgSupported()) {
@ -104,8 +118,9 @@ public class Desktop {
/** /**
* Triggers the OS to open the given file. * Triggers the OS to open the given file.
*
* @param file The file. * @param file The file.
* @throws IOException * @throws IOException
*/ */
public void open(File file) throws IOException { public void open(File file) throws IOException {
if (!awtDesktop.isSupported(java.awt.Desktop.Action.OPEN) && isXdgSupported()) { if (!awtDesktop.isSupported(java.awt.Desktop.Action.OPEN) && isXdgSupported()) {
@ -117,8 +132,9 @@ public class Desktop {
/** /**
* Triggers the OS to edit the given file. * Triggers the OS to edit the given file.
*
* @param file The file. * @param file The file.
* @throws IOException * @throws IOException
*/ */
public void edit(File file) throws IOException { public void edit(File file) throws IOException {
if (!awtDesktop.isSupported(java.awt.Desktop.Action.EDIT) && isXdgSupported()) { if (!awtDesktop.isSupported(java.awt.Desktop.Action.EDIT) && isXdgSupported()) {