Skip to content

Commit

Permalink
Add awt native open commands (#7037)
Browse files Browse the repository at this point in the history
* Add awt native open commands

* Revert to xdg-open if the native awt operation fails.

This is used in the confined linux packages to open URLs

* Fix AllowedToUseAwt annotation use

* Use Thread to avoid swing dependency

* Use JabRefExecutorService to open files in Linux

* Add line in changelog
  • Loading branch information
LyzardKing authored Oct 28, 2020
1 parent 47fd562 commit bfd9840
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ inserting new citations in a OpenOffic/LibreOffice document. [#6957](https://git
- We changed the name of a group type from "Searching for keywords" to "Searching for a keyword". [6995](https://github.com/JabRef/jabref/pull/6995)
- We changed the way JabRef displays the title of a tab and of the window. [4161](https://github.com/JabRef/jabref/issues/4161)
- We changed connect timeouts for server requests to 30 seconds in general and 5 seconds for GROBID server (special) and improved user notifications on connection issues. [7026](https://github.com/JabRef/jabref/pull/7026)
- We changed the way linked files are opened on Linux to use the native openFile method, compatible with confined packages. [7037](https://github.com/JabRef/jabref/pull/7037)

### Fixed

Expand Down
6 changes: 2 additions & 4 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ architectures:
- build-on: amd64

plugs:
desktop:
desktop-legacy:
wayland:
unity7:
home:
unity7:
opengl:
network-bind:
removable-media:
Expand All @@ -46,6 +43,7 @@ apps:

environment:
_JAVA_OPTIONS: "-Duser.home=$SNAP_USER_DATA"
GTK_USE_PORTAL: "1"

parts:
jabref:
Expand Down
63 changes: 43 additions & 20 deletions src/main/java/org/jabref/gui/desktop/os/Linux.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.gui.desktop.os;

import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand All @@ -8,6 +9,7 @@
import java.util.Locale;
import java.util.Optional;

import org.jabref.architecture.AllowedToUseAwt;
import org.jabref.gui.JabRefExecutorService;
import org.jabref.gui.externalfiletype.ExternalFileType;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
Expand All @@ -16,27 +18,48 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@AllowedToUseAwt("Requires AWT to open a file with the native method")
public class Linux implements NativeDesktop {

private static final Logger LOGGER = LoggerFactory.getLogger(Linux.class);

private void nativeOpenFile(String filePath) {
JabRefExecutorService.INSTANCE.execute(() -> {
try {
File file = new File(filePath);
Desktop.getDesktop().open(file);
System.out.println("Open file in default application with Desktop integration");
} catch (IllegalArgumentException e) {
System.out.println("Fail back to xdg-open");
try {
String[] cmd = {"xdg-open", filePath};
Runtime.getRuntime().exec(cmd);
} catch (Exception e2) {
System.out.println("Open operation not successful: " + e2);
}
} catch (IOException e) {
System.out.println("Native open operation not successful: " + e);
}
});
}

@Override
public void openFile(String filePath, String fileType) throws IOException {
Optional<ExternalFileType> type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(fileType);
String viewer;

if (type.isPresent() && !type.get().getOpenWithApplication().isEmpty()) {
viewer = type.get().getOpenWithApplication();
ProcessBuilder processBuilder = new ProcessBuilder(viewer, filePath);
Process process = processBuilder.start();
StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug);
StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug);

JabRefExecutorService.INSTANCE.execute(streamGobblerInput);
JabRefExecutorService.INSTANCE.execute(streamGobblerError);
} else {
viewer = "xdg-open";
nativeOpenFile(filePath);
}
ProcessBuilder processBuilder = new ProcessBuilder(viewer, filePath);
Process process = processBuilder.start();
StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug);
StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug);

JabRefExecutorService.INSTANCE.execute(streamGobblerInput);
JabRefExecutorService.INSTANCE.execute(streamGobblerError);
}

@Override
Expand All @@ -45,21 +68,21 @@ public void openFileWithApplication(String filePath, String application) throws
String[] openWith;
if ((application != null) && !application.isEmpty()) {
openWith = application.split(" ");
} else {
openWith = new String[] {"xdg-open"};
}
String[] cmdArray = new String[openWith.length + 1];
System.arraycopy(openWith, 0, cmdArray, 0, openWith.length);
cmdArray[cmdArray.length - 1] = filePath;
String[] cmdArray = new String[openWith.length + 1];
System.arraycopy(openWith, 0, cmdArray, 0, openWith.length);
cmdArray[cmdArray.length - 1] = filePath;

ProcessBuilder processBuilder = new ProcessBuilder(cmdArray);
Process process = processBuilder.start();
ProcessBuilder processBuilder = new ProcessBuilder(cmdArray);
Process process = processBuilder.start();

StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug);
StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug);
StreamGobbler streamGobblerInput = new StreamGobbler(process.getInputStream(), LOGGER::debug);
StreamGobbler streamGobblerError = new StreamGobbler(process.getErrorStream(), LOGGER::debug);

JabRefExecutorService.INSTANCE.execute(streamGobblerInput);
JabRefExecutorService.INSTANCE.execute(streamGobblerError);
JabRefExecutorService.INSTANCE.execute(streamGobblerInput);
JabRefExecutorService.INSTANCE.execute(streamGobblerError);
} else {
nativeOpenFile(filePath);
}
}

@Override
Expand Down

0 comments on commit bfd9840

Please sign in to comment.