Use xdg-open on Linux Snaps to open PDFs for printing/previews

The Desktop.open() method does not work within a Snap confined
application because it uses gnome apis which are restricted or
only defined within the snap itself. The confined snaps allow
for xdg-open to open the default application to handle opening
the specified mime-type (application/pdf) for print previews and
printing.

Signed-off-by: Billy Olsen <billy.olsen@gmail.com>
This commit is contained in:
Billy Olsen 2020-05-24 12:42:04 -07:00
parent ed2f2cbf0a
commit 3b16a68fb3
2 changed files with 38 additions and 2 deletions

View File

@ -41,7 +41,24 @@ public class SystemInfo {
}
}
/**
* Returns true if the OpenRocket is running a confined manner that may
* require alternative behaviors and experiences.
*
* Note: future versions may return further confinement information and
* this interface is subject to change.
*
* @return true if the system is running in a confined manner, false
* otherwise
*/
public static boolean isConfined() {
switch (getPlatform()) {
case UNIX:
return (System.getenv("SNAP_VERSION") != null);
default:
return false;
}
}
/**

View File

@ -8,9 +8,12 @@ import java.awt.Desktop;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Iterator;
@ -31,6 +34,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.miginfocom.swing.MigLayout;
import net.sf.openrocket.arch.SystemInfo;
import net.sf.openrocket.arch.SystemInfo.Platform;
import net.sf.openrocket.document.OpenRocketDocument;
import net.sf.openrocket.gui.print.PrintController;
import net.sf.openrocket.gui.print.PrintSettings;
@ -321,7 +326,7 @@ public class PrintDialog extends JDialog implements TreeSelectionListener {
// TODO: HIGH: Remove UIManager, and pass settings to the actual printing methods
TemplateProperties.setColors(settings);
File f = generateReport(settings);
desktop.open(f);
openPreviewHelper(f);
} catch (IOException e) {
log.error("Could not open preview.", e);
JOptionPane.showMessageDialog(this, new String[] {
@ -339,6 +344,20 @@ public class PrintDialog extends JDialog implements TreeSelectionListener {
}
}
private void openPreviewHelper(final File f) throws IOException {
if (SystemInfo.getPlatform() == Platform.UNIX && SystemInfo.isConfined()) {
/* When installed via a snap package on Linux, the default option
* to open PDF options using java.awt.Desktop.open() doesn't work
* due to using . Instead, use the xdg-open command
* which will work for URLs.
*/
String command = "xdg-open " + f.getAbsolutePath();
Runtime.getRuntime().exec(command);
} else {
desktop.open(f);
}
}
/**
* Handler for when the "Save as PDF" button is clicked.
*