Implemented scheduling a new alarm
[gregoa/zavai.git] / src / app_gps.vala
1 /*
2  * gpx_trace - zavai GPX trace functions
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 ui {
25 namespace gps {
26
27 public class Waypoint : BigButton
28 {
29         public Waypoint()
30         {
31                 set_label("Take waypoint");
32                 zavai.gps.gpx.tracking_changed += on_gpx_tracking_changed;
33                 clicked += on_clicked;
34                 set_sensitive(zavai.gps.gpx.tracking);
35         }
36
37         protected void on_gpx_tracking_changed(zavai.gps.GPX gpx, bool new_state)
38         {
39                 stderr.printf("Toggled %d\n", (int)new_state);
40                 set_sensitive(new_state);
41         }
42
43         protected void on_clicked()
44         {
45                 stderr.printf("Activate\n");
46                 zavai.gps.gpx.waypoint();
47         }
48 }
49
50 /*
51 class GPXAudioTracer(gtk.ToggleAction):
52     states = [_("Start GPX and audio trace"), _("Stop GPX and audio trace")]
53
54     def __init__(self, registry, **kw):
55         self.state = 0
56         super(GPXAudioTracer, self).__init__("menu.main.gps.gpxaudio", self.states[self.state], None, None)
57
58         self.registry = registry
59         self.recorder = zavai.Recorder(registry)
60         self.set_active(False)
61
62         self.connect("toggled", self.on_toggle)
63
64     def shutdown(self):
65         self.recorder.stop()
66         super(GPXAudioTracer, self).shutdown()
67
68     def on_toggle(self, *args):
69         self.state = (self.state + 1) % len(self.states)
70         self.set_property("label", self.states[self.state])
71         if self.get_active():
72             self.start()
73         else:
74             self.stop()
75
76     def start(self):
77         zavai.info("GPX trace started")
78         gpx = self.registry.resource("gpx")
79         gpx.connect("gpx", self)
80         gpx.add_activity_monitor(self.on_gpx_activity_changed)
81
82     def stop(self):
83         zavai.info("GPX trace ended")
84         gpx = self.registry.resource("gpx")
85         gpx.disconnect("gpx", self)
86         self.recorder.stop()
87         gpx.del_activity_monitor(self.on_gpx_activity_changed)
88
89     def on_gpx_activity_changed(self, gpx, state):
90         if state:
91             self.recorder.start(gpx.basename + ".wav")
92         else:
93             self.recorder.stop()
94 */
95
96 public class GPSRequestLink : Gtk.ToggleButton
97 {
98         protected string service_name;
99         protected string label_start;
100         protected string label_stop;
101         protected zavai.StatusIcon status_icon;
102         protected int fix_status = 0;
103
104         public GPSRequestLink()
105         {
106                 service_name = "gps";
107                 label_start = "Keep GPS on";
108                 label_stop = "Stop keeping GPS on";
109                 set_size_request(0, zavai.config.min_button_height);
110                 toggled += on_toggled;
111
112                 set_label(get_active() ? label_stop : label_start);
113
114                 //tooltip = "GPS status";
115                 try {
116                         fix_status = zavai.gps.gps.device.GetFixStatus();
117                 } catch (Error e) {
118                         fix_status = 0;
119                 }
120                 zavai.gps.gps.device.FixStatusChanged += on_fix_status_changed;
121
122                 // GPS status icon
123                 status_icon = new zavai.StatusIcon();
124                 status_icon.install();
125                 status_icon.clicked += on_status_clicked;
126                 update_icon();
127         }
128
129         protected void update_icon()
130         {
131                 string name;
132                 if (fix_status == 2 || fix_status == 3)
133                         name = zavai.config.icondir + "/" + (get_active() ? "gps_fix_on.png" : "gps_fix_off.png");
134                 else
135                         name = zavai.config.icondir + "/" + (get_active() ? "gps_nofix_on.png" : "gps_nofix_off.png");
136                 stderr.printf("load icon from %s\n", name);
137                 status_icon.set_from_file(name);
138         }
139
140         private void on_fix_status_changed(dynamic DBus.Object pos, int fix_status)
141         {
142                 this.fix_status = fix_status;
143                 update_icon();
144         }
145
146         private void on_toggled(Gtk.Button src)
147         {
148                 Service s = zavai.registry.gets(service_name);
149                 if (get_active())
150                         s.request("servicerequestlink");
151                 else
152                         s.release("servicerequestlink");
153                 set_label(get_active() ? label_stop : label_start);
154                 update_icon();
155         }
156
157         private void on_status_clicked(Gtk.Button b)
158         {
159                 set_active(!get_active());
160         }
161 }
162
163 public void init()
164 {
165     /*
166     registry.register(GPXAudioTracer(registry))
167     */
168
169     // Apps
170     var menu_waypoint = new Waypoint();
171     var menu_gpsrequest = new GPSRequestLink();
172
173     // Menus
174     var menu_gps = zavai.registry.getmenu("menu.gps");
175     menu_gps.add_service_toggle("gps.gpx", "Start GPX trace", "Stop GPX trace");
176     menu_gps.add_applet("ui.gps.monitor");
177     menu_gps.add_widget(menu_waypoint);
178     menu_gps.add_widget(menu_gpsrequest);
179 }
180
181 }
182 }
183 }