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