18
17
# Boston, MA 02111-1307, USA.
24
from awn.extras import _
26
27
class GUITransfer(object):
30
def __init__(self, src, dst, options):
31
def __init__(self, src, dst, actions):
31
32
self.__progress = None
32
33
self.cancel = False
33
self.txt_operation = ""
34
self.txt_operation = _("Copying files")
34
35
self.label_under = None
35
if not (options & gnomevfs.XFER_LINK_ITEMS):
36
if (options & gnomevfs.XFER_REMOVESOURCE):
37
self.txt_operation = "Moving"
38
elif (options & gnomevfs.XFER_EMPTY_DIRECTORIES):
39
self.txt_operation = "Deleting"
41
self.txt_operation = "Copying"
42
self.dialog = gtk.Dialog(title=self.txt_operation + " files",
38
# force copying of non-local objects
39
if (actions & gtk.gdk.ACTION_LINK):
40
if src[0].get_path() is None:
41
actions = gtk.gdk.ACTION_COPY
43
if not (actions & gtk.gdk.ACTION_LINK):
44
if (actions & gtk.gdk.ACTION_MOVE):
45
self.txt_operation = _("Moving files")
47
self.txt_operation = _("Deleting files")
48
self.dialog = gtk.Dialog(title=self.txt_operation,
43
49
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT))
44
50
self.dialog.set_border_width(12)
45
51
self.dialog.set_has_separator(False)
46
52
self.dialog.vbox.set_spacing(2)
47
53
hbox_copy = gtk.HBox(False, 0)
48
54
label_copy = gtk.Label("")
49
label_copy.set_markup("<big><b>" + self.txt_operation + " files</b></big>\n")
55
label_copy.set_markup("<big><b>%s</b></big>\n" % self.txt_operation)
50
56
hbox_copy.pack_start(label_copy, False, False, 0)
51
57
self.dialog.vbox.add(hbox_copy)
52
58
hbox_info = gtk.HBox(False, 0)
53
59
label_fromto = gtk.Label("")
54
60
label_fromto.set_justify(gtk.JUSTIFY_RIGHT)
55
61
hbox_info.pack_start(label_fromto, False, False, 0)
56
srcdir = src[0].parent.path
62
srcdir = src[0].get_parent().get_uri()
58
64
label_fromto.set_markup("<b>From:</b>\n<b>To:</b>")
59
dstdir = dst[0].parent.path
65
dstdir = dst[0].get_parent().get_uri()
62
68
label_fromto.set_markup("<b>From:</b>\n")
79
85
self.status_label = gtk.Label()
80
86
self.dialog.vbox.add(self.status_label)
81
self.dialog.set_size_request(400,180)
87
self.dialog.set_size_request(400,-1)
82
88
self.dialog.connect("response", self._dialog_response)
84
self.handle = gnomevfs.async.xfer(
85
source_uri_list=src, target_uri_list=dst,
87
error_mode=gnomevfs.XFER_ERROR_MODE_ABORT,
88
overwrite_mode=gnomevfs.XFER_OVERWRITE_MODE_ABORT,
89
progress_update_callback=self.update_info_cb,
90
update_callback_data=options,
91
progress_sync_callback=None,
92
sync_callback_data=None
90
self.cancellable = gio.Cancellable()
92
def _copy_callback(file, result, items):
94
if file is None or file.copy_finish(result):
96
source, dest = items.pop()
97
self.label_under.set_markup(
98
"<i>%s %s</i>" % (self.txt_operation, str(source.get_basename())))
99
self.progress_bar.set_text(self.txt_operation + " " +
100
_("%d of %d") % (self.num_items - len(items), self.num_items))
101
source.copy_async(dest, _copy_callback, _copy_progress,
102
cancellable=self.cancellable, user_data=items)
110
def _copy_progress(current, total):
112
if current > 0 and total > 0:
113
fraction = float(current)/total
114
self.progress_bar.set_fraction(fraction)
117
# remove the whole directory
118
print "TODO: We should delete %s" % src
120
elif (actions & gtk.gdk.ACTION_MOVE):
121
# why the hell isn't there gio.File.move_async() ??
122
for item in zip(src, dst):
124
source.move(dest, cancellable=self.cancellable)
127
elif (actions & gtk.gdk.ACTION_LINK):
128
for item in zip(src, dst):
130
dest.make_symbolic_link(source.get_path())
133
else: # gtk.gdk.ACTION_COPY
134
items = zip(src, dst)
136
self.num_items = len(items)
137
_copy_callback(None, None, items)
95
139
# show dialog after 1 sec
96
140
gobject.timeout_add(1000, self._dialog_show)
144
self.dialog.destroy()
98
147
def _dialog_show(self):
99
148
if self.dialog: # and enough data still to be copied?
100
149
self.dialog.show_all()
102
152
def _dialog_response(self, dialog, response):
103
153
if response == gtk.RESPONSE_REJECT or \
104
154
response == gtk.RESPONSE_DELETE_EVENT:
155
self.cancellable.cancel()
108
158
def update_info_cb(self, _reserved, info, data):
109
159
if info.status == gnomevfs.XFER_PROGRESS_STATUS_VFSERROR:
110
160
uri = gnomevfs.URI(info.source_name)
168
219
def __init__(self, uri):
169
220
gobject.GObject.__init__(self)
170
if isinstance(uri, gnomevfs.URI):
221
if isinstance(uri, gio.File):
173
self.uri = gnomevfs.URI(uri.strip())
224
self.uri = gio.File(uri.strip())
226
def create_child(self, short_name):
227
if isinstance(short_name, gio.File):
228
short_name = short_name.get_basename()
229
return self.uri.get_child_for_display_name(short_name)
176
232
def equals(self, uri2):
177
return gnomevfs.uris_match(self.as_string(), uri2.as_string())
233
return self.uri.equal(uri2.as_uri())
180
236
def as_uri(self):
184
240
def as_string(self):
185
ustr = self.uri.scheme + "://"
186
if self.uri.user_name is not None:
187
ustr += self.uri.user_name
188
if self.uri.password is not None:
189
ustr += ":" + self.uri.password
191
if self.uri.host_name is not None:
192
ustr += self.uri.host_name
193
if self.uri.host_port > 0:
194
ustr += ":" + str(self.uri.host_port)
195
if self.uri.path is not None:
196
ustr += self.uri.path
241
return self.uri.get_uri()
200
244
class Monitor(gobject.GObject):
210
254
event_mapping = {
211
gnomevfs.MONITOR_EVENT_CREATED : "created",
212
gnomevfs.MONITOR_EVENT_DELETED : "deleted",
213
gnomevfs.MONITOR_EVENT_CHANGED : "changed",
214
gnomevfs.MONITOR_EVENT_METADATA_CHANGED : "changed"
255
gio.FILE_MONITOR_EVENT_CREATED : "created",
256
gio.FILE_MONITOR_EVENT_DELETED : "deleted",
257
gio.FILE_MONITOR_EVENT_CHANGED : "changed",
258
gio.FILE_MONITOR_EVENT_CHANGES_DONE_HINT: "changed",
259
gio.FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED : "changed"
222
267
assert isinstance(vfs_uri, VfsUri)
223
268
gobject.GObject.__init__(self)
224
269
self.vfs_uri = vfs_uri
225
type = gnomevfs.get_file_info(vfs_uri.as_uri(),
226
gnomevfs.FILE_INFO_DEFAULT |
227
gnomevfs.FILE_INFO_FOLLOW_LINKS).type
228
if type == gnomevfs.FILE_TYPE_DIRECTORY:
271
# FIXME: this might be unnecessary
272
self.type = vfs_uri.as_uri().query_file_type(0, gio.Cancellable())
274
if type == gio.FILE_TYPE_DIRECTORY:
229
275
self.monitor_type = gnomevfs.MONITOR_DIRECTORY
230
elif type == gnomevfs.FILE_TYPE_REGULAR:
276
elif type == gio.FILE_TYPE_REGULAR:
231
277
self.monitor_type = gnomevfs.MONITOR_FILE
233
raise gnomevfs.NotSupportedError
279
raise RuntimeError("Not Supported")
235
self.monitor = gnomevfs.monitor_add(
239
except gnomevfs.NotSupportedError:
282
self.monitor = self.vfs_uri.as_uri().monitor()
283
self.monitor.connect("changed", self._monitor_cb)
243
def _monitor_cb(self, monitor_uri, info_uri, event):
244
signal = self.event_mapping[event]
246
if self.monitor_type == gnomevfs.MONITOR_FILE:
247
self.emit(signal, self.vfs_uri)
249
self.emit(signal, VfsUri(info_uri))
287
def _monitor_cb(self, monitor, monitor_uri, other_uri, event):
290
signal = self.event_mapping[event]
294
if self.type == gio.FILE_TYPE_DIRECTORY:
295
self.emit(signal, VfsUri(monitor_uri))
297
self.emit(signal, self.vfs_uri)
254
gnomevfs.monitor_cancel(self.monitor)
301
self.monitor.cancel()
255
302
self.monitor = None