6882e6567e319258d99c779b314a6bc9386ee282
[gregoa/zavai.git] / src / app_wm.vala
1 /*
2  * app_wm - zavai window management functions
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 using GLib;
22
23 namespace zavai {
24 namespace ui {
25 namespace wm {
26
27 /*
28 static void print_window_state(Gtk.Window w)
29 {
30         Gdk.WindowState state = w.window.get_state();
31         int istate = (int) state;
32         bool is_icon = (state & Gdk.WindowState.ICONIFIED) != 0;
33         bool is_visible = w.visible;
34         bool is_focus = w.is_focus;
35         bool has_focus = ((Gtk.Widget)w).has_focus;
36         bool is_active = w.is_active;
37         bool is_toplevel = w.is_toplevel();
38         bool is_tfocus = w.has_toplevel_focus;
39
40         stderr.printf("istate: %d; is_icon: %s; is_visible: %s; is_focus: %s; has_focus: %s, is_active: %s; is_toplevel: %s; is_tfocus: %s\n",
41                         istate,
42                         is_icon ? "true" : "false",
43                         is_visible ? "true" : "false",
44                         is_focus ? "true" : "false",
45                         has_focus ? "true" : "false",
46                         is_active ? "true" : "false",
47                         is_toplevel ? "true" : "false",
48                         is_tfocus ? "true" : "false"
49                      );
50 }
51 */
52
53 public class RaiseIcon : Gtk.StatusIcon
54 {
55         public RaiseIcon()
56         {
57                 activate += on_activate;
58                 zavai.app.visibility_changed += on_visibility_changed;
59                 update_icon();
60         }
61
62         private void on_visibility_changed(bool visible)
63         {
64                 update_icon();
65         }
66
67         private void on_activate()
68         {
69                 zavai.app.toggle_visibility();
70                 update_icon();
71         }
72
73         protected void update_icon()
74         {
75                 string name = zavai.config.icondir + "/";
76                 if (!zavai.app.visibility)
77                         name += "zavai_on.png";
78                 else
79                         name += "zavai_off.png";
80                 set_from_file(name);
81         }
82 }
83
84 public class CloseOrBack : Gtk.StatusIcon
85 {
86         public CloseOrBack()
87         {
88                 activate += on_activate;
89                 zavai.app.visibility_changed += on_visibility_changed;
90                 update_icon();
91         }
92
93         private void on_visibility_changed(bool visible)
94         {
95                 update_icon();
96         }
97
98         private void on_activate()
99         {
100                 if (zavai.app.visibility)
101                 {
102                         // Back
103                         zavai.app.back();
104                 } else {
105                         // Close current app
106                         Gdk.Window w = zavai.app.get_screen().get_active_window();
107                         if (w != zavai.app.window)
108                         {
109                                 w.destroy();
110                         }
111                 }
112         }
113
114         protected void update_icon()
115         {
116                 string name = zavai.config.icondir + "/";
117                 if (!zavai.app.visibility)
118                         name += "quit_on.png";
119                 else
120                         name += "quit_off.png";
121                 set_from_file(name);
122         }
123 }
124
125 public class WindowList : Applet
126 {
127         Wnck.Tasklist selector;
128
129         public WindowList(string label)
130         {
131                 _label = label;
132                 selector = new Wnck.Tasklist(Wnck.Screen.get_default());
133                 pack_start(selector, true, true, 0);
134         }
135 }
136
137 public class LauncherButton : Gtk.Button
138 {
139         private string exec;
140
141         public LauncherButton(string name, string exec)
142         {
143                 label = name;
144                 this.exec = exec;
145                 clicked += on_clicked;
146                 set_size_request(0, zavai.config.min_button_height);
147         }
148
149         public void on_clicked()
150         {
151                 zavai.log.info("Run program: " + exec);
152                 string[] args = exec.split(" ");
153                 string[] args1 = new string[args.length + 1];
154                 int cout = 0;
155                 for (int cin = 0; cin < args.length; ++cin)
156                 {
157                         if (args[cin][0] == '%') continue;
158                         args1[cout++] = args[cin];
159                 }
160                 args1[cout] = null;
161                 Pid pid;
162                 Process.spawn_async(
163                         Environment.get_home_dir(),
164                         args1,
165                         null,
166                         SpawnFlags.SEARCH_PATH,
167                         null,
168                         out pid);
169         }
170 }
171
172 public class Launcher: Applet
173 {
174         static const string DENTRY_GROUP = "Desktop Entry";
175
176         public Launcher(string label)
177         {
178                 _label = label;
179
180                 var dir = File.new_for_path(zavai.config.homedir);
181                 var enumerator = dir.enumerate_children(FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
182
183                 FileInfo file_info;
184                 var icon_theme = Gtk.IconTheme.get_default();
185                 Gee.ArrayList<LauncherButton> buttons = new Gee.ArrayList<LauncherButton>();
186                 while ((file_info = enumerator.next_file(null)) != null)
187                 {
188                         if (!file_info.get_name().has_suffix(".desktop")) continue;
189                                 
190                         string pathname = zavai.config.homedir + "/" + file_info.get_name();
191                         //stdout.printf("Load %s\n", pathname);
192
193                         string icon = null;
194                         var kf = new KeyFile();
195                         try {
196                                 kf.load_from_file(pathname, KeyFileFlags.NONE);
197                                 if (! kf.has_group(DENTRY_GROUP)) continue;
198                                 if (! kf.has_key(DENTRY_GROUP, "Name")) continue;
199                                 if (! kf.has_key(DENTRY_GROUP, "Exec")) continue;
200                                 if (kf.has_key(DENTRY_GROUP, "Icon"))
201                                         icon = kf.get_string(DENTRY_GROUP, "Icon");
202                         } catch (Error e) {
203                                 zavai.log.error("Skipping file " + pathname + ": " + e.message);
204                                 continue;
205                         }
206                         var name = kf.get_string(DENTRY_GROUP, "Name");
207                         var exec = kf.get_string(DENTRY_GROUP, "Exec");
208                         var button = new LauncherButton(name, exec);
209                         if (icon != null && icon_theme.has_icon(icon))
210                         {
211                                 try {
212                                         var pb = icon_theme.load_icon(icon, zavai.config.min_button_height * 2 / 3, 0);
213                                         button.image = new Gtk.Image.from_pixbuf(pb);
214                                 } catch (Error e) {
215                                         zavai.log.error("Skipping icon " + icon + ": " + e.message);
216                                 }
217                         }
218                         buttons.add(button);
219                 }
220
221                 // Create the table with the launcher buttons
222                 uint ROWMAX = 5;        // Maximum number of rows
223                 uint cols = 1 + (buttons.size + 1) / ROWMAX; // ceil(size/ROWMAX)
224                 uint rows = (buttons.size + 1) / cols;   // ceil(size/cols)
225                 var table = new Gtk.Table(rows, cols, true);
226
227                 // Attach the buttons in the table
228                 uint row = 0;
229                 uint col = 0;
230                 foreach (LauncherButton b in buttons)
231                 {
232                         table.attach(b, col, col+1, row, row+1,
233                                 Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL,
234                                 Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL,
235                                 0, 0);
236                         ++col;
237                         if (col == cols)
238                         {
239                                 col = 0;
240                                 ++row;
241                         }
242                 }
243
244                 pack_start(table, true, true, 0);
245
246 /*
247     try {
248     } catch (Error e) {
249         stderr.printf ("Error: %s\n", e.message);
250         return 1;
251     }
252
253                 for (string file in confdir)
254                 {
255                 }
256 */
257         }
258 }
259
260 RaiseIcon raise_icon;
261 CloseOrBack close_or_back;
262 WindowList window_list;
263 Launcher launcher;
264
265 public void init()
266 {
267         raise_icon = new RaiseIcon();
268         raise_icon.set_visible(true);
269
270         close_or_back = new CloseOrBack();
271         close_or_back.set_visible(true);
272
273         window_list = new WindowList("Current apps");
274         zavai.registry.register_applet("wm.list", window_list);
275         zavai.registry.getmenu("menu.main").add_applet("wm.list");
276
277         try {
278                 launcher = new Launcher("Run program");
279         } catch (Error e) {
280                 zavai.log.error("Not running launcher: " + e.message);
281                 launcher = null;
282         }
283
284         if (launcher != null)
285         {
286                 zavai.registry.register_applet("wm.launcher", launcher);
287                 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
288         }
289 }
290
291 }
292 }
293 }