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