From 46ce4da62c5348bacb8ba2fde5b41bfacaca0bc8 Mon Sep 17 00:00:00 2001
From: Gonzalo Odiard <godiard@sugarlabs.org>
Date: Wed, 3 Nov 2010 16:48:16 -0300
Subject: [PATCH Write] 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
---
AbiWordActivity.py | 40 +++++++++++++++++++++-------------------
1 files changed, 21 insertions(+), 19 deletions(-)
diff --git a/AbiWordActivity.py b/AbiWordActivity.py
index 0f62523..fda1fdf 100644
a
|
b
|
from sugar.activity.widgets import ActivityToolbarButton |
37 | 37 | from sugar.activity.activity import get_bundle_path |
38 | 38 | |
39 | 39 | from sugar.presence import presenceservice |
| 40 | from sugar import mime |
40 | 41 | |
41 | 42 | from sugar.graphics.toolbarbox import ToolbarButton, ToolbarBox |
42 | 43 | from sugar.graphics.toggletoolbutton import ToggleToolButton |
… |
… |
class AbiWordActivity(activity.Activity): |
459 | 460 | def read_file(self, file_path): |
460 | 461 | logging.debug('AbiWordActivity.read_file: %s, mimetype: %s', |
461 | 462 | file_path, self.metadata['mime_type']) |
462 | | if 'source' in self.metadata and self.metadata['source'] == '1': |
463 | | logger.debug('Opening file in view source mode') |
| 463 | if self.metadata['mime_type'] in ['text/plain', 'text/csv'] or \ |
| 464 | 'text/plain' in mime.get_mime_parents(self.metadata['mime_type']): |
464 | 465 | self.abiword_canvas.load_file('file://' + file_path, 'text/plain') |
465 | 466 | else: |
466 | 467 | # we pass no mime/file type, let libabiword autodetect it, |
… |
… |
class AbiWordActivity(activity.Activity): |
468 | 469 | self.abiword_canvas.load_file('file://' + file_path, '') |
469 | 470 | |
470 | 471 | def write_file(self, file_path): |
471 | | logging.debug('AbiWordActivity.write_file') |
472 | | |
473 | | # check if we have a default mimetype; if not, |
474 | | # fall back to OpenDocument |
475 | | # also fallback if we know we cannot export in that format |
476 | | if 'mime_type' not in self.metadata or \ |
477 | | self.metadata['mime_type'] == '' or \ |
478 | | self.metadata['mime_type'] == 'application/msword': |
479 | | self.metadata['mime_type'] = \ |
480 | | 'application/vnd.oasis.opendocument.text' |
481 | | |
482 | | # if we were viewing the source of a file, |
483 | | # then always save as plain text |
484 | | actual_mimetype = self.metadata['mime_type'] |
485 | | if 'source' in self.metadata and self.metadata['source'] == '1': |
| 472 | logging.debug('AbiWordActivity.write_file: %s, mimetype: %s', |
| 473 | file_path, self.metadata['mime_type']) |
| 474 | # if we were editing a text file save as plain text |
| 475 | if self.metadata['mime_type'] in ['text/plain', 'text/csv'] or \ |
| 476 | 'text/plain' in mime.get_mime_parents(self.metadata['mime_type']): |
486 | 477 | logger.debug('Writing file as type source (text/plain)') |
487 | | actual_mimetype = 'text/plain' |
| 478 | self.abiword_canvas.save('file://' + file_path, 'text/plain', '') |
| 479 | else: |
| 480 | #if the file is new, save in .odt format |
| 481 | if self.metadata['mime_type'] == '': |
| 482 | self.metadata['mime_type'] = \ |
| 483 | 'application/vnd.oasis.opendocument.text' |
| 484 | |
| 485 | # Abiword can't save in .doc format, save in .rtf instead |
| 486 | if self.metadata['mime_type'] == 'application/msword': |
| 487 | self.metadata['mime_type'] = 'application/rtf' |
| 488 | |
| 489 | self.abiword_canvas.save('file://' + file_path, |
| 490 | self.metadata['mime_type'], '') |
488 | 491 | |
489 | 492 | self.metadata['fulltext'] = \ |
490 | 493 | self.abiword_canvas.get_content(extension_or_mimetype=".txt") \ |
491 | 494 | [:3000] |
492 | | self.abiword_canvas.save('file://' + file_path, actual_mimetype, '') |