<div dir="ltr"><div>Actually if you for example:</div><div><br></div><div>selection.get_uris (* args, ** kwargs)</div><div><br></div><div>will return a list of uris in selection, so if no uris it will return an empty list, then do not even need to know what type of data is in selection.</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/4/4 Flavio Danesse <span dir="ltr"><<a href="mailto:fdanesse@gmail.com" target="_blank">fdanesse@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div>Manuel, I think this code can be improved and greatly simplified if instead of using things like:</div><div><br></div><div>selection_data.split uris = ('\ n')</div><div><br></div><div>use:</div>
<div><br></div><div>uris = selection.get_uris ()</div><div><br></div><div>Instead of using:</div><div><br></div><div>selection.get_data (* args, ** kwargs)</div><div><br></div><div>You can work directly on selection and get the data type with:</div>
<div><br></div><div>selection.get_data_type (* args, ** kwargs)</div><div><br></div><div>and get straight to the content:</div><div><br></div><div>selection.get_pixbuf (* args, ** kwargs)</div><div>selection.get_text (* args, ** kwargs)</div>
<div>selection.get_uris (* args, ** kwargs)</div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">2013/4/4 Manuel Quiñones <span dir="ltr"><<a href="mailto:manuq@laptop.org" target="_blank">manuq@laptop.org</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Note I have also applied this as a pull request to the github experiment:<br>
<br>
<a href="https://github.com/sugarlabs/sugar/pull/14" target="_blank">https://github.com/sugarlabs/sugar/pull/14</a><br>
<br>
2013/4/4 Manuel Quiñones <<a href="mailto:manuq@laptop.org" target="_blank">manuq@laptop.org</a>>:<br>
<div><div>> TestCase:<br>
><br>
> - open an activity like Browse, Read, Log (not Write, is a special case)<br>
> - bring up the frame<br>
> - drag things to the left side panel of the frame (images, links, or selected text)<br>
><br>
> The elements should be added to the clipboard which is inside the<br>
> panel. They should have the proper icon and they should popup a<br>
> palette to act on them.<br>
><br>
> Note: if the dragged object is a link from Browse, Sugar tries to copy<br>
> the html to the disk. But the current implementation looks wrong. I<br>
> have opened #4477 to track it: <a href="http://bugs.sugarlabs.org/ticket/4477" target="_blank">http://bugs.sugarlabs.org/ticket/4477</a><br>
><br>
> Note: this doesn't solve the inverse operation: drag from the<br>
> clipboard to the activity. This will be done in another patch.<br>
><br>
> API fixes:<br>
><br>
> - gtk.SelectionData.type -> Gtk.SelectionData.get_data_type() [1]<br>
> - gtk.SelectionData.data -> Gtk.SelectionData.get_data() [2]<br>
> - gtk.SelectionData.target -> Gtk.SelectionData.get_target() [3]<br>
> - context.targets -> context.list_targets() [4]<br>
> - context.drop_finish(...) -> Gdk.drop_finish(context, ...) [5]<br>
> - context.drag_status(...) -> Gdk.drag_status(context, ...) [6]<br>
> - context.get_source_widget() -> Gdk.drag_get_source_widget(context) [7]<br>
><br>
> [1] <a href="https://developer.gnome.org/gtk3/3.5/gtk3-Selections.html#gtk-selection-data-get-data-type" target="_blank">https://developer.gnome.org/gtk3/3.5/gtk3-Selections.html#gtk-selection-data-get-data-type</a><br>
> [2] <a href="https://developer.gnome.org/gtk3/3.5/gtk3-Selections.html#gtk-selection-data-get-data" target="_blank">https://developer.gnome.org/gtk3/3.5/gtk3-Selections.html#gtk-selection-data-get-data</a><br>
> [3] <a href="https://developer.gnome.org/gtk3/3.5/gtk3-Selections.html#gtk-selection-data-get-target" target="_blank">https://developer.gnome.org/gtk3/3.5/gtk3-Selections.html#gtk-selection-data-get-target</a><br>
> [4] <a href="https://developer.gnome.org/gdk/stable/gdk-Drag-and-Drop.html#gdk-drag-context-list-targets" target="_blank">https://developer.gnome.org/gdk/stable/gdk-Drag-and-Drop.html#gdk-drag-context-list-targets</a><br>
> [5] <a href="https://developer.gnome.org/gdk/stable/gdk-Drag-and-Drop.html#gdk-drop-finish" target="_blank">https://developer.gnome.org/gdk/stable/gdk-Drag-and-Drop.html#gdk-drop-finish</a><br>
> [6] <a href="https://developer.gnome.org/gdk/stable/gdk-Drag-and-Drop.html#gdk-drag-status" target="_blank">https://developer.gnome.org/gdk/stable/gdk-Drag-and-Drop.html#gdk-drag-status</a><br>
> [7] <a href="https://developer.gnome.org/gtk3/stable/gtk3-Drag-and-Drop.html#gtk-drag-get-source-widget" target="_blank">https://developer.gnome.org/gtk3/stable/gtk3-Drag-and-Drop.html#gtk-drag-get-source-widget</a><br>
><br>
> Cast the type of the Gtk.SelectionData from Gdk.Atom to str. Our code<br>
> treats it as str in sugar3.mime and asks methods like startswith which<br>
> fails if it is not a str-like object.<br>
><br>
> The data for the type 'text/uri-list' comes with a character '\x00' at<br>
> the end now. Remove it so our functions like<br>
> sugar3.mime.split_uri_list don't break.<br>
><br>
> Signed-off-by: Manuel Quiñones <<a href="mailto:manuq@laptop.org" target="_blank">manuq@laptop.org</a>><br>
> ---<br>
> src/jarabe/frame/clipboardtray.py | 46 +++++++++++++++++++++++++--------------<br>
> 1 file changed, 30 insertions(+), 16 deletions(-)<br>
><br>
> diff --git a/src/jarabe/frame/clipboardtray.py b/src/jarabe/frame/clipboardtray.py<br>
> index abc885e..5115d61 100644<br>
> --- a/src/jarabe/frame/clipboardtray.py<br>
> +++ b/src/jarabe/frame/clipboardtray.py<br>
> @@ -76,26 +76,39 @@ class ClipboardTray(tray.VTray):<br>
> return False<br>
><br>
> def _add_selection(self, object_id, selection):<br>
> - if not selection.data:<br>
> + if not selection.get_data():<br>
> return<br>
><br>
> - logging.debug('ClipboardTray: adding type %r', selection.type)<br>
> + selection_data = selection.get_data()<br>
> + selection_type = selection.get_data_type()<br>
> +<br>
> + # The type comes as a Gdk.Atom but we need it as str to<br>
> + # compare it.<br>
> + assert isinstance(selection_type, Gdk.Atom)<br>
> + selection_type = str(selection_type)<br>
> +<br>
> + logging.debug('ClipboardTray: adding type %r', selection_type)<br>
><br>
> cb_service = clipboard.get_instance()<br>
> - if selection.type == 'text/uri-list':<br>
> - uris = selection.data.split('\n')<br>
> + if selection_type == 'text/uri-list':<br>
> + # For 'text/uri-list' type, last character is '\x00'. This<br>
> + # wasn't the case in GTK2.<br>
> + assert selection_data[-1] == '\x00'<br>
> + selection_data = selection_data[:-1]<br>
> +<br>
> + uris = selection_data.split('\n')<br>
> if len(uris) > 1:<br>
> raise NotImplementedError('Multiple uris in text/uri-list' \<br>
> ' still not supported.')<br>
><br>
> cb_service.add_object_format(object_id,<br>
> - selection.type,<br>
> + selection_type,<br>
> uris[0],<br>
> on_disk=True)<br>
> else:<br>
> cb_service.add_object_format(object_id,<br>
> - selection.type,<br>
> - selection.data,<br>
> + selection_type,<br>
> + selection_data,<br>
> on_disk=False)<br>
><br>
> def _object_added_cb(self, cb_service, cb_object):<br>
> @@ -132,9 +145,9 @@ class ClipboardTray(tray.VTray):<br>
> logging.debug('ClipboardTray._drag_motion_cb')<br>
><br>
> if self._internal_drag(context):<br>
> - context.drag_status(Gdk.DragAction.MOVE, time)<br>
> + Gdk.drag_status(context, Gdk.DragAction.MOVE, time)<br>
> else:<br>
> - context.drag_status(Gdk.DragAction.COPY, time)<br>
> + Gdk.drag_status(context, Gdk.DragAction.COPY, time)<br>
> self.props.drag_active = True<br>
><br>
> return True<br>
> @@ -148,15 +161,16 @@ class ClipboardTray(tray.VTray):<br>
> if self._internal_drag(context):<br>
> # TODO: We should move the object within the clipboard here<br>
> if not self._context_map.has_context(context):<br>
> - context.drop_finish(False, Gtk.get_current_event_time())<br>
> + Gdk.drop_finish(context, False, Gtk.get_current_event_time())<br>
> return False<br>
><br>
> cb_service = clipboard.get_instance()<br>
> object_id = cb_service.add_object(name="")<br>
><br>
> - self._context_map.add_context(context, object_id, len(context.targets))<br>
> + context_targets = context.list_targets()<br>
> + self._context_map.add_context(context, object_id, len(context_targets))<br>
><br>
> - for target in context.targets:<br>
> + for target in context_targets:<br>
> if str(target) not in ('TIMESTAMP', 'TARGETS', 'MULTIPLE'):<br>
> widget.drag_get_data(context, target, time)<br>
><br>
> @@ -167,13 +181,13 @@ class ClipboardTray(tray.VTray):<br>
> def drag_data_received_cb(self, widget, context, x, y, selection,<br>
> targetType, time):<br>
> logging.debug('ClipboardTray: got data for target %r',<br>
> - selection.target)<br>
> + selection.get_target())<br>
><br>
> object_id = self._context_map.get_object_id(context)<br>
> try:<br>
> if selection is None:<br>
> logging.warn('ClipboardTray: empty selection for target %s',<br>
> - selection.target)<br>
> + selection.get_target())<br>
> else:<br>
> self._add_selection(object_id, selection)<br>
><br>
> @@ -181,10 +195,10 @@ class ClipboardTray(tray.VTray):<br>
> # If it's the last target to be processed, finish<br>
> # the dnd transaction<br>
> if not self._context_map.has_context(context):<br>
> - context.drop_finish(True, Gtk.get_current_event_time())<br>
> + Gdk.drop_finish(context, True, Gtk.get_current_event_time())<br>
><br>
> def _internal_drag(self, context):<br>
> - source_widget = context.get_source_widget()<br>
> + source_widget = Gtk.drag_get_source_widget(context)<br>
> if source_widget is None:<br>
> return False<br>
> view_ancestor = source_widget.get_ancestor(Gtk.Viewport)<br>
> --<br>
> 1.8.1.4<br>
><br>
<br>
<br>
<br>
</div></div><span><font color="#888888">--<br>
.. manuq ..<br>
</font></span><div><div>_______________________________________________<br>
Sugar-devel mailing list<br>
<a href="mailto:Sugar-devel@lists.sugarlabs.org" target="_blank">Sugar-devel@lists.sugarlabs.org</a><br>
<a href="http://lists.sugarlabs.org/listinfo/sugar-devel" target="_blank">http://lists.sugarlabs.org/listinfo/sugar-devel</a><br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>