Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add awt native open commands #7037

Merged
merged 6 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
new Thread(() -> {
LyzardKing marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}).start();
LyzardKing marked this conversation as resolved.
Show resolved Hide resolved
}

@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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Siedlerchr
Can we not remove this part and replace it with
Runtime.getRuntime().exec?
The code would be a lot cleaner..both here and in the openFileWithApplication methods..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is used when you have defined a specific app in JabRef (Manage external file types in the Settings dialog -> External programs)

At least we need this StreamGobblers which read the input and error streams, otherwise it could lead to hanging
https://www.infoworld.com/article/2071275/when-runtime-exec---won-t.html

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