[Sugar-devel] [PATCH] Change the logic used to determine the format used to save files.

godiard at sugarlabs.org godiard at sugarlabs.org
Thu Oct 14 14:10:14 EDT 2010


From: Gonzalo Odiard <godiard at sugarlabs.org>

Fix the tickets OLPC #5291, OLPC #1925, SL #2127
---
 AbiWordActivity.py |   36 +++++++++++++++++++++++-------------
 1 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/AbiWordActivity.py b/AbiWordActivity.py
index 7deab8c..3613ce5 100644
--- a/AbiWordActivity.py
+++ b/AbiWordActivity.py
@@ -423,28 +423,38 @@ class AbiWordActivity (activity.Activity):
         #self.abiword_canvas.invoke_cmd('com.abisource.abiword.abicollab.olpc.buddyLeft', self.participants[buddy.object_path()], 0, 0)
 
     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':
-            logger.debug('Opening file in view source mode')
-            self.abiword_canvas.load_file('file://' + file_path, 'text/plain') 
+        logging.debug('AbiWordActivity.read_file: %s, mimetype: %s',
+            file_path, self.metadata['mime_type'])
+        if self.metadata['mime_type'] in ['text/plain', 'text/csv']:
+            logging.debug('Opening file in text 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
 
     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
+        # if we were viewing a text file save as plain text
         actual_mimetype = self.metadata['mime_type']
-        if 'source' in self.metadata and self.metadata['source'] == '1':
+        if self.metadata['mime_type'] in ['text/plain', 'text/csv']:
             logger.debug('Writing file as type source (text/plain)')
             actual_mimetype = 'text/plain'
+        else:
+            # if not is a abiword file, fall back to OpenDocument
+            if self.metadata['mime_type'] not in ['application/x-abiword',
+                                                   'text/x-xml-abiword']:
+                actual_mimetype = 'application/vnd.oasis.opendocument.text'
+                # change the extension in the file name and the description
+                self._change_file_ext(self.metadata['title'], '.odt')
+                self._change_file_ext(self.metadata['description'], '.odt')
+
+        self.metadata['mime_type'] = actual_mimetype
 
         self.metadata['fulltext'] = self.abiword_canvas.get_content(extension_or_mimetype=".txt")[:3000]
         self.abiword_canvas.save('file://' + file_path, actual_mimetype, '')
+
+    def _change_file_ext(self, file_name, extension):
+        last_point_posi = file_name.rfind('.')
+        if last_point_posi > -1:
+            file_name = file_name[0:last_point_posi] + extension
+        return file_name
-- 
1.7.2.3



More information about the Sugar-devel mailing list