2007-06-17 20:55:16 +02:00
|
|
|
# Copyright (C) 2007, One Laptop Per Child
|
|
|
|
#
|
|
|
|
# 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-07-03 22:42:17 +02:00
|
|
|
|
2007-02-21 21:12:27 +01:00
|
|
|
from gettext import gettext as _
|
2007-07-01 12:55:10 +02:00
|
|
|
import tempfile
|
|
|
|
import urlparse
|
|
|
|
import os
|
2007-07-03 22:42:17 +02:00
|
|
|
import logging
|
2007-02-21 21:12:27 +01:00
|
|
|
|
2007-06-25 12:49:53 +02:00
|
|
|
import gtk
|
2006-11-15 13:56:19 +01:00
|
|
|
import hippo
|
|
|
|
|
2007-07-01 12:55:10 +02:00
|
|
|
from sugar.graphics.palette import Palette
|
2007-07-14 13:11:59 +02:00
|
|
|
from sugar.graphics.menuitem import MenuItem
|
2007-07-09 16:09:24 +02:00
|
|
|
from sugar.graphics import style
|
2007-07-01 12:55:10 +02:00
|
|
|
from sugar.clipboard import clipboardservice
|
|
|
|
from sugar.datastore import datastore
|
|
|
|
from sugar.objects import mime
|
|
|
|
from sugar import profile
|
2007-08-15 12:19:29 +02:00
|
|
|
from sugar import activity
|
2007-07-01 12:55:10 +02:00
|
|
|
|
|
|
|
class ClipboardMenu(Palette):
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-08-15 12:19:29 +02:00
|
|
|
def __init__(self, object_id, name, percent, preview, activities, installable):
|
2007-07-01 12:55:10 +02:00
|
|
|
Palette.__init__(self, name)
|
|
|
|
|
2007-07-14 13:11:59 +02:00
|
|
|
self.props.position = Palette.RIGHT
|
2007-07-01 12:55:10 +02:00
|
|
|
self._object_id = object_id
|
|
|
|
self._percent = percent
|
2007-08-15 12:19:29 +02:00
|
|
|
self._activities = activities
|
2006-12-14 13:50:42 +01:00
|
|
|
|
2007-07-06 14:36:59 +02:00
|
|
|
self.set_group_id('frame')
|
|
|
|
|
2007-07-14 13:11:59 +02:00
|
|
|
if percent < 100:
|
2007-06-25 12:49:53 +02:00
|
|
|
self._progress_bar = gtk.ProgressBar()
|
2007-07-01 12:55:10 +02:00
|
|
|
self._update_progress_bar()
|
|
|
|
|
|
|
|
self.set_content(self._progress_bar)
|
|
|
|
self._progress_bar.show()
|
2006-12-14 13:50:42 +01:00
|
|
|
else:
|
|
|
|
self._progress_bar = None
|
2007-02-21 21:12:27 +01:00
|
|
|
|
2007-07-01 12:55:10 +02:00
|
|
|
"""
|
2007-02-21 21:12:27 +01:00
|
|
|
if preview:
|
|
|
|
self._preview_text = hippo.CanvasText(text=preview,
|
|
|
|
size_mode=hippo.CANVAS_SIZE_WRAP_WORD)
|
|
|
|
self._preview_text.props.color = color.LABEL_TEXT.get_int()
|
2007-07-09 16:09:24 +02:00
|
|
|
self._preview_text.props.font_desc = \
|
|
|
|
style.FONT_NORMAL.get_pango_desc()
|
2007-02-21 21:12:27 +01:00
|
|
|
self.append(self._preview_text)
|
2007-07-01 12:55:10 +02:00
|
|
|
"""
|
|
|
|
|
2007-07-14 13:11:59 +02:00
|
|
|
self._remove_item = MenuItem(_('Remove'), 'stock-remove')
|
2007-07-01 12:55:10 +02:00
|
|
|
self._remove_item.connect('activate', self._remove_item_activate_cb)
|
2007-08-08 18:39:39 +02:00
|
|
|
self.menu.append(self._remove_item)
|
|
|
|
self._remove_item.show()
|
2007-07-01 12:55:10 +02:00
|
|
|
|
2007-07-14 13:11:59 +02:00
|
|
|
self._open_item = MenuItem(_('Open'), 'stock-keep')
|
2007-08-15 12:19:29 +02:00
|
|
|
self._open_item_activate_sid = self._open_item.connect('activate',
|
|
|
|
self._open_item_activate_cb)
|
2007-08-08 18:39:39 +02:00
|
|
|
self.menu.append(self._open_item)
|
|
|
|
self._open_item.show()
|
2007-07-01 12:55:10 +02:00
|
|
|
|
2007-07-14 13:11:59 +02:00
|
|
|
#self._stop_item = MenuItem(_('Stop download'), 'stock-close')
|
2007-07-01 12:55:10 +02:00
|
|
|
# TODO: Implement stopping downloads
|
|
|
|
#self._stop_item.connect('activate', self._stop_item_activate_cb)
|
2007-07-14 13:11:59 +02:00
|
|
|
#self.append_menu_item(self._stop_item)
|
2007-07-01 12:55:10 +02:00
|
|
|
|
2007-07-14 13:11:59 +02:00
|
|
|
self._journal_item = MenuItem(_('Add to journal'), 'document-save')
|
2007-07-01 12:55:10 +02:00
|
|
|
self._journal_item.connect('activate', self._journal_item_activate_cb)
|
2007-08-08 18:39:39 +02:00
|
|
|
self.menu.append(self._journal_item)
|
|
|
|
self._journal_item.show()
|
2007-07-01 12:55:10 +02:00
|
|
|
|
|
|
|
self._update_items_visibility(installable)
|
2007-08-15 12:19:29 +02:00
|
|
|
self._update_open_submenu()
|
|
|
|
|
|
|
|
def _update_open_submenu(self):
|
|
|
|
submenu = self._open_item.get_submenu()
|
|
|
|
if submenu:
|
|
|
|
for item in submenu.get_children():
|
|
|
|
submenu.remove(item)
|
|
|
|
|
|
|
|
if self._activities is None or len(self._activities) <= 1:
|
|
|
|
if self._open_item_activate_sid is None:
|
|
|
|
self._open_item_activate_sid = self._open_item.connect(
|
|
|
|
'activate',
|
|
|
|
self._open_item_activate_cb)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
if self._open_item_activate_sid is not None:
|
|
|
|
self._open_item.disconnect(self._open_item_activate_sid)
|
|
|
|
self._open_item_activate_sid = None
|
|
|
|
|
|
|
|
if not submenu:
|
|
|
|
submenu = gtk.Menu()
|
|
|
|
self._open_item.set_submenu(submenu)
|
|
|
|
submenu.show()
|
|
|
|
|
|
|
|
for service_name in self._activities:
|
|
|
|
registry = activity.get_registry()
|
|
|
|
activity_info = registry.get_activity(service_name)
|
|
|
|
|
|
|
|
if not activity_info:
|
|
|
|
logging.warning('Activity %s is unknown.' % service_name)
|
|
|
|
|
|
|
|
item = gtk.MenuItem(activity_info.name)
|
|
|
|
item.connect('activate', self._open_submenu_item_activate_cb, service_name)
|
|
|
|
submenu.append(item)
|
|
|
|
item.show()
|
2007-07-01 12:55:10 +02:00
|
|
|
|
|
|
|
def _update_items_visibility(self, installable):
|
2007-08-15 12:19:29 +02:00
|
|
|
if self._percent == 100 and (self._activities or installable):
|
2007-07-14 13:11:59 +02:00
|
|
|
self._remove_item.props.sensitive = True
|
|
|
|
self._open_item.props.sensitive = True
|
|
|
|
#self._stop_item.props.sensitive = False
|
|
|
|
self._journal_item.props.sensitive = True
|
2007-08-15 12:19:29 +02:00
|
|
|
elif self._percent == 100 and (not self._activities and not installable):
|
2007-07-14 13:11:59 +02:00
|
|
|
self._remove_item.props.sensitive = True
|
|
|
|
self._open_item.props.sensitive = False
|
|
|
|
#self._stop_item.props.sensitive = False
|
|
|
|
self._journal_item.props.sensitive = True
|
2006-12-04 20:12:24 +01:00
|
|
|
else:
|
2007-07-20 20:45:22 +02:00
|
|
|
self._remove_item.props.sensitive = True
|
2007-07-14 13:11:59 +02:00
|
|
|
self._open_item.props.sensitive = False
|
|
|
|
# TODO: reenable the stop item when we implement stoping downloads.
|
|
|
|
#self._stop_item.props.sensitive = True
|
|
|
|
self._journal_item.props.sensitive = False
|
|
|
|
|
|
|
|
if self._percent == 100:
|
|
|
|
self._progress_bar.hide()
|
|
|
|
else:
|
|
|
|
self._progress_bar.show()
|
2007-01-11 23:57:06 +01:00
|
|
|
|
2007-07-01 12:55:10 +02:00
|
|
|
def _update_progress_bar(self):
|
2007-06-25 12:49:53 +02:00
|
|
|
if self._progress_bar:
|
2007-07-01 12:55:10 +02:00
|
|
|
self._progress_bar.props.fraction = self._percent / 100.0
|
|
|
|
self._progress_bar.props.text = '%.2f %%' % self._percent
|
2007-06-25 12:49:53 +02:00
|
|
|
|
2007-08-15 12:19:29 +02:00
|
|
|
def set_state(self, name, percent, preview, activities, installable):
|
2007-07-01 12:55:10 +02:00
|
|
|
self.set_primary_text(name)
|
|
|
|
self._percent = percent
|
2007-08-15 12:19:29 +02:00
|
|
|
self._activities = activities
|
2006-12-14 13:50:42 +01:00
|
|
|
if self._progress_bar:
|
2007-07-01 12:55:10 +02:00
|
|
|
self._update_progress_bar()
|
2007-07-14 13:11:59 +02:00
|
|
|
self._update_items_visibility(installable)
|
2007-08-15 12:19:29 +02:00
|
|
|
self._update_open_submenu()
|
2007-07-01 12:55:10 +02:00
|
|
|
|
|
|
|
def _open_item_activate_cb(self, menu_item):
|
|
|
|
if self._percent < 100:
|
|
|
|
return
|
2007-07-11 11:39:40 +02:00
|
|
|
jobject = self._copy_to_journal()
|
2007-08-15 12:19:29 +02:00
|
|
|
jobject.resume(self._activities[0])
|
|
|
|
|
|
|
|
def _open_submenu_item_activate_cb(self, menu_item, service_name):
|
|
|
|
if self._percent < 100:
|
|
|
|
return
|
|
|
|
jobject = self._copy_to_journal()
|
|
|
|
jobject.resume(service_name)
|
2007-07-01 12:55:10 +02:00
|
|
|
|
|
|
|
def _remove_item_activate_cb(self, menu_item):
|
|
|
|
cb_service = clipboardservice.get_instance()
|
|
|
|
cb_service.delete_object(self._object_id)
|
|
|
|
|
|
|
|
def _journal_item_activate_cb(self, menu_item):
|
2007-07-11 11:39:40 +02:00
|
|
|
self._copy_to_journal()
|
|
|
|
|
|
|
|
def _copy_to_journal(self):
|
2007-07-01 12:55:10 +02:00
|
|
|
cb_service = clipboardservice.get_instance()
|
|
|
|
obj = cb_service.get_object(self._object_id)
|
|
|
|
|
2007-07-14 13:53:49 +02:00
|
|
|
format = mime.choose_most_significant(obj['FORMATS'])
|
2007-07-03 22:42:17 +02:00
|
|
|
data = cb_service.get_object_data(self._object_id, format)
|
2007-07-01 12:55:10 +02:00
|
|
|
|
2007-07-03 22:42:17 +02:00
|
|
|
if format == 'text/uri-list':
|
2007-07-01 12:55:10 +02:00
|
|
|
file_path = urlparse.urlparse(data['DATA']).path
|
|
|
|
mime_type = mime.get_for_file(file_path)
|
|
|
|
else:
|
|
|
|
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)
|
2007-07-03 22:42:17 +02:00
|
|
|
mime_type = format
|
2007-07-01 12:55:10 +02:00
|
|
|
|
|
|
|
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)
|
2007-07-11 11:39:40 +02:00
|
|
|
|
|
|
|
return jobject
|
2007-06-17 20:55:16 +02:00
|
|
|
|