Kill button as back button when on zavai
[gregoa/zavai.git] / src / app_wm.vala
index 957d856aa6835ec0a7127cb259ecd5b805de42ce..6882e6567e319258d99c779b314a6bc9386ee282 100644 (file)
@@ -25,107 +25,6 @@ namespace ui {
 namespace wm {
 
 /*
-class GPSOn(gtk.ToggleAction):
-    states = [_("GPS always on"), _("GPS on when needed")]
-
-    def __init__(self, registry, **kw):
-        self.state = 0
-        super(GPSOn, self).__init__("menu.main.gps.alwayson", self.states[self.state], None, None)
-
-        self.registry = registry
-        self.set_active(False)
-
-        self.connect("toggled", self.on_toggle)
-
-    def on_toggle(self, *args):
-        self.state = (self.state + 1) % len(self.states)
-        self.set_property("label", self.states[self.state])
-        if self.get_active():
-            self.start()
-        else:
-            self.stop()
-
-    def start(self):
-        self.registry.resource("gps").connect("gps", self)
-
-    def stop(self):
-        self.registry.resource("gps").disconnect("gps", self)
-
-class GPXTracer(gtk.ToggleAction):
-    states = [_("Start GPX trace"), _("Stop GPX trace")]
-
-    def __init__(self, registry, **kw):
-        self.state = 0
-        super(GPXTracer, self).__init__("menu.main.gps.gpx", self.states[self.state], None, None)
-
-        self.registry = registry
-        self.set_active(False)
-
-        self.connect("toggled", self.on_toggle)
-
-    def on_toggle(self, *args):
-        self.state = (self.state + 1) % len(self.states)
-        self.set_property("label", self.states[self.state])
-        if self.get_active():
-            self.start()
-        else:
-            self.stop()
-
-    def start(self):
-        zavai.info("GPX trace started")
-        self.registry.resource("gpx").connect("gpx", self)
-
-    def stop(self):
-        zavai.info("GPX trace ended")
-        self.registry.resource("gpx").disconnect("gpx", self)
-*/
-
-/*
-class GPXAudioTracer(gtk.ToggleAction):
-    states = [_("Start GPX and audio trace"), _("Stop GPX and audio trace")]
-
-    def __init__(self, registry, **kw):
-        self.state = 0
-        super(GPXAudioTracer, self).__init__("menu.main.gps.gpxaudio", self.states[self.state], None, None)
-
-        self.registry = registry
-        self.recorder = zavai.Recorder(registry)
-        self.set_active(False)
-
-        self.connect("toggled", self.on_toggle)
-
-    def shutdown(self):
-        self.recorder.stop()
-        super(GPXAudioTracer, self).shutdown()
-
-    def on_toggle(self, *args):
-        self.state = (self.state + 1) % len(self.states)
-        self.set_property("label", self.states[self.state])
-        if self.get_active():
-            self.start()
-        else:
-            self.stop()
-
-    def start(self):
-        zavai.info("GPX trace started")
-        gpx = self.registry.resource("gpx")
-        gpx.connect("gpx", self)
-        gpx.add_activity_monitor(self.on_gpx_activity_changed)
-
-    def stop(self):
-        zavai.info("GPX trace ended")
-        gpx = self.registry.resource("gpx")
-        gpx.disconnect("gpx", self)
-        self.recorder.stop()
-        gpx.del_activity_monitor(self.on_gpx_activity_changed)
-
-    def on_gpx_activity_changed(self, gpx, state):
-        if state:
-            self.recorder.start(gpx.basename + ".wav")
-        else:
-            self.recorder.stop()
-*/
-
 static void print_window_state(Gtk.Window w)
 {
        Gdk.WindowState state = w.window.get_state();
@@ -149,33 +48,45 @@ static void print_window_state(Gtk.Window w)
                        is_tfocus ? "true" : "false"
                     );
 }
+*/
 
 public class RaiseIcon : Gtk.StatusIcon
 {
-       /*
-       public bool on_focus_in (Gdk.EventFocus event)
+       public RaiseIcon()
        {
-               stderr.printf("FOCUS IN %c %d\n", event.send_event, event.in);
-               //update_icon();
-               return true;
+               activate += on_activate;
+               zavai.app.visibility_changed += on_visibility_changed;
+               update_icon();
        }
-       public bool on_focus_out (Gdk.EventFocus event)
+
+       private void on_visibility_changed(bool visible)
        {
-               stderr.printf("FOCUS OUT %c %d\n", event.send_event, event.in);
-               //update_icon();
-               return true;
+               update_icon();
        }
-       */
 
-       public RaiseIcon()
+       private void on_activate()
+       {
+               zavai.app.toggle_visibility();
+               update_icon();
+       }
+
+       protected void update_icon()
+       {
+               string name = zavai.config.icondir + "/";
+               if (!zavai.app.visibility)
+                       name += "zavai_on.png";
+               else
+                       name += "zavai_off.png";
+               set_from_file(name);
+       }
+}
+
+public class CloseOrBack : Gtk.StatusIcon
+{
+       public CloseOrBack()
        {
                activate += on_activate;
                zavai.app.visibility_changed += on_visibility_changed;
-               /*
-               zavai.app.focus_in_event += on_focus_in;
-               zavai.app.focus_out_event += on_focus_out;
-               */
-
                update_icon();
        }
 
@@ -186,107 +97,195 @@ public class RaiseIcon : Gtk.StatusIcon
 
        private void on_activate()
        {
-               zavai.app.toggle_visibility();
-               update_icon();
-
+               if (zavai.app.visibility)
+               {
+                       // Back
+                       zavai.app.back();
+               } else {
+                       // Close current app
+                       Gdk.Window w = zavai.app.get_screen().get_active_window();
+                       if (w != zavai.app.window)
+                       {
+                               w.destroy();
+                       }
+               }
        }
 
        protected void update_icon()
        {
                string name = zavai.config.icondir + "/";
                if (!zavai.app.visibility)
-                       name += "kbd_on.png";
+                       name += "quit_on.png";
                else
-                       name += "kbd_off.png";
+                       name += "quit_off.png";
                set_from_file(name);
        }
 }
 
-public class Test : BigButton
+public class WindowList : Applet
 {
-    public Test()
-    {
-           set_label("Window state");
-           clicked += on_clicked;
-    }
+       Wnck.Tasklist selector;
 
-    protected void on_clicked()
-    {
-           print_window_state(zavai.app);
-    }
+       public WindowList(string label)
+       {
+               _label = label;
+               selector = new Wnck.Tasklist(Wnck.Screen.get_default());
+               pack_start(selector, true, true, 0);
+       }
 }
 
-
-/*
-public class Waypoint : BigButton
+public class LauncherButton : Gtk.Button
 {
-    public Waypoint()
-    {
-        set_label("Take waypoint");
-        zavai.gps.gpx.tracking_changed += on_gpx_tracking_changed;
-        clicked += on_clicked;
-        set_sensitive(zavai.gps.gpx.tracking);
-    }
+       private string exec;
 
-    protected void on_gpx_tracking_changed(zavai.gps.GPX gpx, bool new_state)
-    {
-stderr.printf("Toggled %d\n", (int)new_state);
-        set_sensitive(new_state);
-    }
+       public LauncherButton(string name, string exec)
+       {
+               label = name;
+               this.exec = exec;
+               clicked += on_clicked;
+               set_size_request(0, zavai.config.min_button_height);
+       }
 
-    protected void on_clicked()
-    {
-stderr.printf("Activate\n");
-        zavai.gps.gpx.waypoint();
-    }
+       public void on_clicked()
+       {
+               zavai.log.info("Run program: " + exec);
+               string[] args = exec.split(" ");
+               string[] args1 = new string[args.length + 1];
+               int cout = 0;
+               for (int cin = 0; cin < args.length; ++cin)
+               {
+                       if (args[cin][0] == '%') continue;
+                       args1[cout++] = args[cin];
+               }
+               args1[cout] = null;
+               Pid pid;
+               Process.spawn_async(
+                       Environment.get_home_dir(),
+                       args1,
+                       null,
+                       SpawnFlags.SEARCH_PATH,
+                       null,
+                       out pid);
+       }
 }
-*/
 
-/*
-public class GPSRequestLink : Gtk.ToggleButton
+public class Launcher: Applet
 {
-       public GPSRequestLink()
+       static const string DENTRY_GROUP = "Desktop Entry";
+
+       public Launcher(string label)
        {
-        // GPS status icon
-        status_icon = new Gtk.StatusIcon();
-        status_icon.set_visible(true);
-        status_icon.activate += on_status_activate;
-        update_icon();
-       }
+               _label = label;
+
+               var dir = File.new_for_path(zavai.config.homedir);
+               var enumerator = dir.enumerate_children(FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
+
+               FileInfo file_info;
+               var icon_theme = Gtk.IconTheme.get_default();
+               Gee.ArrayList<LauncherButton> buttons = new Gee.ArrayList<LauncherButton>();
+               while ((file_info = enumerator.next_file(null)) != null)
+               {
+                       if (!file_info.get_name().has_suffix(".desktop")) continue;
+                               
+                       string pathname = zavai.config.homedir + "/" + file_info.get_name();
+                       //stdout.printf("Load %s\n", pathname);
+
+                       string icon = null;
+                       var kf = new KeyFile();
+                       try {
+                               kf.load_from_file(pathname, KeyFileFlags.NONE);
+                               if (! kf.has_group(DENTRY_GROUP)) continue;
+                               if (! kf.has_key(DENTRY_GROUP, "Name")) continue;
+                               if (! kf.has_key(DENTRY_GROUP, "Exec")) continue;
+                               if (kf.has_key(DENTRY_GROUP, "Icon"))
+                                       icon = kf.get_string(DENTRY_GROUP, "Icon");
+                       } catch (Error e) {
+                               zavai.log.error("Skipping file " + pathname + ": " + e.message);
+                               continue;
+                       }
+                       var name = kf.get_string(DENTRY_GROUP, "Name");
+                       var exec = kf.get_string(DENTRY_GROUP, "Exec");
+                       var button = new LauncherButton(name, exec);
+                       if (icon != null && icon_theme.has_icon(icon))
+                       {
+                               try {
+                                       var pb = icon_theme.load_icon(icon, zavai.config.min_button_height * 2 / 3, 0);
+                                       button.image = new Gtk.Image.from_pixbuf(pb);
+                               } catch (Error e) {
+                                       zavai.log.error("Skipping icon " + icon + ": " + e.message);
+                               }
+                       }
+                       buttons.add(button);
+               }
+
+               // Create the table with the launcher buttons
+               uint ROWMAX = 5;        // Maximum number of rows
+               uint cols = 1 + (buttons.size + 1) / ROWMAX; // ceil(size/ROWMAX)
+               uint rows = (buttons.size + 1) / cols;   // ceil(size/cols)
+               var table = new Gtk.Table(rows, cols, true);
+
+               // Attach the buttons in the table
+               uint row = 0;
+               uint col = 0;
+               foreach (LauncherButton b in buttons)
+               {
+                       table.attach(b, col, col+1, row, row+1,
+                               Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL,
+                               Gtk.AttachOptions.EXPAND | Gtk.AttachOptions.FILL,
+                               0, 0);
+                       ++col;
+                       if (col == cols)
+                       {
+                               col = 0;
+                               ++row;
+                       }
+               }
+
+               pack_start(table, true, true, 0);
 
-    private void on_fix_status_changed(dynamic DBus.Object pos, int fix_status)
-    {
-        this.fix_status = fix_status;
-        update_icon();
+/*
+    try {
+    } catch (Error e) {
+        stderr.printf ("Error: %s\n", e.message);
+        return 1;
     }
 
-       private void on_toggled(Gtk.Button src)
-       {
-               Service s = zavai.registry.gets(service_name);
-               if (get_active())
-                       s.request("servicerequestlink");
-               else
-                       s.release("servicerequestlink");
-               set_label(get_active() ? label_stop : label_start);
-        update_icon();
+               for (string file in confdir)
+               {
+               }
+*/
        }
-
-    private void on_status_activate()
-    {
-        set_active(!get_active());
-    }
 }
-*/
 
 RaiseIcon raise_icon;
+CloseOrBack close_or_back;
+WindowList window_list;
+Launcher launcher;
 
 public void init()
 {
        raise_icon = new RaiseIcon();
        raise_icon.set_visible(true);
 
-       var menu_test = new Test();
-       zavai.registry.getmenu("menu.main").add_widget(menu_test);
+       close_or_back = new CloseOrBack();
+       close_or_back.set_visible(true);
+
+       window_list = new WindowList("Current apps");
+       zavai.registry.register_applet("wm.list", window_list);
+       zavai.registry.getmenu("menu.main").add_applet("wm.list");
+
+       try {
+               launcher = new Launcher("Run program");
+       } catch (Error e) {
+               zavai.log.error("Not running launcher: " + e.message);
+               launcher = null;
+       }
+
+       if (launcher != null)
+       {
+               zavai.registry.register_applet("wm.launcher", launcher);
+               zavai.registry.getmenu("menu.main").add_applet("wm.launcher");
+       }
 }
 
 }