#216 fix terminal launcher for Gnome/Ubuntu: latest Gnome doesn't run nautilus by default

This commit is contained in:
Anton Keks 2020-03-07 17:32:16 +02:00
parent ee6ecac5b2
commit daa8dbd373
2 changed files with 24 additions and 60 deletions

View File

@ -59,6 +59,9 @@ public class OpenerLauncher {
}
}
}
catch (UserErrorException e) {
throw e;
}
catch (Exception e) {
throw new UserErrorException("opener.failed", openerString);
}

View File

@ -15,22 +15,18 @@ import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import static java.util.Arrays.asList;
/**
* The cross-platform terminal launcher
*
* @author Anton Keks
*/
public class TerminalLauncher {
static final Logger LOG = LoggerFactory.getLogger();
private static final int UNKNOWN = -1;
private static final int XTERM = 0;
private static final int GNOME = 1;
private static final int XFCE = 2;
private static final int KDE = 3;
/** caches last working terminal type */
private static int workingTerminal = UNKNOWN;
/** caches last working terminal emulator */
private static String workingTerminal;
/**
* Launches the execString in the terminal.
@ -39,75 +35,40 @@ public class TerminalLauncher {
* @param workingDir the working directory (or null)
*/
public static void launchInTerminal(String execString, File workingDir) {
try {
if (Platform.WINDOWS) {
// generate a command file :-)
File batFile = File.createTempFile("launch", ".cmd");
batFile.deleteOnExit();
FileWriter writer = new FileWriter(batFile);
writer.write("@rem This is a temporary file generated by Angry IP Scanner\n" +
"@start cmd /k " + execString);
writer.close();
try (FileWriter writer = new FileWriter(batFile)) {
writer.write("@rem This is a temporary file generated by Angry IP Scanner\n" +
"@start cmd /k " + execString);
}
Runtime.getRuntime().exec(batFile.getAbsolutePath(), null, workingDir);
}
else
if (Platform.MAC_OS) {
else if (Platform.MAC_OS) {
Runtime.getRuntime().exec(new String[] {"osascript", "-e", "tell application \"Terminal\" to do script \"" + execString + "\""}, null, workingDir);
}
else { // assume Linux or other Unix
if (workingTerminal == UNKNOWN) {
detectWorkingTerminal();
}
// run detected terminal program
switch (workingTerminal) {
case GNOME:
Runtime.getRuntime().exec(new String[] {"gnome-terminal", "-x", "bash", "-c", execString + ";bash"}, null, workingDir);
break;
case XFCE:
Runtime.getRuntime().exec(new String[] {"xfce4-terminal", "-x", "sh", "-c", execString + ";sh"}, null, workingDir);
break;
case KDE:
Runtime.getRuntime().exec(new String[] {"konsole", "-e", "bash", "-c", execString + ";bash"}, null, workingDir);
break;
default: // XTERM
Runtime.getRuntime().exec(new String[] {"xterm", "-e", "sh", "-c", execString + ";sh"}, null, workingDir);
}
else { // assume Linux
if (workingTerminal == null) workingTerminal = detectWorkingTerminal();
String shell = System.getenv("SHELL");
if (shell == null) shell = "sh";
Runtime.getRuntime().exec(new String[] {workingTerminal, "-e", shell, "-xc", execString + ";" + shell}, null, workingDir);
}
}
catch (Exception e) {
// log and display the error
LOG.log(Level.WARNING, "openTerminal.failed", e);
// if this is the first time, fall back to XTERM
if (workingTerminal != XTERM) {
workingTerminal = XTERM;
launchInTerminal(execString, workingDir);
}
else {
// even XTERM doesn't work...
throw new UserErrorException("openTerminal.failed", execString);
}
throw new UserErrorException("openTerminal.failed", execString);
}
}
private static void detectWorkingTerminal() throws InterruptedException, IOException {
if (Runtime.getRuntime().exec(new String[] {"pidof", "nautilus"}).waitFor() == 0) {
workingTerminal = GNOME;
}
else
if (Runtime.getRuntime().exec(new String[] {"pidof", "xfce4-session", "xfwm4", "Thunar", "xfdesktop"}).waitFor() == 0) {
workingTerminal = XFCE;
}
else
if (Runtime.getRuntime().exec(new String[] {"pidof", "dcopserver"}).waitFor() == 0) {
workingTerminal = KDE;
}
else {
workingTerminal = XTERM;
private static String detectWorkingTerminal() {
for (String term : asList("x-terminal-emulator", "xdg-terminal", "gnome-terminal", "xfce4-terminal", "konsole", "xterm")) {
File file = new File("/usr/bin/" + term);
if (file.exists()) return file.getPath();
}
return "xterm";
}
}