1 """ROX applications should provide good drag-and-drop support. Use this module
2 to allow drops onto widgets in your application."""
5 from rox
import g
, alert
, get_local_path
, _
12 def extract_uris(data
):
13 """Convert a text/uri-list to a python list of URIs"""
14 lines
= data
.split('\r\n')
18 continue # (gmc adds a '\0' line)
23 def provides(context
, type): return type in map(str, context
.targets
)
25 class RemoteFiles(Exception):
28 Exception.__init
__(self
, _('Cannot load files from a remote machine '
29 '(multiple files, or target application/octet-stream not provided)'))
32 """A mix-in class for widgets that can have files/data dropped on
33 them. Object should also be a GtkWidget."""
35 def __init__(self
, types
):
36 """Call this after initialising the widget.
37 Types is a list of MIME-types, or None to only accept files."""
39 targets
= [('text/uri-list', 0, TARGET_URILIST
)]
41 for type in types
+ ['application/octet-stream']:
42 targets
.append((type, 0, TARGET_RAW
))
44 self
.targets
= targets
45 self
.xds_proxy_for(self
)
47 def xds_proxy_for(self
, widget
):
48 "Handle drops on this widget as if they were to 'self'."
49 # (Konqueror requires ACTION_MOVE)
50 widget
.drag_dest_set(g
.DEST_DEFAULT_MOTION | g
.DEST_DEFAULT_HIGHLIGHT
,
52 gdk
.ACTION_COPY | gdk
.ACTION_MOVE | gdk
.ACTION_PRIVATE
)
54 widget
.connect('drag_data_received', self
.xds_data_received
)
56 def xds_data_received(self
, widget
, context
, x
, y
, selection
, info
, time
):
57 "Called when we get some data. Internal."
58 if info
== TARGET_RAW
:
59 self
.xds_load_from_selection(selection
)
61 if info
!= TARGET_URILIST
:
64 uris
= extract_uris(selection
.data
)
66 alert("Nothing to load!")
71 self
.xds_load_uris(uris
)
73 if len(uris
) != 1 or not provides(context
, 'application/octet-stream'):
75 widget
.drag_get_data(context
, 'application/octet-stream', time
)
77 rox
.report_exception()
81 def xds_load_uris(self
, uris
):
82 """Try to load each URI in the list. Override this if you can handle URIs
83 directly. The default method passes each local path to xds_load_from_file()
84 and displays an error for anything else."""
87 path
= get_local_path(uri
)
90 if len(paths
) < len(uris
):
93 self
.xds_load_from_file(path
)
95 def xds_load_from_file(self
, path
):
96 """Try to load this local file. Override this if you have a better way
97 to load files. The default method loads the file and calls xds_load_from_stream()."""
99 self
.xds_load_from_stream(path
, None, open(path
, 'rb'))
101 rox
.report_exception()
103 def xds_load_from_selection(self
, selection
):
104 """Try to load this selection (data from another application). The default
105 puts the data in a cStringIO and calls xds_load_from_stream()."""
106 from cStringIO
import StringIO
107 type = str(selection
.type)
108 self
.xds_load_from_stream(None, type, StringIO(selection
.data
))
110 def xds_load_from_stream(self
, name
, type, stream
):
111 """Called when we get any data sent via drag-and-drop in any way (local
112 file or remote application transfer). You should override this and do
113 something with the data. 'name' may be None (if the data is unnamed),
114 a leafname, or a full path or URI. 'type' is the MIME type, or None if
116 alert('Got some data, but missing code to handle it!\n\n(name="%s";type="%s")'