Drafted code to schedule and inspect at jobs
authorEnrico Zini <enrico@enricozini.org>
Fri, 27 Nov 2009 15:56:32 +0000 (16:56 +0100)
committerEnrico Zini <enrico@enricozini.org>
Fri, 27 Nov 2009 15:56:32 +0000 (16:56 +0100)
src/clock.vala
src/zavai.vala

index bcd25e7fb2b9f94495cd20d787893aace7671bd6..834290d75d0e72d70d104c40cd0d51b2accf5a8f 100644 (file)
@@ -71,14 +71,69 @@ at alarm triggered through zavai: refresh_alarms()
 
 public class Alarm : Object
 {
+       // TODO old stuff
        // Notify of an alarm being triggered
-       //public static signal void trigger(string label);
+       public signal void trigger(Alarm a);
+
+       public time_t deadline;
+       public string label;
+
+       public Alarm(time_t deadline, string label)
+       {
+               this.deadline = deadline;
+               this.label = label;
+       }
+
+
+       // TODO new stuff
 
        // Schedule with at
-       public static void schedule(time_t deadline, string label);
+       public static void schedule(string timespec, string label) throws Error
+       {
+               string argv[4];
+               argv[0] = "/usr/bin/at";
+               argv[1] = timespec;
+               argv[2] = null;
+
+               Pid pid;
+               int stdinfd;
+
+               if (!Process.spawn_async_with_pipes("/", argv, null, SpawnFlags.STDERR_TO_DEV_NULL, null, out pid, out stdinfd, null, null))
+                       return;
+
+               {
+                       FileStream fs = FileStream.fdopen(stdinfd, "w");
+                       fs.printf("# Zavai variables start here\n");
+                       fs.printf("ZAVAI_LABEL=\"%s\"\n", label.escape(""));
+                       fs.printf("# Zavai commands starts here\n");
+                       fs.printf("echo \"$ZAVAI_LABEL\" | zavai --notify");
+               }
+               
+               Process.close_pid(pid);
+       }
 
        // Get the label of the job with the given at ID
-       public static string getLabel(int atID);
+       public static string? getLabel(int atID)
+       {
+               string label = null;
+               at.jobContents(atID, fd => {
+                       FileStream fs = FileStream.fdopen(fd, "r");
+                       while (true)
+                       {
+                               string? line = fs.read_line();
+                               if (line == null) break;
+                               if (line.has_prefix("ZAVAI_LABEL=\""))
+                               {
+                                       size_t size = line.size();
+                                       if (size < 15) continue;
+                                       label = line.substring(13, (long)(size - 15));
+                                       break;
+                               }
+                       }
+                       return true;
+               });
+               return label;
+       }
 }
 
 private int alarm_compare(void* a, void* b)
index e75fcf962b86bcc311a2eedeb2de36f7db0359c7..749ebefe319c26273fbde6a093fcc6d19a8aed40 100644 (file)
@@ -50,6 +50,7 @@ static int main (string[] args) {
                string? line;
                while ((line = fs.read_line()) != null)
                        stdout.printf(" LN %s\n", line);
+               return true;
        });
 
        return 0;