Find script for gsm
[gregoa/zavai.git] / src / app_gps.vala
1 /*
2  * gpx_trace - zavai GPX trace functions
3  *
4  * Copyright (C) 2009--2010  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.toggled += on_gpx_toggled;
33         clicked += on_clicked;
34         set_sensitive(zavai.gps.gpx.started);
35     }
36
37     protected void on_gpx_toggled(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         set_active(zavai.gps.gps.started);
109         toggled += on_toggled;
110
111         set_label(get_active() ? label_stop : label_start);
112
113         //tooltip = "GPS status";
114         fix_status = zavai.gps.gps.fix_status();
115         zavai.gps.gps.fix_status_changed += on_fix_status_changed;
116
117         // GPS status icon
118         status_icon = new zavai.StatusIcon();
119         status_icon.install();
120         status_icon.clicked += on_status_clicked;
121         update_icon();
122     }
123
124     protected void update_icon()
125     {
126         string name;
127         if (fix_status != libgps.STATUS_NO_FIX)
128             name = zavai.config.icondir + "/" + (get_active() ? "gps_fix_on.png" : "gps_fix_off.png");
129         else
130             name = zavai.config.icondir + "/" + (get_active() ? "gps_nofix_on.png" : "gps_nofix_off.png");
131         stderr.printf("load icon from %s\n", name);
132         status_icon.set_from_file(name);
133     }
134
135     private void on_fix_status_changed(int fix_status)
136     {
137         this.fix_status = fix_status;
138         update_icon();
139     }
140
141     private void on_toggled(Gtk.Button src)
142     {
143         if (get_active())
144             zavai.gps.gps.request("servicerequestlink");
145         else
146             zavai.gps.gps.release("servicerequestlink");
147         set_label(get_active() ? label_stop : label_start);
148         update_icon();
149     }
150
151     private void on_status_clicked(Gtk.Button b)
152     {
153         set_active(!get_active());
154     }
155 }
156
157 public void init()
158 {
159     /*
160     registry.register(GPXAudioTracer(registry))
161     */
162
163     // Apps
164     var menu_waypoint = new Waypoint();
165     var menu_gpsrequest = new GPSRequestLink();
166
167     // Menus
168     zavai.menu_gps.add_service_toggle(zavai.gps.gpx, "Start GPX trace", "Stop GPX trace");
169     //zavai.menu_gps.add_applet("ui.gps.monitor");
170     zavai.menu_gps.add_widget(menu_waypoint);
171     zavai.menu_gps.add_widget(menu_gpsrequest);
172     //zavai.menu_gps.add_widget(new GPSStuck());
173 }
174
175 }
176 }
177 }