From 01b7a047176302a42c0e2a413261073fd444e04d Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 23 Jun 2023 14:55:13 +0200 Subject: [qemu] Add missing file --- .../openslx/runvirt/plugin/qemu/EditorRunner.java | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/EditorRunner.java diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/EditorRunner.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/EditorRunner.java new file mode 100644 index 00000000..6dbb26c5 --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/EditorRunner.java @@ -0,0 +1,50 @@ +package org.openslx.runvirt.plugin.qemu; + +import java.io.IOException; +import java.lang.ProcessBuilder.Redirect; +import java.util.concurrent.TimeUnit; + +/** + * Small wrapper around Process that will try to launch any sensible GUI + * text editor on the given file. + */ +public class EditorRunner +{ + + private static final String[] EDITORS = { + "mousepad", + "leafpad", + "gedit", + "gvim", + "medit", + "kate", + "featherpad", + }; + + public static void open( String file ) + { + for ( String editor : EDITORS ) { + if ( open( editor, file ) ) + return; + } + return; + } + + private static boolean open( String editor, String file ) + { + ProcessBuilder pr = new ProcessBuilder( editor, file ); + pr.redirectError( Redirect.DISCARD ); + pr.redirectOutput( Redirect.DISCARD ); + try { + final Process p = pr.start(); + p.waitFor( 1, TimeUnit.SECONDS ); + if ( !p.isAlive() ) + return false; + p.waitFor(); + } catch ( IOException | InterruptedException e ) { + return false; + } + return true; + } + +} -- cgit v1.2.3-55-g7522