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