From ddcbcba3666676cfc496434983ccfd56ed60007a Mon Sep 17 00:00:00 2001
From: Gonzalo Odiard <godiard@sugarlabs.org>
Date: Mon, 25 Oct 2010 11:13:32 -0300
Subject: [PATCH 1/8] Change the logic for reading a writing files to preserve the type
The previous logic probably is from when write was used
to see the code of the activities.
Now i check the file mime_type and write according to it.
---
AbiWordActivity.py | 38 ++++++++++++++++++++++++++------------
1 files changed, 26 insertions(+), 12 deletions(-)
diff --git a/AbiWordActivity.py b/AbiWordActivity.py
index 4f585af..397c64c 100644
a
|
b
|
class AbiWordActivity (activity.Activity): |
427 | 427 | |
428 | 428 | def read_file(self, file_path): |
429 | 429 | logging.debug('AbiWordActivity.read_file: %s, mimetype: %s', file_path, self.metadata['mime_type']) |
430 | | if 'source' in self.metadata and self.metadata['source'] == '1': |
431 | | logger.debug('Opening file in view source mode') |
432 | | self.abiword_canvas.load_file('file://' + file_path, 'text/plain') |
| 430 | |
| 431 | if self.metadata['mime_type'] in ['text/plain', 'text/csv']: |
| 432 | logging.debug('Opening file in text mode') |
| 433 | self.abiword_canvas.load_file('file://' + file_path, 'text/plain') |
433 | 434 | else: |
434 | 435 | 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 |
435 | 436 | |
436 | 437 | def write_file(self, file_path): |
437 | 438 | logging.debug('AbiWordActivity.write_file') |
438 | 439 | |
439 | | # check if we have a default mimetype; if not, fall back to OpenDocument |
440 | | # also fallback if we know we cannot export in that format |
441 | | if 'mime_type' not in self.metadata or self.metadata['mime_type'] == '' or \ |
442 | | self.metadata['mime_type'] == 'application/msword': |
443 | | self.metadata['mime_type'] = 'application/vnd.oasis.opendocument.text' |
444 | | |
445 | | # if we were viewing the source of a file, |
446 | | # then always save as plain text |
| 440 | logging.error('AbiWordActivity.write_file: %s, mimetype: %s', |
| 441 | file_path, self.metadata) |
| 442 | # if we were viewing a text file save as plain text |
447 | 443 | actual_mimetype = self.metadata['mime_type'] |
448 | | if 'source' in self.metadata and self.metadata['source'] == '1': |
| 444 | if self.metadata['mime_type'] in ['text/plain', 'text/csv']: |
449 | 445 | logger.debug('Writing file as type source (text/plain)') |
450 | 446 | actual_mimetype = 'text/plain' |
| 447 | else: |
| 448 | # if not is a abiword file, fall back to OpenDocument |
| 449 | if self.metadata['mime_type'] not in ['application/x-abiword', |
| 450 | 'text/x-xml-abiword']: |
| 451 | actual_mimetype = 'application/vnd.oasis.opendocument.text' |
| 452 | # change the extension in the file name and the description |
| 453 | self._change_file_ext(self.metadata['title'], '.odt') |
| 454 | if 'description' in self.metadata: |
| 455 | self._change_file_ext(self.metadata['description'], '.odt') |
| 456 | |
| 457 | self.metadata['mime_type'] = actual_mimetype |
451 | 458 | |
452 | 459 | self.metadata['fulltext'] = self.abiword_canvas.get_content(extension_or_mimetype=".txt")[:3000] |
453 | 460 | self.abiword_canvas.save('file://' + file_path, actual_mimetype, '') |
| 461 | |
| 462 | def _change_file_ext(self, file_name, extension): |
| 463 | if file_name != None: |
| 464 | last_point_posi = file_name.rfind('.') |
| 465 | if last_point_posi > -1: |
| 466 | file_name = file_name[0:last_point_posi] + extension |
| 467 | return file_name |