Show on the map the displacement of the detected APs
[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, config):
12     scanner.start( config.scan_period , config.store_log )
13     if button._id :
14         button.disconnect( button._id )
15     button._id = button.connect("clicked", global_stop, scanner, config)
16     button.set_label("Switch GPS Off")
17
18 def global_stop(button, scanner, config):
19     scanner.stop()
20     if button._id :
21         button.disconnect( button._id )
22     button._id = button.connect("clicked", global_start, scanner, config)
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.ReplayScanner ) :
47
48     def scan ( self ) :
49         wifimap.ReplayScanner.scan( self )
50         self.report()
51
52     def report ( self ) :
53         self.status.set_label( wifimap.ReplayScanner.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             self.map.recenter( self.info[4:6] )
61             pixmap,mask = self.map.get_pixbuf().render_pixmap_and_mask()
62             pointsize = 2
63             if self.newaps :
64                 pointsize += 2
65             self.map.plot( pixmap , ( float(self.info[4]) , float(self.info[5]) ) , "red" , pointsize )
66             for mac,ap in self.aps.iteritems() :
67                 if self.oldpos.get( mac ) :
68                     self.map.line( pixmap , self.oldpos[mac] , ( ap[1]/ap[0] , ap[2]/ap[0] ) , "green" )
69                 self.map.plot( pixmap , ( ap[1]/ap[0] , ap[2]/ap[0] ) , "green" , 2 )
70             self.map.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.map.win_x, self.map.win_y )
71             self.map.show()
72
73
74 class AbstractWifiscanner :
75
76     def __init__ ( self ) :
77
78         _scanner = scanner( "wlan0" )
79
80         self.connect("delete_event", gtk.main_quit, None)
81
82         self.vbox = gtk.VBox(homogeneous=False, spacing=0)
83
84         # Top frame creation
85         top_frame = gtk.Frame()
86
87         hbox = gtk.HBox(homogeneous=False, spacing=0)
88         top_frame.add(hbox)
89
90         # Bottom frame creation
91         bottom_frame = gtk.Frame()
92         self.vbox.pack_end(bottom_frame, expand=False)
93
94         self.vbox.pack_end(top_frame)
95
96         bottom_box = gtk.HBox(homogeneous=False, spacing=0)
97         bottom_frame.add( bottom_box )
98
99         # Top frame population
100         notebook = gtk.Notebook()
101         hbox.pack_start( notebook )
102
103         scrollview = gtk.ScrolledWindow()
104         notebook.append_page( scrollview , gtk.Label("Scanning") )
105         self.map = MapWindow()
106         notebook.append_page( self.map , gtk.Label("Map") )
107
108         buttons = gtk.VBox(homogeneous=False, spacing=0)
109         hbox.pack_end(buttons, expand=False)
110
111         textview = self.TextView( "Scan results ..." )
112         scrollview.add( textview )
113         scrollview.set_policy( gtk.POLICY_NEVER , gtk.POLICY_AUTOMATIC )
114
115         # Buttons creation
116         button = self.Button( "Switch GPS On")
117         button._id = button.connect("clicked", global_start, _scanner, self.map.config)
118         buttons.pack_start(button, expand=False)
119
120         button_scan = self.Button( "Start scanning")
121         button_scan._id = button_scan.connect("clicked", start_scan, _scanner)
122         buttons.pack_start(button_scan, expand=False)
123
124         toggle_button = self.CheckButton( "Use Assisted GPS" )
125         toggle_button.connect("toggled", enable_agps)
126         buttons.pack_start(toggle_button, expand=False)
127
128         # Bottom frame population
129         status = gtk.Label( "status bar ..." )
130         _scanner.status = status
131         _scanner.buffer = textview.get_buffer() 
132         _scanner.map = self.map.child
133         bottom_box.pack_start( status , expand=False , padding=20 )
134
135     def run ( self ) :
136         gtk.main()
137
138 def settings_cb ( widget , map ) :
139     window = wifimap.config.SettingsWindow( map.config , map.child.SetZoom )
140
141
142 if hildon :
143
144     class MapWindow ( gtk.Frame ) :
145
146         def __init__(self):
147             gtk.Frame.__init__( self )
148
149             self.config = wifimap.config.Configuration( 'scanner' )
150             self.add( wifimap.simpleMapWidget( self.config ) )
151
152     class Wifiscanner ( AbstractWifiscanner , hildon.StackableWindow ) :
153
154         def __init__ ( self ) :
155             hildon.StackableWindow.__init__( self )
156             self.set_title( "Wifihood Scanner" )
157             program = hildon.Program.get_instance()
158             program.add_window(self)
159
160             AbstractWifiscanner.__init__( self )
161             self.add(self.vbox)
162
163             self.create_menu( )
164
165             self.show_all()
166
167         def TextView ( self , placeholder=None ) :
168             textview = hildon.TextView()
169             if  placeholder :
170                 textview.set_placeholder(  placeholder )
171             textview.set_editable( False )
172             textview.set_cursor_visible( False )
173             textview.modify_font( pango.FontDescription("Courier 12") )
174             return textview
175  
176         def Button ( self , label="" ) :
177             button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL, label)
178             return button
179
180         def CheckButton ( self , label=None ) :
181             toggle_button = hildon.CheckButton( gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT )
182             if label :
183                 toggle_button.set_label( label )
184             return toggle_button
185
186         def create_menu ( self ) :
187
188             menubar = hildon.AppMenu()
189             self.set_app_menu( menubar )
190
191             settings = hildon.Button(gtk.HILDON_SIZE_AUTO,
192                                      hildon.BUTTON_ARRANGEMENT_VERTICAL,
193                                      "Settings",
194                                      None)
195             settings.connect( "clicked", settings_cb , self.map )
196             menubar.append( settings )
197
198             menubar.show_all()
199
200 else :
201
202     class MapWindow ( gtk.Frame ) :
203
204         def __init__(self):
205             gtk.Frame.__init__( self )
206
207             self.config = wifimap.config.Configuration( 'scanner' )
208             self.add( wifimap.simpleMapWidget( self.config , (640,400) ) )
209
210     class Wifiscanner ( AbstractWifiscanner , gtk.Window ) :
211
212         def __init__ ( self ) :
213             gtk.Window.__init__( self )
214             self.resize(640,400)
215
216             AbstractWifiscanner.__init__( self )
217             self.add(self.vbox)
218
219             self.create_menu()
220
221             self.show_all()
222
223         def TextView ( self , placeholder=None ) :
224             textview = gtk.TextView()
225             if placeholder :
226                 textview.get_buffer().set_text( placeholder )
227             textview.set_editable( False )
228             textview.set_cursor_visible( False )
229             textview.modify_font( pango.FontDescription("Courier 12") )
230             return textview
231  
232         def Button ( self , label="" ) :
233             button = gtk.Button( label )
234             return button
235
236         def CheckButton ( self , label=None ) :
237             toggle_button = gtk.CheckButton()
238             if label :
239                 toggle_button.set_label( label )
240             return toggle_button
241
242         def create_menu ( self ) :
243
244             menubar = gtk.MenuBar()
245             self.vbox.pack_start( menubar )
246
247             settings = gtk.MenuItem( "Settings" )
248             settings.connect( "activate", settings_cb , self.map )
249             menubar.append( settings )
250
251             menubar.show_all()
252
253 window = Wifiscanner()
254 window.run()
255