From 5be95aeb1158173e822a47f053240a3db36da083 Mon Sep 17 00:00:00 2001 From: Marc Maurer Date: Wed, 31 Oct 2007 18:11:08 +0000 Subject: First implementation of 4165: Write must expose UI to allow specifying output format (uwog) --- (limited to 'toolbar.py') diff --git a/toolbar.py b/toolbar.py index aafe549..73aa4db 100644 --- a/toolbar.py +++ b/toolbar.py @@ -17,6 +17,8 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA from gettext import gettext as _ import logging +import os +import time import abiword import gtk @@ -27,7 +29,10 @@ from sugar.graphics.toggletoolbutton import ToggleToolButton from sugar.graphics.combobox import ComboBox from sugar.graphics.toolcombobox import ToolComboBox from sugar.graphics.objectchooser import ObjectChooser +from sugar.activity.activity import ActivityToolbar from sugar.activity.activity import EditToolbar +from sugar.graphics.menuitem import MenuItem +from sugar.datastore import datastore logger = logging.getLogger('write-activity') @@ -39,6 +44,49 @@ TOOLBAR_IMAGE = 3 TOOLBAR_TABLE = 4 TOOLBAR_VIEW = 5 +class WriteActivityToolbarExtension: + + # file mime type, abiword exporter properties, drop down name, journal entry postfix + _EXPORT_FORMATS = [['application/rtf', _('Rich Text (RTF)'), _('RTF'), ""], + ['text/html', _('Hypertext (HTML)'), _('HTML'), "html4:yes; declare-xml:no; embed-css:yes; embed-images:yes;"], + ['text/plain', _('Plain Text (TXT)'), _('TXT'), ""]] + + def __init__(self, activity, toolbox, abiword_canvas): + + self._activity = activity + self._abiword_canvas = abiword_canvas + self._activity_toolbar = toolbox.get_activity_toolbar() + self._keep_palette = self._activity_toolbar.keep.get_palette() + + # hook up the export formats to the Keep button + for i, f in enumerate(self._EXPORT_FORMATS): + menu_item = MenuItem(f[1]) + menu_item.connect('activate', self._export_as_cb, f[0], f[2], f[3]) + self._keep_palette.menu.append(menu_item) + menu_item.show() + + def _export_as_cb(self, menu_item, mimetype, jpostfix, exp_props): + logger.debug('exporting file, mimetype: %s, exp_props: %s', mimetype, exp_props); + + # special case HTML export to set the activity name as the HTML title + if mimetype == "text/html": + exp_props += " title:" + self._activity.metadata['title'] + ';'; + + # create a new journal item + fileObject = datastore.create() + fileObject.metadata['title'] = self._activity.metadata['title'] + ' (' + jpostfix + ')'; + fileObject.metadata['mime_type'] = mimetype + fileObject.metadata['fulltext'] = self._abiword_canvas.get_content(extension_or_mimetype=".txt")[:3000] + + # write out the document contents in the requested format + fileObject.file_path = os.path.join(self._activity.get_activity_root(), 'data', '%i' % time.time()) + self._abiword_canvas.save('file://' + fileObject.file_path, mimetype, exp_props) + + # store the journal item + datastore.write(fileObject, transfer_ownership=True) + fileObject.destroy() + del fileObject + class WriteEditToolbar(EditToolbar): def __init__(self, toolbox, abiword_canvas, text_toolbar): -- cgit v0.9.1