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
Changes from 1 commit
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
58 changes: 38 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 @@ -20,23 +21,39 @@ public class Linux implements NativeDesktop {

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

private void nativeOpenFile(String filePath) {
if (Desktop.isDesktopSupported()) {
new Thread(() -> {
try {
File file = new File(filePath);
Desktop.getDesktop().open(file);
LOGGER.info("Opened file with Desktop integration");
} catch (Exception e) {
LOGGER.error("Open operation not successful: " + e.getMessage());
LyzardKing marked this conversation as resolved.
Show resolved Hide resolved
}
}).start();
} else {
LOGGER.error("No operation possible");
}
}

@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 +62,22 @@ 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;
LOGGER.info("Opening with " + openWith);
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