Re: Exposing yourself over d-bus



Alex Launi wrote:
> It would be awesome if we could talk to nautilus
> over d-bus and find out what file(s) are currently selected

I recently regained interest in GNOME Do and with all the DBus talk
lately I thought I'd write up a quick hack (30 minutes) to see if in the
meantime something could be done to allow GNOME Do to get the current
item (files or directories) selection from Nautilus.

The good news, there sorta is ;-) The bad news? Well, it doesn't really
return the currently selected items but the items that were last
selected (good enough I'd say) and it also doesn't work per-window (it's
globally basically).

Anyhow, here's a typical session to request the paths/uris for the last
active items (after starting the DBus service using `python
NautilusDBus.py`):

import dbus
import dbus.service

INTERFACE = "org.google.code.nautilusdbus.Service"
OBJECT_PATH = "/org/google/code/nautilusdbus/Service"
SERVICE = "org.google.code.nautilussvn.NautilusDBus"

session_bus = dbus.SessionBus()
dbus_service = session_bus.get_object(SERVICE, OBJECT_PATH)

for path in dbus_service.GetSelectedPaths(): print path

Best regards,

Bruce
# Wrote this up in some 30 minutes, allows other programs to query
# a DBus service running for which items are selected in Nautilus.
#
# Here's a typical session:
#
# Start the service: 
# $ python NautilusDBus.py
#
# Start Nautilus:
# $ nautilus -q; nautilus --no-desktop .
#
# Communicate with the DBus service from your application:
#
# import dbus
# import dbus.service
# 
# INTERFACE = "org.google.code.nautilusdbus.Service"
# OBJECT_PATH = "/org/google/code/nautilusdbus/Service"
# SERVICE = "org.google.code.nautilussvn.NautilusDBus"
# 
# session_bus = dbus.SessionBus()
# dbus_service = session_bus.get_object(SERVICE, OBJECT_PATH)
# 
# for path in dbus_service.GetSelectedPaths(): print path
# 

import traceback

# Normally one wouldn't do this but this is a quick hack and I want
# everything in a single file (the nautilus module is only available
# when running inside Nautilus).
if __name__ != "__main__":
    import nautilus

import gobject

import dbus
import dbus.service
import dbus.mainloop.glib

INTERFACE = "org.google.code.nautilusdbus.Service"
OBJECT_PATH = "/org/google/code/nautilusdbus/Service"
SERVICE = "org.google.code.nautilussvn.NautilusDBus"

if __name__ != "__main__":
    class NautilusDBus(nautilus.MenuProvider):
        
        def __init__(self):
            # Connect to the DBus service
            self.session_bus = dbus.SessionBus()
            self.dbus_service = self.session_bus.get_object(SERVICE, OBJECT_PATH)
            
        def get_file_items(self, window, items):
            if len(items) == 0: return []
            
            # We could convert the uris to actual paths but hey who cares? :-)
            self.dbus_service.SetSelectedPaths([item.get_uri() for item in items])
            
            return [] # Don't think we actually have to return anything per se
            
class Service(dbus.service.Object):
    
    selected_paths = []
    
    def __init__(self, connection):
        dbus.service.Object.__init__(self, connection, OBJECT_PATH)
    
    @dbus.service.method(INTERFACE, in_signature="as", out_signature="")
    def SetSelectedPaths(self, paths):
        self.selected_paths = paths
    
    @dbus.service.method(INTERFACE, in_signature="", out_signature="as")
    def GetSelectedPaths(self):
        # Returns a dbus.Array (as, array of strings)
        return self.selected_paths
        
if __name__ == "__main__":
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    session_bus = dbus.SessionBus()
    name = dbus.service.BusName(SERVICE, session_bus) 
    service = Service(session_bus)
    mainloop = gobject.MainLoop()
    mainloop.run()

Attachment: signature.asc
Description: OpenPGP digital signature



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]