Δημοσιεύτηκε: 09 Ιαν 2009, 19:07
από cyberpython
Λοιπόν το κοίταξα λίγο και τελικά ο φίλος που το έγραψε κάνει έλεγχο των διεργασιών που "τρέχουν" ώστε να καθορίσει σε ποιό περιβάλλον εργασίας βρισκόμαστε, ποιός Window Manager χρησιμοποιείται κλπ. Δεν είναι και ότι πιο "εκλεπτυσμένο", αλλά λειτουργεί (τουλάχιστον στις περισσότερες περιπτώσεις, τώρα αν κάποιος τρέχει το settings manager του xfce σε gnome μάλλον δε θα λειτουργήσει σωστά).

Χρησιμοποιώντας ως οδηγό τον παραπάνω κώδικα σε Perl έγραψα την παρακάτω τάξη σε Java που επιστρέφει τα ονόματα του desktop environment, window manager, widget theme και icon theme. Έχω κάνει κάποιες αλλαγές για να λειτουργεί καλύτερα σε KDE από ότι το Perl script και για το περιβάλλον εργασίας Gnome ο έλεγχος δε γίνεται μέσω των ενεργών διεργασιών αλλά μέσω μίας μεταβλητής συστήματος της JVM.

Κώδικας: Επιλογή όλων

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());
}
}


Αποθηκεύστε το αρχείο με όνομα
Κώδικας: Επιλογή όλων
DesktopEnvironmentInfo.java


Το μεταγλωττίζετε με την εντολή (πρέπει να είναι εγκατεστημένο κάποιο JDK):
Κώδικας: Επιλογή όλων
javac DesktopEnvironmentInfo.java


και το τρέχετε με την εντολή:
Κώδικας: Επιλογή όλων
java DesktopEnvironmentInfo