Implement map tab on hildon
[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.add( wifiview.mapWidget( self.config ) )#, (640,400) ) )
128
129     class Wifiscanner ( AbstractWifiscanner , hildon.Window ) :
130
131         def __init__ ( self ) :
132             hildon.Window.__init__( self )
133             program = hildon.Program.get_instance()
134             program.add_window(self)
135
136             AbstractWifiscanner.__init__( self )
137             self.add(self.vbox)
138
139         def TextView ( self , placeholder=None ) :
140             textview = hildon.TextView()
141             if  placeholder :
142                 textview.set_placeholder(  placeholder )
143             textview.set_editable( False )
144             textview.set_cursor_visible( False )
145             textview.modify_font( pango.FontDescription("Courier 12") )
146             return textview
147  
148         def Button ( self , label="" ) :
149             button = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT, hildon.BUTTON_ARRANGEMENT_VERTICAL, label)
150             return button
151
152         def CheckButton ( self , label=None ) :
153             toggle_button = hildon.CheckButton( gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT )
154             if label :
155                 toggle_button.set_label( label )
156             return toggle_button
157
158 else :
159
160     class MapWindow ( gtk.Frame ) :
161
162         def __init__(self):
163             gtk.Frame.__init__( self )
164
165             self.config = wifimap.config.Configuration()
166             self.config.zoom = 16
167             self.add( wifiview.mapWidget( self.config , (640,400) ) )
168
169     class Wifiscanner ( AbstractWifiscanner , gtk.Window ) :
170
171         def __init__ ( self ) :
172             gtk.Window.__init__( self )
173             self.resize(640,400)
174
175             AbstractWifiscanner.__init__( self )
176             self.add(self.vbox)
177
178         def TextView ( self , placeholder=None ) :
179             textview = gtk.TextView()
180             if placeholder :
181                 textview.get_buffer().set_text( placeholder )
182             textview.set_editable( False )
183             textview.set_cursor_visible( False )
184             textview.modify_font( pango.FontDescription("Courier 12") )
185             return textview
186  
187         def Button ( self , label="" ) :
188             button = gtk.Button( label )
189             return button
190
191         def CheckButton ( self , label=None ) :
192             toggle_button = gtk.CheckButton()
193             if label :
194                 toggle_button.set_label( label )
195             return toggle_button
196