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
|
|
|
|
import gobject
|
|
|
|
import dbus
|
|
|
|
import dbus.service
|
|
|
|
from sugar import env
|
2007-01-05 21:13:46 +01:00
|
|
|
from sugar import util
|
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'
|
|
|
|
ICON_KEY = 'ICON'
|
|
|
|
PREVIEW_KEY = 'PREVIEW'
|
2007-01-11 23:57:06 +01:00
|
|
|
ACTIVITY_KEY = 'ACTIVITY'
|
2007-01-05 21:13:46 +01:00
|
|
|
FORMATS_KEY = 'FORMATS'
|
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
class ClipboardDBusServiceHelper(dbus.service.Object):
|
|
|
|
|
|
|
|
_CLIPBOARD_DBUS_INTERFACE = "org.laptop.Clipboard"
|
|
|
|
_CLIPBOARD_OBJECT_PATH = "/org/laptop/Clipboard"
|
|
|
|
|
|
|
|
def __init__(self, parent):
|
|
|
|
self._parent = parent
|
|
|
|
self._objects = {}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
# dbus methods
|
|
|
|
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
|
|
|
in_signature="ss", out_signature="")
|
|
|
|
def add_object(self, object_id, name):
|
|
|
|
self._objects[object_id] = ClipboardObject(object_id, name)
|
|
|
|
self.object_added(object_id, name)
|
|
|
|
logging.debug('Added object ' + object_id + ' with name ' + name)
|
|
|
|
|
|
|
|
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
2007-01-05 21:13:46 +01:00
|
|
|
in_signature="ssayb", out_signature="", byte_arrays=True)
|
2006-12-13 22:36:05 +01:00
|
|
|
def add_object_format(self, object_id, format_type, data, on_disk):
|
|
|
|
cb_object = self._objects[object_id]
|
2007-01-05 21:13:46 +01:00
|
|
|
cb_object.add_format(Format(format_type, data, on_disk))
|
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
if on_disk:
|
2007-01-05 21:13:46 +01:00
|
|
|
logging.debug('Added format of type ' + format_type + ' with path at ' + data)
|
2006-12-13 22:36:05 +01:00
|
|
|
else:
|
|
|
|
logging.debug('Added in-memory format of type ' + format_type + '.')
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
self.object_state_changed(object_id, {NAME_KEY: cb_object.get_name(),
|
|
|
|
PERCENT_KEY: cb_object.get_percent(),
|
|
|
|
ICON_KEY: cb_object.get_icon(),
|
2007-01-11 23:57:06 +01:00
|
|
|
PREVIEW_KEY: cb_object.get_preview(),
|
|
|
|
ACTIVITY_KEY: cb_object.get_activity()})
|
2006-12-13 22:36:05 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
|
|
|
in_signature="s", out_signature="")
|
|
|
|
def delete_object(self, object_id):
|
|
|
|
del self._objects[object_id]
|
|
|
|
self.object_deleted(object_id)
|
|
|
|
logging.debug('Deleted object with object_id ' + object_id)
|
|
|
|
|
|
|
|
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
|
|
|
in_signature="si", out_signature="")
|
2007-01-05 21:13:46 +01:00
|
|
|
def set_object_percent(self, object_id, percent):
|
2006-12-13 22:36:05 +01:00
|
|
|
cb_object = self._objects[object_id]
|
|
|
|
cb_object.set_percent(percent)
|
2007-01-05 21:13:46 +01:00
|
|
|
self.object_state_changed(object_id, {NAME_KEY: cb_object.get_name(),
|
|
|
|
PERCENT_KEY: percent,
|
|
|
|
ICON_KEY: cb_object.get_icon(),
|
2007-01-11 23:57:06 +01:00
|
|
|
PREVIEW_KEY: cb_object.get_preview(),
|
|
|
|
ACTIVITY_KEY: cb_object.get_activity()})
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
logging.debug('Changed object with object_id ' + object_id +
|
|
|
|
' with percent ' + str(percent))
|
2006-12-13 22:36:05 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
2007-01-05 21:13:46 +01:00
|
|
|
in_signature="s", out_signature="a{sv}")
|
|
|
|
def get_object(self, object_id):
|
2006-12-13 22:36:05 +01:00
|
|
|
cb_object = self._objects[object_id]
|
|
|
|
formats = cb_object.get_formats()
|
2007-01-05 21:13:46 +01:00
|
|
|
format_types = []
|
2006-12-13 22:36:05 +01:00
|
|
|
|
|
|
|
for type, format in formats.iteritems():
|
2007-01-05 21:13:46 +01:00
|
|
|
format_types.append(type)
|
2006-12-13 22:36:05 +01:00
|
|
|
|
2007-01-05 21:13:46 +01:00
|
|
|
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-01-11 23:57:06 +01:00
|
|
|
ACTIVITY_KEY: cb_object.get_activity(),
|
2007-01-05 21:13:46 +01:00
|
|
|
FORMATS_KEY: format_types}
|
|
|
|
return result_dict
|
2006-12-13 22:36:05 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
|
|
|
in_signature="ss", out_signature="ay")
|
2007-01-05 21:13:46 +01:00
|
|
|
def get_object_data(self, object_id, format_type):
|
2006-12-13 22:36:05 +01:00
|
|
|
cb_object = self._objects[object_id]
|
|
|
|
formats = cb_object.get_formats()
|
|
|
|
return formats[format_type].get_data()
|
|
|
|
|
|
|
|
# dbus signals
|
|
|
|
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="ss")
|
|
|
|
def object_added(self, object_id, name):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="s")
|
|
|
|
def object_deleted(self, object_id):
|
|
|
|
pass
|
|
|
|
|
2007-01-05 21:13:46 +01:00
|
|
|
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="sa{sv}")
|
|
|
|
def object_state_changed(self, object_id, values):
|
2006-12-13 22:36:05 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
class ClipboardService(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._dbus_helper = ClipboardDBusServiceHelper(self)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
loop = gobject.MainLoop()
|
|
|
|
try:
|
|
|
|
loop.run()
|
2007-01-05 21:13:46 +01:00
|
|
|
except KeyboardInterrupt:
|
2006-12-13 22:36:05 +01:00
|
|
|
print 'Ctrl+C pressed, exiting...'
|