When called with --popup and is running, close the popup
authorEnrico Zini <enrico@enricozini.org>
Mon, 2 Nov 2009 20:40:13 +0000 (21:40 +0100)
committerEnrico Zini <enrico@enricozini.org>
Mon, 2 Nov 2009 20:40:13 +0000 (21:40 +0100)
src/zavai-calendar.vala

index afc90e1d7f6e5e9758dbc7f5b4145847dfd6f331..5d34139fd11f470b3c43c4389402a284787de40f 100644 (file)
@@ -22,6 +22,35 @@ using GLib;
 
 //string VERSION = "0.1";
 
+static Posix.pid_t is_running()
+{
+       string pidfile = zavai.config.homedir + "/calendar.pid";
+       string contents;
+       try {
+               if (!FileUtils.get_contents(pidfile, out contents))
+                       return 0;
+       } catch (FileError e) {
+               return 0;
+       }
+       int val = contents.to_int();
+       string procdir = "/proc/%d".printf(val);
+       if (FileUtils.test(procdir, FileTest.IS_DIR))
+               return (Posix.pid_t)val;
+       else
+               return 0;
+}
+
+static void make_pidfile()
+{
+       string pidfile = zavai.config.homedir + "/calendar.pid";
+       FileUtils.set_contents(pidfile, "%d".printf(Posix.getpid()));
+}
+
+static void on_kill(int sig)
+{
+       Gtk.main_quit();
+}
+
 static int main (string[] args) {
        bool opt_popup = false;
        GLib.OptionEntry[] entries = new GLib.OptionEntry[] {
@@ -62,6 +91,17 @@ static int main (string[] args) {
        Gtk.Window win = new Gtk.Window(Gtk.WindowType.TOPLEVEL);
        if (opt_popup)
        {
+               Posix.pid_t pid = is_running();
+               if (pid != 0)
+               {
+                       // Kill a running calendar
+                       Posix.kill(pid, Posix.SIGINT);
+                       FileUtils.unlink(zavai.config.homedir + "/calendar.pid");
+                       return 0;
+               }
+
+               make_pidfile();
+
                win.set_decorated(false);
                //win.set_resizable(false);
                win.set_border_width(5);
@@ -79,16 +119,13 @@ static int main (string[] args) {
        win.show_all();
        win.show();
 
-       // # Shutdown the main loop on SIGINT
-       // def on_kill(signum, frame):
-       //     gtk.main_quit()
-       // signal.signal(signal.SIGINT, on_kill)
-       // signal.signal(signal.SIGTERM, on_kill)
+       // Shutdown the main loop on SIGINT
+       Posix.signal(Posix.SIGINT, on_kill);
+       Posix.signal(Posix.SIGTERM, on_kill);
 
        Gtk.main();
 
-       // zavai.info("Shutting down")
-       //zavai.registry.shutdown();
+       calendar.flush();
 
        return 0;
 }