BUGFIX : wrong arguments for hildon TextView creation
[wifihood] / wifiscanner / wifiscanner
1 #!/usr/bin/python
2
3 import wifimap
4
5 import gtk , pango
6 try :
7     import hildon
8 except :
9     from hildongtk import hildon
10
11 def global_start(button, scanner, config):
12     scanner.start( int( 1000 * 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.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             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 Wifiscanner ( hildon.StackableWindow ) :
75
76     def __init__ ( self ) :
77
78         hildon.StackableWindow.__init__( self )
79         self.set_title( "Wifihood Scanner" )
80         program = hildon.Program.get_instance()
81         program.add_window(self)
82
83         config = wifimap.config.Configuration( 'scanner' )
84         _scanner = scanner( config , "wlan0" )
85
86         self.connect("delete_event", gtk.main_quit, None)
87
88         self.vbox = gtk.VBox(homogeneous=False, spacing=0)
89
90         # Top frame creation
91         top_frame = gtk.Frame()
92
93         hbox = gtk.HBox(homogeneous=False, spacing=0)
94         top_frame.add(hbox)
95
96         # Bottom frame creation
97         bottom_frame = gtk.Frame()
98         self.vbox.pack_end(bottom_frame, expand=False)
99
100         self.vbox.pack_end(top_frame)
101
102         bottom_box = gtk.HBox(homogeneous=False, spacing=0)
103         bottom_frame.add( bottom_box )
104
105         # Top frame population
106         notebook = gtk.Notebook()
107         hbox.pack_start( notebook )
108
109         scrollview = gtk.ScrolledWindow()
110         notebook.append_page( scrollview , gtk.Label("Scanning") )
111         self.map = MapWindow( config )
112         notebook.append_page( self.map , gtk.Label("Map") )
113
114         buttons = gtk.VBox(homogeneous=False, spacing=0)
115         hbox.pack_end(buttons, expand=False)
116
117         textview = hildon.TextView()
118         textview.set_placeholder( "Scan results ..." )
119         textview.set_editable( False )
120         textview.set_cursor_visible( False )
121         textview.modify_font( pango.FontDescription("Courier 12") )
122         scrollview.add( textview )
123         scrollview.set_policy( gtk.POLICY_NEVER , gtk.POLICY_AUTOMATIC )
124
125         # Buttons creation
126         button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL, "Switch GPS On" )
127         button._id = button.connect("clicked", global_start, _scanner, config)
128         buttons.pack_start(button, expand=False)
129
130         button_scan = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL, "Start scanning" )
131         button_scan._id = button_scan.connect("clicked", start_scan, _scanner)
132         buttons.pack_start(button_scan, expand=False)
133
134         toggle_button = hildon.CheckButton( gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT )
135         toggle_button.set_label( "Use Assisted GPS" )
136         toggle_button.connect("toggled", enable_agps)
137         buttons.pack_start(toggle_button, expand=False)
138
139         # Bottom frame population
140         status = gtk.Label( "status bar ..." )
141         _scanner.status = status
142         _scanner.buffer = textview.get_buffer() 
143         _scanner.map = self.map.child
144         bottom_box.pack_start( status , expand=False , padding=20 )
145
146         self.add(self.vbox)
147
148         self.create_menu( )
149
150         self.show_all()
151
152     def create_menu ( self ) :
153
154             menubar = hildon.AppMenu()
155             self.set_app_menu( menubar )
156
157             settings = hildon.Button(gtk.HILDON_SIZE_AUTO,
158                                      hildon.BUTTON_ARRANGEMENT_VERTICAL,
159                                      "Settings",
160                                      None)
161             settings.connect( "clicked", settings_cb , self.map )
162             menubar.append( settings )
163
164             menubar.show_all()
165
166     def run ( self ) :
167         gtk.main()
168
169
170 def settings_cb ( widget , map ) :
171     window = wifimap.config.SettingsWindow( map.config , map.child.SetZoom )
172
173
174 class MapWindow ( gtk.Frame ) :
175
176         def __init__( self , config ):
177             gtk.Frame.__init__( self )
178
179             self.config = config
180             self.add( wifimap.simpleMapWidget( self.config ) )
181
182
183 window = Wifiscanner()
184 window.run()
185