b9f04f72ba3cf365f0da24f6f743b6b7cf1f4965
[wifi-assistant] / package / src / wifi_assistant / daemon.py
1 #!/usr/bin/python2.5
2 import gtk, hildon
3 import conic
4 import gobject
5
6 from launcher import Launcher
7 from settings import Settings
8
9 def _(str):
10     return str
11
12 class Daemon():
13     
14     def __init__(self, launcher, settings, parent_window):
15         self._settings = settings
16         self._parent = parent_window
17         self._launcher = launcher
18         self._popup = self._settings.getUsePopup()
19
20
21     def launch_browser(self):
22         url = self._settings.getUrlToOpen()
23         self._launcher.openUrl(url)
24     
25     
26     def show_decision_dialog(self, ssid):
27         if not self._popup:
28             return
29         
30         dialog = gtk.Dialog(ssid, parent)
31         dialog.vbox.set_homogeneous(False)
32         
33         dialog.add_button(_('No'), gtk.RESPONSE_NO)
34         dialog.add_button(_('Yes'), gtk.RESPONSE_YES)
35         
36         label = gtk.Label(_('New network connection established - do you wish to launch a browser?'))
37         label.set_line_wrap(True)
38         #label.set_justify(gtk.JUSTIFY_LEFT)
39         #label.set_alignment(0, 0.5)
40         dialog.vbox.add(label)
41     
42         checkbox = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
43         checkbox.set_label(_('Remember this decision'))
44         checkbox.set_active(True)
45         dialog.vbox.add(checkbox)
46         
47         dialog.show_all()
48         result = dialog.run()
49         dialog.hide()
50         
51         launchBrowser = (result == gtk.RESPONSE_YES)
52         if checkbox.get_active():
53             self._settings.setLaunchSettings(ssid, launchBrowser)
54         
55         return launchBrowser
56     
57     
58     def connection_established(self, ssid):
59         value = self._settings.getLaunchSettings(ssid)
60         if value is None:
61             if self.show_decision_dialog(ssid):
62                 self.launch_browser()
63             return
64     
65         if value.get_bool():
66             self.launch_browser()
67     
68
69     def _connection_cb(self, connection, event):
70         status = event.get_status()
71         if status == conic.STATUS_CONNECTED:
72             # assemble id > name dict
73             iap_id_to_name = {}
74             iaps = connection.get_all_iaps()
75             for iap in iaps:
76                 iap_id = iap.get_id()
77                 iap_name = iap.get_name()
78                 iap_id_to_name[iap_id] = iap_name
79             
80             # get name of network
81             iap_id = event.get_iap_id()
82             iap_name = None
83             if (iap_id_to_name.has_key(iap_id)):
84                 iap_name = iap_id_to_name[iap_id]
85             self.connection_established(iap_name)
86     
87
88     def start(self):
89         self._connection = conic.Connection()
90         self._connection.connect("connection-event", self._connection_cb)
91         self._connection.set_property("automatic-connection-events", True)
92         self._settings.addUsePopupListener(self._activateCallback)
93         gtk.main()
94      
95     
96     def stop(self):
97         self._settings.removeUsePopupListener(self._activateCallback)
98         self._connection.set_property("automatic-connection-events", False)
99         
100     
101     def _activateCallback(self, gconfClient, id, gconfEntry, x):
102         self._popup = gconfEntry.get_value().get_bool()
103
104
105 if __name__ == "__main__":
106     d = Daemon(Launcher(), Settings(), gtk.Window())
107  
108     d.start()
109     d.stop()