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:
		
							parent
							
								
									ed2f2cbf0a
								
							
						
					
					
						commit
						3b16a68fb3
					
				| @ -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; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
| 	 | 	 | ||||||
| 	 | 	 | ||||||
| 	/** | 	/** | ||||||
|  | |||||||
| @ -8,9 +8,12 @@ import java.awt.Desktop; | |||||||
| import java.awt.Window; | import java.awt.Window; | ||||||
| import java.awt.event.ActionEvent; | import java.awt.event.ActionEvent; | ||||||
| import java.awt.event.ActionListener; | import java.awt.event.ActionListener; | ||||||
|  | import java.io.BufferedInputStream; | ||||||
|  | import java.io.BufferedReader; | ||||||
| import java.io.File; | import java.io.File; | ||||||
| import java.io.FileOutputStream; | import java.io.FileOutputStream; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
|  | import java.io.InputStreamReader; | ||||||
| import java.util.Enumeration; | import java.util.Enumeration; | ||||||
| import java.util.Iterator; | import java.util.Iterator; | ||||||
| 
 | 
 | ||||||
| @ -31,6 +34,8 @@ import org.slf4j.Logger; | |||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
| 
 | 
 | ||||||
| import net.miginfocom.swing.MigLayout; | 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.document.OpenRocketDocument; | ||||||
| import net.sf.openrocket.gui.print.PrintController; | import net.sf.openrocket.gui.print.PrintController; | ||||||
| import net.sf.openrocket.gui.print.PrintSettings; | 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 | 				// TODO: HIGH: Remove UIManager, and pass settings to the actual printing methods | ||||||
| 				TemplateProperties.setColors(settings); | 				TemplateProperties.setColors(settings); | ||||||
| 				File f = generateReport(settings); | 				File f = generateReport(settings); | ||||||
| 				desktop.open(f); | 				openPreviewHelper(f); | ||||||
| 			} catch (IOException e) { | 			} catch (IOException e) { | ||||||
| 				log.error("Could not open preview.", e); | 				log.error("Could not open preview.", e); | ||||||
| 				JOptionPane.showMessageDialog(this, new String[] { | 				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. | 	 * Handler for when the "Save as PDF" button is clicked. | ||||||
| 	 * | 	 * | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user