Be smarter when choosing one mime type from the targets offered.

master
Tomeu Vizoso 17 years ago
parent ddecddcb42
commit cc2b8884c0

@ -20,6 +20,7 @@ import urlparse
from sugar.objects import mime
from sugar import activity
from sugar import util
import objecttypeservice
@ -106,32 +107,21 @@ class ClipboardObject:
return self._formats
def get_mime_type(self):
logging.debug('Choosing between %r.' % self._formats.keys())
if not self._formats:
return ''
if 'text/uri-list' in self._formats.keys():
format = util.choose_most_significant_mime_type(self._formats.keys())
if format == 'text/uri-list':
data = self._formats['text/uri-list'].get_data()
uris = data.split('\n')
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():
if mime_type.startswith(mime_category) and \
not mime_type.split('/')[1].startswith('_'):
mime_type = mime_type.split(';')[0]
logging.debug('Choosed %r!' % mime_type)
return mime_type
if 'STRING' in self._formats.keys():
return 'text/plain'
format = mime.get_for_file(uri.path)
logging.debug('Returning first: %r.' % self._formats.keys()[0])
return self._formats.keys()[0]
return format
class Format:

@ -13,10 +13,12 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from gettext import gettext as _
import tempfile
import urlparse
import os
import logging
import gtk
import hippo
@ -31,6 +33,7 @@ from sugar.clipboard import clipboardservice
from sugar.datastore import datastore
from sugar.objects import mime
from sugar import profile
from sugar import util
class ClipboardMenu(Palette):
@ -151,18 +154,13 @@ class ClipboardMenu(Palette):
cb_service = clipboardservice.get_instance()
obj = cb_service.get_object(self._object_id)
if len(obj['FORMATS']) == 0:
return
format = util.choose_most_significant_mime_type(obj['FORMATS'])
data = cb_service.get_object_data(self._object_id, format)
if 'text/uri-list' in obj['FORMATS']:
data = cb_service.get_object_data(self._object_id, 'text/uri-list')
if format == 'text/uri-list':
file_path = urlparse.urlparse(data['DATA']).path
mime_type = mime.get_for_file(file_path)
else:
# TODO: Find a way to choose the best mime-type from all the available.
mime_type = obj['FORMATS'][0]
data = cb_service.get_object_data(self._object_id, mime_type)
if data['ON_DISK']:
file_path = urlparse.urlparse(data['DATA']).path
else:
@ -171,6 +169,7 @@ class ClipboardMenu(Palette):
os.write(f, data['DATA'])
finally:
os.close(f)
mime_type = format
jobject = datastore.create()
jobject.metadata['title'] = _('Clipboard object: %s.') % obj['NAME']

@ -22,6 +22,8 @@ import random
import binascii
import string
import os
import logging
from ConfigParser import ConfigParser
from ConfigParser import NoOptionError
@ -125,3 +127,26 @@ def set_proc_title(title):
return True
except:
return False
def choose_most_significant_mime_type(mime_types):
logging.debug('Choosing between %r.' % mime_types)
if not mime_types:
return ''
if 'text/uri-list' in mime_types:
return 'text/uri-list'
for mime_category in ['image/', 'text/', 'application/']:
for mime_type in mime_types:
if mime_type.startswith(mime_category) and \
not mime_type.split('/')[1].startswith('_'):
mime_type = mime_type.split(';')[0]
logging.debug('Choosed %r!' % mime_type)
return mime_type
if 'STRING' in mime_types:
return 'text/plain'
logging.debug('Returning first: %r.' % mime_types[0])
return mime_types[0]

Loading…
Cancel
Save