#2695: Recognize text files as such.

master
Tomeu Vizoso 17 years ago
parent cace8c0a47
commit dfe8ff6806

@ -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

@ -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':

@ -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

Loading…
Cancel
Save