[GTK] Fix drag and drop files in files_tab

Encoutered an error reordering files by dragging in the files tab:
   TypeError: can't pickle TreePath objects

The issue was get_selected_row  now returns a list of TreePath objects which
cannot be pickled. Also the set_text method only accept unicode text to
pickled bytes cannot be used.

The fix is to convert the TreePaths to strings and use json to encode
the list of strings for set_text.
This commit is contained in:
Calum Lind 2019-05-20 20:46:00 +01:00
commit 827987fe7d

View file

@ -9,10 +9,10 @@
from __future__ import division, unicode_literals from __future__ import division, unicode_literals
import json
import logging import logging
import os.path import os.path
import six.moves.cPickle as pickle # noqa: N813
from gi.repository import Gio, Gtk from gi.repository import Gio, Gtk
from gi.repository.Gdk import DragAction, ModifierType, keyval_name from gi.repository.Gdk import DragAction, ModifierType, keyval_name
from gi.repository.GObject import TYPE_UINT64 from gi.repository.GObject import TYPE_UINT64
@ -813,14 +813,14 @@ class FilesTab(Tab):
def _on_drag_data_get_data(self, treeview, context, selection, target_id, etime): def _on_drag_data_get_data(self, treeview, context, selection, target_id, etime):
paths = self.listview.get_selection().get_selected_rows()[1] paths = self.listview.get_selection().get_selected_rows()[1]
selection.set_text(pickle.dumps(paths, protocol=2)) selection.set_text(json.dumps([str(path) for path in paths]), -1)
def _on_drag_data_received_data( def _on_drag_data_received_data(
self, treeview, context, x, y, selection, info, etime self, treeview, context, x, y, selection, info, etime
): ):
try: try:
selected = pickle.loads(selection.get_data()) selected = json.loads(selection.get_data())
except pickle.UnpicklingError: except TypeError:
log.debug('Invalid selection data: %s', selection.get_data()) log.debug('Invalid selection data: %s', selection.get_data())
return return
log.debug('selection.data: %s', selected) log.debug('selection.data: %s', selected)