c24567d9041694cde8db47a2da9c192f493081ff
[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     protected Wnck.Tasklist selector;
128     protected AppletPushLink launcher_link;
129
130     public WindowList(string label)
131     {
132         _label = label;
133         selector = new Wnck.Tasklist(Wnck.Screen.get_default());
134         pack_start(selector, true, true, 0);
135
136         launcher_link = new AppletPushLink(zavai.ui.wm.launcher);
137         button_box.pack_start(launcher_link, true, true, 0);
138     }
139 }
140
141 public class LauncherButton : Gtk.Button
142 {
143     private string exec;
144
145     public LauncherButton(string name, string exec)
146     {
147         label = name;
148         this.exec = exec;
149         clicked += on_clicked;
150         set_size_request(0, zavai.config.min_button_height);
151     }
152
153     public void on_clicked()
154     {
155         zavai.log.info("Run program: " + exec);
156         string[] args = exec.split(" ");
157         string[] args1 = new string[args.length + 1];
158         int cout = 0;
159         for (int cin = 0; cin < args.length; ++cin)
160         {
161             if (args[cin][0] == '%') continue;
162             args1[cout++] = args[cin];
163         }
164         args1[cout] = null;
165         Pid pid;
166         try {
167             Process.spawn_async(
168                 Environment.get_home_dir(),
169                 args1,
170                 null,
171                 SpawnFlags.SEARCH_PATH,
172                 null,
173                 out pid);
174         } catch (SpawnError e) {
175             zavai.log.error("Launching " + exec + ": " + e.message);
176         }
177     }
178 }
179
180 public class Launcher: Applet
181 {
182     static const string DENTRY_GROUP = "Desktop Entry";
183
184     public Launcher(string label) throws Error
185     {
186         _label = label;
187
188         var dir = File.new_for_path(zavai.config.homedir);
189         var enumerator = dir.enumerate_children(FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
190
191         FileInfo file_info;
192         var icon_theme = Gtk.IconTheme.get_default();
193         List<LauncherButton> buttons = new List<LauncherButton>();
194         int buttons_size = 0;
195         while ((file_info = enumerator.next_file(null)) != null)
196         {
197             if (!file_info.get_name().has_suffix(".desktop")) continue;
198
199             string pathname = zavai.config.homedir + "/" + file_info.get_name();
200             //stdout.printf("Load %s\n", pathname);
201
202             string icon = null;
203             var kf = new KeyFile();
204             try {
205                 kf.load_from_file(pathname, KeyFileFlags.NONE);
206                 if (! kf.has_group(DENTRY_GROUP)) continue;
207                 if (! kf.has_key(DENTRY_GROUP, "Name")) continue;
208                 if (! kf.has_key(DENTRY_GROUP, "Exec")) continue;
209                 if (kf.has_key(DENTRY_GROUP, "Icon"))
210                     icon = kf.get_string(DENTRY_GROUP, "Icon");
211             } catch (Error e) {
212                 zavai.log.error("Skipping file " + pathname + ": " + e.message);
213                 continue;
214             }
215             var name = kf.get_string(DENTRY_GROUP, "Name");
216             var exec = kf.get_string(DENTRY_GROUP, "Exec");
217             var button = new LauncherButton(name, exec);
218             if (icon != null && icon_theme.has_icon(icon))
219             {
220                 try {
221                     var pb = icon_theme.load_icon(icon, zavai.config.min_button_height * 2 / 3, 0);
222                     button.image = new Gtk.Image.from_pixbuf(pb);
223                 } catch (Error e) {
224                     zavai.log.error("Skipping icon " + icon + ": " + e.message);
225                 }
226             }
227             button.clicked += a => { this.back_to_main(); zavai.app.ensure_hidden(); };
228             buttons.append(button);
229             ++buttons_size;
230         }
231
232         // Create the table with the launcher buttons
233         uint ROWMAX = 5;    // Maximum number of rows
234         uint cols = 1 + (buttons_size + 1) / ROWMAX; // ceil(size/ROWMAX)
235         uint rows = (buttons_size + 1) / cols;   // ceil(size/cols)
236         var table = new Gtk.Table(rows, cols, true);
237
238         // Attach the buttons in the table
239         uint row = 0;
240         uint col = 0;
241         foreach (LauncherButton b in buttons)
242         {
243             table.attach(b, col, col+1, row, row+1,
244                 Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL,
245                 Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL,
246                 0, 0);
247             ++col;
248             if (col == cols)
249             {
250                 col = 0;
251                 ++row;
252             }
253         }
254
255         pack_start(table, true, true, 0);
256
257 /*
258     try {
259     } catch (Error e) {
260         stderr.printf ("Error: %s\n", e.message);
261         return 1;
262     }
263
264         for (string file in confdir)
265         {
266         }
267 */
268     }
269 }
270
271 public class AppShortcut : Object
272 {
273     private StatusIcon icon;
274
275     public AppShortcut()
276     {
277         icon = new StatusIcon();
278         icon.install();
279         icon.clicked += on_icon_clicked;
280         icon.set_from_file(zavai.config.icondir + "/apps.png");
281     }
282
283     public void on_icon_clicked()
284     {
285         zavai.app.push_applet(zavai.ui.wm.window_list);
286     }
287 }
288
289 RaiseIcon raise_icon;
290 CloseOrBack close_or_back;
291 WindowList window_list;
292 Launcher launcher;
293 AppShortcut app_shortcut;
294
295 public void init()
296 {
297     raise_icon = new RaiseIcon();
298     raise_icon.set_visible(true);
299
300     close_or_back = new CloseOrBack();
301     close_or_back.set_visible(true);
302
303     app_shortcut = new AppShortcut();
304
305     try {
306         launcher = new Launcher("Run program");
307     } catch (Error e) {
308         stderr.printf("Error creating app launcher: %s\n", e.message);
309         launcher = null;
310     }
311
312     window_list = new WindowList("Apps");
313     zavai.menu_main.add_applet(window_list);
314
315     //zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
316 }
317
318 }
319 }
320 }