Turn back wifiscanner and wifiview modules into programs
[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
59
60 class AbstractWifiscanner :
61
62     def __init__ ( self ) :
63
64         _scanner = scanner()
65
66         self.connect("delete_event", gtk.main_quit, None)
67
68         self.vbox = gtk.VBox(homogeneous=False, spacing=0)
69
70         # Top frame creation
71         top_frame = gtk.Frame()
72         self.vbox.pack_start(top_frame)
73
74         hbox = gtk.HBox(homogeneous=False, spacing=0)
75         top_frame.add(hbox)
76
77         # Bottom frame creation
78         bottom_frame = gtk.Frame()
79         self.vbox.pack_end(bottom_frame, expand=False)
80
81         bottom_box = gtk.HBox(homogeneous=False, spacing=0)
82         bottom_frame.add( bottom_box )
83
84         # Top frame population
85         notebook = gtk.Notebook()
86         hbox.pack_start( notebook )
87
88         scrollview = gtk.ScrolledWindow()
89         notebook.append_page( scrollview , gtk.Label("Scanning") )
90         notebook.append_page( MapWindow() , gtk.Label("Map") )
91
92         buttons = gtk.VBox(homogeneous=False, spacing=0)
93         hbox.pack_end(buttons, expand=False)
94
95         textview = self.TextView( "Scan results ..." )
96         scrollview.add( textview )
97         scrollview.set_policy( gtk.POLICY_NEVER , gtk.POLICY_AUTOMATIC )
98
99         # Buttons creation
100         button = self.Button( "Switch GPS On")
101         button._id = button.connect("clicked", global_start, _scanner)
102         buttons.pack_start(button, expand=False)
103
104         button_scan = self.Button( "Start scanning")
105         button_scan._id = button_scan.connect("clicked", start_scan, _scanner)
106         buttons.pack_start(button_scan, expand=False)
107
108         toggle_button = self.CheckButton( "Use Assisted GPS" )
109         toggle_button.connect("toggled", enable_agps)
110         buttons.pack_start(toggle_button, expand=False)
111
112         # Bottom frame population
113         status = gtk.Label( "status bar ..." )
114         _scanner.status = status
115         _scanner.buffer = textview.get_buffer() 
116         bottom_box.pack_start( status , expand=False , padding=20 )
117
118     def run ( self ) :
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             self.show_all()
145
146         def TextView ( self , placeholder=None ) :
147             textview = hildon.TextView()
148             if  placeholder :
149                 textview.set_placeholder(  placeholder )
150             textview.set_editable( False )
151             textview.set_cursor_visible( False )
152             textview.modify_font( pango.FontDescription("Courier 12") )
153             return textview
154  
155         def Button ( self , label="" ) :
156             button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL, label)
157             return button
158
159         def CheckButton ( self , label=None ) :
160             toggle_button = hildon.CheckButton( gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT )
161             if label :
162                 toggle_button.set_label( label )
163             return toggle_button
164
165 else :
166
167     class MapWindow ( gtk.Frame ) :
168
169         def __init__(self):
170             gtk.Frame.__init__( self )
171
172             self.config = wifimap.config.Configuration()
173             self.config.zoom = 16
174             self.add( wifimap.simpleMapWidget( self.config , (640,400) ) )
175
176     class Wifiscanner ( AbstractWifiscanner , gtk.Window ) :
177
178         def __init__ ( self ) :
179             gtk.Window.__init__( self )
180             self.resize(640,400)
181
182             AbstractWifiscanner.__init__( self )
183             self.add(self.vbox)
184
185             self.show_all()
186
187         def TextView ( self , placeholder=None ) :
188             textview = gtk.TextView()
189             if placeholder :
190                 textview.get_buffer().set_text( placeholder )
191             textview.set_editable( False )
192             textview.set_cursor_visible( False )
193             textview.modify_font( pango.FontDescription("Courier 12") )
194             return textview
195  
196         def Button ( self , label="" ) :
197             button = gtk.Button( label )
198             return button
199
200         def CheckButton ( self , label=None ) :
201             toggle_button = gtk.CheckButton()
202             if label :
203                 toggle_button.set_label( label )
204             return toggle_button
205
206 window = Wifiscanner()
207 window.run()
208