* added logging in daemon
authorFredrik Wendt <fredrik@wendt.se>
Wed, 25 Aug 2010 23:01:14 +0000 (00:01 +0100)
committerFredrik Wendt <fredrik@wendt.se>
Wed, 25 Aug 2010 23:01:14 +0000 (00:01 +0100)
* refactored out popup dialog to wf.gui
* fixed namespaces in main.py files

package/src/wifi_assistant/daemon/daemon.py
package/src/wifi_assistant/daemon/main.py
package/src/wifi_assistant/gui/main.py
package/src/wifi_assistant/gui/popup_dialog.py [new file with mode: 0644]
package/test/automatic_tests.py
package/test/unit/gui/popup_dialog_test.py [new file with mode: 0644]

index 5948e17..ba46949 100644 (file)
@@ -2,12 +2,15 @@
 import gtk, hildon
 import conic
 import gobject
+import logging
 
 from launcher import Launcher
 
 def _(str):
     return str
 
+log = logging.getLogger("wfdaemon")
+
 class Daemon():
     
     def __init__(self, launcher, application_settings, network_settings, parent_window):
@@ -16,9 +19,11 @@ class Daemon():
         self._parent = parent_window
         self._launcher = launcher
         self._popup = self._application_settings.getUsePopup()
+        log.debug("_popup(%s)", self._popup)
 
 
     def connectionEstablished(self, ssid):
+        log.debug("connectionEstablished(%s)", ssid)
         settings = self._network_settings.get(ssid)
         if settings is None:
             if self.showDecisionDialog(ssid):
@@ -31,6 +36,7 @@ class Daemon():
     
 
     def launchBrowser(self, settings):
+        log.debug("launchBrowser(%s)", settings)
         browser_name = settings.getNameOfBrowserToLaunch()
         browser_options = settings.getBrowserOptions()
         if 'url' in browser_options:
@@ -41,26 +47,28 @@ class Daemon():
             self.launchBrowser(settings)
     
     def showDecisionDialog(self, ssid):
+        log.debug("showDecisionDialog(%s)", ssid)
         if not self._popup:
             return False
         
-        dialog = self._createDialog(ssid)
+        from wifi_assistant.gui.popup_dialog import PopupDialog
+        PopupDialog(self._parent, self._showDecisionDialogCallback).show(ssid)
         
-        dialog.show_all()
-        result = dialog.run()
-        dialog.hide()
+    def _showDecisionDialogCallback(self, ssid, launch_browser, remember):        
+        log.debug("_showDecisionDialogCallback(%s, %s, %s)", ssid, launch_browser, remember)
+        setting = self._network_settings.getDefaultSettings()
+        setting.setNetworkName(ssid)
+        setting.setLaunchingOfBrowserEnabled(launch_browser)
         
-        launch_browser = (result == gtk.RESPONSE_YES)
-        if checkbox.get_active():
-            setting = NetworkSetting()
-            setting.setNetworkName(ssid)
-            setting.setLaunchingOfBrowserEnabled(launch_browser)
+        if remember:
             self._network_settings.save(setting)
         
-        return launch_browser
+        if launch_browser:
+            self.launchBrowser(setting)
     
     
     def start(self):
+        log.debug("start")
         self._connection = conic.Connection()
         self._connection.connect("connection-event", self._connectionEventCallback)
         self._connection.set_property("automatic-connection-events", True)
@@ -69,32 +77,13 @@ class Daemon():
         
      
     def stop(self):
+        log.debug("stop")
         self._application_settings.unregisterUsePopupListener(self._usePopupEventCallback)
         self._connection.set_property("automatic-connection-events", False)
         
     
-    def _createDialog(self, ssid):
-        dialog = gtk.Dialog(ssid, self._parent)
-        dialog.vbox.set_homogeneous(False)
-        
-        dialog.add_button(_('No'), gtk.RESPONSE_NO)
-        dialog.add_button(_('Yes'), gtk.RESPONSE_YES)
-        
-        label = gtk.Label(_('New network connection established - do you wish to launch a browser?'))
-        label.set_line_wrap(True)
-        #label.set_justify(gtk.JUSTIFY_LEFT)
-        #label.set_alignment(0, 0.5)
-        dialog.vbox.add(label)
-    
-        checkbox = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
-        checkbox.set_label(_('Remember this decision'))
-        checkbox.set_active(True)
-        dialog.vbox.add(checkbox)
-        
-        return dialog
-    
-        
     def _connectionEventCallback(self, connection, event):
+        log.debug("_connectionEventCallback(%s)", event)
         status = event.get_status()
         if status == conic.STATUS_CONNECTED:
             # assemble id > name dict
@@ -114,6 +103,7 @@ class Daemon():
     
 
     def _usePopupEventCallback(self, gconfClient, id, gconfEntry, x):
+        log.debug("_usePopupEventCallback(%s)", gconfEntry)
         self._popup = gconfEntry.get_value().get_bool()
 
 
index 966b301..1795c99 100644 (file)
@@ -3,6 +3,7 @@ import conic
 import gtk, hildon
 import gobject
 from gnome import gconf
+import logging, sys
 
 from wifi_assistant.daemon.daemon import Daemon
 from wifi_assistant.daemon.launcher import Launcher
@@ -10,6 +11,8 @@ from wifi_assistant.settings.application_settings import ApplicationSettings
 from wifi_assistant.settings.network_settings import NetworkSettings
 
 if __name__ == "__main__":
+    logging.basicConfig(level=logging.DEBUG, stream=sys.__stdout__)
+    
     gconf_client = gconf.client_get_default()
     gconf_root_dir = '/apps/maemo/wifi_assistant'
     network_settings = NetworkSettings(gconf_client, gconf_root_dir)
index b4071c7..11213b2 100644 (file)
@@ -4,7 +4,9 @@ import gtk, gobject
 from gnome import gconf
 import hildon
 
-from wifi_assistant.launcher import Launcher
+import logging, sys
+
+from wifi_assistant.daemon.launcher import Launcher
 from wifi_assistant.settings.application_settings import ApplicationSettings
 from wifi_assistant.settings.network_settings import NetworkSettings
 from wifi_assistant.gui.config_gui import ConfigGui
@@ -13,6 +15,8 @@ def _(str):
     return str
 
 if __name__ == "__main__":
+    logging.basicConfig(level=logging.DEBUG, stream=sys.__stdout__)
+    
     gconf_client = gconf.client_get_default()
     gconf_root_dir = '/apps/maemo/wifi_assistant'
     network_settings = NetworkSettings(gconf_client, gconf_root_dir)
diff --git a/package/src/wifi_assistant/gui/popup_dialog.py b/package/src/wifi_assistant/gui/popup_dialog.py
new file mode 100644 (file)
index 0000000..f8c9671
--- /dev/null
@@ -0,0 +1,57 @@
+#!/usr/bin/python2.5
+import gtk, hildon
+import logging
+
+def _(str):
+    return str
+
+log = logging.getLogger("PopupDialog")
+
+class PopupDialog():
+    
+    YES_BUTTON = 1
+    NO_BUTTON = 2
+    
+    def __init__(self, parent_window, callback):
+        """self.callback(ssid, launch_browser, remember)"""
+        self._parent_window = parent_window
+        self._callback = callback
+
+
+    def show(self, ssid):
+        log.debug("_show(%s)", ssid)
+        dialog = gtk.Dialog(ssid, self._parent_window)
+        dialog.vbox.set_homogeneous(False)
+        
+        dialog.add_button(_('No'), PopupDialog.NO_BUTTON)
+        dialog.add_button(_('Yes'), PopupDialog.YES_BUTTON)
+        
+        label = gtk.Label(_('New network connection established - do you wish to launch a browser?'))
+        label.set_line_wrap(True)
+        #label.set_justify(gtk.JUSTIFY_LEFT)
+        #label.set_alignment(0, 0.5)
+        dialog.vbox.add(label)
+    
+        checkbox = hildon.CheckButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
+        checkbox.set_label(_('Remember this decision'))
+        checkbox.set_active(True)
+        dialog.vbox.add(checkbox)
+        
+        dialog.show_all()
+        result = self._runDialog(dialog)
+        dialog.hide()
+        
+        launch_browser = None
+        if result == PopupDialog.YES_BUTTON:
+            launch_browser = True
+        if result == PopupDialog.NO_BUTTON:
+            launch_browser = False
+        
+        if launch_browser is not None:
+            remember = checkbox.get_active()
+            self._callback(ssid, launch_browser, remember)
+    
+
+    def _runDialog(self, dialog):
+        result = dialog.run()
+        return result
index 3c1c3a4..00e38ff 100644 (file)
@@ -1,8 +1,7 @@
 #!/usr/bin/python2.5
 
 import unittest
-import logging
-import sys
+import logging, sys
 
 logging.basicConfig(level=logging.DEBUG, stream=sys.__stdout__)
 
@@ -14,6 +13,7 @@ from unit.gui.about_dialog_test import AboutDialogTest
 from unit.gui.add_network_dialog_test import AddNetworkDialogTest
 from unit.gui.application_settings_dialog_test import ApplicationSettingsDialogTest
 from unit.gui.edit_network_dialog_test import EditNetworkDialogTest
+from unit.gui.popup_dialog_test import PopupDialogTest
 
 from unit.settings.application_settings_test import ApplicationSettingsTest
 from unit.settings.network_settings_test import NetworkSettingsTest
diff --git a/package/test/unit/gui/popup_dialog_test.py b/package/test/unit/gui/popup_dialog_test.py
new file mode 100644 (file)
index 0000000..adb21c5
--- /dev/null
@@ -0,0 +1,54 @@
+from gnome import gconf
+from wifi_assistant.gui.popup_dialog import PopupDialog
+
+import unittest
+from unit.pie import *
+
+ssid = 'Network Name'
+
+class PopupDialogTest(unittest.TestCase):
+
+    def setUp(self):
+        self.parent_window = None
+        self.launch_callback = Mock()
+        
+        #given(self.launch_callback).call().willReturn(None)
+        self.launch_callback.replay()
+        self.testee = PopupDialog(self.parent_window, self.launch_callback.call)
+
+
+    def test_clickingYesButtonCallsCallback(self):
+        self._makeRunDialogReturn(PopupDialog.YES_BUTTON)
+        self.testee.show(ssid)
+        verify(self.launch_callback).call(ssid, True, True)
+
+
+    def test_clickingNoButtonCallsCallback(self):
+        self._makeRunDialogReturn(PopupDialog.NO_BUTTON)
+        self.testee.show(ssid)
+        verify(self.launch_callback).call(ssid, False, True)
+
+
+    def test_cancellingDialogDoesntCallCallback(self):
+        self._makeRunDialogReturn(-1)
+        self.testee.show(ssid)
+        verify(self.launch_callback, never()).call()
+    
+    
+    def test_clickingYesButWithoutRemembering(self):
+        pass
+        # FIXME
+
+
+    def _makeRunDialogReturn(self, value):
+        class method():
+            def __init__(self, value_to_return):
+                self._value_to_return = value_to_return
+            def __call__(self, x):
+                return self._value_to_return
+        self.testee._runDialog = method(value)
+        
+    
+if __name__ == '__main__':
+    unittest.main()