Started keyboard handling
[gregoa/zavai.git] / src / app_keyboard.vala
1 /*
2  * app_keyboard - zavai keyboard show/hide 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 kbd {
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 public class Waypoint : BigButton
81 {
82     public Waypoint()
83     {
84         set_label("Take waypoint");
85         zavai.gps.gpx.tracking_changed += on_gpx_tracking_changed;
86         clicked += on_clicked;
87         set_sensitive(zavai.gps.gpx.tracking);
88     }
89
90     protected void on_gpx_tracking_changed(zavai.gps.GPX gpx, bool new_state)
91     {
92 stderr.printf("Toggled %d\n", (int)new_state);
93         set_sensitive(new_state);
94     }
95
96     protected void on_clicked()
97     {
98 stderr.printf("Activate\n");
99         zavai.gps.gpx.waypoint();
100     }
101 }
102
103 class GPXAudioTracer(gtk.ToggleAction):
104     states = [_("Start GPX and audio trace"), _("Stop GPX and audio trace")]
105
106     def __init__(self, registry, **kw):
107         self.state = 0
108         super(GPXAudioTracer, self).__init__("menu.main.gps.gpxaudio", self.states[self.state], None, None)
109
110         self.registry = registry
111         self.recorder = zavai.Recorder(registry)
112         self.set_active(False)
113
114         self.connect("toggled", self.on_toggle)
115
116     def shutdown(self):
117         self.recorder.stop()
118         super(GPXAudioTracer, self).shutdown()
119
120     def on_toggle(self, *args):
121         self.state = (self.state + 1) % len(self.states)
122         self.set_property("label", self.states[self.state])
123         if self.get_active():
124             self.start()
125         else:
126             self.stop()
127
128     def start(self):
129         zavai.info("GPX trace started")
130         gpx = self.registry.resource("gpx")
131         gpx.connect("gpx", self)
132         gpx.add_activity_monitor(self.on_gpx_activity_changed)
133
134     def stop(self):
135         zavai.info("GPX trace ended")
136         gpx = self.registry.resource("gpx")
137         gpx.disconnect("gpx", self)
138         self.recorder.stop()
139         gpx.del_activity_monitor(self.on_gpx_activity_changed)
140
141     def on_gpx_activity_changed(self, gpx, state):
142         if state:
143             self.recorder.start(gpx.basename + ".wav")
144         else:
145             self.recorder.stop()
146 */
147
148 /*
149 public class GPSRequestLink : Gtk.ToggleButton
150 {
151         protected string service_name;
152         protected string label_start;
153         protected string label_stop;
154     protected Gtk.StatusIcon status_icon;
155     protected int fix_status = 0;
156
157         public GPSRequestLink()
158         {
159         service_name = "gps";
160         label_start = "Keep GPS on";
161         label_stop = "Stop keeping GPS on";
162                 set_size_request(0, zavai.config.min_button_height);
163                 toggled += on_toggled;
164
165                 set_label(get_active() ? label_stop : label_start);
166
167         //tooltip = "GPS status";
168         try {
169             fix_status = zavai.gps.gps.device.GetFixStatus();
170         } catch (Error e) {
171             fix_status = 0;
172         }
173         zavai.gps.gps.device.FixStatusChanged += on_fix_status_changed;
174
175         // GPS status icon
176         status_icon = new Gtk.StatusIcon();
177         status_icon.set_visible(true);
178         status_icon.activate += on_status_activate;
179         update_icon();
180         }
181
182     protected void update_icon()
183     {
184         string name;
185         if (fix_status == 2 || fix_status == 3)
186             name = zavai.config.icondir + "/" + (get_active() ? "gps_fix_on.png" : "gps_fix_off.png");
187         else
188             name = zavai.config.icondir + "/" + (get_active() ? "gps_nofix_on.png" : "gps_nofix_off.png");
189 stderr.printf("load icon from %s\n", name);
190         status_icon.set_from_file(name);
191     }
192
193     private void on_fix_status_changed(dynamic DBus.Object pos, int fix_status)
194     {
195         this.fix_status = fix_status;
196         update_icon();
197     }
198
199         private void on_toggled(Gtk.Button src)
200         {
201                 Service s = zavai.registry.gets(service_name);
202                 if (get_active())
203                         s.request("servicerequestlink");
204                 else
205                         s.release("servicerequestlink");
206                 set_label(get_active() ? label_stop : label_start);
207         update_icon();
208         }
209
210     private void on_status_activate()
211     {
212         set_active(!get_active());
213     }
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
228     /*
229     var menu_waypoint = new Waypoint();
230     var menu_gpsrequest = new GPSRequestLink();
231                 //label_on = "Stop GPX trace";
232                 //label_off = "Start GPX trace";
233                 //label_on = "Stop GPS monitor";
234                 //label_off = "Start GPS monitor";
235                 //label_on = "Stop GPS position tracking";
236                 //label_off = "Start GPS position tracking";
237                 //label_on = "Stop keeping GPS on";
238                 //label_off = "Keep GPS on";
239
240     // Menus
241     var menu_gps = new zavai.Menu("GPS");
242     //menu_gps.add_applet("app.debug.useless");
243     menu_gps.add_service_toggle("gps.gpx", "Start GPX trace", "Stop GPX trace");
244     menu_gps.add_widget(menu_waypoint);
245     menu_gps.add_widget(menu_gpsrequest);
246
247     zavai.registry.register_menu("menu.gps", menu_gps);
248     zavai.registry.getmenu("menu.main").add_applet("menu.gps");
249     */
250 }
251
252 }
253 }
254 }