GPS status icon synchronised with menu entry
[gregoa/zavai.git] / vala / 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 namespace zavai {
22 namespace ui {
23 namespace gps {
24
25 /*
26 class GPSOn(gtk.ToggleAction):
27     states = [_("GPS always on"), _("GPS on when needed")]
28
29     def __init__(self, registry, **kw):
30         self.state = 0
31         super(GPSOn, self).__init__("menu.main.gps.alwayson", self.states[self.state], None, None)
32
33         self.registry = registry
34         self.set_active(False)
35
36         self.connect("toggled", self.on_toggle)
37
38     def on_toggle(self, *args):
39         self.state = (self.state + 1) % len(self.states)
40         self.set_property("label", self.states[self.state])
41         if self.get_active():
42             self.start()
43         else:
44             self.stop()
45
46     def start(self):
47         self.registry.resource("gps").connect("gps", self)
48
49     def stop(self):
50         self.registry.resource("gps").disconnect("gps", self)
51
52 class GPXTracer(gtk.ToggleAction):
53     states = [_("Start GPX trace"), _("Stop GPX trace")]
54
55     def __init__(self, registry, **kw):
56         self.state = 0
57         super(GPXTracer, self).__init__("menu.main.gps.gpx", self.states[self.state], None, None)
58
59         self.registry = registry
60         self.set_active(False)
61
62         self.connect("toggled", self.on_toggle)
63
64     def on_toggle(self, *args):
65         self.state = (self.state + 1) % len(self.states)
66         self.set_property("label", self.states[self.state])
67         if self.get_active():
68             self.start()
69         else:
70             self.stop()
71
72     def start(self):
73         zavai.info("GPX trace started")
74         self.registry.resource("gpx").connect("gpx", self)
75
76     def stop(self):
77         zavai.info("GPX trace ended")
78         self.registry.resource("gpx").disconnect("gpx", self)
79 */
80
81 public class Waypoint : BigButton
82 {
83     public Waypoint()
84     {
85         set_label("Take waypoint");
86         zavai.gps.gpx.tracking_changed += on_gpx_tracking_changed;
87         clicked += on_clicked;
88         set_sensitive(zavai.gps.gpx.tracking);
89     }
90
91     protected void on_gpx_tracking_changed(zavai.gps.GPX gpx, bool new_state)
92     {
93 stderr.printf("Toggled %d\n", (int)new_state);
94         set_sensitive(new_state);
95     }
96
97     protected void on_clicked()
98     {
99 stderr.printf("Activate\n");
100         zavai.gps.gpx.waypoint();
101     }
102 }
103
104 /*
105 class GPXAudioTracer(gtk.ToggleAction):
106     states = [_("Start GPX and audio trace"), _("Stop GPX and audio trace")]
107
108     def __init__(self, registry, **kw):
109         self.state = 0
110         super(GPXAudioTracer, self).__init__("menu.main.gps.gpxaudio", self.states[self.state], None, None)
111
112         self.registry = registry
113         self.recorder = zavai.Recorder(registry)
114         self.set_active(False)
115
116         self.connect("toggled", self.on_toggle)
117
118     def shutdown(self):
119         self.recorder.stop()
120         super(GPXAudioTracer, self).shutdown()
121
122     def on_toggle(self, *args):
123         self.state = (self.state + 1) % len(self.states)
124         self.set_property("label", self.states[self.state])
125         if self.get_active():
126             self.start()
127         else:
128             self.stop()
129
130     def start(self):
131         zavai.info("GPX trace started")
132         gpx = self.registry.resource("gpx")
133         gpx.connect("gpx", self)
134         gpx.add_activity_monitor(self.on_gpx_activity_changed)
135
136     def stop(self):
137         zavai.info("GPX trace ended")
138         gpx = self.registry.resource("gpx")
139         gpx.disconnect("gpx", self)
140         self.recorder.stop()
141         gpx.del_activity_monitor(self.on_gpx_activity_changed)
142
143     def on_gpx_activity_changed(self, gpx, state):
144         if state:
145             self.recorder.start(gpx.basename + ".wav")
146         else:
147             self.recorder.stop()
148 */
149
150 public class GPSRequestLink : Gtk.ToggleButton
151 {
152         protected string service_name;
153         protected string label_start;
154         protected string label_stop;
155     protected Gtk.StatusIcon status_icon;
156     protected int fix_status = 0;
157
158         public GPSRequestLink()
159         {
160         service_name = "gps";
161         label_start = "Keep GPS on";
162         label_stop = "Stop keeping GPS on";
163                 set_size_request(0, zavai.config.min_button_height);
164                 toggled += on_toggled;
165
166                 set_label(get_active() ? label_stop : label_start);
167
168         //tooltip = "GPS status";
169         try {
170             fix_status = zavai.gps.gps.device.GetFixStatus();
171         } catch (Error e) {
172             fix_status = 0;
173         }
174         zavai.gps.gps.device.FixStatusChanged += on_fix_status_changed;
175
176         // GPS status icon
177         status_icon = new Gtk.StatusIcon();
178         status_icon.set_visible(true);
179         status_icon.activate += on_status_activate;
180         update_icon();
181         }
182
183     protected void update_icon()
184     {
185         string name;
186         if (fix_status == 2 || fix_status == 3)
187             name = zavai.config.icondir + "/" + (get_active() ? "gps_fix_on.png" : "gps_fix_off.png");
188         else
189             name = zavai.config.icondir + "/" + (get_active() ? "gps_nofix_on.png" : "gps_nofix_off.png");
190 stderr.printf("load icon from %s\n", name);
191         status_icon.set_from_file(name);
192     }
193
194     private void on_fix_status_changed(dynamic DBus.Object pos, int fix_status)
195     {
196         this.fix_status = fix_status;
197         update_icon();
198     }
199
200         private void on_toggled(Gtk.Button src)
201         {
202                 Service s = zavai.registry.gets(service_name);
203                 if (get_active())
204                         s.request("servicerequestlink");
205                 else
206                         s.release("servicerequestlink");
207                 set_label(get_active() ? label_stop : label_start);
208         update_icon();
209         }
210
211     private void on_status_activate()
212     {
213         set_active(!get_active());
214     }
215 }
216
217 public void init()
218 {
219     /*
220     registry.register(GPXAudioTracer(registry))
221     registry.register(GPXWaypoint(registry))
222
223     // Apps
224     var useless = new Useless();
225     zavai.registry.register_applet("app.debug.useless", useless);
226     */
227     var menu_waypoint = new Waypoint();
228     var menu_gpsrequest = new GPSRequestLink();
229                 //label_on = "Stop GPX trace";
230                 //label_off = "Start GPX trace";
231                 //label_on = "Stop GPS monitor";
232                 //label_off = "Start GPS monitor";
233                 //label_on = "Stop GPS position tracking";
234                 //label_off = "Start GPS position tracking";
235                 //label_on = "Stop keeping GPS on";
236                 //label_off = "Keep GPS on";
237
238     // Menus
239     var menu_gps = new zavai.Menu("GPS");
240     //menu_gps.add_applet("app.debug.useless");
241     menu_gps.add_service_toggle("gps.gpx", "Start GPX trace", "Stop GPX trace");
242     menu_gps.add_widget(menu_waypoint);
243     menu_gps.add_widget(menu_gpsrequest);
244
245     zavai.registry.register_menu("menu.gps", menu_gps);
246     zavai.registry.getmenu("menu.main").add_applet("menu.gps");
247 }
248
249 }
250 }
251 }