2007-03-14 04:57:29 +01:00
|
|
|
# Copyright (C) 2007, Red Hat, Inc.
|
2007-03-14 13:32:05 +01:00
|
|
|
# Copyright (C) 2007, One Laptop Per Child
|
2007-03-14 04:57:29 +01:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2007-01-05 21:13:46 +01:00
|
|
|
import logging
|
2007-06-17 20:55:16 +02:00
|
|
|
from gettext import gettext as _
|
2007-01-05 21:13:46 +01:00
|
|
|
|
2007-03-14 13:32:05 +01:00
|
|
|
import gobject
|
2007-08-31 14:53:59 +02:00
|
|
|
import gtk
|
2007-03-14 13:32:05 +01:00
|
|
|
|
2007-08-31 14:53:59 +02:00
|
|
|
from sugar.graphics.radiotoolbutton import RadioToolButton
|
2007-02-23 13:09:33 +01:00
|
|
|
from sugar.graphics.xocolor import XoColor
|
2007-08-31 14:53:59 +02:00
|
|
|
from sugar.graphics.icon import Icon
|
2007-07-31 16:46:06 +02:00
|
|
|
from sugar.graphics import style
|
2006-12-13 22:36:05 +01:00
|
|
|
from sugar.clipboard import clipboardservice
|
2007-09-20 18:20:21 +02:00
|
|
|
from sugar.bundle.activitybundle import ActivityBundle
|
2007-01-06 22:29:13 +01:00
|
|
|
from sugar import util
|
2007-06-17 20:55:16 +02:00
|
|
|
from sugar import profile
|
2006-11-15 13:56:19 +01:00
|
|
|
|
2007-08-31 14:53:59 +02:00
|
|
|
from view.clipboardmenu import ClipboardMenu
|
|
|
|
from view.frame.frameinvoker import FrameWidgetInvoker
|
2007-03-14 13:32:05 +01:00
|
|
|
|
2007-08-31 14:53:59 +02:00
|
|
|
class ClipboardIcon(RadioToolButton):
|
|
|
|
__gtype_name__ = 'SugarClipboardIcon'
|
2006-11-15 13:56:19 +01:00
|
|
|
|
2007-08-31 14:53:59 +02:00
|
|
|
def __init__(self, object_id, name, group):
|
|
|
|
RadioToolButton.__init__(self, group=group)
|
2006-12-13 22:36:05 +01:00
|
|
|
self._object_id = object_id
|
2006-12-04 20:12:24 +01:00
|
|
|
self._name = name
|
|
|
|
self._percent = 0
|
2007-01-05 21:13:46 +01:00
|
|
|
self._preview = None
|
2007-04-11 18:22:52 +02:00
|
|
|
self._activity = None
|
2007-08-31 14:53:59 +02:00
|
|
|
self.owns_clipboard = False
|
|
|
|
|
|
|
|
self._icon = Icon()
|
|
|
|
self._icon.props.xo_color = profile.get_color()
|
|
|
|
self.set_icon_widget(self._icon)
|
|
|
|
self._icon.show()
|
2007-07-01 12:55:10 +02:00
|
|
|
|
|
|
|
cb_service = clipboardservice.get_instance()
|
2007-08-31 14:53:59 +02:00
|
|
|
cb_service.connect('object-state-changed', self._object_state_changed_cb)
|
2007-07-01 12:55:10 +02:00
|
|
|
obj = cb_service.get_object(self._object_id)
|
|
|
|
|
|
|
|
self.palette = ClipboardMenu(self._object_id, self._name, self._percent,
|
|
|
|
self._preview, self._activity,
|
2007-09-06 11:33:16 +02:00
|
|
|
self._is_bundle(obj['FORMATS']))
|
2007-08-31 14:53:59 +02:00
|
|
|
self.palette.props.invoker = FrameWidgetInvoker(self)
|
|
|
|
|
|
|
|
self.child.connect('drag_data_get', self._drag_data_get_cb)
|
|
|
|
self.connect('notify::active', self._notify_active_cb)
|
2007-03-14 13:32:05 +01:00
|
|
|
|
2007-09-06 11:33:16 +02:00
|
|
|
def _is_bundle(self, formats):
|
|
|
|
# A bundle will have only one format.
|
2007-09-20 18:20:21 +02:00
|
|
|
return formats and formats[0] in [ActivityBundle.MIME_TYPE,
|
|
|
|
ActivityBundle.DEPRECATED_MIME_TYPE]
|
2007-09-06 11:33:16 +02:00
|
|
|
|
2007-08-31 14:53:59 +02:00
|
|
|
def get_object_id(self):
|
|
|
|
return self._object_id
|
2007-03-14 13:32:05 +01:00
|
|
|
|
2007-08-31 14:53:59 +02:00
|
|
|
def _drag_data_get_cb(self, widget, context, selection, targetType, eventTime):
|
|
|
|
logging.debug('_drag_data_get_cb: requested target ' + selection.target)
|
|
|
|
|
|
|
|
cb_service = clipboardservice.get_instance()
|
|
|
|
data = cb_service.get_object_data(self._object_id, selection.target)['DATA']
|
|
|
|
|
|
|
|
selection.set(selection.target, 8, data)
|
|
|
|
|
|
|
|
def _put_in_clipboard(self):
|
|
|
|
logging.debug('ClipboardIcon._put_in_clipboard')
|
|
|
|
targets = self._get_targets()
|
|
|
|
if targets:
|
|
|
|
clipboard = gtk.Clipboard()
|
|
|
|
if not clipboard.set_with_data(targets,
|
|
|
|
self._clipboard_data_get_cb,
|
|
|
|
self._clipboard_clear_cb,
|
|
|
|
targets):
|
|
|
|
logging.error('GtkClipboard.set_with_data failed!')
|
|
|
|
else:
|
|
|
|
self.owns_clipboard = True
|
2007-03-14 13:32:05 +01:00
|
|
|
|
2007-08-31 14:53:59 +02:00
|
|
|
def _clipboard_data_get_cb(self, clipboard, selection, info, targets):
|
|
|
|
if not selection.target in [target[0] for target in targets]:
|
|
|
|
logging.warning('ClipboardIcon._clipboard_data_get_cb: asked %s but' \
|
|
|
|
' only have %r.' % (selection.target, targets))
|
|
|
|
return
|
|
|
|
cb_service = clipboardservice.get_instance()
|
|
|
|
data = cb_service.get_object_data(self._object_id, selection.target)['DATA']
|
|
|
|
|
|
|
|
selection.set(selection.target, 8, data)
|
|
|
|
|
|
|
|
def _clipboard_clear_cb(self, clipboard, targets):
|
|
|
|
logging.debug('ClipboardIcon._clipboard_clear_cb')
|
|
|
|
self.owns_clipboard = False
|
|
|
|
|
|
|
|
def _object_state_changed_cb(self, cb_service, object_id, name, percent,
|
|
|
|
icon_name, preview, activity):
|
|
|
|
|
|
|
|
if object_id != self._object_id:
|
|
|
|
return
|
2007-03-14 13:32:05 +01:00
|
|
|
|
2007-04-20 20:12:49 +02:00
|
|
|
cb_service = clipboardservice.get_instance()
|
|
|
|
obj = cb_service.get_object(self._object_id)
|
|
|
|
|
2007-07-04 20:24:35 +02:00
|
|
|
if icon_name:
|
2007-08-31 14:53:59 +02:00
|
|
|
self._icon.props.icon_name = icon_name
|
2007-07-04 20:24:35 +02:00
|
|
|
else:
|
2007-08-31 14:53:59 +02:00
|
|
|
self._icon.props.icon_name = 'application-octet-stream'
|
2007-07-04 20:24:35 +02:00
|
|
|
|
2007-08-31 14:53:59 +02:00
|
|
|
self.child.drag_source_set(gtk.gdk.BUTTON1_MASK,
|
|
|
|
self._get_targets(),
|
|
|
|
gtk.gdk.ACTION_COPY)
|
|
|
|
self.child.drag_source_set_icon_name(self._icon.props.icon_name)
|
|
|
|
|
2007-01-05 21:13:46 +01:00
|
|
|
self._name = name
|
2007-04-11 16:14:58 +02:00
|
|
|
self._percent = percent
|
2007-04-11 18:22:52 +02:00
|
|
|
self._preview = preview
|
|
|
|
self._activity = activity
|
2007-09-06 11:33:16 +02:00
|
|
|
self.palette.set_state(name, percent, preview, activity,
|
|
|
|
self._is_bundle(obj['FORMATS']))
|
2007-04-11 18:22:52 +02:00
|
|
|
|
2007-08-31 14:53:59 +02:00
|
|
|
if self.props.active:
|
|
|
|
self._put_in_clipboard()
|
2007-02-24 14:35:31 +01:00
|
|
|
|
2007-08-31 14:53:59 +02:00
|
|
|
def _notify_active_cb(self, widget, pspec):
|
|
|
|
if self.props.active:
|
|
|
|
self._put_in_clipboard()
|
2007-02-24 14:35:31 +01:00
|
|
|
else:
|
2007-08-31 14:53:59 +02:00
|
|
|
self.owns_clipboard = False
|
|
|
|
|
|
|
|
def _get_targets(self):
|
|
|
|
cb_service = clipboardservice.get_instance()
|
|
|
|
|
|
|
|
attrs = cb_service.get_object(self._object_id)
|
|
|
|
format_types = attrs[clipboardservice.FORMATS_KEY]
|
|
|
|
|
|
|
|
targets = []
|
|
|
|
for format_type in format_types:
|
|
|
|
targets.append((format_type, 0, 0))
|
|
|
|
|
|
|
|
return targets
|
2007-04-20 20:12:49 +02:00
|
|
|
|