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

Fix linux file opening by using Process Builder #8853

Merged
merged 13 commits into from
Jun 5, 2022
66 changes: 44 additions & 22 deletions src/main/java/org/jabref/gui/desktop/os/Linux.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Locale;
import java.util.Optional;

Expand Down Expand Up @@ -89,41 +90,62 @@ public void openFileWithApplication(String filePath, String application) throws
public void openFolderAndSelectFile(Path filePath) throws IOException {
String desktopSession = System.getenv("DESKTOP_SESSION");

String cmd = "xdg-open " + filePath.toAbsolutePath().getParent().toString(); // default command
String absoluteFilePath = filePath.toAbsolutePath().toString();
String[] cmd = {"xdg-open", absoluteFilePath}; // default command

if (desktopSession != null) {
desktopSession = desktopSession.toLowerCase(Locale.ROOT);
if (desktopSession.contains("gnome")) {
btut marked this conversation as resolved.
Show resolved Hide resolved
cmd = "nautilus --select " + filePath.toString().replace(" ", "\\ ");
cmd = new String[] {"nautilus", "--select", absoluteFilePath};
} else if (desktopSession.contains("kde") || desktopSession.contains("plasma")) {
cmd = "dolphin --select " + filePath.toString().replace(" ", "\\ ");
cmd = new String[] {"dolphin", "--select", absoluteFilePath};
} else if (desktopSession.contains("mate")) {
cmd = "caja --select " + filePath.toString().replace(" ", "\\ ");
cmd = new String[] {"caja", "--select", absoluteFilePath};
} else if (desktopSession.contains("cinnamon")) {
cmd = "nemo --select " + filePath.toString().replace(" ", "\\ ");
cmd = new String[] {"nemo", absoluteFilePath}; // Although nemo is based on nautilus it does not support --select, it directly highlights the file
}
}
Runtime.getRuntime().exec(cmd);
ProcessBuilder processBuilder = new ProcessBuilder((cmd));
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
public void openConsole(String absolutePath) throws IOException {
Runtime runtime = Runtime.getRuntime();
Process p = runtime.exec("readlink /etc/alternatives/x-terminal-emulator");
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

String emulatorName = reader.readLine();
Copy link
Contributor

Choose a reason for hiding this comment

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

I get null here. I think /etc/alternatives is a Debian thing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe if (Files.exists(/etc/alternatives...) ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, but what if it does not exist? What terminal emulator do we choose then?

Copy link
Member Author

Choose a reason for hiding this comment

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

Turns out there is no real solution, just checking multiple places https://superuser.com/a/1461079

Copy link
Contributor

Choose a reason for hiding this comment

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

So how do we proceed? Leave it as is? Show an alert message that we cannot determine a TE?
Do you think this is a feature that many people use?

Copy link
Member Author

Choose a reason for hiding this comment

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

Show an alert message. Unable to open terminal. I doubt many use this

if (emulatorName != null) {
emulatorName = emulatorName.substring(emulatorName.lastIndexOf(File.separator) + 1);

if (emulatorName.contains("gnome")) {
runtime.exec("gnome-terminal --working-directory=" + absolutePath);
} else if (emulatorName.contains("xfce4")) {
runtime.exec("xfce4-terminal --working-directory=" + absolutePath);
} else if (emulatorName.contains("konsole")) {
runtime.exec("konsole --workdir=" + absolutePath);
} else {
runtime.exec(emulatorName, null, new File(absolutePath));

ProcessBuilder processBuilder = new ProcessBuilder("readlink", "/etc/alternatives/x-terminal-emulator");
Process process = processBuilder.start();

try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String emulatorName = reader.readLine();
if (emulatorName != null) {
emulatorName = emulatorName.substring(emulatorName.lastIndexOf(File.separator) + 1);

absolutePath = "\"" + absolutePath + "\"";

String[] cmd = {};
if (emulatorName.contains("gnome")) {
cmd = new String[] {"gnome-terminal", "--working-directory=" + absolutePath};
} else if (emulatorName.contains("xfce4")) {
cmd = new String[] {"xfce4-terminal", "--working-directory=" + absolutePath};
} else if (emulatorName.contains("konsole")) {
cmd = new String[] {"konsole --workdir=" + absolutePath};
} else {
cmd = new String[] {emulatorName, absolutePath};
}
LOGGER.debug("Terminal cmd {}", Arrays.toString(cmd));
process = new ProcessBuilder(cmd).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);
}
}
}
Expand Down