1 package net.sf.glxdesktop;
2
3 import java.io.IOException;
4
5 import net.sf.glxdesktop.parser.ConfigFileTools;
6
7 import org.apache.oro.text.regex.MalformedPatternException;
8
9 /***
10 * Utility Class to manage Gnome configuration.
11 * @author Alphonse Van Assche
12 * @date 21 sept. 06
13 * @version $
14 */
15 public class GnomeUtils {
16
17 private static final String DEFAULT_SESSION_FILE = "/usr/share/gnome/default.session";
18
19 /***
20 * Add a new default Gnome session entry.
21 * @param command to execute.
22 * @param priority of the command.
23 * see default.session man page for more informations
24 * @return true if the entry is corectly added
25 */
26 public static boolean addNewGSessionEntry(String command, int priority) {
27 boolean status = false;
28 String sessionItem;
29 int numClient, lastEntry;
30 if (!checkGSessionEntry(command)) {
31 try {
32 ConfigFileTools defaultSession = new ConfigFileTools(DEFAULT_SESSION_FILE);
33 numClient = Integer.parseInt(defaultSession.findValueByKey(
34 "num_clients", "="));
35 lastEntry = numClient -1;
36 defaultSession.replaceValues("num_clients=.*", "num_clients="
37 + (numClient + 1));
38 sessionItem = numClient + ",id=default" + numClient + "\n";
39 sessionItem += numClient + ",Priority=" + priority + "\n";
40 sessionItem += numClient + ",RestartCommand=" + command
41 + " default" + numClient + "\n";
42 defaultSession.appendText(" default" + lastEntry, sessionItem);
43 defaultSession.save();
44 status = true;
45 } catch (MalformedPatternException e) {
46
47 e.printStackTrace();
48 }
49 }
50 return status;
51 }
52
53 /***
54 * Check if a default Gnome session entry, is present.
55 * @param command to check
56 * @return true if the entry is found.
57 */
58 public static boolean checkGSessionEntry(String command) {
59 boolean status = false;
60 ConfigFileTools defaultSession = new ConfigFileTools(DEFAULT_SESSION_FILE);
61 try {
62 status = defaultSession.checkTextInFile(command);
63 } catch (MalformedPatternException e) {
64
65 e.printStackTrace();
66 }
67 return status;
68 }
69
70 /***
71 * Remove a default Gnome session entry.
72 * @param command to remove from the default session.
73 * @return true if the entry is corectly removed.
74 */
75 public static boolean removeGSessionEntry(String command) {
76 boolean status = false;
77 int entry, numClient;
78 ConfigFileTools defaultSession = new ConfigFileTools(DEFAULT_SESSION_FILE);
79 try {
80 entry = Integer.parseInt(defaultSession.findValueByKey(command + ".*", "default"));
81 numClient = Integer.parseInt(defaultSession.findValueByKey(
82 "num_clients", "="));
83 defaultSession.removeTextInFile(entry + ",.*");
84 defaultSession.replaceValues("num_clients=.*", "num_clients="
85 + (numClient - 1));
86 if (numClient > entry +1) {
87 for (int i = 0; i < (numClient - entry); i++) {
88 defaultSession.replaceValues((entry + i) + ",", (entry + i - 1) + ",");
89 defaultSession.replaceValues("default" + (entry + i), "default" + (entry + i - 1));
90 }
91 }
92 defaultSession.save();
93 status = true;
94 } catch (MalformedPatternException e) {
95
96 e.printStackTrace();
97 } catch (IOException e) {
98
99 e.printStackTrace();
100 }
101 return status;
102 }
103
104 public static void main(String[] args) {
105
106 GnomeUtils.addNewGSessionEntry("gnome-window-decorator", 50);
107 GnomeUtils.addNewGSessionEntry("compiz --replace gconf", 60);
108 }
109 }