~ubuntu-branches/ubuntu/maverick/awn-extras-applets/maverick

« back to all changes in this revision

Viewing changes to applets/unmaintained/stacks/stacks_vfs.py

  • Committer: Bazaar Package Importer
  • Author(s): Julien Lavergne
  • Date: 2010-08-29 14:29:52 UTC
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20100829142952-rhvuetyms9bv5uu7
Tags: upstream-0.4.0+bzr1372
ImportĀ upstreamĀ versionĀ 0.4.0+bzr1372

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
 
 
3
2
# Copyright (c) 2007 Timon ter Braak
4
3
#
5
4
# This library is free software; you can redistribute it and/or
18
17
# Boston, MA 02111-1307, USA.
19
18
 
20
19
import gobject
21
 
import gnomevfs
 
20
import gio
22
21
import gtk
23
22
import pango
24
 
import os
 
23
 
 
24
from awn.extras import _
 
25
 
25
26
 
26
27
class GUITransfer(object):
27
28
 
28
29
    dialog = None
29
30
 
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"
40
 
            else:
41
 
                self.txt_operation = "Copying"
42
 
            self.dialog = gtk.Dialog(title=self.txt_operation + " files",
 
36
        self.num_items = 1
 
37
 
 
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
 
42
 
 
43
        if not (actions & gtk.gdk.ACTION_LINK):
 
44
            if (actions & gtk.gdk.ACTION_MOVE):
 
45
                self.txt_operation = _("Moving files")
 
46
            elif (actions == 0):
 
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()
57
63
            if len(dst) > 0:
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()
60
66
            else:
61
67
                dstdir = ""
62
68
                label_fromto.set_markup("<b>From:</b>\n")
78
84
 
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)
83
89
 
84
 
        self.handle = gnomevfs.async.xfer(
85
 
            source_uri_list=src, target_uri_list=dst,
86
 
            xfer_options=options,
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
93
 
            )
 
90
        self.cancellable = gio.Cancellable()
 
91
 
 
92
        def _copy_callback(file, result, items):
 
93
            try:
 
94
                if file is None or file.copy_finish(result):
 
95
                    if len(items) > 0:
 
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)
 
103
                    else:
 
104
                        self._finish()
 
105
                else:
 
106
                    print "copy failed"
 
107
            except gio.Error:
 
108
                self._finish()
 
109
 
 
110
        def _copy_progress(current, total):
 
111
            if self.dialog:
 
112
                if current > 0 and total > 0:
 
113
                   fraction = float(current)/total
 
114
                   self.progress_bar.set_fraction(fraction)
 
115
 
 
116
        if actions == 0:
 
117
            # remove the whole directory
 
118
            print "TODO: We should delete %s" % src
 
119
 
 
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):
 
123
                source, dest = item
 
124
                source.move(dest, cancellable=self.cancellable)
 
125
            self._finish()
 
126
 
 
127
        elif (actions & gtk.gdk.ACTION_LINK):
 
128
            for item in zip(src, dst):
 
129
                source, dest = item
 
130
                dest.make_symbolic_link(source.get_path())
 
131
            self._finish()
 
132
 
 
133
        else: # gtk.gdk.ACTION_COPY
 
134
            items = zip(src, dst)
 
135
            items.reverse()
 
136
            self.num_items = len(items)
 
137
            _copy_callback(None, None, items)
94
138
 
95
139
        # show dialog after 1 sec
96
140
        gobject.timeout_add(1000, self._dialog_show)
97
141
 
 
142
    def _finish(self):
 
143
        if self.dialog:
 
144
            self.dialog.destroy()
 
145
            self.dialog = None
 
146
 
98
147
    def _dialog_show(self):
99
148
        if self.dialog: # and enough data still to be copied?
100
149
            self.dialog.show_all()
 
150
        return False
101
151
 
102
152
    def _dialog_response(self, dialog, response):
103
153
        if response == gtk.RESPONSE_REJECT or \
104
154
           response == gtk.RESPONSE_DELETE_EVENT:
105
 
            self.cancel = True
106
 
 
107
 
 
 
155
            self.cancellable.cancel()
 
156
 
 
157
    '''
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)
159
209
            # TODO: remove partial target?
160
210
            return 0
161
211
        return 1
 
212
    '''
162
213
 
163
214
 
164
215
class VfsUri(gobject.GObject):
167
218
 
168
219
    def __init__(self, uri):
169
220
        gobject.GObject.__init__(self)
170
 
        if isinstance(uri, gnomevfs.URI):
 
221
        if isinstance(uri, gio.File):
171
222
            self.uri = uri
172
223
        else:
173
 
            self.uri = gnomevfs.URI(uri.strip())
 
224
            self.uri = gio.File(uri.strip())
 
225
 
 
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)
174
230
 
175
231
 
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())
178
234
 
179
235
 
180
236
    def as_uri(self):
182
238
 
183
239
 
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 
190
 
            ustr += "@"
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
197
 
        return ustr
 
241
        return self.uri.get_uri()
198
242
 
199
243
 
200
244
class Monitor(gobject.GObject):
208
252
    }
209
253
 
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"
215
260
    }
216
261
 
217
262
    monitor = None
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:
 
270
 
 
271
        # FIXME: this might be unnecessary
 
272
        self.type = vfs_uri.as_uri().query_file_type(0, gio.Cancellable())
 
273
        '''
 
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
232
278
        else:
233
 
            raise gnomevfs.NotSupportedError
 
279
            raise RuntimeError("Not Supported")
 
280
        '''
234
281
        try:
235
 
            self.monitor = gnomevfs.monitor_add(
236
 
                    vfs_uri.as_string(),
237
 
                    self.monitor_type,
238
 
                    self._monitor_cb)
239
 
        except gnomevfs.NotSupportedError:
 
282
            self.monitor = self.vfs_uri.as_uri().monitor()
 
283
            self.monitor.connect("changed", self._monitor_cb)
 
284
        except Exception:
240
285
            return None
241
286
 
242
 
 
243
 
    def _monitor_cb(self, monitor_uri, info_uri, event):
244
 
        signal = self.event_mapping[event]
245
 
        if signal:
246
 
            if self.monitor_type == gnomevfs.MONITOR_FILE:
247
 
                self.emit(signal, self.vfs_uri)
248
 
            else:
249
 
                self.emit(signal, VfsUri(info_uri))
250
 
 
 
287
    def _monitor_cb(self, monitor, monitor_uri, other_uri, event):
 
288
        signal = None
 
289
        try:
 
290
            signal = self.event_mapping[event]
 
291
        except:
 
292
            return
 
293
 
 
294
        if self.type == gio.FILE_TYPE_DIRECTORY:
 
295
            self.emit(signal, VfsUri(monitor_uri))
 
296
        else:
 
297
            self.emit(signal, self.vfs_uri)
251
298
 
252
299
    def close(self):
253
300
        try: 
254
 
            gnomevfs.monitor_cancel(self.monitor)
 
301
            self.monitor.cancel()
255
302
            self.monitor = None
 
303
            self.vfs_uri = None
256
304
        except:
257
305
            return