Added icon, title and preview for clipboard objects. Also allow for opening docs and rtfs from the web.
This commit is contained in:
@@ -9,7 +9,8 @@ sugardir = $(pkgdatadir)/services/clipboard
|
||||
sugar_PYTHON = \
|
||||
__init__.py \
|
||||
clipboardobject.py \
|
||||
clipboardservice.py
|
||||
clipboardservice.py \
|
||||
typeregistry.py
|
||||
|
||||
|
||||
bin_SCRIPTS = sugar-clipboard
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import typeregistry
|
||||
|
||||
class ClipboardObject:
|
||||
|
||||
def __init__(self, id, name):
|
||||
@@ -8,10 +10,23 @@ class ClipboardObject:
|
||||
|
||||
def get_id(self):
|
||||
return self._id
|
||||
|
||||
def _get_type_info(self):
|
||||
type_registry = typeregistry.get_instance()
|
||||
return type_registry.get_type(self._formats)
|
||||
|
||||
def get_name(self):
|
||||
return self._name
|
||||
if self._name:
|
||||
return self._name
|
||||
else:
|
||||
return self._get_type_info().get_name()
|
||||
|
||||
def get_icon(self):
|
||||
return self._get_type_info().get_icon()
|
||||
|
||||
def get_preview(self):
|
||||
return self._get_type_info().get_preview()
|
||||
|
||||
def get_percent(self):
|
||||
return self._percent
|
||||
|
||||
@@ -23,7 +38,7 @@ class ClipboardObject:
|
||||
|
||||
def get_formats(self):
|
||||
return self._formats
|
||||
|
||||
|
||||
class Format:
|
||||
|
||||
def __init__(self, type, data, on_disk):
|
||||
|
||||
@@ -19,8 +19,15 @@ import gobject
|
||||
import dbus
|
||||
import dbus.service
|
||||
from sugar import env
|
||||
from sugar import util
|
||||
from clipboardobject import ClipboardObject, Format
|
||||
|
||||
NAME_KEY = 'NAME'
|
||||
PERCENT_KEY = 'PERCENT'
|
||||
ICON_KEY = 'ICON'
|
||||
PREVIEW_KEY = 'PREVIEW'
|
||||
FORMATS_KEY = 'FORMATS'
|
||||
|
||||
class ClipboardDBusServiceHelper(dbus.service.Object):
|
||||
|
||||
_CLIPBOARD_DBUS_INTERFACE = "org.laptop.Clipboard"
|
||||
@@ -43,21 +50,20 @@ class ClipboardDBusServiceHelper(dbus.service.Object):
|
||||
logging.debug('Added object ' + object_id + ' with name ' + name)
|
||||
|
||||
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
||||
in_signature="ssayb", out_signature="")
|
||||
in_signature="ssayb", out_signature="", byte_arrays=True)
|
||||
def add_object_format(self, object_id, format_type, data, on_disk):
|
||||
|
||||
# FIXME: Take it out when using the 0.80 dbus bindings
|
||||
s = ""
|
||||
for i in data:
|
||||
s += chr(i)
|
||||
|
||||
cb_object = self._objects[object_id]
|
||||
cb_object.add_format(Format(format_type, s, on_disk))
|
||||
|
||||
cb_object.add_format(Format(format_type, data, on_disk))
|
||||
|
||||
if on_disk:
|
||||
logging.debug('Added format of type ' + format_type + ' with path at ' + s)
|
||||
logging.debug('Added format of type ' + format_type + ' with path at ' + data)
|
||||
else:
|
||||
logging.debug('Added in-memory format of type ' + format_type + '.')
|
||||
|
||||
self.object_state_changed(object_id, {NAME_KEY: cb_object.get_name(),
|
||||
PERCENT_KEY: cb_object.get_percent(),
|
||||
ICON_KEY: cb_object.get_icon(),
|
||||
PREVIEW_KEY: cb_object.get_preview()})
|
||||
|
||||
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
||||
in_signature="s", out_signature="")
|
||||
@@ -68,30 +74,39 @@ class ClipboardDBusServiceHelper(dbus.service.Object):
|
||||
|
||||
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
||||
in_signature="si", out_signature="")
|
||||
def set_object_state(self, object_id, percent):
|
||||
def set_object_percent(self, object_id, percent):
|
||||
cb_object = self._objects[object_id]
|
||||
cb_object.set_percent(percent)
|
||||
self.object_state_changed(object_id, percent)
|
||||
logging.debug('Changed object with object_id ' + object_id + ' with percent ' + str(percent))
|
||||
self.object_state_changed(object_id, {NAME_KEY: cb_object.get_name(),
|
||||
PERCENT_KEY: percent,
|
||||
ICON_KEY: cb_object.get_icon(),
|
||||
PREVIEW_KEY: cb_object.get_preview()})
|
||||
|
||||
logging.debug('Changed object with object_id ' + object_id +
|
||||
' with percent ' + str(percent))
|
||||
|
||||
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
||||
in_signature="s", out_signature="as")
|
||||
def get_object_format_types(self, object_id):
|
||||
in_signature="s", out_signature="a{sv}")
|
||||
def get_object(self, object_id):
|
||||
cb_object = self._objects[object_id]
|
||||
formats = cb_object.get_formats()
|
||||
array = []
|
||||
format_types = []
|
||||
|
||||
for type, format in formats.iteritems():
|
||||
array.append(type)
|
||||
format_types.append(type)
|
||||
|
||||
return array
|
||||
result_dict = {NAME_KEY: cb_object.get_name(),
|
||||
PERCENT_KEY: cb_object.get_percent(),
|
||||
ICON_KEY: cb_object.get_icon(),
|
||||
PREVIEW_KEY: cb_object.get_preview(),
|
||||
FORMATS_KEY: format_types}
|
||||
return result_dict
|
||||
|
||||
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
||||
in_signature="ss", out_signature="ay")
|
||||
def get_object_data(self, object_id, format_type):
|
||||
def get_object_data(self, object_id, format_type):
|
||||
cb_object = self._objects[object_id]
|
||||
formats = cb_object.get_formats()
|
||||
|
||||
return formats[format_type].get_data()
|
||||
|
||||
# dbus signals
|
||||
@@ -103,8 +118,8 @@ class ClipboardDBusServiceHelper(dbus.service.Object):
|
||||
def object_deleted(self, object_id):
|
||||
pass
|
||||
|
||||
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="si")
|
||||
def object_state_changed(self, object_id, percent):
|
||||
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="sa{sv}")
|
||||
def object_state_changed(self, object_id, values):
|
||||
pass
|
||||
|
||||
class ClipboardService(object):
|
||||
@@ -115,5 +130,5 @@ class ClipboardService(object):
|
||||
loop = gobject.MainLoop()
|
||||
try:
|
||||
loop.run()
|
||||
except idboardInterrupt:
|
||||
except KeyboardInterrupt:
|
||||
print 'Ctrl+C pressed, exiting...'
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
import logging
|
||||
|
||||
class FileType:
|
||||
def __init__(self, formats):
|
||||
self._formats = formats
|
||||
|
||||
def get_name(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def get_icon(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def get_preview(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
raise NotImplementedError
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class TextFileType(FileType):
|
||||
|
||||
_types = set(['text/plain', 'UTF8_STRING', 'STRING'])
|
||||
|
||||
def get_name(self):
|
||||
return 'Text snippet'
|
||||
|
||||
def get_icon(self):
|
||||
return 'activity-xbook'
|
||||
|
||||
def get_preview(self):
|
||||
for format, data in self._formats.iteritems():
|
||||
if format in TextFileType._types:
|
||||
return str(data.get_data())
|
||||
|
||||
return ''
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return mime_type in cls._types
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class ImageFileType(FileType):
|
||||
|
||||
_types = set(['image/jpeg', 'image/gif', 'image/png', 'image/tiff'])
|
||||
|
||||
def get_name(self):
|
||||
return 'Image'
|
||||
|
||||
def get_icon(self):
|
||||
return 'activity-sketch'
|
||||
|
||||
def get_preview(self):
|
||||
return ''
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return mime_type in cls._types
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class UriFileType(FileType):
|
||||
|
||||
_types = set(['_NETSCAPE_URL'])
|
||||
|
||||
def get_name(self):
|
||||
return 'URL'
|
||||
|
||||
def get_icon(self):
|
||||
return 'activity-web'
|
||||
|
||||
def get_preview(self):
|
||||
for format, data in self._formats.iteritems():
|
||||
if format in UriFileType._types:
|
||||
string = data.get_data()
|
||||
title = string.split("\n")[1]
|
||||
return title
|
||||
|
||||
return ''
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return mime_type in cls._types
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class PdfFileType(FileType):
|
||||
|
||||
_types = set(['application/pdf'])
|
||||
|
||||
def get_name(self):
|
||||
return 'PDF file'
|
||||
|
||||
def get_icon(self):
|
||||
return 'activity-xbook'
|
||||
|
||||
def get_preview(self):
|
||||
return ''
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return mime_type in cls._types
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class MsWordFileType(FileType):
|
||||
|
||||
_types = set(['application/msword'])
|
||||
|
||||
def get_name(self):
|
||||
return 'MS Word file'
|
||||
|
||||
def get_icon(self):
|
||||
return 'activity-abiword'
|
||||
|
||||
def get_preview(self):
|
||||
return ''
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return mime_type in cls._types
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class RtfFileType(FileType):
|
||||
|
||||
_types = set(['application/rtf', 'text/rtf'])
|
||||
|
||||
def get_name(self):
|
||||
return 'RTF file'
|
||||
|
||||
def get_icon(self):
|
||||
return 'activity-abiword'
|
||||
|
||||
def get_preview(self):
|
||||
return ''
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return mime_type in cls._types
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class UnknownFileType(FileType):
|
||||
def get_name(self):
|
||||
return 'Object'
|
||||
|
||||
def get_icon(self):
|
||||
return 'stock-missing'
|
||||
|
||||
def get_preview(self):
|
||||
return ''
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return true
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class TypeRegistry:
|
||||
def __init__(self):
|
||||
self._types = []
|
||||
self._types.append(PdfFileType)
|
||||
self._types.append(MsWordFileType)
|
||||
self._types.append(RtfFileType)
|
||||
self._types.append(UriFileType)
|
||||
self._types.append(ImageFileType)
|
||||
self._types.append(TextFileType)
|
||||
|
||||
def get_type(self, formats):
|
||||
for file_type in self._types:
|
||||
for format, data in formats.iteritems():
|
||||
if file_type.matches_mime_type(format):
|
||||
return file_type(formats)
|
||||
|
||||
return UnknownFileType(formats)
|
||||
|
||||
_type_registry = None
|
||||
def get_instance():
|
||||
global _type_registry
|
||||
if not _type_registry:
|
||||
_type_registry = TypeRegistry()
|
||||
return _type_registry
|
||||
Reference in New Issue
Block a user