Commits

Thomas Gläßle committed 99fa445

Implement method to connect objects to daemon

  • Participants
  • Parent commits ffe1813

Comments (0)

Files changed (4)

udiskie/automount.py

 """
 __all__ = ['AutoMounter']
 
-import logging
-
 class AutoMounter:
     """
     Automatically mount newly added media.
     """
     def __init__(self, mounter):
-        self.log = logging.getLogger('udiskie.mount.AutoMounter')
         self.mounter = mounter
 
-    def connect(self, daemon):
-        daemon.connect('device_added', self.device_added)
-        daemon.connect('media_added', self.media_added)
-
-    def disconnect(self, daemon):
-        daemon.disconnect('device_added', self.device_added)
-        daemon.disconnect('media_added', self.media_added)
-
     def device_added(self, udevice):
         self.mounter.add_device(udevice)
 
         daemon = udiskie.daemon.Daemon(bus, udisks=udisks)
     if run_daemon and not options.suppress_notify:
         notify = udiskie.notify.Notify('udiskie.mount')
-        notify.connect(daemon)
+        daemon.connect(notify)
     if run_daemon:
         automount = udiskie.automount.AutoMounter(mounter)
-        automount.connect(daemon)
+        daemon.connect(automount)
 
     # mount all present devices
     if options.all:

udiskie/daemon.py

         for handler in self.event_handlers[event]:
             handler(device, *args)
 
-    def connect(self, event, handler):
+    def connect(self, handler, event=None):
         """Connect an event handler."""
-        self.event_handlers[event].append(handler)
+        if event:
+            self.event_handlers[event].append(handler)
+        else:
+            for event in self.event_handlers:
+                if hasattr(handler, event):
+                    self.connect(getattr(handler, event), event)
 
-    def disconnect(self, event, handler):
+    def disconnect(self, handler, event=None):
         """Disconnect an event handler."""
-        self.event_handlers.remove(handler)
+        if event:
+            self.event_handlers.remove(handler)
+        else:
+            for event in self.event_handlers:
+                if hasattr(handler, event):
+                    self.disconnect(getattr(handler, event), event)
 
     # udisks event listeners
     def _device_added(self, device_name):

udiskie/notify.py

         """Initialize notifier."""
         pynotify.init(name)
 
-    def connect(self, daemon):
-        """Connect to udisks daemon."""
-        daemon.connect('device_mounted', self.mount)
-        daemon.connect('device_unmounted', self.umount)
-        daemon.connect('device_unlocked', self.unlock)
-        daemon.connect('device_locked', self.lock)
-
-    def disconnect(self, daemon):
-        """Disconnect from udisks daemon."""
-        daemon.disconnect('device_mounted', self.mount)
-        daemon.disconnect('device_unmounted', self.umount)
-        daemon.disconnect('device_unlocked', self.unlock)
-        daemon.disconnect('device_locked', self.lock)
-
     # event handlers:
-    def mount(self, device):
+    def device_mounted(self, device):
         try:
             device_file = device.device_file
             mount_path = device.mount_paths[0]
         except gio.Error:
             pass
 
-    def umount(self, device):
+    def device_umounted(self, device):
         try:
             device_file = device.device_file
             pynotify.Notification('Device unmounted',
         except gio.Error:
             pass
 
-    def lock(self, device):
+    def device_locked(self, device):
         try:
             device_file = device.device_file
             pynotify.Notification('Device locked',
         except gio.Error:
             pass
 
-    def unlock(self, device):
+    def device_unlocked(self, device):
         try:
             device_file = device.device_file
             pynotify.Notification('Device unlocked',