Add extension to files that come into the clipboard without one.

This commit is contained in:
Tomeu Vizoso 2007-06-21 13:07:11 +02:00
parent 3ebb8f1291
commit 427e9a00d7
3 changed files with 29 additions and 5 deletions

View File

@ -97,11 +97,11 @@ class ClipboardObject:
if 'text/uri-list' in self._formats.keys():
data = self._formats['text/uri-list'].get_data()
uris = data.split('\n')
# TODO: could we do better when there are several uris?
uri = urlparse.urlparse(uris[0], 'file')
if uri.scheme == 'file':
logging.debug('Choosed %r!' % mime.get_for_file(uri.path))
return mime.get_for_file(uri.path)
if len(uris) == 1 or not uris[1]:
uri = urlparse.urlparse(uris[0], 'file')
if uri.scheme == 'file':
logging.debug('Choosed %r!' % mime.get_for_file(uri.path))
return mime.get_for_file(uri.path)
for mime_category in ['image/', 'text/', 'application/']:
for mime_type in self._formats.keys():

View File

@ -22,6 +22,7 @@ import hippo
import gtk
from sugar import util
from sugar.objects import mime
from view.clipboardicon import ClipboardIcon
from sugar.clipboard import clipboardservice
@ -104,6 +105,11 @@ class ClipboardBox(hippo.CanvasBox):
uri = urlparse.urlparse(uris[0])
path, file_name = os.path.split(uri.path)
root, ext = os.path.splitext(file_name)
if not ext or ext == '.':
mime_type = mime.get_for_file(uri.path)
file_name = root + '.' + mime.get_primary_extension(mime_type)
# Copy the file, as it will be deleted when the dnd operation finishes.
new_file_path = os.path.join(path, 'cb' + file_name)
shutil.copyfile(uri.path, new_file_path)

View File

@ -9,3 +9,21 @@ def get_for_file(file_name):
def get_from_file_name(file_name):
return _sugarext.get_mime_type_from_file_name(file_name)
_extensions_cache = {}
def get_primary_extension(mime_type):
if _extensions_cache.has_key(mime_type):
return _extensions_cache[mime_type]
f = open('/etc/mime.types')
while True:
line = f.readline()
cols = line.replace('\t', ' ').split(' ')
if mime_type == cols[0]:
for col in cols[1:]:
if col:
_extensions_cache[mime_type] = col
return col
_extensions_cache[mime_type] = None
return None