Cyberpython Online

JAVA: Πληροφορίες για το περιβάλλον εργασίας σε Gnome / KDE / XFCE

Posted in Linux, java, programming, Προγραμματισμός by cyberpython on Ιανουαρίου 10, 2009

dukeΤις τελευταίες ημέρες προσπαθώ να γράψω μία βιβλιοθήκη παρόμοια με το IconService του JDIC που δε θα χρησιμοποιεί το JNI και δυναμικές βιβλιοθήκες (dll και so).Όπως είναι αυτονόητο δε θα έχει τη δυνατότητα εύρεσης του επιθυμητού εικονιδίου σε περιβάλλον Windows, αφού στη συγκεκριμένη πλατφόρμα τα εικονίδια δε βρίσκονται σε κάποια συγκεκριμένη τοποθεσία ως αυτόνομα αρχεία. Στόχος είναι η εύρεση εικονιδίων σε περιβάλλοντα εργασίας που βασίζονται στις κατευθυντήριες γραμμές του Freedesktop.org, όπως είναι το Gnome, το XFCE και το KDE.

Για να μπορέσουμε όμως να βρούμε τα επιθυμητά εικονίδια χρησιμοποιώντας τον αλγόριθμο που περιγράφεται στον παραπάνω σύνδεσμο θα πρέπει να γνωρίζουμε το όνομα του τρέχοντος icon-theme. Μετά από πολύ ψάξιμο χωρίς αποτέλεσμα, ο φίλος Gourgi (Γουργιώτης Γιώργος) μου υπέδειξε ένα Perl script (του οποίου τον αρχικό δημιουργό δεν κατάφερα να βρω δυστυχώς) που δίνει πληροφορίες σχετικά με το σύστημα ελέγχοντας τις ενεργές διεργασίες που τρέχουν. Χρησιμοποιώντας αυτό το script ως οδηγό κατασκεύσα την παρακάτω τάξη που μπορεί να μας δώσει τα ονόματα του τρέχοντος περιβάλλοντος εργασίας, του window manager, του widget-theme αλλά και του icon-theme.

DesktopEnvironmentInfo.java:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

public class DesktopEnvironmentInfo {

    private final String UNIX_PRINT_PROC_CMD = "ps -A";
    private String desktopName;
    private String windowManager;
    private String widgetTheme;
    private String iconTheme;
    private Hashtable<String, String> wmList;
    private Hashtable<String, String> deList;

    /**
     * Default constructor
     */
    public DesktopEnvironmentInfo() {

        initialize();
        Vector<String> runningProcesses = listRunningProcessesOnUnix();
        //printStringVector(runningProcesses, System.out);

        this.desktopName = findDesktopName(runningProcesses);
        this.windowManager = findWMName(runningProcesses);

        findThemeAndIconTheme(this.desktopName);
    }

    /**
     * 
     * @return The name of the current Desktop Environment : <br>
     *          <ul>
     *          <b>gnome</b> - for GNOME
     *          <b>kde</b> - for KDE
     *          <b>xfce4</b> - for XFCE
     *          <b>unknown</b> - for other desktop environments
     *          </ul>
     */
    public String getDesktopEnvironmentName() {
        return this.desktopName;
    }

    /**
     * 
     * @return The name of the current Window Manager : <br>
     *          <ul>
     *          <b>beryl</b> - for Beryl
     *          <b>compiz</b> - for Compiz
     *          <b>emerald</b> - for Emerald
     *          <b>fluxbox</b> - for Fluxbox
     *          <b>openbox</b> - for Openbox
     *          <b>blackbox</b> - for Blackbox
     *          <b>xfwm</b> - for Xfwm
     *          <b>metacity</b> - for Metacity
     *          <b>kwin</b> - for Kwin
     *          <b>fvwm</b> - for FVWM
     *          <b>enlightenment</b> - for Enlightenment
     *          <b>icewm</b> - for IceWM
     *          <b>wmaker</b> - for Window Maker
     *          <b>pekwm</b> - for PekWM
     *          <b>unknown</b> - for other Window Managers
     *          </ul>
     */
    public String getWindowManagerName() {
        return this.windowManager;
    }

    /**
     * 
     * @return The name of the current icon widgetTheme.<br>
     *         If the dekstop environment is not Gnome/KDE/XFCE or the 
     *         icon theme could not be determined then "unknown" is returned.<br>
     *          ***WARNING*** : On KDE if the user has not changed the default
     *          icon theme then the result will be "default.kde".
     */
    public String getIconThemeName() {
        return this.iconTheme;
    }

    /**
     * 
     * @return The name of the current widgetTheme.<br>
     *         If the dekstop environment is not Gnome/KDE/XFCE or the 
     *         widget theme could not be determined then "unknown" is returned.<br>
     *          ***WARNING*** : On KDE if the user has not changed the default
     *          widget theme then the result will be "default".
     */
    public String getWidgetThemeName() {
        return this.widgetTheme;
    }

    private void initialize() {

        this.wmList = new Hashtable<String, String>();
        this.wmList.put("beryl", "Beryl");
        this.wmList.put("compiz", "Compiz");
        this.wmList.put("emerald", "Emerald");
        this.wmList.put("fluxbox", "Fluxbox");
        this.wmList.put("openbox", "Openbox");
        this.wmList.put("blackbox", "Blackbox");
        this.wmList.put("xfwm4", "Xfwm4");
        this.wmList.put("metacity", "Metacity");
        this.wmList.put("kwin", "Kwin");
        this.wmList.put("fvwm", "FVWM");
        this.wmList.put("enlightenment", "Enlightenment");
        this.wmList.put("icewm", "IceWM");
        this.wmList.put("wmaker", "Window Maker");
        this.wmList.put("pekwm", "PekWM");

        this.deList = new Hashtable<String, String>();
        this.deList.put("xfce-mcs-manage", "xfce4");
        this.deList.put("ksmserver", "kde");
    }

    /**
     * Determines the name of the current desktop environment.
     * For GNOME it uses the JAVA VM System property sun.desktop
     * For KDE and XFCE checks the running processes to find out
     * which DE is currently active.
     * @return The name of the current Desktop Environment : <br>
     *          <ul>
     *          <b>gnome</b> - for GNOME
     *          <b>kde</b> - for KDE
     *          <b>xfce4</b> - for XFCE
     *          <b>unknown</b> - for other desktop environments
     *          </ul>
     */
    private String findDesktopName(Vector<String> processes) {
        String name = System.getProperty("sun.desktop");
        if (name != null) {
            name = name.toLowerCase();
            if (name.equals("gnome")) {
                return "gnome";
            }
        }

        Enumeration en = deList.keys();

        while (en.hasMoreElements()) {
            String deProc = (String) en.nextElement();
            String deName = deList.get(deProc);

            if (processes.contains(deProc)) {
                return deName;
            }
        }

        return "unknown";
    }

    /**
     * Determines the name of the current window manager by
     * checking the running processes.
     * @return The name of the current Window Manager : <br>
     *          <ul>
     *          <b>beryl</b> - for Beryl
     *          <b>compiz</b> - for Compiz
     *          <b>emerald</b> - for Emerald
     *          <b>fluxbox</b> - for Fluxbox
     *          <b>openbox</b> - for Openbox
     *          <b>blackbox</b> - for Blackbox
     *          <b>xfwm</b> - for Xfwm
     *          <b>metacity</b> - for Metacity
     *          <b>kwin</b> - for Kwin
     *          <b>fvwm</b> - for FVWM
     *          <b>enlightenment</b> - for Enlightenment
     *          <b>icewm</b> - for IceWM
     *          <b>wmaker</b> - for Window Maker
     *          <b>pekwm</b> - for PekWM
     *          <b>unknown</b> - for other Window Managers
     *          </ul>
     */
    private String findWMName(Vector<String> processes) {

        Enumeration en = wmList.keys();

        while (en.hasMoreElements()) {
            String wmProc = (String) en.nextElement();
            String wmName = wmList.get(wmProc);

            if (processes.contains(wmProc)) {
                return wmName;
            }
        }

        return "unknown";

    }

    private void findThemeAndIconTheme(String deName) {

        this.widgetTheme = "unknown";
        this.iconTheme = "unknown";

        if (deName != null) {
            deName = deName.toLowerCase();

            if (deName.equals("gnome")) {

                final String GET_GTK_THEME_CMD = "gconftool-2 -g /desktop/gnome/interface/gtk_theme";
                Vector<String> cmdResults = execCommand(GET_GTK_THEME_CMD);
                if (cmdResults.size() > 0) {
                    this.widgetTheme = cmdResults.get(0);
                }

                final String GET_ICON_THEME_CMD = "gconftool-2 -g /desktop/gnome/interface/icon_theme";
                cmdResults = execCommand(GET_ICON_THEME_CMD);
                if (cmdResults.size() > 0) {
                    this.iconTheme = cmdResults.get(0);
                }

            }//Done with Gnome            
            else if (deName.equals("kde")) {
                this.widgetTheme = "default";
                this.iconTheme = "default.kde";
                String userHome = System.getProperty("user.home");

                if (userHome != null) {
                    File kdeglobals = new File(userHome + "/.kde/share/config/kdeglobals");

                    try {

                        BufferedReader br = new BufferedReader(new FileReader(kdeglobals));
                        String line;

                        boolean f1 = false;
                        boolean f2 = false;
                        boolean finished = false;
                        while (((line = br.readLine()) != null) && (finished == false)) {
                            line = line.trim();
                            if (line.equals("[General]")) {
                                boolean done = false;
                                while (((line = br.readLine()) != null) && (done == false)) {
                                    line = line.trim();
                                    if (line.startsWith("widgetStyle")) {
                                        int start = line.indexOf("=");
                                        this.widgetTheme = line.substring(start + 1);
                                        done = true;
                                    }
                                }
                                f1 = true;
                            }
                            if (line.equals("[Icons]")) {
                                boolean done = false;
                                while (((line = br.readLine()) != null) && (done == false)) {
                                    line = line.trim();
                                    if (line.startsWith("Theme")) {
                                        int start = line.indexOf("=");
                                        this.iconTheme = line.substring(start + 1);
                                        done = true;
                                    }
                                }
                                f2 = true;
                            }
                            finished = f1 && f2;
                            if (line == null) {
                                break;
                            }
                        }

                    } catch (Exception e) {
                    }
                }
            }//Done with KDE
            else if (deName.equals("xfce4")) {

                String userHome = System.getProperty("user.home");

                if (userHome != null) {
                    File xfceGTKSettings = new File(userHome + "/.config/xfce4/mcs_settings/gtk.xml");

                    try {

                        BufferedReader br = new BufferedReader(new FileReader(xfceGTKSettings));
                        String line;

                        boolean f1 = false;
                        boolean f2 = false;
                        boolean finished = false;

                        while (((line = br.readLine()) != null) && (finished == false)) {
                            line = line.trim();

                            if (line.startsWith("<option name=\"Net/ThemeName\"")) {
                                int start_index = line.lastIndexOf("=") + 2;
                                int end_index = line.lastIndexOf("\"");
                                this.widgetTheme = line.substring(start_index, end_index);
                                f1 = true;
                            }
                            if (line.startsWith("<option name=\"Net/IconThemeName\"")) {
                                int start_index = line.lastIndexOf("=") + 2;
                                int end_index = line.lastIndexOf("\"");
                                this.iconTheme = line.substring(start_index, end_index);
                                f2 = true;
                            }

                            finished = f1 && f2;
                            if (line == null) {
                                break;
                            }
                        }

                    } catch (Exception e) {
                    }

                }
            }//Done with XFCE

        }

    }

    /**
     * Helper function to execute external processes
     * @param cmd The command to be executed
     * @return A Vector of strings representing the commands output
     */
    private Vector<String> execCommand(String cmd) {

        Vector<String> result = new Vector<String>();

        Runtime rt = Runtime.getRuntime();
        try {
            Process p = rt.exec(cmd);
            BufferedReader input =
                    new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = new String();
            while ((line = input.readLine()) != null) {
                result.add(line);
            }
            input.close();
        } catch (IOException ioe) {
            System.err.println(ioe.toString());
        }

        return result;
    }

    /**
     * Lists the running processes on a Unix machine by invoking the 'ps -A' command
     * @return A Vector of strings representing the names of the running processes
     */
    private Vector<String> listRunningProcessesOnUnix() {
        Vector<String> v = execCommand("ps -A");

        Vector<String> result = new Vector<String>(v.size());
        Enumeration e = v.elements();

        while (e.hasMoreElements()) {
            String[] s = ((String) e.nextElement()).split("\\s");
            result.add(s[s.length - 1]);
        }

        return result;

    }

    public static void main(String[] args) {

        DesktopEnvironmentInfo de = new DesktopEnvironmentInfo();

        System.out.println("Desktop Environment: " + de.getDesktopEnvironmentName());
        System.out.println("Window manager: " + de.getWindowManagerName());
        System.out.println("Widgets Theme: " + de.getWidgetThemeName());
        System.out.println("Icon theme: " + de.getIconThemeName());
    }
}

Υποβολή απάντησης