From a2c8c1ef1084929b2fdd9b8bc0e933486b8895fb Mon Sep 17 00:00:00 2001 From: Gonzalo Odiard Date: Tue, 09 Nov 2010 14:25:29 +0000 Subject: Change the logic for preserve the mime type at reading and saving files Open the text files in plain/text format, save the new file like .odt files and preserve the mime type in all the cases but .doc files (abiword saves them like .rtf) Related to tickets SL #2127, OLPC #8972, OLPC #5291 and OLPC #1925 --- diff --git a/AbiWordActivity.py b/AbiWordActivity.py index 9e9ec84..5a9206d 100644 --- a/AbiWordActivity.py +++ b/AbiWordActivity.py @@ -34,6 +34,7 @@ import telepathy.client from sugar.activity.activity import Activity, ActivityToolbox, EditToolbar from sugar.presence import presenceservice from sugar.graphics import style +from sugar import mime from abiword import Canvas import toolbar @@ -325,30 +326,41 @@ class AbiWordActivity (Activity): def read_file(self, file_path): logging.debug('AbiWordActivity.read_file: %s, mimetype: %s', file_path, self.metadata['mime_type']) - if 'source' in self.metadata and self.metadata['source'] == '1': + mime_parents = mime.get_mime_parents(self.metadata['mime_type']) + if self.metadata['mime_type'] in ['text/plain', 'text/csv'] or \ + 'text/plain' in mime_parents: logger.debug('Opening file in view source mode') self.abiword_canvas.load_file('file://' + file_path, 'text/plain') else: - self.abiword_canvas.load_file('file://' + file_path, '') # we pass no mime/file type, let libabiword autodetect it, so we can handle multiple file formats + # we pass no mime/file type, let libabiword autodetect it, + # so we can handle multiple file formats + self.abiword_canvas.load_file('file://' + file_path, '') def write_file(self, file_path): - logging.debug('AbiWordActivity.write_file') - - # check if we have a default mimetype; if not, fall back to OpenDocument - # also fallback if we know we cannot export in that format - if 'mime_type' not in self.metadata or self.metadata['mime_type'] == '' or \ - self.metadata['mime_type'] == 'application/msword': - self.metadata['mime_type'] = 'application/vnd.oasis.opendocument.text' - - # if we were viewing the source of a file, - # then always save as plain text - actual_mimetype = self.metadata['mime_type']; - if 'source' in self.metadata and self.metadata['source'] == '1': + logging.debug('AbiWordActivity.write_file: %s, mimetype: %s', + file_path, self.metadata['mime_type']) + # if we were editing a text file save as plain text + mime_parents = mime.get_mime_parents(self.metadata['mime_type']) + if self.metadata['mime_type'] in ['text/plain', 'text/csv'] or \ + 'text/plain' in mime_parents: logger.debug('Writing file as type source (text/plain)') - actual_mimetype = 'text/plain' + self.abiword_canvas.save('file://' + file_path, 'text/plain', '') + else: + #if the file is new, save in .odt format + if self.metadata['mime_type'] == '': + self.metadata['mime_type'] = \ + 'application/vnd.oasis.opendocument.text' + + # Abiword can't save in .doc format, save in .rtf instead + if self.metadata['mime_type'] == 'application/msword': + self.metadata['mime_type'] = 'application/rtf' + + self.abiword_canvas.save('file://' + file_path, + self.metadata['mime_type'], '') - self.metadata['fulltext'] = self.abiword_canvas.get_content(extension_or_mimetype=".txt")[:3000] - self.abiword_canvas.save('file://' + file_path, actual_mimetype, ''); + self.metadata['fulltext'] = \ + self.abiword_canvas.get_content(extension_or_mimetype=".txt") \ + [:3000] def _selection_cb(self, abi, b): self._edit_toolbar.copy.set_sensitive(True) -- cgit v0.9.1