Some attempt to compile it with new vala, failed
[gregoa/zavai.git] / plugins / 55_gpx_trace.py
1 # gpx_trace - zavai GPX trace functions
2 #
3 # Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19 from gettext import gettext as _
20 import gtk
21 import zavai
22
23 class GPSOn(gtk.ToggleAction):
24     states = [_("GPS always on"), _("GPS on when needed")]
25
26     def __init__(self, registry, **kw):
27         self.state = 0
28         super(GPSOn, self).__init__("menu.main.gps.alwayson", self.states[self.state], None, None)
29
30         self.registry = registry
31         self.set_active(False)
32
33         self.connect("toggled", self.on_toggle)
34
35     def on_toggle(self, *args):
36         self.state = (self.state + 1) % len(self.states)
37         self.set_property("label", self.states[self.state])
38         if self.get_active():
39             self.start()
40         else:
41             self.stop()
42
43     def start(self):
44         self.registry.resource("gps").connect("gps", self)
45
46     def stop(self):
47         self.registry.resource("gps").disconnect("gps", self)
48
49 class GPXTracer(gtk.ToggleAction):
50     states = [_("Start GPX trace"), _("Stop GPX trace")]
51
52     def __init__(self, registry, **kw):
53         self.state = 0
54         super(GPXTracer, self).__init__("menu.main.gps.gpx", self.states[self.state], None, None)
55
56         self.registry = registry
57         self.set_active(False)
58
59         self.connect("toggled", self.on_toggle)
60
61     def on_toggle(self, *args):
62         self.state = (self.state + 1) % len(self.states)
63         self.set_property("label", self.states[self.state])
64         if self.get_active():
65             self.start()
66         else:
67             self.stop()
68
69     def start(self):
70         zavai.info("GPX trace started")
71         self.registry.resource("gpx").connect("gpx", self)
72
73     def stop(self):
74         zavai.info("GPX trace ended")
75         self.registry.resource("gpx").disconnect("gpx", self)
76
77 class GPXWaypoint(gtk.Action):
78     def __init__(self, registry, **kw):
79         super(GPXWaypoint, self).__init__("menu.main.gps.waypoint", _("Take waypoint"), None, None)
80         self.gpx = registry.resource("gpx")
81         self.gpx.add_activity_monitor(self.on_gpx_activity_changed)
82         self.connect("activate", self.on_activate)
83
84     def on_gpx_activity_changed(self, gpx, state):
85         self.set_sensitive(state)
86
87     def on_activate(self, *args):
88         self.gpx.waypoint()
89
90 class GPXAudioTracer(gtk.ToggleAction):
91     states = [_("Start GPX and audio trace"), _("Stop GPX and audio trace")]
92
93     def __init__(self, registry, **kw):
94         self.state = 0
95         super(GPXAudioTracer, self).__init__("menu.main.gps.gpxaudio", self.states[self.state], None, None)
96
97         self.registry = registry
98         self.recorder = zavai.Recorder(registry)
99         self.set_active(False)
100
101         self.connect("toggled", self.on_toggle)
102
103     def shutdown(self):
104         self.recorder.stop()
105         super(GPXAudioTracer, self).shutdown()
106
107     def on_toggle(self, *args):
108         self.state = (self.state + 1) % len(self.states)
109         self.set_property("label", self.states[self.state])
110         if self.get_active():
111             self.start()
112         else:
113             self.stop()
114
115     def start(self):
116         zavai.info("GPX trace started")
117         gpx = self.registry.resource("gpx")
118         gpx.connect("gpx", self)
119         gpx.add_activity_monitor(self.on_gpx_activity_changed)
120
121     def stop(self):
122         zavai.info("GPX trace ended")
123         gpx = self.registry.resource("gpx")
124         gpx.disconnect("gpx", self)
125         self.recorder.stop()
126         gpx.del_activity_monitor(self.on_gpx_activity_changed)
127
128     def on_gpx_activity_changed(self, gpx, state):
129         if state:
130             self.recorder.start(gpx.basename + ".wav")
131         else:
132             self.recorder.stop()
133
134 def init(conf = None, registry = None, **kw):
135     registry.register(GPXTracer(registry))
136     registry.register(GPXAudioTracer(registry))
137     registry.register(GPXWaypoint(registry))
138     registry.register(GPSOn(registry))