2006-12-13 22:36:05 +01:00
|
|
|
import logging
|
2006-11-15 13:56:19 +01:00
|
|
|
import dbus
|
|
|
|
import gobject
|
|
|
|
|
2007-01-05 21:13:46 +01:00
|
|
|
NAME_KEY = 'NAME'
|
|
|
|
PERCENT_KEY = 'PERCENT'
|
|
|
|
ICON_KEY = 'ICON'
|
|
|
|
PREVIEW_KEY = 'PREVIEW'
|
|
|
|
FORMATS_KEY = 'FORMATS'
|
2006-12-13 22:36:05 +01:00
|
|
|
|
2006-11-15 13:56:19 +01:00
|
|
|
DBUS_SERVICE = "org.laptop.Clipboard"
|
|
|
|
DBUS_INTERFACE = "org.laptop.Clipboard"
|
|
|
|
DBUS_PATH = "/org/laptop/Clipboard"
|
|
|
|
|
|
|
|
class ClipboardService(gobject.GObject):
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
__gsignals__ = {
|
|
|
|
'object-added': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2006-12-13 22:36:05 +01:00
|
|
|
([str, str])),
|
2006-12-04 20:12:24 +01:00
|
|
|
'object-deleted': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([str])),
|
|
|
|
'object-state-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2007-01-05 21:13:46 +01:00
|
|
|
([str, str, int, str, str])),
|
2006-12-04 20:12:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
|
|
|
|
self._dbus_service = None
|
|
|
|
bus = dbus.SessionBus()
|
|
|
|
bus.add_signal_receiver(self._name_owner_changed_cb,
|
|
|
|
signal_name="NameOwnerChanged",
|
|
|
|
dbus_interface="org.freedesktop.DBus")
|
|
|
|
# Try to register to ClipboardService, if we fail, we'll try later.
|
|
|
|
try:
|
|
|
|
self._connect_clipboard_signals()
|
|
|
|
except dbus.DBusException, exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _connect_clipboard_signals(self):
|
|
|
|
bus = dbus.SessionBus()
|
|
|
|
proxy_obj = bus.get_object(DBUS_SERVICE, DBUS_PATH)
|
|
|
|
self._dbus_service = dbus.Interface(proxy_obj, DBUS_SERVICE)
|
|
|
|
self._dbus_service.connect_to_signal('object_added',
|
|
|
|
self._object_added_cb)
|
|
|
|
self._dbus_service.connect_to_signal('object_deleted',
|
|
|
|
self._object_deleted_cb)
|
|
|
|
self._dbus_service.connect_to_signal('object_state_changed',
|
|
|
|
self._object_state_changed_cb)
|
2006-11-15 13:56:19 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _name_owner_changed_cb(self, name, old, new):
|
|
|
|
if name != DBUS_SERVICE:
|
|
|
|
return
|
|
|
|
|
|
|
|
if (not old and not len(old)) and (new and len(new)):
|
|
|
|
# ClipboardService started up
|
|
|
|
self._connect_clipboard_signals()
|
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
def _object_added_cb(self, object_id, name):
|
|
|
|
self.emit('object-added', object_id, name)
|
2006-11-15 13:56:19 +01:00
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
def _object_deleted_cb(self, object_id):
|
|
|
|
self.emit('object-deleted', object_id)
|
2006-11-15 13:56:19 +01:00
|
|
|
|
2007-01-05 21:13:46 +01:00
|
|
|
def _object_state_changed_cb(self, object_id, values):
|
|
|
|
self.emit('object-state-changed', object_id, values[NAME_KEY],
|
|
|
|
values[PERCENT_KEY], values[ICON_KEY], values[PREVIEW_KEY])
|
2006-11-15 13:56:19 +01:00
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
def add_object(self, object_id, name):
|
|
|
|
self._dbus_service.add_object(object_id, name)
|
|
|
|
|
|
|
|
def add_object_format(self, object_id, formatType, data, on_disk):
|
|
|
|
self._dbus_service.add_object_format(object_id,
|
|
|
|
formatType,
|
2007-01-05 21:13:46 +01:00
|
|
|
data,
|
2006-12-13 22:36:05 +01:00
|
|
|
on_disk)
|
|
|
|
|
|
|
|
def delete_object(self, object_id):
|
|
|
|
self._dbus_service.delete_object(object_id)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-01-05 21:13:46 +01:00
|
|
|
def set_object_percent(self, object_id, percent):
|
|
|
|
self._dbus_service.set_object_percent(object_id, percent)
|
2006-11-15 13:56:19 +01:00
|
|
|
|
2007-01-05 21:13:46 +01:00
|
|
|
def get_object(self, object_id):
|
|
|
|
result_dict = self._dbus_service.get_object(object_id,)
|
2006-12-13 22:36:05 +01:00
|
|
|
|
2007-01-05 21:13:46 +01:00
|
|
|
return (result_dict[NAME_KEY], result_dict[PERCENT_KEY],
|
|
|
|
result_dict[ICON_KEY], result_dict[PREVIEW_KEY],
|
|
|
|
result_dict[FORMATS_KEY])
|
|
|
|
|
|
|
|
def get_object_data(self, object_id, formatType):
|
|
|
|
return self._dbus_service.get_object_data(object_id, formatType,
|
|
|
|
byte_arrays=True)
|
2006-12-13 22:36:05 +01:00
|
|
|
|
2006-11-15 13:56:19 +01:00
|
|
|
_clipboard_service = None
|
|
|
|
def get_instance():
|
2006-12-04 20:12:24 +01:00
|
|
|
global _clipboard_service
|
|
|
|
if not _clipboard_service:
|
|
|
|
_clipboard_service = ClipboardService()
|
|
|
|
return _clipboard_service
|