]> ToastFreeware Gitweb - gregoa/zavai.git/blob - src/clock.vala
Implemented a sorted alarm list
[gregoa/zavai.git] / src / clock.vala
1 /*
2  * clock - clock resource for zavai
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 using GLib;
22
23 namespace zavai {
24 namespace clock {
25
26 public enum SourceType
27 {
28     SYSTEM,
29     GPS
30 }
31
32 public class Alarm : Object
33 {
34     public signal void trigger();
35
36     public time_t deadline;
37     public string label;
38
39     public Alarm(time_t deadline, string label)
40     {
41         this.deadline = deadline;
42         this.label = label;
43     }
44 }
45
46 private int alarm_compare(void* a, void* b)
47 {
48     return (int)(((Alarm*)a)->deadline - ((Alarm*)b)->deadline);
49 }
50
51 public class Clock: zavai.Service
52 {
53     protected time_t last_gps_time;
54     protected time_t last_gps_time_system_time;
55     protected time_t last_system_time;
56         protected dynamic DBus.Object gps_time;
57     protected uint system_time_timeout;
58     protected time_t last_minute;
59     protected time_t chosen_time;
60     protected SourceType chosen_type;
61
62     protected SList<Alarm> alarms;
63
64     // Ticks once a minute
65     public signal void minute_changed(long time, SourceType source);
66
67         public Clock()
68         {
69                 name = "clock";
70         alarms = null;
71         last_minute = 0;
72         last_gps_time = 0;
73         last_gps_time_system_time = 0;
74         last_system_time = time_t();
75         chosen_time = last_system_time;
76         
77                 gps_time = zavai.registry.sbus.get_object(
78                         "org.freesmartphone.ogpsd",
79                         "/org/freedesktop/Gypsy",
80                         "org.freedesktop.Gypsy.Time");
81         }
82
83     public void schedule(Alarm a)
84     {
85         alarms.insert_sorted(a, alarm_compare);
86         zavai.log.info("Next alarm: " + alarms.data.label + " at " + Time.local(alarms.data.deadline).to_string());
87     }
88
89     public void check_alarms()
90     {
91         last_system_time = time_t();
92         update_time();
93         while (alarms != null && alarms.data.deadline <= chosen_time)
94         {
95             Alarm a = alarms.data;
96             alarms.remove(a);
97             zavai.log.info("Triggering " + a.label);
98             a.trigger();
99         }
100     }
101
102         private void on_gps_time(dynamic DBus.Object pos, int t)
103         {
104         if (t == 0)
105         {
106             last_gps_time_system_time = 0;
107             update_time();
108         } else {
109             last_gps_time = (time_t)t;
110             last_gps_time_system_time = time_t();
111             update_time();
112         }
113         }
114
115     private bool on_system_time()
116     {
117         last_system_time = time_t();
118         update_time();
119         return true;
120     }
121
122     private void update_time()
123     {
124         if (last_gps_time_system_time + 10 > last_system_time)
125         {
126             chosen_time = last_gps_time;
127             chosen_type = SourceType.GPS;
128         }
129         else
130         {
131             chosen_time = last_system_time;
132             chosen_type = SourceType.SYSTEM;
133         }
134         if (chosen_time / 60 != last_minute)
135         {
136             last_minute = chosen_time / 60;
137             minute_changed(chosen_time, chosen_type);
138         }
139     }
140
141         /// Request GPS resource
142         public override void start()
143         {
144                 if (started) return;
145
146         system_time_timeout = Timeout.add(5000, on_system_time);
147                 gps_time.TimeChanged += on_gps_time;
148         last_system_time = time_t();
149         update_time();
150
151                 base.start();
152         }
153
154         public override void stop()
155         {
156                 if (!started) return;
157
158         Source.remove(system_time_timeout);
159                 gps_time.TimeChanged -= on_gps_time;
160
161                 base.stop();
162         }
163 }
164
165 public Clock clock = null;
166
167 public void init()
168 {
169     clock = new Clock();
170
171         zavai.registry.register_service(clock);
172 }
173
174 }
175 }