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