From 427e9a00d739452482f4e53bd0744702933c0d1a Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Thu, 21 Jun 2007 13:07:11 +0200 Subject: [PATCH] Add extension to files that come into the clipboard without one. --- services/clipboard/clipboardobject.py | 10 +++++----- shell/view/frame/clipboardbox.py | 6 ++++++ sugar/objects/mime.py | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/services/clipboard/clipboardobject.py b/services/clipboard/clipboardobject.py index faca452e..f4e66179 100644 --- a/services/clipboard/clipboardobject.py +++ b/services/clipboard/clipboardobject.py @@ -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(): diff --git a/shell/view/frame/clipboardbox.py b/shell/view/frame/clipboardbox.py index 1eb1684b..3bef447a 100644 --- a/shell/view/frame/clipboardbox.py +++ b/shell/view/frame/clipboardbox.py @@ -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) diff --git a/sugar/objects/mime.py b/sugar/objects/mime.py index b50ada40..0030e533 100644 --- a/sugar/objects/mime.py +++ b/sugar/objects/mime.py @@ -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