29
31
from sugar3.datastore import datastore
32
from sugar3.activity.activity import PREVIEW_SIZE
32
35
J_DBUS_SERVICE = 'org.laptop.Journal'
33
36
J_DBUS_INTERFACE = 'org.laptop.Journal'
34
37
J_DBUS_PATH = '/org/laptop/Journal'
39
FILTER_TYPE_MIME_BY_ACTIVITY = 'mime_by_activity'
40
FILTER_TYPE_GENERIC_MIME = 'generic_mime'
41
FILTER_TYPE_ACTIVITY = 'activity'
44
def get_preview_pixbuf(preview_data, width=-1, height=-1):
45
"""Retrive a pixbuf with the content of the preview field
48
metadata -- the metadata dictionary.
49
Can't be None, use metadata.get('preview', '')
50
width -- the pixbuf width, if is not set, the default width will be used
51
height -- the pixbuf width, if is not set, the default height will be used
53
Return: a Pixbuf or None if couldn't create it
57
width = PREVIEW_SIZE[0]
60
height = PREVIEW_SIZE[1]
64
if len(preview_data) > 4:
65
if preview_data[1:4] != 'PNG':
66
# TODO: We are close to be able to drop this.
68
preview_data = base64.b64decode(preview_data)
70
png_file = StringIO.StringIO(preview_data)
72
# Load image and scale to dimensions
73
surface = cairo.ImageSurface.create_from_png(png_file)
74
png_width = surface.get_width()
75
png_height = surface.get_height()
77
preview_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
79
cr = cairo.Context(preview_surface)
81
scale_w = width * 1.0 / png_width
82
scale_h = height * 1.0 / png_height
83
scale = min(scale_w, scale_h)
85
cr.scale(scale, scale)
87
cr.set_source_rgba(1, 1, 1, 0)
88
cr.set_operator(cairo.OPERATOR_SOURCE)
90
cr.set_source_surface(surface)
93
pixbuf = Gdk.pixbuf_get_from_surface(preview_surface, 0, 0,
96
logging.exception('Error while loading the preview')
37
101
class ObjectChooser(object):
39
def __init__(self, parent=None, what_filter=None):
103
def __init__(self, parent=None, what_filter=None, filter_type=None,
105
"""Initialise the ObjectChoser
107
parent -- the widget calling ObjectChooser
109
what_filter -- string
111
string should be an activity bundle_id or a generic mime
112
type as defined in mime.py used to determine which objects
113
will be presented in the object chooser
115
filter_type -- string
117
string should be one of [None, FILTER_TYPE_GENERIC_MIME,
118
FILTER_TYPE_ACTIVITY, FILTER_TYPE_MIME_BY_ACTIVITY]
120
If filter_type is None, the default behavior of the
121
what_filter is applied (for backward compatibility),
122
this option is DEPRECATED.
124
If filter_type is FILTER_TYPE_GENERIC_MIME, the
125
what_filter should be a generic mime type defined in
126
mime.py; the object chooser will filter based in the
127
'mime_type' metadata field.
129
If filter_type is FILTER_TYPE_ACTIVITY, the what_filter
130
should by an activity bundle_id; the object chooser
131
will filter based in the 'activity' metadata field.
133
If filter_type is FILTER_TYPE_MIME_BY_ACTIVITY, the
134
what_filter should be an activity bundle_id; the object
135
chooser will filter based on the 'mime_type' metadata
136
field and the mime types defined by the activity in the
139
show_preview -- boolean
141
if True will show the preview image asociated with
142
the object in the Journal. This option is only available
143
if filter_type is selected.
40
146
if parent is None:
42
148
elif hasattr(parent, 'get_window') and hasattr(parent.get_window(),
61
177
self._bus = dbus.SessionBus(mainloop=self._main_loop)
62
178
self._bus.add_signal_receiver(
63
self.__name_owner_changed_cb,
64
signal_name='NameOwnerChanged',
65
dbus_interface='org.freedesktop.DBus',
179
self.__name_owner_changed_cb,
180
signal_name='NameOwnerChanged',
181
dbus_interface='org.freedesktop.DBus',
68
184
obj = self._bus.get_object(J_DBUS_SERVICE, J_DBUS_PATH)
69
185
journal = dbus.Interface(obj, J_DBUS_INTERFACE)