2006-12-13 22:36:05 +01:00
|
|
|
# Copyright (C) 2006, Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
import logging
|
2007-03-14 18:04:10 +01:00
|
|
|
import os
|
|
|
|
import shutil
|
2007-07-10 20:35:51 +02:00
|
|
|
import urlparse
|
|
|
|
import tempfile
|
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
import dbus
|
|
|
|
import dbus.service
|
2007-07-10 20:35:51 +02:00
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
from sugar import env
|
2007-01-05 21:13:46 +01:00
|
|
|
from sugar import util
|
2007-07-10 20:35:51 +02:00
|
|
|
from sugar.objects import mime
|
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
from clipboardobject import ClipboardObject, Format
|
|
|
|
|
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'
|
2007-08-15 12:19:29 +02:00
|
|
|
ACTIVITIES_KEY = 'ACTIVITIES'
|
2007-01-05 21:13:46 +01:00
|
|
|
FORMATS_KEY = 'FORMATS'
|
|
|
|
|
2007-06-17 20:55:16 +02:00
|
|
|
TYPE_KEY = 'TYPE'
|
|
|
|
DATA_KEY = 'DATA'
|
|
|
|
ON_DISK_KEY = 'ON_DISK'
|
|
|
|
|
2007-06-12 21:57:49 +02:00
|
|
|
class ClipboardService(dbus.service.Object):
|
2006-12-13 22:36:05 +01:00
|
|
|
|
|
|
|
_CLIPBOARD_DBUS_INTERFACE = "org.laptop.Clipboard"
|
|
|
|
_CLIPBOARD_OBJECT_PATH = "/org/laptop/Clipboard"
|
2007-03-14 05:50:06 +01:00
|
|
|
_CLIPBOARD_OBJECTS_PATH = _CLIPBOARD_OBJECT_PATH + "/Objects/"
|
2006-12-13 22:36:05 +01:00
|
|
|
|
2007-06-12 21:57:49 +02:00
|
|
|
def __init__(self):
|
2006-12-13 22:36:05 +01:00
|
|
|
self._objects = {}
|
2007-03-14 05:50:06 +01:00
|
|
|
self._next_id = 0
|
2006-12-13 22:36:05 +01:00
|
|
|
|
|
|
|
bus = dbus.SessionBus()
|
|
|
|
bus_name = dbus.service.BusName(self._CLIPBOARD_DBUS_INTERFACE, bus=bus)
|
|
|
|
dbus.service.Object.__init__(self, bus_name, self._CLIPBOARD_OBJECT_PATH)
|
|
|
|
|
2007-03-14 05:50:06 +01:00
|
|
|
def _get_next_object_id(self):
|
|
|
|
self._next_id += 1
|
|
|
|
return self._next_id
|
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
# dbus methods
|
|
|
|
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
2007-03-14 05:50:06 +01:00
|
|
|
in_signature="s", out_signature="o")
|
|
|
|
def add_object(self, name):
|
2007-06-20 21:28:00 +02:00
|
|
|
logging.debug('ClipboardService.add_object')
|
2007-03-14 05:50:06 +01:00
|
|
|
op = self._CLIPBOARD_OBJECTS_PATH + "%d" % self._get_next_object_id()
|
|
|
|
self._objects[op] = ClipboardObject(op, name)
|
|
|
|
self.object_added(dbus.ObjectPath(op), name)
|
|
|
|
logging.debug('Added object ' + op + ' with name ' + name)
|
|
|
|
return dbus.ObjectPath(op)
|
2006-12-13 22:36:05 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
2007-01-05 21:13:46 +01:00
|
|
|
in_signature="ssayb", out_signature="", byte_arrays=True)
|
2007-03-14 05:50:06 +01:00
|
|
|
def add_object_format(self, object_path, format_type, data, on_disk):
|
2007-06-20 21:28:00 +02:00
|
|
|
logging.debug('ClipboardService.add_object_format')
|
2007-03-14 05:50:06 +01:00
|
|
|
cb_object = self._objects[str(object_path)]
|
2007-08-08 18:08:07 +02:00
|
|
|
|
2007-09-17 02:39:02 +02:00
|
|
|
if format_type == 'XdndDirectSave0':
|
|
|
|
cb_object.add_format(Format(format_type, data, on_disk))
|
|
|
|
elif on_disk and cb_object.get_percent() == 100:
|
2007-07-10 20:35:51 +02:00
|
|
|
new_uri = self._copy_file(data)
|
|
|
|
cb_object.add_format(Format(format_type, new_uri, on_disk))
|
|
|
|
logging.debug('Added format of type ' + format_type + ' with path at ' + new_uri)
|
2006-12-13 22:36:05 +01:00
|
|
|
else:
|
2007-09-09 15:04:26 +02:00
|
|
|
cb_object.add_format(Format(format_type, data, on_disk))
|
2006-12-13 22:36:05 +01:00
|
|
|
logging.debug('Added in-memory format of type ' + format_type + '.')
|
2007-01-05 21:13:46 +01:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
self.object_state_changed(object_path, {NAME_KEY: cb_object.get_name(),
|
|
|
|
PERCENT_KEY: cb_object.get_percent(),
|
|
|
|
ICON_KEY: cb_object.get_icon(),
|
|
|
|
PREVIEW_KEY: cb_object.get_preview(),
|
2007-08-15 12:19:29 +02:00
|
|
|
ACTIVITIES_KEY: cb_object.get_activities()})
|
2006-12-13 22:36:05 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
2007-03-14 05:50:06 +01:00
|
|
|
in_signature="o", out_signature="")
|
|
|
|
def delete_object(self, object_path):
|
2007-04-17 21:53:34 +02:00
|
|
|
cb_object = self._objects.pop(str(object_path))
|
|
|
|
cb_object.destroy()
|
2007-03-14 05:50:06 +01:00
|
|
|
self.object_deleted(object_path)
|
|
|
|
logging.debug('Deleted object with object_id ' + object_path)
|
2006-12-13 22:36:05 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
2007-03-14 05:50:06 +01:00
|
|
|
in_signature="oi", out_signature="")
|
|
|
|
def set_object_percent(self, object_path, percent):
|
|
|
|
cb_object = self._objects[str(object_path)]
|
|
|
|
if percent < 0 or percent > 100:
|
|
|
|
raise ValueError("invalid percentage")
|
2007-03-14 15:09:41 +01:00
|
|
|
if cb_object.get_percent() > percent:
|
|
|
|
raise ValueError("invalid percentage; less than current percent")
|
|
|
|
if cb_object.get_percent() == percent:
|
|
|
|
# ignore setting same percentage
|
|
|
|
return
|
2007-03-14 18:04:10 +01:00
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
cb_object.set_percent(percent)
|
2007-03-14 18:04:10 +01:00
|
|
|
|
|
|
|
if percent == 100:
|
2007-09-09 15:04:26 +02:00
|
|
|
formats = cb_object.get_formats()
|
|
|
|
for format_name, format in formats.iteritems():
|
2007-09-17 02:39:02 +02:00
|
|
|
if format.is_on_disk() and format_name != 'XdndDirectSave0':
|
2007-07-10 20:35:51 +02:00
|
|
|
new_uri = self._copy_file(format.get_data())
|
|
|
|
format.set_data(new_uri)
|
2007-03-14 18:04:10 +01:00
|
|
|
|
2007-09-09 15:04:26 +02:00
|
|
|
# Add a text/plain format to objects that are text but lack it
|
|
|
|
if 'text/plain' not in formats.keys():
|
|
|
|
if 'UTF8_STRING' in formats.keys():
|
|
|
|
self.add_object_format(object_path,
|
|
|
|
'text/plain',
|
|
|
|
data=formats['UTF8_STRING'].get_data(),
|
|
|
|
on_disk=False)
|
|
|
|
elif 'text/unicode' in formats.keys():
|
|
|
|
self.add_object_format(object_path,
|
|
|
|
'text/plain',
|
|
|
|
data=formats['UTF8_STRING'].get_data(),
|
|
|
|
on_disk=False)
|
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
self.object_state_changed(object_path, {NAME_KEY: cb_object.get_name(),
|
|
|
|
PERCENT_KEY: percent,
|
|
|
|
ICON_KEY: cb_object.get_icon(),
|
|
|
|
PREVIEW_KEY: cb_object.get_preview(),
|
2007-08-15 12:19:29 +02:00
|
|
|
ACTIVITIES_KEY: cb_object.get_activities()})
|
2006-12-13 22:36:05 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
2007-03-14 05:50:06 +01:00
|
|
|
in_signature="o", out_signature="a{sv}")
|
|
|
|
def get_object(self, object_path):
|
2007-06-20 21:28:00 +02:00
|
|
|
logging.debug('ClipboardService.get_object')
|
2007-07-20 20:45:22 +02:00
|
|
|
|
|
|
|
if not self._objects.has_key(str(object_path)):
|
|
|
|
return dbus.Dictionary({}, signature='sv')
|
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
cb_object = self._objects[str(object_path)]
|
|
|
|
formats = cb_object.get_formats()
|
2007-07-20 20:45:22 +02:00
|
|
|
format_types = dbus.Array([], signature='s')
|
2007-04-11 18:22:52 +02:00
|
|
|
|
|
|
|
for type, format in formats.iteritems():
|
|
|
|
format_types.append(type)
|
|
|
|
|
|
|
|
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(),
|
2007-08-15 12:19:29 +02:00
|
|
|
ACTIVITIES_KEY: cb_object.get_activities(),
|
2007-04-11 18:22:52 +02:00
|
|
|
FORMATS_KEY: format_types}
|
|
|
|
return dbus.Dictionary(result_dict)
|
2006-12-13 22:36:05 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
2007-06-17 20:55:16 +02:00
|
|
|
in_signature="os", out_signature="a{sv}")
|
2007-06-20 21:28:00 +02:00
|
|
|
def get_object_data(self, object_path, format_type):
|
|
|
|
logging.debug('ClipboardService.get_object_data')
|
2007-03-14 05:50:06 +01:00
|
|
|
cb_object = self._objects[str(object_path)]
|
2007-06-17 20:55:16 +02:00
|
|
|
format = cb_object.get_formats()[format_type]
|
|
|
|
result_dict = {TYPE_KEY: format.get_type(),
|
|
|
|
DATA_KEY: dbus.ByteArray(format.get_data()),
|
|
|
|
ON_DISK_KEY: format.is_on_disk()}
|
|
|
|
return dbus.Dictionary(result_dict)
|
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
# dbus signals
|
2007-03-14 05:50:06 +01:00
|
|
|
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="os")
|
|
|
|
def object_added(self, object_path, name):
|
2006-12-13 22:36:05 +01:00
|
|
|
pass
|
|
|
|
|
2007-03-14 05:50:06 +01:00
|
|
|
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="o")
|
|
|
|
def object_deleted(self, object_path):
|
2006-12-13 22:36:05 +01:00
|
|
|
pass
|
|
|
|
|
2007-03-14 05:50:06 +01:00
|
|
|
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="oa{sv}")
|
|
|
|
def object_state_changed(self, object_path, values):
|
2006-12-13 22:36:05 +01:00
|
|
|
pass
|
|
|
|
|
2007-07-10 20:35:51 +02:00
|
|
|
def _copy_file(self, original_uri):
|
|
|
|
uri = urlparse.urlparse(original_uri)
|
|
|
|
path, file_name = os.path.split(uri.path)
|
|
|
|
|
|
|
|
root, ext = os.path.splitext(file_name)
|
|
|
|
if not ext or ext == '.':
|
|
|
|
mime_type = mime.get_for_file(uri.path)
|
|
|
|
ext = '.' + mime.get_primary_extension(mime_type)
|
|
|
|
|
|
|
|
f, new_file_path = tempfile.mkstemp(ext, root)
|
|
|
|
del f
|
|
|
|
shutil.copyfile(uri.path, new_file_path)
|
|
|
|
|
|
|
|
return 'file://' + new_file_path
|
|
|
|
|
2007-06-20 21:28:00 +02:00
|
|
|
_instance = None
|
|
|
|
|
|
|
|
def get_instance():
|
|
|
|
global _instance
|
|
|
|
if not _instance:
|
|
|
|
_instance = ClipboardService()
|
|
|
|
return _instance
|