Plot valid gps points as they arrive through scanner
[wifihood] / wifiscanner / wifiscanner
1 #!/usr/bin/python
2
3 import wifimap
4
5 import gtk , pango
6 try :
7     import hildon
8 except :
9     hildon = False
10
11 def global_start(button, scanner):
12     scanner.start()
13     if button._id :
14         button.disconnect( button._id )
15     button._id = button.connect("clicked", global_stop, scanner)
16     button.set_label("Switch GPS Off")
17
18 def global_stop(button, scanner):
19     scanner.stop()
20     if button._id :
21         button.disconnect( button._id )
22     button._id = button.connect("clicked", global_start, scanner)
23     button.set_label("Switch GPS On")
24
25 def enable_agps(button):
26     if button.get_active() :
27         print "%s state is active" % button
28
29 def start_scan(button, scanner):
30     # BUG : If gps is not started in advance, database is not opened and an exception happens
31     scanner.scan()
32     if button._id :
33         button.disconnect( button._id )
34     button._id = button.connect("clicked", stop_scan, scanner)
35     button.set_label("Stop scanning")
36
37 def stop_scan(button, scanner):
38     # FIXME : This method do not clear the scheduled scan
39     scanner.scan_timeout = 0
40     if button._id :
41         button.disconnect( button._id )
42     button._id = button.connect("clicked", start_scan, scanner)
43     button.set_label("Start scanning")
44
45
46 class scanner ( wifimap.Scanner ) :
47
48     def scan ( self ) :
49         wifimap.Scanner.scan( self )
50         self.report()
51
52     def report ( self ) :
53         self.status.set_label( wifimap.Scanner.report(self) )
54         start, end = self.buffer.get_bounds()
55         self.buffer.delete( start , end )
56         for mac,rss in self.scanlist.iteritems() :
57             self.buffer.insert_at_cursor( "%s %5d\n" % ( mac , rss ) )
58         if self.info[0] == "FIX" :
59             self.map.hide()
60             pixmap,mask = self.map.get_pixbuf().render_pixmap_and_mask()
61             self.map.plot( pixmap , ( float(self.info[4]) , float(self.info[5]) ) , "red" , 2 )
62             self.map.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.map.win_x, self.map.win_y )
63             self.map.show()
64
65
66 class AbstractWifiscanner :
67
68     def __init__ ( self ) :
69
70         _scanner = scanner()
71
72         self.connect("delete_event", gtk.main_quit, None)
73
74         self.vbox = gtk.VBox(homogeneous=False, spacing=0)
75
76         # Top frame creation
77         top_frame = gtk.Frame()
78         self.vbox.pack_start(top_frame)
79
80         hbox = gtk.HBox(homogeneous=False, spacing=0)
81         top_frame.add(hbox)
82
83         # Bottom frame creation
84         bottom_frame = gtk.Frame()
85         self.vbox.pack_end(bottom_frame, expand=False)
86
87         bottom_box = gtk.HBox(homogeneous=False, spacing=0)
88         bottom_frame.add( bottom_box )
89
90         # Top frame population
91         notebook = gtk.Notebook()
92         hbox.pack_start( notebook )
93
94         scrollview = gtk.ScrolledWindow()
95         notebook.append_page( scrollview , gtk.Label("Scanning") )
96         notebook.append_page( MapWindow() , gtk.Label("Map") )
97
98         buttons = gtk.VBox(homogeneous=False, spacing=0)
99         hbox.pack_end(buttons, expand=False)
100
101         textview = self.TextView( "Scan results ..." )
102         scrollview.add( textview )
103         scrollview.set_policy( gtk.POLICY_NEVER , gtk.POLICY_AUTOMATIC )
104
105         # Buttons creation
106         button = self.Button( "Switch GPS On")
107         button._id = button.connect("clicked", global_start, _scanner)
108         buttons.pack_start(button, expand=False)
109
110         button_scan = self.Button( "Start scanning")
111         button_scan._id = button_scan.connect("clicked", start_scan, _scanner)
112         buttons.pack_start(button_scan, expand=False)
113
114         toggle_button = self.CheckButton( "Use Assisted GPS" )
115         toggle_button.connect("toggled", enable_agps)
116         buttons.pack_start(toggle_button, expand=False)
117
118         # Bottom frame population
119         status = gtk.Label( "status bar ..." )
120         _scanner.status = status
121         _scanner.buffer = textview.get_buffer() 
122         _scanner.map = notebook.get_nth_page(1).child
123         bottom_box.pack_start( status , expand=False , padding=20 )
124
125     def run ( self ) :
126         gtk.main()
127
128 if hildon :
129
130     class MapWindow ( gtk.Frame ) :
131
132         def __init__(self):
133             gtk.Frame.__init__( self )
134
135             self.config = wifimap.config.Configuration()
136             self.config.zoom = 16
137             self.map = wifimap.simpleMapWidget( self.config )
138             self.map.plot_APs()
139             self.add( self.map )
140
141     class Wifiscanner ( AbstractWifiscanner , hildon.Window ) :
142
143         def __init__ ( self ) :
144             hildon.Window.__init__( self )
145             program = hildon.Program.get_instance()
146             program.add_window(self)
147
148             AbstractWifiscanner.__init__( self )
149             self.add(self.vbox)
150
151             self.show_all()
152
153         def TextView ( self , placeholder=None ) :
154             textview = hildon.TextView()
155             if  placeholder :
156                 textview.set_placeholder(  placeholder )
157             textview.set_editable( False )
158             textview.set_cursor_visible( False )
159             textview.modify_font( pango.FontDescription("Courier 12") )
160             return textview
161  
162         def Button ( self , label="" ) :
163             button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL, label)
164             return button
165
166         def CheckButton ( self , label=None ) :
167             toggle_button = hildon.CheckButton( gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT )
168             if label :
169                 toggle_button.set_label( label )
170             return toggle_button
171
172 else :
173
174     class MapWindow ( gtk.Frame ) :
175
176         def __init__(self):
177             gtk.Frame.__init__( self )
178
179             self.config = wifimap.config.Configuration()
180             self.config.zoom = 16
181             self.add( wifimap.simpleMapWidget( self.config , (640,400) ) )
182
183     class Wifiscanner ( AbstractWifiscanner , gtk.Window ) :
184
185         def __init__ ( self ) :
186             gtk.Window.__init__( self )
187             self.resize(640,400)
188
189             AbstractWifiscanner.__init__( self )
190             self.add(self.vbox)
191
192             self.show_all()
193
194         def TextView ( self , placeholder=None ) :
195             textview = gtk.TextView()
196             if placeholder :
197                 textview.get_buffer().set_text( placeholder )
198             textview.set_editable( False )
199             textview.set_cursor_visible( False )
200             textview.modify_font( pango.FontDescription("Courier 12") )
201             return textview
202  
203         def Button ( self , label="" ) :
204             button = gtk.Button( label )
205             return button
206
207         def CheckButton ( self , label=None ) :
208             toggle_button = gtk.CheckButton()
209             if label :
210                 toggle_button.set_label( label )
211             return toggle_button
212
213 window = Wifiscanner()
214 window.run()
215