]> ToastFreeware Gitweb - gregoa/zavai.git/blob - src/app_gps.vala
Simplified by referencing to resources directly instead of through labels
[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 label_start;
99     protected string label_stop;
100     protected zavai.StatusIcon status_icon;
101     protected int fix_status = 0;
102
103     public GPSRequestLink()
104     {
105         label_start = "Keep GPS on";
106         label_stop = "Stop keeping GPS on";
107         set_size_request(0, zavai.config.min_button_height);
108         toggled += on_toggled;
109
110         set_label(get_active() ? label_stop : label_start);
111
112         //tooltip = "GPS status";
113         try {
114             fix_status = zavai.gps.gps.device.GetFixStatus();
115         } catch (Error e) {
116             fix_status = 0;
117         }
118         zavai.gps.gps.device.FixStatusChanged += on_fix_status_changed;
119
120         // GPS status icon
121         status_icon = new zavai.StatusIcon();
122         status_icon.install();
123         status_icon.clicked += on_status_clicked;
124         update_icon();
125     }
126
127     protected void update_icon()
128     {
129         string name;
130         if (fix_status == 2 || fix_status == 3)
131             name = zavai.config.icondir + "/" + (get_active() ? "gps_fix_on.png" : "gps_fix_off.png");
132         else
133             name = zavai.config.icondir + "/" + (get_active() ? "gps_nofix_on.png" : "gps_nofix_off.png");
134         stderr.printf("load icon from %s\n", name);
135         status_icon.set_from_file(name);
136     }
137
138     private void on_fix_status_changed(dynamic DBus.Object pos, int fix_status)
139     {
140         this.fix_status = fix_status;
141         update_icon();
142     }
143
144     private void on_toggled(Gtk.Button src)
145     {
146         if (get_active())
147             zavai.gps.gps.request("servicerequestlink");
148         else
149             zavai.gps.gps.release("servicerequestlink");
150         set_label(get_active() ? label_stop : label_start);
151         update_icon();
152     }
153
154     private void on_status_clicked(Gtk.Button b)
155     {
156         set_active(!get_active());
157     }
158 }
159
160 public class GPSStuck : Gtk.Button
161 {
162     protected bool happened;
163
164     public GPSStuck()
165     {
166         label = "The GPS is stuck";
167         happened = false;
168         clicked += on_clicked;
169         set_size_request(0, zavai.config.min_button_height);
170     }
171
172     public void on_clicked()
173     {
174         zavai.gps.gps.power_cycle(happened);
175         happened = !happened;
176         if (happened)
177             label = "The GPS is stuck AGAIN";
178         else
179             label = "The GPS is stuck";
180     }
181 }
182
183 public void init()
184 {
185     /*
186     registry.register(GPXAudioTracer(registry))
187     */
188
189     // Apps
190     var menu_waypoint = new Waypoint();
191     var menu_gpsrequest = new GPSRequestLink();
192
193     // Menus
194     zavai.menu_gps.add_service_toggle(zavai.gps.gpx, "Start GPX trace", "Stop GPX trace");
195     //zavai.menu_gps.add_applet("ui.gps.monitor");
196     zavai.menu_gps.add_widget(menu_waypoint);
197     zavai.menu_gps.add_widget(menu_gpsrequest);
198     zavai.menu_gps.add_widget(new GPSStuck());
199 }
200
201 }
202 }
203 }