]> ToastFreeware Gitweb - gregoa/zavai.git/blobdiff - src/app_main.vala
Added log viewer
[gregoa/zavai.git] / src / app_main.vala
index 7a116d2fb471999d008ef1112fa3d6bf5b5c18aa..0faa7274ba4bb6c9080dc64caea93693db686940 100644 (file)
@@ -24,110 +24,458 @@ namespace zavai {
 namespace ui {
 namespace main {
 
+/*
 public class StatusBar : Gtk.HBox
 {
     void StatusBar()
     {
     }
 }
+*/
 
 public class Clock : Gtk.VBox
 {
     protected Gtk.Label l_date;
+    protected Gtk.Button l_date_button;
     protected Gtk.Label l_time;
+    protected Gtk.Label l_deadline;
+    protected time_t last_time;
+    protected zavai.clock.SourceType last_time_type;
+    protected time_t last_deadline;
+    protected string last_deadline_label;
+    protected Log log;
 
-    protected time_t last_gps_time;
-    protected time_t last_gps_time_system_time;
-    protected time_t last_system_time;
-       protected dynamic DBus.Object gps_time;
-    protected uint system_time_timeout;
-    protected time_t last_minute;
-
-    public Clock()
+    construct
     {
+        last_time = time_t();
+        last_time_type = zavai.clock.SourceType.SYSTEM;
+        last_deadline = 0;
+        last_deadline_label = "";
+
         l_date = new Gtk.Label("--");
         l_date.modify_font(Pango.FontDescription.from_string("Sans 40"));
-        pack_start(l_date, false, false, 0);
+        l_date_button = new Gtk.Button();
+        l_date_button.set_image(l_date);
+        l_date_button.relief = Gtk.ReliefStyle.NONE;
+        l_date_button.clicked += on_date_clicked;
+        pack_start(l_date_button, false, false, 0);
         l_time = new Gtk.Label("--:--");
         l_time.modify_font(Pango.FontDescription.from_string("Sans 60"));
         pack_start(l_time, false, false, 0);
+        l_deadline = new Gtk.Label("");
+        l_deadline.modify_font(Pango.FontDescription.from_string("Sans 30"));
+        l_deadline.set_justify(Gtk.Justification.CENTER);
+        pack_start(l_deadline, false, false, 0);
+
+        log = new Log();
+        pack_start(log, false, false, 0);
 
-        last_minute = 0;
-        last_gps_time = 0;
-        last_gps_time_system_time = 0;
-        last_system_time = time_t();
-        
-               gps_time = zavai.registry.sbus.get_object(
-                       "org.freesmartphone.ogpsd",
-                       "/org/freedesktop/Gypsy",
-                       "org.freedesktop.Gypsy.Time");
+        zavai.clock.clock.minute_changed += on_minute_changed;
+        zavai.clock.clock.schedule_changed += on_schedule_changed;
+        on_schedule_changed(zavai.clock.clock.next_alarm());
 
-        system_time_timeout = Timeout.add(5000, on_system_time);
-               gps_time.TimeChanged += on_gps_time;
-        update_time();
+        zavai.clock.clock.request("ui.main.clock");
     }
 
-    public void update_time()
+    private void on_date_clicked(Gtk.Button b)
     {
-        time_t new_last_minute;
-        string type;
-        if (last_gps_time_system_time + 10 > last_system_time)
-        {
-            new_last_minute = last_gps_time / 60;
-            type = "gps";
-        }
-        else
+        zavai.app.push_applet(zavai.ui.calendar.calendar);
+    }
+
+    private void on_schedule_changed(zavai.clock.Alarm? next)
+    {
+        if (next == null)
         {
-            new_last_minute = last_system_time / 60;
-            type = "sys";
+            last_deadline = 0;
+            last_deadline_label = "";
+        } else {
+            last_deadline = next.ev.deadline;
+            last_deadline_label = next.label;
         }
-        if (new_last_minute != last_minute)
+        on_minute_changed((long)last_time, last_time_type);
+    }
+
+    private void on_minute_changed(long ts, zavai.clock.SourceType type)
+    {
+        last_time = (time_t)ts;
+        last_time_type = type;
+
+        string typetag = "unknown";
+        switch (type)
         {
-            last_minute = new_last_minute;
-            var t = Time.local(last_minute * 60);
-            l_date.set_text(t.format("%a %d %b"));
-            l_time.set_text("%2d:%02d (%s)".printf(t.hour, t.minute, type));
+            case zavai.clock.SourceType.GPS:
+                typetag = "gps";
+                break;
+            case zavai.clock.SourceType.SYSTEM:
+                typetag = "sys";
+                break;
         }
-    }
 
-       private void on_gps_time(dynamic DBus.Object pos, int t)
-       {
-stderr.printf("GPS TIME %d\n", t);
-               last_gps_time = (time_t)t;
-        last_gps_time_system_time = time_t();
-        update_time();
-       }
+        var t = Time.local((time_t)ts);
+        l_date.set_text(t.format("%a %d %b"));
+        l_time.set_text("%2d:%02d (%s)".printf(t.hour, t.minute, typetag));
 
-    private bool on_system_time()
-    {
-        last_system_time = time_t();
-        update_time();
-        return true;
+        if (last_deadline == 0)
+            l_deadline.set_text("");
+        else
+        {
+            int remaining = (int)(last_deadline - last_time);
+            int hours = remaining / 3600;
+            int minutes = (remaining % 3600) / 60;
+            if (hours == 0 && minutes == 0)
+                l_deadline.set_text(last_deadline_label + "\nanytime now");
+            else
+                l_deadline.set_text("%s\nin %02dh %02dm".printf(last_deadline_label, hours, minutes));
+        }
     }
 }
 
 public class Status : Applet
 {
+    public Gtk.HBox status_icons;
     public Clock clock;
+    public Gtk.Label gsm_status;
+    public Gtk.Label gsm_info;
     public AppletPushLink menu;
 
-       public Status(string label)
-       {
-               _label = label;
+    public Status(string label)
+    {
+        _label = label;
+        status_icons = new Gtk.HBox(false, 0);
         clock = new Clock();
-        menu = new AppletPushLink("menu.main");
+        gsm_status = new Gtk.Label("");
+        gsm_info = new Gtk.Label("");
+        menu = new AppletPushLink(zavai.menu_main);
+
+        pack_start(status_icons, false, false, 0);
+        pack_start(clock, false, false, 0);
+        pack_start(gsm_status, false, false, 0);
+        pack_start(gsm_info, false, false, 0);
+        // pack_start(music.player, false, false, 0);
+        pack_end(menu, false, false, 0);
 
-               pack_start(clock, false, false, 0);
-               pack_end(menu, false, false, 0);
-       }
+        zavai.gsm.gsm.status_changed += (msg) => { gsm_status.set_text(msg); };
+        zavai.gsm.gsm.info_changed += () => {
+stderr.printf("NEW INFO %s %s %d\n", zavai.gsm.gsm.info_provider, zavai.gsm.gsm.info_registration, zavai.gsm.gsm.info_signal_strength);
+            string text = "%s (%s)".printf(zavai.gsm.gsm.info_provider, zavai.gsm.gsm.info_registration);
+            if (zavai.gsm.gsm.info_signal_strength != -1)
+                text = "%s %d%%".printf(text, zavai.gsm.gsm.info_signal_strength);
+            gsm_info.set_text(text);
+        };
+    }
 }
 
-Status status;
+public class IncDec : Gtk.HBox
+{
+    protected Gtk.Button b_decmore;
+    protected Gtk.Button b_dec;
+    protected Gtk.Button b_inc;
+    protected Gtk.Button b_incmore;
+    static const int BH = 50;
+    static const int BW = 50;
+
+    public signal void tweaked(IncDec id, int amount);
+
+    public IncDec()
+    {
+        homogeneous = true;
+        b_decmore = new Gtk.Button.with_label("«");
+        b_decmore.set_size_request(BW, BH);
+        b_dec = new Gtk.Button.with_label("<");
+        b_dec.set_size_request(BW, BH);
+        b_inc = new Gtk.Button.with_label(">");
+        b_inc.set_size_request(BW, BH);
+        b_incmore = new Gtk.Button.with_label("»");
+        b_incmore.set_size_request(BW, BH);
+        pack_start(b_decmore, false, true, 0);
+        pack_start(b_dec, false, true, 0);
+        pack_start(b_inc, false, true, 0);
+        pack_start(b_incmore, false, true, 0);
+        b_decmore.clicked += on_clicked;
+        b_dec.clicked += on_clicked;
+        b_inc.clicked += on_clicked;
+        b_incmore.clicked += on_clicked;
+    }
+
+    protected void on_clicked(Gtk.Button b)
+    {
+        if (b == b_decmore)
+            tweaked(this, -10);
+        else if (b == b_dec)
+            tweaked(this, -1);
+        else if (b == b_inc)
+            tweaked(this, 1);
+        else if (b == b_incmore)
+            tweaked(this, 10);
+    }
+}
+
+public abstract class AddDeadline : Applet
+{
+    protected Gtk.ComboBoxEntry dl_label;
+    protected Gtk.Button dl_submit;
+
+    public AddDeadline()
+    {
+        dl_label = new Gtk.ComboBoxEntry.text();
+        dl_label.append_text("Pasta");
+        dl_label.append_text("Talk");
+        dl_label.append_text("Leave");
+        dl_label.changed += on_label_changed;
+
+        var hbox = new Gtk.HBox(false, 0);
+        hbox.pack_start(new Gtk.Label("Label: "), false, false, 0);
+        hbox.pack_start(dl_label, true, true, 0);
+        pack_start(hbox, false, false, 0);
+
+        dl_submit = new Gtk.Button.with_label("Activate");
+        dl_submit.set_sensitive(false);
+        dl_submit.clicked += on_submit;
+        button_box.pack_start(dl_submit, true, true, 0);
+    }
+
+    public override void start()
+    {
+        ((Gtk.Entry)dl_label.get_child()).set_text("");
+        update();
+    }
+
+    protected virtual void update()
+    {
+        dl_submit.set_sensitive(((Gtk.Entry)dl_label.get_child()).get_text() == "" ? false : true);
+    }
+
+    protected abstract time_t get_deadline();
+
+    protected void on_label_changed(Gtk.ComboBox e)
+    {
+        update();
+    }
+
+    protected void on_submit(Gtk.Button b)
+    {
+        string label = ((Gtk.Entry)dl_label.get_child()).get_text();
+        time_t deadlinets = get_deadline();
+        Time deadline = Time.local(deadlinets);
+        string timespec = deadline.format("%H:%M %m/%d/%Y");
+        zavai.log.info("Scheduling deadline " + label + " at " + timespec);
+        zavai.clock.clock.schedule(timespec, label);
+        back();
+    }
+}
+
+public class AddAbsoluteDeadline : AddDeadline
+{
+    protected Gtk.Label dl_day;
+    protected IncDec dl_day_tweak;
+    protected Gtk.Label dl_hour;
+    protected IncDec dl_hour_tweak;
+    protected Gtk.Label dl_min;
+    protected IncDec dl_min_tweak;
+    protected time_t dl_time;
+
+    public AddAbsoluteDeadline()
+    {
+        _label = "Add absolute deadline";
+        dl_time = 0;
+
+        dl_day = new Gtk.Label("");
+        dl_day.set_justify(Gtk.Justification.RIGHT);
+        dl_day_tweak = new IncDec();
+        dl_day_tweak.tweaked += on_tweak;
+        var hbox = new Gtk.HBox(false, 0);
+        hbox.pack_start(new Gtk.Label("Day: "), false, false, 0);
+        hbox.pack_start(dl_day, true, true, 0);
+        hbox.pack_start(dl_day_tweak, false, false, 0);
+        pack_start(hbox, false, false, 0);
+
+        dl_hour = new Gtk.Label("");
+        dl_hour.set_justify(Gtk.Justification.RIGHT);
+        dl_hour_tweak = new IncDec();
+        dl_hour_tweak.tweaked += on_tweak;
+        hbox = new Gtk.HBox(false, 0);
+        hbox.pack_start(new Gtk.Label("Hour: "), false, false, 0);
+        hbox.pack_start(dl_hour, true, true, 0);
+        hbox.pack_start(dl_hour_tweak, false, false, 0);
+        pack_start(hbox, false, false, 0);
+
+        dl_min = new Gtk.Label("");
+        dl_min.set_justify(Gtk.Justification.RIGHT);
+        dl_min_tweak = new IncDec();
+        dl_min_tweak.tweaked += on_tweak;
+        hbox = new Gtk.HBox(false, 0);
+        hbox.pack_start(new Gtk.Label("Minute: "), false, false, 0);
+        hbox.pack_start(dl_min, true, true, 0);
+        hbox.pack_start(dl_min_tweak, false, false, 0);
+        pack_start(hbox, false, false, 0);
+    }
+
+    public override void start()
+    {
+        dl_time = time_t();
+        base.start();
+    }
+
+    protected override void update()
+    {
+        Time t = Time.local(dl_time);
+        dl_day.set_text(t.format("%a %d %b"));
+        dl_hour.set_text(t.format("%H"));
+        dl_min.set_text(t.format("%M"));
+        base.update();
+    }
+
+    protected override time_t get_deadline()
+    {
+        return dl_time;
+    }
+
+    protected void on_tweak(IncDec id, int amount)
+    {
+        time_t old = dl_time;
+        if (id == dl_day_tweak)
+            dl_time += amount * 3600 * 24;
+        else if (id == dl_hour_tweak)
+            dl_time += amount * 3600;
+        else if (id == dl_min_tweak)
+            dl_time += amount * 60;
+        time_t now = time_t();
+        if (dl_time < now)
+            dl_time = old;
+        update();
+    }
+}
+
+public class AddRelativeDeadline : AddDeadline
+{
+    protected Gtk.Label dl_hour;
+    protected IncDec dl_hour_tweak;
+    protected Gtk.Label dl_min;
+    protected IncDec dl_min_tweak;
+    protected int dl_time;
+
+    public AddRelativeDeadline()
+    {
+        _label = "Add relative deadline";
+        dl_time = 0;
+
+        dl_hour = new Gtk.Label("");
+        dl_hour.set_justify(Gtk.Justification.RIGHT);
+        dl_hour_tweak = new IncDec();
+        dl_hour_tweak.tweaked += on_tweak;
+        var hbox = new Gtk.HBox(false, 0);
+        hbox.pack_start(new Gtk.Label("Hour: "), false, false, 0);
+        hbox.pack_start(dl_hour, true, true, 0);
+        hbox.pack_start(dl_hour_tweak, false, false, 0);
+        pack_start(hbox, false, false, 0);
+
+        dl_min = new Gtk.Label("");
+        dl_min.set_justify(Gtk.Justification.RIGHT);
+        dl_min_tweak = new IncDec();
+        dl_min_tweak.tweaked += on_tweak;
+        hbox = new Gtk.HBox(false, 0);
+        hbox.pack_start(new Gtk.Label("Minute: "), false, false, 0);
+        hbox.pack_start(dl_min, true, true, 0);
+        hbox.pack_start(dl_min_tweak, false, false, 0);
+        pack_start(hbox, false, false, 0);
+    }
+
+    public override void start()
+    {
+        dl_time = 0;
+        base.start();
+    }
+
+    protected override void update()
+    {
+        dl_hour.set_text("%2d".printf(dl_time/60));
+        dl_min.set_text("%2d".printf(dl_time%60));
+        base.update();
+    }
+
+    protected void on_tweak(IncDec id, int amount)
+    {
+        int old = dl_time;
+        if (id == dl_hour_tweak)
+            dl_time += amount * 60;
+        else if (id == dl_min_tweak)
+            dl_time += amount;
+        if (dl_time < 0)
+            dl_time = old;
+        update();
+    }
+
+    protected override time_t get_deadline()
+    {
+        return time_t() + dl_time * 60;
+    }
+}
+
+public class Log : Gtk.VBox
+{
+    protected Gtk.Label l_info;
+    protected Gtk.Button l_info_button;
+
+    construct
+    {
+        l_info = new Gtk.Label("");
+        // l_info.modify_font(Pango.FontDescription.from_string("Sans 60"));
+        pack_start(l_info, false, false, 0);
+
+        l_info_button = new Gtk.Button();
+        l_info_button.set_image(l_info);
+        l_info_button.relief = Gtk.ReliefStyle.NONE;
+        l_info_button.clicked += (b) => {
+            zavai.app.push_applet(zavai.ui.logview.log);
+        };
+        pack_start(l_info_button, false, false, 0);
+
+        zavai.log.log.entries_changed += refresh;
+
+        refresh();
+    }
+
+    public void refresh()
+    {
+        int count = 0;
+        zavai.log.log.list_entries((d, f) => {
+            ++count;
+            return true;
+        });
+        l_info.set_text("%d log entries".printf(count));
+        l_info_button.set_sensitive(count != 0);
+        //zavai.log.Log l = zavai.log.log.load(args[2]);
+    }
+}
+
+/*
+public class AddDailyDeadline : Applet
+{
+    public AddDailyDeadline()
+    {
+        _label = "Add daily deadline";
+    }
+}
+*/
+
+public Status status;
+public AddAbsoluteDeadline aad;
+public AddRelativeDeadline ard;
+//AddDailyDeadline add;
 
 public void init()
 {
     status = new Status("Zavai");
-    zavai.registry.register_applet("zavai.status", status);
+    aad = new AddAbsoluteDeadline();
+    ard = new AddRelativeDeadline();
+    /*
+    add = new AddDailyDeadline();
+    zavai.registry.register_applet("clock.adddaily", add);
+    */
+
+    zavai.menu_misc.add_applet(aad);
+    zavai.menu_misc.add_applet(ard);
+    //menu_misc.add_applet("clock.adddaily");
 }
 
 }