2 * app - zavai main window
4 * Copyright (C) 2009-2010 Enrico Zini <enrico@enricozini.org>
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.
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.
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
25 public class Zavai : Gtk.Window, zavai.Resource
27 public bool visibility = true;
28 public signal void visibility_changed(bool visible);
36 destroy += Gtk.main_quit;
37 set_events(get_events() | Gdk.EventMask.VISIBILITY_NOTIFY_MASK);
38 set_position(Gtk.WindowPosition.MOUSE);
39 visibility_notify_event += on_visibility;
40 set_skip_pager_hint(true);
41 set_skip_taskbar_hint(true);
42 //set_type_hint(Gdk.WindowTypeHint.DESKTOP);
44 zavai.registry.register(this);
47 private bool on_visibility(Gdk.Event event)
49 visibility = (event.visibility.state == Gdk.VisibilityState.UNOBSCURED);
50 //visible = visibility;
51 visibility_changed(visibility);
55 public void toggle_visibility()
61 visibility_changed(visibility);
62 //zavai.app.iconify();
66 set_skip_pager_hint(true);
67 set_skip_taskbar_hint(true);
71 public void ensure_visible()
77 set_skip_pager_hint(true);
78 set_skip_taskbar_hint(true);
82 public void ensure_hidden()
88 visibility_changed(visibility);
92 public void show_applet(Applet applet)
94 // Remove the current applet
102 // Add the new applet
109 public void push_applet(Applet applet)
111 // Make the function idempotent
112 if (current == applet)
115 // Remove the current applet
118 //stderr.printf("push applet remove %s\n", current_name);
119 applet.back_link = current;
125 //stderr.printf("push applet add %s\n", name);
126 // Add the new applet
139 public void back_to_main()
141 show_applet(zavai.ui.main.status);
144 public void shutdown()
150 set_size_request(300, 500);
152 if (zavai.config.profile == "laptop")
155 zavai.app.ensure_hidden();
156 zavai.ui.wm.raise_icon.update_icon();
162 public void run_script(string command)
164 zavai.log.info("Run program: " + command);
165 string[] args = command.split(" ");
169 Environment.get_home_dir(),
172 SpawnFlags.SEARCH_PATH,
175 } catch (SpawnError e) {
176 zavai.log.error("Running " + command + ": " + e.message);
180 public int run_script_sync(string command, out string std_out, out string std_err)
183 zavai.log.info("Run program: " + command);
184 string[] args = command.split(" ");
186 bool res = Process.spawn_sync(Environment.get_home_dir(), args, null, SpawnFlags.SEARCH_PATH, null, out std_out, out std_err, out status);
187 } catch (SpawnError e) {
188 zavai.log.error("Running " + command + ": " + e.message);
194 public abstract class Applet : Gtk.VBox, Resource
196 // 'label' property: label to show in window title or button names
197 protected string _label;
198 public string label {
199 get { return this._label; }
208 public signal void label_changed();
210 protected Gtk.HBox button_box;
212 // 'back_link' property: link to use to "go back". If null, do not show
214 protected AppletLink _back_link = null;
215 public Applet back_link {
216 get { return _back_link.target; }
218 //stderr.printf("Set back link of %s to %s\n", _label, value);
219 if (value == null && _back_link != null)
221 _back_link.target = value;
222 button_box.remove(_back_link);
223 } else if (value != null) {
224 if (_back_link.target == null)
226 _back_link.target = value;
227 button_box.pack_end(_back_link, true, true, 0);
230 _back_link.target = value;
237 button_box = new Gtk.HBox(true, 0);
238 this.homogeneous = false;
240 pack_end(button_box, false, true, 0);
241 _back_link = new AppletStraightLink();
242 zavai.registry.register(this);
245 public virtual void back()
247 _back_link.activate_applet();
250 public virtual void back_to_main()
252 zavai.app.back_to_main();
255 public void shutdown()
260 public virtual void start() {}
261 public virtual void stop() {}
264 public class Menu : Applet
266 public Menu(string label)
271 public void add_applet(Applet target)
273 pack_start(new AppletPushLink(target), false, false, 0);
276 public void add_service_toggle(Service service, string label_start, string label_stop)
278 pack_start(new ServiceRequestLink(service, label_start, label_stop), false, false, 0);
281 public void add_widget(Gtk.Widget w)
283 pack_start(w, false, false, 0);
287 public class BigButton : Gtk.Button
291 set_size_request(0, zavai.config.min_button_height);
295 public abstract class AppletLink : BigButton
297 protected Applet _target;
298 public Applet target {
299 get { return _target; }
304 _target.label_changed -= on_label_changed;
306 bool was_shown = _target != null;
310 set_label(_target.label);
311 _target.label_changed += on_label_changed;
312 if (!was_shown) show();
314 if (was_shown) hide();
319 private void on_label_changed(Applet a)
324 private abstract void on_clicked(Gtk.Button src);
326 public AppletLink(Applet? applet = null)
331 clicked += on_clicked;
334 public virtual void activate_applet()
340 public class AppletStraightLink : AppletLink
342 private override void on_clicked(Gtk.Button src)
344 //stderr.printf("straight link: %s\n", _target);
346 zavai.app.show_applet(_target);
349 public AppletStraightLink(Applet? applet = null)
355 public class AppletPushLink : AppletLink
357 private override void on_clicked(Gtk.Button src)
359 //stderr.printf("push link: %s\n", _target);
361 zavai.app.push_applet(_target);
364 public AppletPushLink(Applet? applet = null)
370 public class ServiceRequestLink : Gtk.ToggleButton
372 protected Service service;
373 protected string label_start;
374 protected string label_stop;
376 private void on_toggled(Gtk.Button src)
379 service.request("servicerequestlink");
381 service.release("servicerequestlink");
382 set_label(get_active() ? label_stop : label_start);
385 private void on_service_toggled(bool val)
388 //set_label(val ? label_stop : label_start);
391 public ServiceRequestLink(Service service, string label_start, string label_stop)
393 this.service = service;
394 this.label_start = label_start;
395 this.label_stop = label_stop;
396 set_size_request(0, zavai.config.min_button_height);
397 set_active(service.started);
398 toggled += on_toggled;
399 service.toggled += on_service_toggled;
401 set_label(get_active() ? label_stop : label_start);
405 public class StatusIcon : Gtk.Button
407 private Gtk.Image image_widget;
411 relief = Gtk.ReliefStyle.NONE;
412 image_widget = new Gtk.Image();
413 image = image_widget;
416 public void set_from_file(string file)
418 image_widget.set_from_file(file);
421 public void install()
423 zavai.ui.main.status.status_icons.pack_start(this, false, false, 0);
428 public zavai.Zavai app;
429 public zavai.Menu menu_main;
430 public zavai.Menu menu_gps;
431 public zavai.Menu menu_gsm;
432 public zavai.Menu menu_misc;
438 zavai.app = new zavai.Zavai();
439 menu_main = new zavai.Menu("Main menu");
442 menu_gps = new zavai.Menu("GPS");
443 menu_main.add_applet(menu_gps);
445 menu_gsm = new zavai.Menu("GSM");
446 menu_main.add_applet(menu_gsm);
448 menu_misc = new zavai.Menu("Misc");
449 menu_main.add_applet(menu_misc);