Move scanning area as the position changes (fixes #6887)
[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             self.map.recenter( self.info[4:6] )
61             pixmap,mask = self.map.get_pixbuf().render_pixmap_and_mask()
62             self.map.plot( pixmap , ( float(self.info[4]) , float(self.info[5]) ) , "red" , 2 )
63             self.map.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.map.win_x, self.map.win_y )
64             self.map.show()
65
66
67 class AbstractWifiscanner :
68
69     def __init__ ( self ) :
70
71         _scanner = scanner( "wlan0" )
72
73         self.connect("delete_event", gtk.main_quit, None)
74
75         self.vbox = gtk.VBox(homogeneous=False, spacing=0)
76
77         # Top frame creation
78         top_frame = gtk.Frame()
79         self.vbox.pack_start(top_frame)
80
81         hbox = gtk.HBox(homogeneous=False, spacing=0)
82         top_frame.add(hbox)
83
84         # Bottom frame creation
85         bottom_frame = gtk.Frame()
86         self.vbox.pack_end(bottom_frame, expand=False)
87
88         bottom_box = gtk.HBox(homogeneous=False, spacing=0)
89         bottom_frame.add( bottom_box )
90
91         # Top frame population
92         notebook = gtk.Notebook()
93         hbox.pack_start( notebook )
94
95         scrollview = gtk.ScrolledWindow()
96         notebook.append_page( scrollview , gtk.Label("Scanning") )
97         notebook.append_page( MapWindow() , gtk.Label("Map") )
98
99         buttons = gtk.VBox(homogeneous=False, spacing=0)
100         hbox.pack_end(buttons, expand=False)
101
102         textview = self.TextView( "Scan results ..." )
103         scrollview.add( textview )
104         scrollview.set_policy( gtk.POLICY_NEVER , gtk.POLICY_AUTOMATIC )
105
106         # Buttons creation
107         button = self.Button( "Switch GPS On")
108         button._id = button.connect("clicked", global_start, _scanner)
109         buttons.pack_start(button, expand=False)
110
111         button_scan = self.Button( "Start scanning")
112         button_scan._id = button_scan.connect("clicked", start_scan, _scanner)
113         buttons.pack_start(button_scan, expand=False)
114
115         toggle_button = self.CheckButton( "Use Assisted GPS" )
116         toggle_button.connect("toggled", enable_agps)
117         buttons.pack_start(toggle_button, expand=False)
118
119         # Bottom frame population
120         status = gtk.Label( "status bar ..." )
121         _scanner.status = status
122         _scanner.buffer = textview.get_buffer() 
123         _scanner.map = notebook.get_nth_page(1).child
124         bottom_box.pack_start( status , expand=False , padding=20 )
125
126     def run ( self ) :
127         gtk.main()
128
129 if hildon :
130
131     class MapWindow ( gtk.Frame ) :
132
133         def __init__(self):
134             gtk.Frame.__init__( self )
135
136             self.config = wifimap.config.Configuration()
137             self.config.zoom = 16
138             self.add( wifimap.simpleMapWidget( self.config ) )
139
140     class Wifiscanner ( AbstractWifiscanner , hildon.Window ) :
141
142         def __init__ ( self ) :
143             hildon.Window.__init__( self )
144             program = hildon.Program.get_instance()
145             program.add_window(self)
146
147             AbstractWifiscanner.__init__( self )
148             self.add(self.vbox)
149
150             self.show_all()
151
152         def TextView ( self , placeholder=None ) :
153             textview = hildon.TextView()
154             if  placeholder :
155                 textview.set_placeholder(  placeholder )
156             textview.set_editable( False )
157             textview.set_cursor_visible( False )
158             textview.modify_font( pango.FontDescription("Courier 12") )
159             return textview
160  
161         def Button ( self , label="" ) :
162             button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL, label)
163             return button
164
165         def CheckButton ( self , label=None ) :
166             toggle_button = hildon.CheckButton( gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT )
167             if label :
168                 toggle_button.set_label( label )
169             return toggle_button
170
171 else :
172
173     class MapWindow ( gtk.Frame ) :
174
175         def __init__(self):
176             gtk.Frame.__init__( self )
177
178             self.config = wifimap.config.Configuration()
179             self.config.zoom = 16
180             self.add( wifimap.simpleMapWidget( self.config , (640,400) ) )
181
182     class Wifiscanner ( AbstractWifiscanner , gtk.Window ) :
183
184         def __init__ ( self ) :
185             gtk.Window.__init__( self )
186             self.resize(640,400)
187
188             AbstractWifiscanner.__init__( self )
189             self.add(self.vbox)
190
191             self.show_all()
192
193         def TextView ( self , placeholder=None ) :
194             textview = gtk.TextView()
195             if placeholder :
196                 textview.get_buffer().set_text( placeholder )
197             textview.set_editable( False )
198             textview.set_cursor_visible( False )
199             textview.modify_font( pango.FontDescription("Courier 12") )
200             return textview
201  
202         def Button ( self , label="" ) :
203             button = gtk.Button( label )
204             return button
205
206         def CheckButton ( self , label=None ) :
207             toggle_button = gtk.CheckButton()
208             if label :
209                 toggle_button.set_label( label )
210             return toggle_button
211
212 window = Wifiscanner()
213 window.run()
214