2007-06-22 21:47:30 +02:00
|
|
|
# Copyright (C) 2007, One Laptop Per Child
|
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library 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
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
# Boston, MA 02111-1307, USA.
|
|
|
|
|
2008-10-28 14:19:01 +01:00
|
|
|
"""
|
|
|
|
STABLE.
|
|
|
|
"""
|
|
|
|
|
2007-06-22 21:47:30 +02:00
|
|
|
import logging
|
2013-06-26 20:27:41 +02:00
|
|
|
import StringIO
|
|
|
|
import cairo
|
2007-06-22 21:47:30 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
from gi.repository import GObject
|
|
|
|
from gi.repository import Gtk
|
2011-12-15 02:23:18 +01:00
|
|
|
from gi.repository import Gdk
|
2007-12-03 12:42:21 +01:00
|
|
|
import dbus
|
2007-06-22 21:47:30 +02:00
|
|
|
|
2011-10-29 10:44:18 +02:00
|
|
|
from sugar3.datastore import datastore
|
2013-06-26 20:27:41 +02:00
|
|
|
from sugar3.activity.activity import PREVIEW_SIZE
|
2007-06-22 21:47:30 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2007-12-03 12:42:21 +01:00
|
|
|
J_DBUS_SERVICE = 'org.laptop.Journal'
|
|
|
|
J_DBUS_INTERFACE = 'org.laptop.Journal'
|
|
|
|
J_DBUS_PATH = '/org/laptop/Journal'
|
2007-09-03 21:26:30 +02:00
|
|
|
|
2013-06-18 17:23:00 +02:00
|
|
|
FILTER_TYPE_MIME_BY_ACTIVITY = 'mime_by_activity'
|
|
|
|
FILTER_TYPE_GENERIC_MIME = 'generic_mime'
|
|
|
|
FILTER_TYPE_ACTIVITY = 'activity'
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2013-06-26 20:27:41 +02:00
|
|
|
def get_preview_pixbuf(preview_data, width=-1, height=-1):
|
|
|
|
"""Retrive a pixbuf with the content of the preview field
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
metadata -- the metadata dictionary.
|
|
|
|
Can't be None, use metadata.get('preview', '')
|
|
|
|
width -- the pixbuf width, if is not set, the default width will be used
|
|
|
|
height -- the pixbuf width, if is not set, the default height will be used
|
|
|
|
|
|
|
|
Return: a Pixbuf or None if couldn't create it
|
|
|
|
|
|
|
|
"""
|
|
|
|
if width == -1:
|
|
|
|
width = PREVIEW_SIZE[0]
|
|
|
|
|
|
|
|
if height == -1:
|
|
|
|
height = PREVIEW_SIZE[1]
|
|
|
|
|
|
|
|
pixbuf = None
|
|
|
|
|
|
|
|
if len(preview_data) > 4:
|
|
|
|
if preview_data[1:4] != 'PNG':
|
|
|
|
# TODO: We are close to be able to drop this.
|
|
|
|
import base64
|
|
|
|
preview_data = base64.b64decode(preview_data)
|
|
|
|
|
|
|
|
png_file = StringIO.StringIO(preview_data)
|
|
|
|
try:
|
|
|
|
# Load image and scale to dimensions
|
|
|
|
surface = cairo.ImageSurface.create_from_png(png_file)
|
|
|
|
png_width = surface.get_width()
|
|
|
|
png_height = surface.get_height()
|
|
|
|
|
|
|
|
preview_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
|
|
|
|
width, height)
|
|
|
|
cr = cairo.Context(preview_surface)
|
|
|
|
|
|
|
|
scale_w = width * 1.0 / png_width
|
|
|
|
scale_h = height * 1.0 / png_height
|
|
|
|
scale = min(scale_w, scale_h)
|
|
|
|
|
|
|
|
cr.scale(scale, scale)
|
|
|
|
|
|
|
|
cr.set_source_rgba(1, 1, 1, 0)
|
|
|
|
cr.set_operator(cairo.OPERATOR_SOURCE)
|
|
|
|
cr.paint()
|
|
|
|
cr.set_source_surface(surface)
|
|
|
|
cr.paint()
|
|
|
|
|
|
|
|
pixbuf = Gdk.pixbuf_get_from_surface(preview_surface, 0, 0,
|
|
|
|
width, height)
|
|
|
|
except Exception:
|
|
|
|
logging.exception('Error while loading the preview')
|
|
|
|
|
|
|
|
return pixbuf
|
|
|
|
|
|
|
|
|
2007-12-03 12:42:21 +01:00
|
|
|
class ObjectChooser(object):
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2013-07-04 17:31:34 +02:00
|
|
|
def __init__(self, parent=None, what_filter=None, filter_type=None,
|
|
|
|
show_preview=False):
|
2013-06-18 17:23:00 +02:00
|
|
|
"""Initialise the ObjectChoser
|
|
|
|
|
|
|
|
parent -- the widget calling ObjectChooser
|
|
|
|
|
|
|
|
what_filter -- string
|
|
|
|
|
|
|
|
string should be an activity bundle_id or a generic mime
|
|
|
|
type as defined in mime.py used to determine which objects
|
|
|
|
will be presented in the object chooser
|
|
|
|
|
|
|
|
filter_type -- string
|
|
|
|
|
|
|
|
string should be one of [None, FILTER_TYPE_GENERIC_MIME,
|
|
|
|
FILTER_TYPE_ACTIVITY, FILTER_TYPE_MIME_BY_ACTIVITY]
|
|
|
|
|
|
|
|
If filter_type is None, the default behavior of the
|
|
|
|
what_filter is applied (for backward compatibility),
|
|
|
|
this option is DEPRECATED.
|
|
|
|
|
|
|
|
If filter_type is FILTER_TYPE_GENERIC_MIME, the
|
|
|
|
what_filter should be a generic mime type defined in
|
|
|
|
mime.py; the object chooser will filter based in the
|
|
|
|
'mime_type' metadata field.
|
|
|
|
|
|
|
|
If filter_type is FILTER_TYPE_ACTIVITY, the what_filter
|
|
|
|
should by an activity bundle_id; the object chooser
|
|
|
|
will filter based in the 'activity' metadata field.
|
|
|
|
|
|
|
|
If filter_type is FILTER_TYPE_MIME_BY_ACTIVITY, the
|
|
|
|
what_filter should be an activity bundle_id; the object
|
|
|
|
chooser will filter based on the 'mime_type' metadata
|
|
|
|
field and the mime types defined by the activity in the
|
|
|
|
activity.info file.
|
2013-07-04 17:31:34 +02:00
|
|
|
|
|
|
|
show_preview -- boolean
|
|
|
|
|
|
|
|
if True will show the preview image asociated with
|
|
|
|
the object in the Journal. This option is only available
|
|
|
|
if filter_type is selected.
|
2013-06-18 17:23:00 +02:00
|
|
|
"""
|
|
|
|
|
2007-12-03 12:42:21 +01:00
|
|
|
if parent is None:
|
|
|
|
parent_xid = 0
|
2011-11-15 21:32:03 +01:00
|
|
|
elif hasattr(parent, 'get_window') and hasattr(parent.get_window(),
|
2012-05-19 22:43:16 +02:00
|
|
|
'get_xid'):
|
|
|
|
parent_xid = parent.get_window().get_xid()
|
2007-12-03 12:42:21 +01:00
|
|
|
else:
|
|
|
|
parent_xid = parent
|
|
|
|
|
|
|
|
self._parent_xid = parent_xid
|
2013-07-04 17:31:34 +02:00
|
|
|
self._show_preview = show_preview
|
2007-12-03 12:42:21 +01:00
|
|
|
self._main_loop = None
|
|
|
|
self._object_id = None
|
|
|
|
self._bus = None
|
|
|
|
self._chooser_id = None
|
2011-11-15 19:29:07 +01:00
|
|
|
self._response_code = Gtk.ResponseType.NONE
|
2009-01-06 19:17:48 +01:00
|
|
|
self._what_filter = what_filter
|
2013-06-18 17:23:00 +02:00
|
|
|
if filter_type is not None:
|
|
|
|
# verify is one of the availables types
|
|
|
|
# add here more types if needed
|
|
|
|
if filter_type not in [FILTER_TYPE_MIME_BY_ACTIVITY,
|
|
|
|
FILTER_TYPE_GENERIC_MIME,
|
|
|
|
FILTER_TYPE_ACTIVITY]:
|
|
|
|
raise Exception('filter_type not implemented')
|
|
|
|
|
|
|
|
self._filter_type = filter_type
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-12-03 12:42:21 +01:00
|
|
|
def run(self):
|
|
|
|
self._object_id = None
|
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
self._main_loop = GObject.MainLoop()
|
2007-12-03 12:42:21 +01:00
|
|
|
|
|
|
|
self._bus = dbus.SessionBus(mainloop=self._main_loop)
|
|
|
|
self._bus.add_signal_receiver(
|
2013-05-18 04:27:04 +02:00
|
|
|
self.__name_owner_changed_cb,
|
|
|
|
signal_name='NameOwnerChanged',
|
|
|
|
dbus_interface='org.freedesktop.DBus',
|
|
|
|
arg0=J_DBUS_SERVICE)
|
2007-12-03 12:42:21 +01:00
|
|
|
|
|
|
|
obj = self._bus.get_object(J_DBUS_SERVICE, J_DBUS_PATH)
|
|
|
|
journal = dbus.Interface(obj, J_DBUS_INTERFACE)
|
|
|
|
journal.connect_to_signal('ObjectChooserResponse',
|
|
|
|
self.__chooser_response_cb)
|
|
|
|
journal.connect_to_signal('ObjectChooserCancelled',
|
|
|
|
self.__chooser_cancelled_cb)
|
2009-01-06 13:30:20 +01:00
|
|
|
|
2009-01-06 19:17:48 +01:00
|
|
|
if self._what_filter is None:
|
|
|
|
what_filter = ''
|
2009-01-06 13:30:20 +01:00
|
|
|
else:
|
2009-01-06 19:17:48 +01:00
|
|
|
what_filter = self._what_filter
|
2009-01-06 13:30:20 +01:00
|
|
|
|
2013-06-24 07:14:57 +02:00
|
|
|
if self._filter_type is None:
|
|
|
|
self._chooser_id = journal.ChooseObject(
|
|
|
|
self._parent_xid, what_filter)
|
|
|
|
else:
|
|
|
|
self._chooser_id = journal.ChooseObjectWithFilter(
|
2013-07-04 17:31:34 +02:00
|
|
|
self._parent_xid, what_filter, self._filter_type,
|
|
|
|
self._show_preview)
|
2007-12-03 12:42:21 +01:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
Gdk.threads_leave()
|
2007-12-03 12:42:21 +01:00
|
|
|
try:
|
|
|
|
self._main_loop.run()
|
|
|
|
finally:
|
2011-11-15 19:29:07 +01:00
|
|
|
Gdk.threads_enter()
|
2007-12-03 12:42:21 +01:00
|
|
|
self._main_loop = None
|
|
|
|
|
|
|
|
return self._response_code
|
2007-06-22 21:47:30 +02:00
|
|
|
|
|
|
|
def get_selected_object(self):
|
2007-12-03 12:42:21 +01:00
|
|
|
if self._object_id is None:
|
2007-06-22 21:47:30 +02:00
|
|
|
return None
|
|
|
|
else:
|
2007-12-03 12:42:21 +01:00
|
|
|
return datastore.get(self._object_id)
|
|
|
|
|
|
|
|
def destroy(self):
|
|
|
|
self._cleanup()
|
|
|
|
|
|
|
|
def _cleanup(self):
|
|
|
|
if self._main_loop is not None:
|
|
|
|
self._main_loop.quit()
|
|
|
|
self._main_loop = None
|
|
|
|
self._bus = None
|
|
|
|
|
|
|
|
def __chooser_response_cb(self, chooser_id, object_id):
|
|
|
|
if chooser_id != self._chooser_id:
|
|
|
|
return
|
2009-08-24 12:54:02 +02:00
|
|
|
logging.debug('ObjectChooser.__chooser_response_cb: %r', object_id)
|
2011-11-15 19:29:07 +01:00
|
|
|
self._response_code = Gtk.ResponseType.ACCEPT
|
2007-12-03 12:42:21 +01:00
|
|
|
self._object_id = object_id
|
|
|
|
self._cleanup()
|
|
|
|
|
|
|
|
def __chooser_cancelled_cb(self, chooser_id):
|
|
|
|
if chooser_id != self._chooser_id:
|
|
|
|
return
|
2009-08-24 12:54:02 +02:00
|
|
|
logging.debug('ObjectChooser.__chooser_cancelled_cb: %r', chooser_id)
|
2011-11-15 19:29:07 +01:00
|
|
|
self._response_code = Gtk.ResponseType.CANCEL
|
2007-12-03 12:42:21 +01:00
|
|
|
self._cleanup()
|
|
|
|
|
|
|
|
def __name_owner_changed_cb(self, name, old, new):
|
|
|
|
logging.debug('ObjectChooser.__name_owner_changed_cb')
|
|
|
|
# Journal service disappeared from the bus
|
2011-11-15 19:29:07 +01:00
|
|
|
self._response_code = Gtk.ResponseType.CANCEL
|
2007-12-03 12:42:21 +01:00
|
|
|
self._cleanup()
|