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-03-14 05:50:06 +01:00
|
|
|
import os
|
2007-04-20 20:12:49 +02:00
|
|
|
import urlparse
|
2007-06-17 20:55:16 +02:00
|
|
|
import tempfile
|
|
|
|
from gettext import gettext as _
|
2007-01-05 21:13:46 +01:00
|
|
|
|
2007-03-14 13:32:05 +01:00
|
|
|
import gobject
|
|
|
|
|
2007-02-21 21:12:27 +01:00
|
|
|
from sugar.graphics.canvasicon import CanvasIcon
|
2006-12-13 22:36:05 +01:00
|
|
|
from view.clipboardmenu import ClipboardMenu
|
2007-02-23 13:09:33 +01:00
|
|
|
from sugar.graphics.xocolor import XoColor
|
2007-02-22 22:51:24 +01:00
|
|
|
from sugar.graphics import units
|
2007-02-24 14:35:31 +01:00
|
|
|
from sugar.graphics import color
|
2007-02-21 20:20:36 +01:00
|
|
|
from sugar.activity import activityfactory
|
2007-06-17 20:55:16 +02:00
|
|
|
from sugar.activity.bundle import Bundle
|
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
|
2007-06-17 20:55:16 +02:00
|
|
|
from sugar.datastore import datastore
|
|
|
|
from sugar.objects import mime
|
|
|
|
from sugar import profile
|
2006-11-15 13:56:19 +01:00
|
|
|
|
2007-02-21 21:12:27 +01:00
|
|
|
class ClipboardIcon(CanvasIcon):
|
2007-03-14 13:32:05 +01:00
|
|
|
__gtype_name__ = 'SugarClipboardIcon'
|
|
|
|
|
|
|
|
__gproperties__ = {
|
|
|
|
'selected' : (bool, None, None, False,
|
|
|
|
gobject.PARAM_READWRITE)
|
|
|
|
}
|
2006-11-15 13:56:19 +01:00
|
|
|
|
2007-02-21 21:12:27 +01:00
|
|
|
def __init__(self, popup_context, object_id, name):
|
|
|
|
CanvasIcon.__init__(self)
|
|
|
|
self._popup_context = popup_context
|
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-03-14 13:32:05 +01:00
|
|
|
self._selected = False
|
|
|
|
self._hover = False
|
2007-02-22 22:51:24 +01:00
|
|
|
self.props.box_width = units.grid_to_pixels(1)
|
|
|
|
self.props.box_height = units.grid_to_pixels(1)
|
|
|
|
self.props.scale = units.STANDARD_ICON_SCALE
|
2007-06-23 20:07:25 +02:00
|
|
|
self.props.xo_color = XoColor(profile.get_color().to_string())
|
2006-12-04 20:12:24 +01:00
|
|
|
self._menu = None
|
2007-03-14 13:32:05 +01:00
|
|
|
|
|
|
|
def do_set_property(self, pspec, value):
|
|
|
|
if pspec.name == 'selected':
|
|
|
|
self._set_selected(value)
|
|
|
|
self.emit_paint_needed(0, 0, -1, -1)
|
|
|
|
else:
|
|
|
|
CanvasIcon.do_set_property(self, pspec, value)
|
|
|
|
|
|
|
|
def do_get_property(self, pspec):
|
|
|
|
if pspec.name == 'selected':
|
|
|
|
return self._selected
|
|
|
|
else:
|
|
|
|
return CanvasIcon.do_get_property(self, pspec)
|
|
|
|
|
|
|
|
def _set_selected(self, selected):
|
|
|
|
self._selected = selected
|
|
|
|
if selected:
|
|
|
|
if not self._hover:
|
|
|
|
self.props.background_color = color.DESKTOP_BACKGROUND.get_int()
|
|
|
|
else:
|
|
|
|
self.props.background_color = color.TOOLBAR_BACKGROUND.get_int()
|
|
|
|
|
2007-02-21 21:12:27 +01:00
|
|
|
def get_popup(self):
|
2007-04-20 20:12:49 +02:00
|
|
|
cb_service = clipboardservice.get_instance()
|
|
|
|
obj = cb_service.get_object(self._object_id)
|
|
|
|
formats = obj['FORMATS']
|
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
self._menu = ClipboardMenu(self._name, self._percent, self._preview,
|
2007-04-20 20:12:49 +02:00
|
|
|
self._activity,
|
|
|
|
formats[0] == 'application/vnd.olpc-x-sugar')
|
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-02-21 21:12:27 +01:00
|
|
|
def get_popup_context(self):
|
|
|
|
return self._popup_context
|
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
def set_state(self, name, percent, icon_name, preview, activity):
|
2007-04-20 20:12:49 +02:00
|
|
|
cb_service = clipboardservice.get_instance()
|
|
|
|
obj = cb_service.get_object(self._object_id)
|
2007-06-07 13:07:25 +02:00
|
|
|
if obj['FORMATS'] and obj['FORMATS'][0] == 'application/vnd.olpc-x-sugar':
|
|
|
|
installable = True
|
|
|
|
else:
|
|
|
|
installable = False
|
2007-04-20 20:12:49 +02:00
|
|
|
|
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
|
|
|
|
self.set_property("icon_name", icon_name)
|
2007-04-11 16:14:58 +02:00
|
|
|
if self._menu:
|
2007-04-20 20:12:49 +02:00
|
|
|
self._menu.set_state(name, percent, preview, activity, installable)
|
2007-04-11 18:22:52 +02:00
|
|
|
|
2007-04-20 20:12:49 +02:00
|
|
|
if (activity or installable) and percent < 100:
|
2007-04-11 18:22:52 +02:00
|
|
|
self.props.xo_color = XoColor("#000000,#424242")
|
|
|
|
else:
|
2007-06-23 20:07:25 +02:00
|
|
|
self.props.xo_color = XoColor(profile.get_color().to_string())
|
2007-04-11 18:22:52 +02:00
|
|
|
|
|
|
|
def _open_file(self):
|
2007-04-20 20:12:49 +02:00
|
|
|
if self._percent < 100:
|
2007-04-11 18:22:52 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
# Get the file path
|
|
|
|
cb_service = clipboardservice.get_instance()
|
|
|
|
obj = cb_service.get_object(self._object_id)
|
|
|
|
formats = obj['FORMATS']
|
2007-04-20 20:12:49 +02:00
|
|
|
if len(formats) == 0:
|
|
|
|
return
|
2007-04-11 18:22:52 +02:00
|
|
|
|
2007-04-20 20:12:49 +02:00
|
|
|
if not self._activity and \
|
|
|
|
not formats[0] == 'application/vnd.olpc-x-sugar':
|
|
|
|
return
|
|
|
|
|
2007-06-17 20:55:16 +02:00
|
|
|
uri = cb_service.get_object_data(self._object_id, formats[0])['DATA']
|
2007-04-20 20:12:49 +02:00
|
|
|
if not uri.startswith('file://'):
|
|
|
|
return
|
|
|
|
|
2007-05-05 10:59:21 +02:00
|
|
|
(scheme, netloc, path, params, query, fragment) = urlparse.urlparse(uri)
|
2007-04-11 18:22:52 +02:00
|
|
|
|
2007-04-20 20:12:49 +02:00
|
|
|
# FIXME: would be better to check for format.onDisk
|
|
|
|
try:
|
|
|
|
path_exists = os.path.exists(path)
|
|
|
|
except TypeError:
|
|
|
|
path_exists = False
|
|
|
|
|
|
|
|
if path_exists:
|
|
|
|
if self._activity:
|
2007-04-11 18:22:52 +02:00
|
|
|
activityfactory.create_with_uri(self._activity, uri)
|
|
|
|
else:
|
2007-04-20 20:12:49 +02:00
|
|
|
self._install_xo(path)
|
|
|
|
else:
|
|
|
|
logging.debug("Clipboard item file path %s didn't exist" % path)
|
2007-01-11 23:57:06 +01:00
|
|
|
|
2007-02-21 21:12:27 +01:00
|
|
|
def _popup_action_cb(self, popup, menu_item):
|
|
|
|
action = menu_item.props.action_id
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
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)
|
2007-01-11 23:57:06 +01:00
|
|
|
elif action == ClipboardMenu.ACTION_OPEN:
|
|
|
|
self._open_file()
|
2007-06-17 20:55:16 +02:00
|
|
|
elif action == ClipboardMenu.ACTION_SAVE_TO_JOURNAL:
|
|
|
|
self._save_to_journal()
|
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
def get_object_id(self):
|
|
|
|
return self._object_id
|
2007-02-24 14:35:31 +01:00
|
|
|
|
|
|
|
def prelight(self, enter):
|
|
|
|
if enter:
|
2007-03-14 13:32:05 +01:00
|
|
|
self._hover = True
|
2007-02-24 14:35:31 +01:00
|
|
|
self.props.background_color = color.BLACK.get_int()
|
|
|
|
else:
|
2007-03-14 13:32:05 +01:00
|
|
|
self._hover = False
|
|
|
|
if self._selected:
|
|
|
|
self.props.background_color = color.DESKTOP_BACKGROUND.get_int()
|
|
|
|
else:
|
|
|
|
self.props.background_color = color.TOOLBAR_BACKGROUND.get_int()
|
2007-04-20 20:12:49 +02:00
|
|
|
|
|
|
|
def _install_xo(self, path):
|
2007-06-17 20:55:16 +02:00
|
|
|
bundle = Bundle(path)
|
|
|
|
if not bundle.is_installed():
|
|
|
|
bundle.install()
|
|
|
|
|
|
|
|
def _save_to_journal(self):
|
|
|
|
cb_service = clipboardservice.get_instance()
|
|
|
|
obj = cb_service.get_object(self._object_id)
|
|
|
|
|
|
|
|
if len(obj['FORMATS']) == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
if 'text/uri-list' in obj['FORMATS']:
|
|
|
|
data = cb_service.get_object_data(self._object_id, 'text/uri-list')
|
|
|
|
file_path = urlparse.urlparse(data['DATA']).path
|
|
|
|
mime_type = mime.get_for_file(file_path)
|
|
|
|
else:
|
|
|
|
# TODO: Find a way to choose the best mime-type from all the available.
|
|
|
|
mime_type = obj['FORMATS'][0]
|
|
|
|
|
|
|
|
data = cb_service.get_object_data(self._object_id, mime_type)
|
|
|
|
if data['ON_DISK']:
|
|
|
|
file_path = urlparse.urlparse(data['DATA']).path
|
|
|
|
else:
|
|
|
|
f, file_path = tempfile.mkstemp()
|
|
|
|
try:
|
|
|
|
os.write(f, data['data'])
|
|
|
|
finally:
|
|
|
|
os.close(f)
|
|
|
|
|
|
|
|
jobject = datastore.create()
|
|
|
|
jobject.metadata['title'] = _('Clipboard object: %s.') % obj['NAME']
|
|
|
|
jobject.metadata['keep'] = '0'
|
|
|
|
jobject.metadata['buddies'] = ''
|
|
|
|
jobject.metadata['preview'] = ''
|
|
|
|
jobject.metadata['icon-color'] = profile.get_color().to_string()
|
|
|
|
jobject.metadata['mime_type'] = mime_type
|
|
|
|
jobject.file_path = file_path
|
|
|
|
datastore.write(jobject)
|
|
|
|
|