Skip non-desktop files
[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                 } else {
104                         // Close current app
105                         Gdk.Window w = zavai.app.get_screen().get_active_window();
106                         if (w != zavai.app.window)
107                         {
108                                 w.destroy();
109                         }
110                 }
111         }
112
113         protected void update_icon()
114         {
115                 string name = zavai.config.icondir + "/";
116                 if (!zavai.app.visibility)
117                         name += "quit_on.png";
118                 else
119                         name += "quit_off.png";
120                 set_from_file(name);
121         }
122 }
123
124 public class WindowList : Applet
125 {
126         Wnck.Tasklist selector;
127
128         public WindowList(string label)
129         {
130                 _label = label;
131                 selector = new Wnck.Tasklist(Wnck.Screen.get_default());
132                 pack_start(selector, true, true, 0);
133         }
134 }
135
136 public class LauncherButton : Gtk.Button
137 {
138         private string exec;
139
140         public LauncherButton(string name, string exec)
141         {
142                 label = name;
143                 this.exec = exec;
144                 clicked += on_clicked;
145                 set_size_request(0, zavai.config.min_button_height);
146         }
147
148         public void on_clicked()
149         {
150                 zavai.log.info("Run program: " + exec);
151                 string[] args = new string[] { exec, null };
152                 Pid pid;
153                 Process.spawn_async(
154                         Environment.get_home_dir(),
155                         args,
156                         null,
157                         SpawnFlags.SEARCH_PATH,
158                         null,
159                         out pid);
160         }
161 }
162
163 public class Launcher: Applet
164 {
165         static const string DENTRY_GROUP = "Desktop Entry";
166
167         public Launcher(string label)
168         {
169                 _label = label;
170
171                 var dir = File.new_for_path(zavai.config.homedir);
172                 var enumerator = dir.enumerate_children(FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
173
174                 FileInfo file_info;
175                 var icon_theme = Gtk.IconTheme.get_default();
176                 while ((file_info = enumerator.next_file(null)) != null)
177                 {
178                         if (!file_info.get_name().has_suffix(".desktop")) continue;
179                                 
180                         string pathname = zavai.config.homedir + "/" + file_info.get_name();
181                         //stdout.printf("Load %s\n", pathname);
182
183                         string icon = null;
184                         var kf = new KeyFile();
185                         try {
186                                 kf.load_from_file(pathname, KeyFileFlags.NONE);
187                                 if (! kf.has_group(DENTRY_GROUP)) continue;
188                                 if (! kf.has_key(DENTRY_GROUP, "Name")) continue;
189                                 if (! kf.has_key(DENTRY_GROUP, "Exec")) continue;
190                                 if (kf.has_key(DENTRY_GROUP, "Icon"))
191                                         icon = kf.get_string(DENTRY_GROUP, "Icon");
192                         } catch (Error e) {
193                                 zavai.log.error("Skipping file " + pathname + ": " + e.message);
194                                 continue;
195                         }
196                         var name = kf.get_string(DENTRY_GROUP, "Name");
197                         var exec = kf.get_string(DENTRY_GROUP, "Exec");
198                         var button = new LauncherButton(name, exec);
199                         if (icon != null && icon_theme.has_icon(icon))
200                         {
201                                 try {
202                                         var pb = icon_theme.load_icon(icon, zavai.config.min_button_height * 2 / 3, 0);
203                                         button.image = new Gtk.Image.from_pixbuf(pb);
204                                 } catch (Error e) {
205                                         zavai.log.error("Skipping icon " + icon + ": " + e.message);
206                                 }
207                         }
208                         pack_start(button, false, false, 0);
209                 }
210
211 /*
212     try {
213     } catch (Error e) {
214         stderr.printf ("Error: %s\n", e.message);
215         return 1;
216     }
217
218                 for (string file in confdir)
219                 {
220                 }
221 */
222         }
223 }
224
225 RaiseIcon raise_icon;
226 CloseOrBack close_or_back;
227 WindowList window_list;
228 Launcher launcher;
229
230 public void init()
231 {
232         raise_icon = new RaiseIcon();
233         raise_icon.set_visible(true);
234
235         close_or_back = new CloseOrBack();
236         close_or_back.set_visible(true);
237
238         window_list = new WindowList("Current apps");
239         zavai.registry.register_applet("wm.list", window_list);
240         zavai.registry.getmenu("menu.main").add_applet("wm.list");
241
242         try {
243                 launcher = new Launcher("Run program");
244         } catch (Error e) {
245                 zavai.log.error("Not running launcher: " + e.message);
246                 launcher = null;
247         }
248
249         if (launcher != null)
250         {
251                 zavai.registry.register_applet("wm.launcher", launcher);
252                 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
253         }
254 }
255
256 }
257 }
258 }