Attended to various warnings
[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                 try {
163                         Process.spawn_async(
164                                 Environment.get_home_dir(),
165                                 args1,
166                                 null,
167                                 SpawnFlags.SEARCH_PATH,
168                                 null,
169                                 out pid);
170                 } catch (SpawnError e) {
171                         zavai.log.error("Launching " + exec + ": " + e.message);
172                 }
173         }
174 }
175
176 public class Launcher: Applet
177 {
178         static const string DENTRY_GROUP = "Desktop Entry";
179
180         public Launcher(string label)
181         {
182                 _label = label;
183
184                 var dir = File.new_for_path(zavai.config.homedir);
185                 var enumerator = dir.enumerate_children(FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
186
187                 FileInfo file_info;
188                 var icon_theme = Gtk.IconTheme.get_default();
189                 Gee.ArrayList<LauncherButton> buttons = new Gee.ArrayList<LauncherButton>();
190                 while ((file_info = enumerator.next_file(null)) != null)
191                 {
192                         if (!file_info.get_name().has_suffix(".desktop")) continue;
193                                 
194                         string pathname = zavai.config.homedir + "/" + file_info.get_name();
195                         //stdout.printf("Load %s\n", pathname);
196
197                         string icon = null;
198                         var kf = new KeyFile();
199                         try {
200                                 kf.load_from_file(pathname, KeyFileFlags.NONE);
201                                 if (! kf.has_group(DENTRY_GROUP)) continue;
202                                 if (! kf.has_key(DENTRY_GROUP, "Name")) continue;
203                                 if (! kf.has_key(DENTRY_GROUP, "Exec")) continue;
204                                 if (kf.has_key(DENTRY_GROUP, "Icon"))
205                                         icon = kf.get_string(DENTRY_GROUP, "Icon");
206                         } catch (Error e) {
207                                 zavai.log.error("Skipping file " + pathname + ": " + e.message);
208                                 continue;
209                         }
210                         var name = kf.get_string(DENTRY_GROUP, "Name");
211                         var exec = kf.get_string(DENTRY_GROUP, "Exec");
212                         var button = new LauncherButton(name, exec);
213                         if (icon != null && icon_theme.has_icon(icon))
214                         {
215                                 try {
216                                         var pb = icon_theme.load_icon(icon, zavai.config.min_button_height * 2 / 3, 0);
217                                         button.image = new Gtk.Image.from_pixbuf(pb);
218                                 } catch (Error e) {
219                                         zavai.log.error("Skipping icon " + icon + ": " + e.message);
220                                 }
221                         }
222                         buttons.add(button);
223                 }
224
225                 // Create the table with the launcher buttons
226                 uint ROWMAX = 5;        // Maximum number of rows
227                 uint cols = 1 + (buttons.size + 1) / ROWMAX; // ceil(size/ROWMAX)
228                 uint rows = (buttons.size + 1) / cols;   // ceil(size/cols)
229                 var table = new Gtk.Table(rows, cols, true);
230
231                 // Attach the buttons in the table
232                 uint row = 0;
233                 uint col = 0;
234                 foreach (LauncherButton b in buttons)
235                 {
236                         table.attach(b, col, col+1, row, row+1,
237                                 Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL,
238                                 Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL,
239                                 0, 0);
240                         ++col;
241                         if (col == cols)
242                         {
243                                 col = 0;
244                                 ++row;
245                         }
246                 }
247
248                 pack_start(table, true, true, 0);
249
250 /*
251     try {
252     } catch (Error e) {
253         stderr.printf ("Error: %s\n", e.message);
254         return 1;
255     }
256
257                 for (string file in confdir)
258                 {
259                 }
260 */
261         }
262 }
263
264 RaiseIcon raise_icon;
265 CloseOrBack close_or_back;
266 WindowList window_list;
267 Launcher launcher;
268
269 public void init()
270 {
271         raise_icon = new RaiseIcon();
272         raise_icon.set_visible(true);
273
274         close_or_back = new CloseOrBack();
275         close_or_back.set_visible(true);
276
277         window_list = new WindowList("Current apps");
278         zavai.registry.register_applet("wm.list", window_list);
279         zavai.registry.getmenu("menu.main").add_applet("wm.list");
280
281         launcher = new Launcher("Run program");
282         zavai.registry.register_applet("wm.launcher", launcher);
283         zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
284 }
285
286 }
287 }
288 }