2007-06-24 13:10:53 +02:00
|
|
|
# Copyright (C) 2007, One Laptop Per Child
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2007-04-17 21:53:34 +02:00
|
|
|
import os
|
|
|
|
import logging
|
2007-06-17 14:24:46 +02:00
|
|
|
import urlparse
|
2007-04-17 21:53:34 +02:00
|
|
|
|
2007-06-20 21:28:00 +02:00
|
|
|
from sugar.objects import mime
|
|
|
|
|
|
|
|
import objecttypeservice
|
2007-08-08 18:08:07 +02:00
|
|
|
import bundleregistry
|
2007-04-11 18:22:52 +02:00
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
class ClipboardObject:
|
2007-04-11 18:22:52 +02:00
|
|
|
|
2007-03-14 05:50:06 +01:00
|
|
|
def __init__(self, object_path, name):
|
|
|
|
self._id = object_path
|
2006-12-13 22:36:05 +01:00
|
|
|
self._name = name
|
|
|
|
self._percent = 0
|
|
|
|
self._formats = {}
|
2007-03-14 05:50:06 +01:00
|
|
|
|
2007-04-17 21:53:34 +02:00
|
|
|
def destroy(self):
|
|
|
|
for type, format in self._formats.iteritems():
|
|
|
|
format.destroy()
|
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
def get_id(self):
|
|
|
|
return self._id
|
2007-01-05 21:13:46 +01:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
def _get_type_info(self):
|
2007-06-20 21:28:00 +02:00
|
|
|
logging.debug('_get_type_info')
|
|
|
|
type_registry = objecttypeservice.get_instance()
|
|
|
|
return type_registry.GetTypeForMIME(self.get_mime_type())
|
2007-04-11 18:22:52 +02:00
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
def get_name(self):
|
2007-04-11 18:22:52 +02:00
|
|
|
if self._name:
|
|
|
|
return self._name
|
|
|
|
else:
|
2007-06-20 21:28:00 +02:00
|
|
|
type_info = self._get_type_info()
|
|
|
|
if type_info:
|
|
|
|
return type_info['name']
|
|
|
|
else:
|
|
|
|
return ''
|
2007-04-11 18:22:52 +02:00
|
|
|
|
|
|
|
def get_icon(self):
|
2007-06-20 21:28:00 +02:00
|
|
|
type_info = self._get_type_info()
|
|
|
|
if type_info:
|
|
|
|
return type_info['icon']
|
|
|
|
else:
|
|
|
|
return ''
|
2007-04-11 18:22:52 +02:00
|
|
|
|
|
|
|
def get_preview(self):
|
2007-06-20 21:28:00 +02:00
|
|
|
# TODO: should previews really be here?
|
|
|
|
#return self._get_type_info().get_preview()
|
|
|
|
return ''
|
2007-04-11 18:22:52 +02:00
|
|
|
|
2007-08-15 12:19:29 +02:00
|
|
|
def get_activities(self):
|
2007-06-20 21:28:00 +02:00
|
|
|
mime = self.get_mime_type()
|
|
|
|
if not mime:
|
|
|
|
return ''
|
2007-08-08 18:08:07 +02:00
|
|
|
|
|
|
|
registry = bundleregistry.get_registry()
|
2007-06-20 21:28:00 +02:00
|
|
|
activities = registry.get_activities_for_type(self.get_mime_type())
|
|
|
|
if activities:
|
2007-08-15 12:19:29 +02:00
|
|
|
return [activity.get_service_name() for activity in activities]
|
2007-06-20 21:28:00 +02:00
|
|
|
else:
|
|
|
|
return ''
|
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
def get_percent(self):
|
|
|
|
return self._percent
|
|
|
|
|
|
|
|
def set_percent(self, percent):
|
|
|
|
self._percent = percent
|
|
|
|
|
|
|
|
def add_format(self, format):
|
|
|
|
self._formats[format.get_type()] = format
|
|
|
|
|
|
|
|
def get_formats(self):
|
|
|
|
return self._formats
|
2007-06-20 21:28:00 +02:00
|
|
|
|
|
|
|
def get_mime_type(self):
|
|
|
|
if not self._formats:
|
|
|
|
return ''
|
|
|
|
|
2007-07-14 13:53:49 +02:00
|
|
|
format = mime.choose_most_significant(self._formats.keys())
|
2007-09-26 10:09:26 +02:00
|
|
|
if format == 'text/uri-list':
|
2007-06-20 21:28:00 +02:00
|
|
|
data = self._formats['text/uri-list'].get_data()
|
2007-09-26 11:00:16 +02:00
|
|
|
uri = urlparse.urlparse(mime.split_uri_list(data)[0], 'file')
|
2007-09-17 02:39:02 +02:00
|
|
|
if uri.scheme == 'file':
|
|
|
|
if os.path.exists(uri.path):
|
|
|
|
format = mime.get_for_file(uri.path)
|
|
|
|
else:
|
|
|
|
format = mime.get_from_file_name(uri.path)
|
|
|
|
logging.debug('Choosed %r!' % format)
|
2007-06-20 21:48:15 +02:00
|
|
|
|
2007-07-03 22:42:17 +02:00
|
|
|
return format
|
2007-06-20 21:28:00 +02:00
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
class Format:
|
2007-04-11 18:22:52 +02:00
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
def __init__(self, type, data, on_disk):
|
2007-09-26 10:09:26 +02:00
|
|
|
self.owns_disk_data = False
|
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
self._type = type
|
|
|
|
self._data = data
|
|
|
|
self._on_disk = on_disk
|
|
|
|
|
2007-04-17 21:53:34 +02:00
|
|
|
def destroy(self):
|
|
|
|
if self._on_disk:
|
2007-06-17 14:24:46 +02:00
|
|
|
uri = urlparse.urlparse(self._data)
|
2007-07-20 20:45:22 +02:00
|
|
|
if os.path.isfile(uri.path):
|
|
|
|
os.remove(uri.path)
|
2007-04-17 21:53:34 +02:00
|
|
|
|
2006-12-13 22:36:05 +01:00
|
|
|
def get_type(self):
|
|
|
|
return self._type
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return self._data
|
2007-03-14 18:04:10 +01:00
|
|
|
|
2007-07-10 20:35:51 +02:00
|
|
|
def set_data(self, data):
|
2007-03-14 18:04:10 +01:00
|
|
|
self._data = data
|
|
|
|
|
2007-06-17 20:55:16 +02:00
|
|
|
def is_on_disk(self):
|
2007-03-14 18:04:10 +01:00
|
|
|
return self._on_disk
|