diff options
author | Tomeu Vizoso <tomeu@tomeuvizoso.net> | 2007-08-16 19:41:42 (GMT) |
---|---|---|
committer | Tomeu Vizoso <tomeu@tomeuvizoso.net> | 2007-08-16 19:41:42 (GMT) |
commit | dfe8ff68065d5191a2ade265432000327b0de7ce (patch) | |
tree | 39d59c774a5654fd303c301e36044af01db7ebe1 | |
parent | cace8c0a478065c4d7b815291974bc7510f5942b (diff) |
#2695: Recognize text files as such.
-rw-r--r-- | NEWS | 3 | ||||
-rw-r--r-- | shell/view/frame/clipboardbox.py | 2 | ||||
-rw-r--r-- | sugar/objects/mime.py | 28 |
3 files changed, 30 insertions, 3 deletions
@@ -1,6 +1,7 @@ +* #2695: Recognize text files as such. (tomeu) * #2669: Add a border to the inner of the frame. (marco) * #2703: Update macedonian translation. (ArangelAngov) -* #2543: Offer multiple activities for opening clipboard objects. +* #2543: Offer multiple activities for opening clipboard objects. (tomeu) Snapshot d93122bf5e diff --git a/shell/view/frame/clipboardbox.py b/shell/view/frame/clipboardbox.py index 60c154b..6c5870f 100644 --- a/shell/view/frame/clipboardbox.py +++ b/shell/view/frame/clipboardbox.py @@ -92,7 +92,7 @@ class ClipboardBox(hippo.CanvasBox): if not selection.data: return - logging.debug('ClipboardBox: adding type ' + selection.type + ' ' + selection.data) + logging.debug('ClipboardBox: adding type ' + selection.type) cb_service = clipboardservice.get_instance() if selection.type == 'text/uri-list': diff --git a/sugar/objects/mime.py b/sugar/objects/mime.py index 80eac9b..74e4e16 100644 --- a/sugar/objects/mime.py +++ b/sugar/objects/mime.py @@ -1,4 +1,5 @@ # Copyright (C) 2006-2007, Red Hat, Inc. +# Copyright (C) 2007, One Laptop Per Child # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -20,7 +21,13 @@ import logging from sugar import _sugarext def get_for_file(file_name): - return _sugarext.get_mime_type_for_file(file_name) + mime_type = _sugarext.get_mime_type_for_file(file_name) + if mime_type == 'application/octet-stream': + if _file_looks_like_text(file_name): + return 'text/plain' + else: + return 'application/octet-stream' + return mime_type def get_from_file_name(file_name): return _sugarext.get_mime_type_from_file_name(file_name) @@ -80,3 +87,22 @@ def choose_most_significant(mime_types): logging.debug('Returning first: %r.' % mime_types[0]) return mime_types[0] + +def _file_looks_like_text(file_name): + f = open(file_name, 'r') + try: + sample = f.read(256) + finally: + f.close() + + if '\000' in sample: + return False + + for encoding in ('ascii', 'latin_1', 'utf_8', 'utf_16'): + try: + string = unicode(sample, encoding) + return True + except Exception, e: + pass + + return False |