e2b8b283c7d4b25278cd210a5a420cba5adea2ce
[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                 stderr.printf("COMMAND: %s\n", exec);
151         }
152 }
153
154 public class Launcher: Applet
155 {
156         static const string DENTRY_GROUP = "Desktop Entry";
157
158         public Launcher(string label)
159         {
160                 _label = label;
161
162                 var dir = File.new_for_path(zavai.config.homedir);
163                 var enumerator = dir.enumerate_children(FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
164
165                 FileInfo file_info;
166                 var icon_theme = Gtk.IconTheme.get_default();
167                 while ((file_info = enumerator.next_file(null)) != null)
168                 {
169                         string pathname = zavai.config.homedir + "/" + file_info.get_name();
170                         //stdout.printf("Load %s\n", pathname);
171
172                         string icon = null;
173                         var kf = new KeyFile();
174                         try {
175                                 kf.load_from_file(pathname, KeyFileFlags.NONE);
176                                 if (! kf.has_group(DENTRY_GROUP)) continue;
177                                 if (! kf.has_key(DENTRY_GROUP, "Name")) continue;
178                                 if (! kf.has_key(DENTRY_GROUP, "Exec")) continue;
179                                 if (kf.has_key(DENTRY_GROUP, "Icon"))
180                                         icon = kf.get_string(DENTRY_GROUP, "Icon");
181                         } catch (Error e) {
182                                 zavai.log.error("Skipping file " + pathname + ": " + e.message);
183                                 continue;
184                         }
185                         var name = kf.get_string(DENTRY_GROUP, "Name");
186                         var exec = kf.get_string(DENTRY_GROUP, "Exec");
187                         var button = new LauncherButton(name, exec);
188                         if (icon != null && icon_theme.has_icon(icon))
189                         {
190                                 try {
191                                         var pb = icon_theme.load_icon(icon, zavai.config.min_button_height * 2 / 3, 0);
192                                         button.image = new Gtk.Image.from_pixbuf(pb);
193                                 } catch (Error e) {
194                                         zavai.log.error("Skipping icon " + icon + ": " + e.message);
195                                 }
196                         }
197                         pack_start(button, false, false, 0);
198                 }
199
200 /*
201     try {
202     } catch (Error e) {
203         stderr.printf ("Error: %s\n", e.message);
204         return 1;
205     }
206
207                 for (string file in confdir)
208                 {
209                 }
210 */
211         }
212 }
213
214 RaiseIcon raise_icon;
215 CloseOrBack close_or_back;
216 WindowList window_list;
217 Launcher launcher;
218
219 public void init()
220 {
221         raise_icon = new RaiseIcon();
222         raise_icon.set_visible(true);
223
224         close_or_back = new CloseOrBack();
225         close_or_back.set_visible(true);
226
227         window_list = new WindowList("Current apps");
228         zavai.registry.register_applet("wm.list", window_list);
229         zavai.registry.getmenu("menu.main").add_applet("wm.list");
230
231         try {
232                 launcher = new Launcher("Run program");
233         } catch (Error e) {
234                 zavai.log.error("Not running launcher: " + e.message);
235                 launcher = null;
236         }
237         
238         if (launcher != null)
239         {
240                 zavai.registry.register_applet("wm.launcher", launcher);
241                 zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
242         }
243 }
244
245 }
246 }
247 }