2007-01-05 21:13:46 +01:00
|
|
|
import logging
|
|
|
|
|
2006-11-15 13:56:19 +01:00
|
|
|
from sugar.graphics.menuicon import MenuIcon
|
2006-12-13 22:36:05 +01:00
|
|
|
from view.clipboardmenu import ClipboardMenu
|
2006-11-15 13:56:19 +01:00
|
|
|
from sugar.activity import ActivityFactory
|
2006-12-13 22:36:05 +01:00
|
|
|
from sugar.clipboard import clipboardservice
|
2007-01-06 22:29:13 +01:00
|
|
|
from sugar import util
|
2006-11-15 13:56:19 +01:00
|
|
|
|
|
|
|
class ClipboardIcon(MenuIcon):
|
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
def __init__(self, menu_shell, object_id, name):
|
2007-01-05 21:13:46 +01:00
|
|
|
MenuIcon.__init__(self, menu_shell)
|
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
|
2006-12-04 20:12:24 +01:00
|
|
|
self.connect('activated', self._icon_activated_cb)
|
|
|
|
self._menu = None
|
|
|
|
|
|
|
|
def create_menu(self):
|
2007-01-05 21:13:46 +01:00
|
|
|
self._menu = ClipboardMenu(self._name, self._percent, self._preview)
|
2006-12-04 20:12:24 +01:00
|
|
|
self._menu.connect('action', self._popup_action_cb)
|
|
|
|
return self._menu
|
2006-11-15 13:56:19 +01:00
|
|
|
|
2007-01-05 21:13:46 +01:00
|
|
|
def set_state(self, name, percent, icon_name, preview):
|
|
|
|
self._name = name
|
2006-12-04 20:12:24 +01:00
|
|
|
self._percent = percent
|
2007-01-05 21:13:46 +01:00
|
|
|
self._preview = preview
|
|
|
|
self.set_icon_name(icon_name)
|
2006-12-04 20:12:24 +01:00
|
|
|
if self._menu:
|
2007-01-05 21:13:46 +01:00
|
|
|
self._menu.set_state(name, percent, preview)
|
|
|
|
|
|
|
|
def _get_activity_for_mime_type(self, mime_type):
|
|
|
|
# FIXME: We should use some kind of registry that could be extended by
|
|
|
|
# newly installed activities.
|
|
|
|
if mime_type == "application/pdf":
|
|
|
|
return "org.laptop.sugar.Xbook"
|
|
|
|
elif mime_type in ["application/msword", "text/rtf", "application/rtf"]:
|
|
|
|
return "org.laptop.AbiWordActivity"
|
|
|
|
else:
|
|
|
|
return None
|
2006-11-15 13:56:19 +01:00
|
|
|
|
2007-01-07 06:04:30 +01:00
|
|
|
def _activity_create_success_cb(self, handler, activity):
|
|
|
|
activity.start(util.unique_id())
|
|
|
|
activity.execute("open_document", [self._object_id])
|
|
|
|
|
|
|
|
def _activity_create_error_cb(self, handler, err):
|
|
|
|
pass
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _icon_activated_cb(self, icon):
|
2007-01-07 06:04:30 +01:00
|
|
|
if self._percent < 100:
|
|
|
|
return
|
2006-11-15 13:56:19 +01:00
|
|
|
|
2007-01-07 06:04:30 +01:00
|
|
|
cb_service = clipboardservice.get_instance()
|
|
|
|
(name, percent, icon, preview, format_types) = \
|
|
|
|
cb_service.get_object(self._object_id)
|
|
|
|
if not format_types:
|
|
|
|
return
|
|
|
|
|
|
|
|
logging.debug("_icon_activated_cb: " + self._object_id)
|
|
|
|
activity_type = self._get_activity_for_mime_type(format_types[0])
|
|
|
|
if not activity_type:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Launch the activity to handle this item
|
|
|
|
handler = ActivityFactory.create(activity_type)
|
|
|
|
handler.connect('success', self._activity_create_success_cb)
|
|
|
|
handler.connect('error', self._activity_create_error_cb)
|
2007-01-05 21:13:46 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _popup_action_cb(self, popup, action):
|
|
|
|
self.popdown()
|
|
|
|
|
|
|
|
if action == ClipboardMenu.ACTION_STOP_DOWNLOAD:
|
|
|
|
raise "Stopping downloads still not implemented."
|
|
|
|
elif action == ClipboardMenu.ACTION_DELETE:
|
2006-12-13 22:36:05 +01:00
|
|
|
cb_service = clipboardservice.get_instance()
|
|
|
|
cb_service.delete_object(self._object_id)
|
|
|
|
|
|
|
|
def get_object_id(self):
|
|
|
|
return self._object_id
|