MERGE : incorporate changes on cleaning branch to completelly decouple wifiview and...
[wifihood] / wifiscanner / wifiscanner.py
1
2 import wifimap
3
4 import gtk , pango
5 try :
6     import hildon
7 except :
8     hildon = False
9
10 def global_start(button, scanner):
11     scanner.start()
12     if button._id :
13         button.disconnect( button._id )
14     button._id = button.connect("clicked", global_stop, scanner)
15     button.set_label("Switch GPS Off")
16
17 def global_stop(button, scanner):
18     scanner.stop()
19     if button._id :
20         button.disconnect( button._id )
21     button._id = button.connect("clicked", global_start, scanner)
22     button.set_label("Switch GPS On")
23
24 def enable_agps(button):
25     if button.get_active() :
26         print "%s state is active" % button
27
28 def start_scan(button, scanner):
29     # BUG : If gps is not started in advance, database is not opened and an exception happens
30     scanner.scan()
31     if button._id :
32         button.disconnect( button._id )
33     button._id = button.connect("clicked", stop_scan, scanner)
34     button.set_label("Stop scanning")
35
36 def stop_scan(button, scanner):
37     # FIXME : This method do not clear the scheduled scan
38     scanner.scan_timeout = 0
39     if button._id :
40         button.disconnect( button._id )
41     button._id = button.connect("clicked", start_scan, scanner)
42     button.set_label("Start scanning")
43
44
45 class scanner ( wifimap.Scanner ) :
46
47     def scan ( self ) :
48         wifimap.Scanner.scan( self )
49         self.report()
50
51     def report ( self ) :
52         self.status.set_label( wifimap.Scanner.report(self) )
53         start, end = self.buffer.get_bounds()
54         self.buffer.delete( start , end )
55         for mac,rss in self.scanlist.iteritems() :
56             self.buffer.insert_at_cursor( "%s %5d\n" % ( mac , rss ) )
57
58
59 class AbstractWifiscanner :
60
61     def __init__ ( self ) :
62
63         _scanner = scanner()
64
65         self.connect("delete_event", gtk.main_quit, None)
66
67         self.vbox = gtk.VBox(homogeneous=False, spacing=0)
68
69         # Top frame creation
70         top_frame = gtk.Frame()
71         self.vbox.pack_start(top_frame)
72
73         hbox = gtk.HBox(homogeneous=False, spacing=0)
74         top_frame.add(hbox)
75
76         # Bottom frame creation
77         bottom_frame = gtk.Frame()
78         self.vbox.pack_end(bottom_frame, expand=False)
79
80         bottom_box = gtk.HBox(homogeneous=False, spacing=0)
81         bottom_frame.add( bottom_box )
82
83         # Top frame population
84         notebook = gtk.Notebook()
85         hbox.pack_start( notebook )
86
87         scrollview = gtk.ScrolledWindow()
88         notebook.append_page( scrollview , gtk.Label("Scanning") )
89         notebook.append_page( MapWindow() , gtk.Label("Map") )
90
91         buttons = gtk.VBox(homogeneous=False, spacing=0)
92         hbox.pack_end(buttons, expand=False)
93
94         textview = self.TextView( "Scan results ..." )
95         scrollview.add( textview )
96         scrollview.set_policy( gtk.POLICY_NEVER , gtk.POLICY_AUTOMATIC )
97
98         # Buttons creation
99         button = self.Button( "Switch GPS On")
100         button._id = button.connect("clicked", global_start, _scanner)
101         buttons.pack_start(button, expand=False)
102
103         button_scan = self.Button( "Start scanning")
104         button_scan._id = button_scan.connect("clicked", start_scan, _scanner)
105         buttons.pack_start(button_scan, expand=False)
106
107         toggle_button = self.CheckButton( "Use Assisted GPS" )
108         toggle_button.connect("toggled", enable_agps)
109         buttons.pack_start(toggle_button, expand=False)
110
111         # Bottom frame population
112         status = gtk.Label( "status bar ..." )
113         _scanner.status = status
114         _scanner.buffer = textview.get_buffer() 
115         bottom_box.pack_start( status , expand=False , padding=20 )
116
117     def run ( self ) :
118         self.show_all()
119         gtk.main()
120
121 if hildon :
122
123     class MapWindow ( gtk.Frame ) :
124
125         def __init__(self):
126             gtk.Frame.__init__( self )
127
128             self.config = wifimap.config.Configuration()
129             self.config.zoom = 16
130             self.map = wifimap.simpleMapWidget( self.config )
131             self.map.plot_APs()
132             self.add( self.map )
133
134     class Wifiscanner ( AbstractWifiscanner , hildon.Window ) :
135
136         def __init__ ( self ) :
137             hildon.Window.__init__( self )
138             program = hildon.Program.get_instance()
139             program.add_window(self)
140
141             AbstractWifiscanner.__init__( self )
142             self.add(self.vbox)
143
144         def TextView ( self , placeholder=None ) :
145             textview = hildon.TextView()
146             if  placeholder :
147                 textview.set_placeholder(  placeholder )
148             textview.set_editable( False )
149             textview.set_cursor_visible( False )
150             textview.modify_font( pango.FontDescription("Courier 12") )
151             return textview
152  
153         def Button ( self , label="" ) :
154             button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL, label)
155             return button
156
157         def CheckButton ( self , label=None ) :
158             toggle_button = hildon.CheckButton( gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT )
159             if label :
160                 toggle_button.set_label( label )
161             return toggle_button
162
163 else :
164
165     class MapWindow ( gtk.Frame ) :
166
167         def __init__(self):
168             gtk.Frame.__init__( self )
169
170             self.config = wifimap.config.Configuration()
171             self.config.zoom = 16
172             self.add( wifimap.simpleMapWidget( self.config , (640,400) ) )
173
174     class Wifiscanner ( AbstractWifiscanner , gtk.Window ) :
175
176         def __init__ ( self ) :
177             gtk.Window.__init__( self )
178             self.resize(640,400)
179
180             AbstractWifiscanner.__init__( self )
181             self.add(self.vbox)
182
183         def TextView ( self , placeholder=None ) :
184             textview = gtk.TextView()
185             if placeholder :
186                 textview.get_buffer().set_text( placeholder )
187             textview.set_editable( False )
188             textview.set_cursor_visible( False )
189             textview.modify_font( pango.FontDescription("Courier 12") )
190             return textview
191  
192         def Button ( self , label="" ) :
193             button = gtk.Button( label )
194             return button
195
196         def CheckButton ( self , label=None ) :
197             toggle_button = gtk.CheckButton()
198             if label :
199                 toggle_button.set_label( label )
200             return toggle_button
201