2007-04-14 08:00:51 +02:00
|
|
|
"""UI class to access system-level clipboard object"""
|
2007-06-24 13:10:53 +02:00
|
|
|
# Copyright (C) 2007, One Laptop Per Child
|
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
# Boston, MA 02111-1307, USA.
|
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'
|
2007-04-11 18:22:52 +02:00
|
|
|
ICON_KEY = 'ICON'
|
|
|
|
PREVIEW_KEY = 'PREVIEW'
|
|
|
|
ACTIVITY_KEY = 'ACTIVITY'
|
2007-01-05 21:13:46 +01:00
|
|
|
FORMATS_KEY = 'FORMATS'
|
2006-12-13 22:36:05 +01:00
|
|
|
|
2007-06-17 20:55:16 +02:00
|
|
|
TYPE_KEY = 'TYPE'
|
|
|
|
DATA_KEY = 'DATA'
|
|
|
|
ON_DISK_KEY = 'ON_DISK'
|
|
|
|
|
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):
|
2007-04-14 08:00:51 +02:00
|
|
|
"""GUI interfaces for the system clipboard dbus service
|
|
|
|
|
|
|
|
This object is used to provide convenient access to the clipboard
|
|
|
|
service (see source/services/clipboard/clipboardservice.py). It
|
|
|
|
provides utility methods for adding/getting/removing objects from
|
|
|
|
the clipboard as well as generating events when such events occur.
|
|
|
|
|
|
|
|
Meaning is source/services/clipboard/clipboardobject.py
|
|
|
|
objects when describing "objects" on the clipboard.
|
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
__gsignals__ = {
|
2007-04-11 18:22:52 +02:00
|
|
|
'object-added': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([str, str])),
|
2006-12-04 20:12:24 +01:00
|
|
|
'object-deleted': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2007-04-11 18:22:52 +02:00
|
|
|
([str])),
|
2006-12-04 20:12:24 +01:00
|
|
|
'object-state-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2007-04-11 18:22:52 +02:00
|
|
|
([str, str, int, str, str, str])),
|
2006-12-04 20:12:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self):
|
2007-04-14 08:00:51 +02:00
|
|
|
"""Initialise the ClipboardService instance
|
|
|
|
|
|
|
|
If the service is not yet active in the background uses
|
|
|
|
a signal watcher to connect when the service appears.
|
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
gobject.GObject.__init__(self)
|
2007-02-01 20:21:56 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._dbus_service = None
|
2007-02-01 20:21:56 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
bus = dbus.SessionBus()
|
2007-02-01 20:21:56 +01:00
|
|
|
self._nameOwnerChangedHandler = bus.add_signal_receiver(
|
|
|
|
self._name_owner_changed_cb,
|
|
|
|
signal_name="NameOwnerChanged",
|
|
|
|
dbus_interface="org.freedesktop.DBus",
|
|
|
|
arg0=DBUS_SERVICE)
|
|
|
|
|
|
|
|
self._connected = False
|
2006-12-04 20:12:24 +01:00
|
|
|
# Try to register to ClipboardService, if we fail, we'll try later.
|
|
|
|
try:
|
|
|
|
self._connect_clipboard_signals()
|
|
|
|
except dbus.DBusException, exception:
|
2007-02-01 20:21:56 +01:00
|
|
|
logging.debug(exception)
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _connect_clipboard_signals(self):
|
2007-04-14 08:00:51 +02:00
|
|
|
"""Connect dbus signals to our GObject signal generating callbacks"""
|
2006-12-04 20:12:24 +01:00
|
|
|
bus = dbus.SessionBus()
|
2007-02-01 20:21:56 +01:00
|
|
|
if not self._connected:
|
|
|
|
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)
|
|
|
|
self._connected = True
|
|
|
|
|
|
|
|
bus.remove_signal_receiver(self._nameOwnerChangedHandler)
|
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):
|
2007-04-14 08:00:51 +02:00
|
|
|
"""On backend service creation, connect to the server"""
|
2007-02-01 20:21:56 +01:00
|
|
|
if not old and new:
|
2006-12-04 20:12:24 +01:00
|
|
|
# ClipboardService started up
|
|
|
|
self._connect_clipboard_signals()
|
2007-02-01 20:21:56 +01:00
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
def _object_added_cb(self, object_id, name):
|
2007-04-14 08:00:51 +02:00
|
|
|
"""Emit an object-added GObject event when dbus event arrives"""
|
2007-03-14 05:50:06 +01:00
|
|
|
self.emit('object-added', str(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):
|
2007-04-14 08:00:51 +02:00
|
|
|
"""Emit an object-deleted GObject event when dbus event arrives"""
|
2007-03-14 05:50:06 +01:00
|
|
|
self.emit('object-deleted', str(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):
|
2007-04-14 08:00:51 +02:00
|
|
|
"""Emit an object-state-changed GObject event when dbus event arrives
|
|
|
|
|
|
|
|
GObject event has:
|
|
|
|
|
|
|
|
object_id
|
|
|
|
name
|
|
|
|
percent
|
|
|
|
icon
|
|
|
|
preview
|
|
|
|
activity
|
|
|
|
|
|
|
|
From the ClipboardObject instance which is being described.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
self.emit('object-state-changed', str(object_id), values[NAME_KEY],
|
|
|
|
values[PERCENT_KEY], values[ICON_KEY], values[PREVIEW_KEY],
|
|
|
|
values[ACTIVITY_KEY])
|
2006-11-15 13:56:19 +01:00
|
|
|
|
2007-03-14 05:50:06 +01:00
|
|
|
def add_object(self, name):
|
2007-04-14 08:00:51 +02:00
|
|
|
"""Add a new object to the path
|
|
|
|
|
|
|
|
returns dbus path-name for the new object's cliboard service,
|
|
|
|
this is used for all future references to the cliboard object.
|
|
|
|
|
|
|
|
Note:
|
|
|
|
That service is actually provided by the clipboard
|
|
|
|
service object, not the ClipboardObject
|
|
|
|
"""
|
2007-03-14 05:50:06 +01:00
|
|
|
return str(self._dbus_service.add_object(name))
|
2006-12-13 22:36:05 +01:00
|
|
|
|
|
|
|
def add_object_format(self, object_id, formatType, data, on_disk):
|
2007-04-14 08:00:51 +02:00
|
|
|
"""Annotate given object on the clipboard with new information
|
|
|
|
|
|
|
|
object_id -- dbus path as returned from add_object
|
|
|
|
formatType -- XXX what should this be? mime type?
|
|
|
|
data -- storage format for the clipped object?
|
|
|
|
on_disk -- whether the data is on-disk (non-volatile) or in
|
|
|
|
memory (volatile)
|
|
|
|
|
|
|
|
Last three arguments are just passed directly to the
|
|
|
|
clipboardobject.Format instance on the server side.
|
|
|
|
|
|
|
|
returns None
|
|
|
|
"""
|
2007-03-14 05:50:06 +01:00
|
|
|
self._dbus_service.add_object_format(dbus.ObjectPath(object_id),
|
2006-12-13 22:36:05 +01:00
|
|
|
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):
|
2007-04-14 08:00:51 +02:00
|
|
|
"""Remove the given object from the clipboard
|
|
|
|
|
|
|
|
object_id -- dbus path as returned from add_object
|
|
|
|
"""
|
2007-03-14 05:50:06 +01:00
|
|
|
self._dbus_service.delete_object(dbus.ObjectPath(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):
|
2007-04-14 08:00:51 +02:00
|
|
|
"""Set the "percentage" for the given clipboard object
|
|
|
|
|
|
|
|
object_id -- dbus path as returned from add_object
|
|
|
|
percentage -- numeric value from 0 to 100 inclusive
|
|
|
|
|
|
|
|
Object percentages which are set to 100% trigger "file-completed"
|
|
|
|
operations, see the backend ClipboardService's
|
|
|
|
_handle_file_completed method for details.
|
|
|
|
|
|
|
|
returns None
|
|
|
|
"""
|
2007-03-14 05:50:06 +01:00
|
|
|
self._dbus_service.set_object_percent(dbus.ObjectPath(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):
|
2007-04-14 08:00:51 +02:00
|
|
|
"""Retrieve the clipboard object structure for given object
|
|
|
|
|
|
|
|
object_id -- dbus path as returned from add_object
|
|
|
|
|
|
|
|
Retrieves the metadata description of a given object, but
|
|
|
|
*not* the data for the object. Use get_object_data passing
|
|
|
|
one of the values in the FORMATS_KEY value in order to
|
|
|
|
retrieve the data.
|
|
|
|
|
|
|
|
returns dictionary with
|
|
|
|
NAME_KEY: str,
|
|
|
|
PERCENT_KEY: number,
|
|
|
|
ICON_KEY: str,
|
|
|
|
PREVIEW_KEY: XXX what is it?,
|
|
|
|
ACTIVITY_KEY: source activity id,
|
|
|
|
FORMATS_KEY: list of XXX what is it?
|
|
|
|
"""
|
2007-03-14 05:50:06 +01:00
|
|
|
return self._dbus_service.get_object(dbus.ObjectPath(object_id),)
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
def get_object_data(self, object_id, formatType):
|
2007-04-14 08:00:51 +02:00
|
|
|
"""Retrieve object's data in the given formatType
|
|
|
|
|
|
|
|
object_id -- dbus path as returned from add_object
|
|
|
|
formatType -- format specifier XXX of what description
|
|
|
|
|
2007-06-17 20:55:16 +02:00
|
|
|
returns dictionary with
|
|
|
|
TYPE_KEY: str,
|
|
|
|
DATA_KEY: str,
|
|
|
|
ON_DISK_KEY: bool
|
2007-04-14 08:00:51 +02:00
|
|
|
"""
|
2007-03-14 05:50:06 +01:00
|
|
|
return self._dbus_service.get_object_data(dbus.ObjectPath(object_id),
|
|
|
|
formatType,
|
2007-01-05 21:13:46 +01:00
|
|
|
byte_arrays=True)
|
2007-06-17 20:55:16 +02:00
|
|
|
|
2006-11-15 13:56:19 +01:00
|
|
|
_clipboard_service = None
|
|
|
|
def get_instance():
|
2007-04-14 08:00:51 +02:00
|
|
|
"""Retrieve this process's interface to the clipboard service"""
|
2006-12-04 20:12:24 +01:00
|
|
|
global _clipboard_service
|
|
|
|
if not _clipboard_service:
|
|
|
|
_clipboard_service = ClipboardService()
|
|
|
|
return _clipboard_service
|