Use Gio.content_type_guess to identify mime type - Fixes #4715

Replace the use of a custom implentation in SugarExt

Signed-off-by: Gonzalo Odiard <godiard@sugarlabs.org>
This commit is contained in:
Gonzalo Odiard 2014-01-30 16:02:38 -03:00 committed by Daniel Narvaez
parent 3c46bf1430
commit faa0d42084

View File

@ -124,18 +124,22 @@ def get_for_file(file_name):
file_name = os.path.realpath(file_name) file_name = os.path.realpath(file_name)
mime_type = SugarExt.mime_get_mime_type_for_file(file_name, None) f = Gio.File.new_for_path(file_name)
if mime_type == 'application/octet-stream': try:
if _file_looks_like_text(file_name): info = f.query_info(Gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0, None)
return 'text/plain' mime_type = info.get_content_type()
else: except GLib.GError:
return 'application/octet-stream' mime_type = Gio.content_type_guess(file_name, None)[0]
return mime_type return mime_type
def get_from_file_name(file_name): def get_from_file_name(file_name):
return SugarExt.mime_get_mime_type_from_file_name(file_name) """
DEPRECATED: 0.102 (removed in 4 releases)
Use Gio.content_type_guess(file_name, None)[0] instead.
"""
return Gio.content_type_guess(file_name, None)[0]
def get_mime_icon(mime_type): def get_mime_icon(mime_type):
@ -258,26 +262,6 @@ def split_uri_list(uri_list):
return GLib.uri_list_extract_uris(uri_list) return GLib.uri_list_extract_uris(uri_list)
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:
unicode(sample, encoding)
return True
except Exception:
pass
return False
def _get_generic_type_for_mime(mime_type): def _get_generic_type_for_mime(mime_type):
for generic_type in _generic_types: for generic_type in _generic_types:
if mime_type in generic_type['types']: if mime_type in generic_type['types']: