1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# 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
import os
import logging
import urlparse
from sugar import mime
from sugar.bundle.activitybundle import ActivityBundle
class ClipboardObject(object):
def __init__(self, object_path, name):
self._id = object_path
self._name = name
self._percent = 0
self._formats = {}
def destroy(self):
for format in self._formats.itervalues():
format.destroy()
def get_id(self):
return self._id
def get_name(self):
name = self._name
if not name:
name = mime.get_mime_description(self.get_mime_type())
if not name:
name = ''
return name
def get_icon(self):
return mime.get_mime_icon(self.get_mime_type())
def get_preview(self):
# TODO: should previews really be here?
#return self._get_type_info().get_preview()
return ''
def is_bundle(self):
# A bundle will have only one format.
if not self._formats:
return False
else:
return self._formats.keys()[0] in [ActivityBundle.MIME_TYPE,
ActivityBundle.DEPRECATED_MIME_TYPE]
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
def get_mime_type(self):
if not self._formats:
return ''
format = mime.choose_most_significant(self._formats.keys())
if format == 'text/uri-list':
data = self._formats['text/uri-list'].get_data()
uri = urlparse.urlparse(mime.split_uri_list(data)[0], 'file')
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)
return format
class Format(object):
def __init__(self, mime_type, data, on_disk):
self.owns_disk_data = False
self._type = mime_type
self._data = data
self._on_disk = on_disk
def destroy(self):
if self._on_disk:
uri = urlparse.urlparse(self._data)
if os.path.isfile(uri.path):
os.remove(uri.path)
def get_type(self):
return self._type
def get_data(self):
return self._data
def set_data(self, data):
self._data = data
def is_on_disk(self):
return self._on_disk
|