def __init__(self, url, destroy_after_fetching=True, cancelable=True): """Create an UrlFetchProgressBox Keyword arguments: url -- the URL to fetch destroy_after_fetching -- should this widget destroy itself once the URL is fetched? Defaults to True. cancelable -- whether to show cancel button. Defaults to True. """ gtk.HBox.__init__(self, False, 2) self.progressbar = gtk.ProgressBar() gobject.timeout_add(100, self.__tick) parts = [x for x in url.split("/") if x] self.progressbar.set_text(_("Downloading %s") % parts[-1]) self.running = True self.progressbar.show() self.pack_start(self.progressbar, True) self.destroy_after_fetching = destroy_after_fetching self.cancel_button = gtk.Button(stock=gtk.STOCK_CANCEL) if cancelable: self.cancel_button.show() self.cancel_button.set_sensitive(False) self.cancel_button.connect("clicked", self.__cancel) self.pack_end(self.cancel_button, False) self.cancel_button.set_sensitive(True) self.__canceller = gio.Cancellable() self.stream = gio.File(url) self.stream.load_contents_async(self.__download_finished, cancellable=self.__canceller)
def testLoadContents(self): self._f.write("testing load_contents") self._f.seek(0) c = gio.Cancellable() cont, leng, etag = self.file.load_contents(cancellable=c) self.assertEqual(cont, "testing load_contents") self.assertEqual(leng, 21) self.assertNotEqual(etag, '')
def download_catalogue(): def find_song_info(catalogue): for info in catalogue.infolist(): if info.filename.endswith("song_info.xml"): return info.filename return None def download_progress(complete, total): self.__load_progress = (complete, total) self.__notify_status_changed() def download_finished(uri, result): try: success = uri.copy_finish(result) except: success = False if not success: return # done downloading, unzip to real location catalog_zip = zipfile.ZipFile(magnatune_song_info_temp) catalog = open(magnatune_song_info, 'w') filename = find_song_info(catalog_zip) if filename is None: rb.error_dialog( title=_("Unable to load catalog"), message= _("Rhythmbox could not understand the Magnatune catalog, please file a bug." )) return catalog.write(catalog_zip.read(filename)) catalog.close() catalog_zip.close() dest.delete() self.__updating = False self.__catalogue_loader = None self.__notify_status_changed() load_catalogue() self.__updating = True dest = gio.File(magnatune_song_info_temp) self.__catalogue_loader = gio.Cancellable() try: # For some reason, gio.FILE_COPY_OVERWRITE doesn't work for copy_async dest.delete() except: pass magnatune_song_info_uri.copy_async( dest, download_finished, progress_callback=download_progress, flags=gio.FILE_COPY_OVERWRITE, cancellable=self.__catalogue_loader)
def __init__(self, applet, vfs_uri, icon_size): Backend.__init__(self, applet, icon_size) self.backend_uri = vfs_uri uri = self.backend_uri.as_uri() if not uri.query_exists(): # create folder try: uri.get_parent().make_directory_with_parents(gio.Cancellable()) except gio.Error: pass # create file self.handle = uri.create() else: self.handle = uri.append_to()
def testSetDisplayNameAsync(self): def callback(gfile, result): try: new_gfile = gfile.set_display_name_finish(result) new_name = new_gfile.get_basename() self.assertEqual(new_name, "new.txt") deleted = new_gfile.delete() self.assertEqual(deleted, True) finally: loop.quit() canc = gio.Cancellable() self.file.set_display_name_async("new.txt", callback, cancellable=canc) loop = glib.MainLoop() loop.run()
def testSetAttributesAsync(self): def callback(gfile, result): try: info = gfile.set_attributes_finish(result) usec = info.get_attribute_uint32("time::modified-usec") self.assertEqual(usec, 10) finally: loop.quit() info = gio.FileInfo() info.set_attribute_uint32("time::modified-usec", 10) canc = gio.Cancellable() self.file.set_attributes_async(info, callback) loop = glib.MainLoop() loop.run()
def __init__(self, func, msg, timeout_queue=TIMEOUT_WAITING_TURN, timeout_func=TIMEOUT_EXECUTION): self.__func = func self.__msg = msg self.__timeout_queue = timeout_queue self.__timeout_task = timeout_func self.__cancel = gio.Cancellable() self.__queue_event = threading.Event() self.__task_event = threading.Event() self.__abort_event = threading.Event() self.__state = self.STATE_IDLE self.__result = None self.__id = self.__new_id(self) self.__error = None self.__command = ""
def __init__(self, parent, url, target, finished_cb): """ Arguments: parent: a gtk.Window url: the URL to fetch target: a gio.File object at which to save the URL's contents finished_cb: a function taking three arguments: updater: this Updater object schedule: the parsed schedule, or None if download/parsing failed exc: the exception which occurred, or None if schedule is not None """ # Set up the widget gtk.Dialog.__init__(self, title='Downloading the latest schedule…', parent=parent, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)) self.__progress = gtk.ProgressBar() self.__progress.set_fraction(0.0) self.vbox.pack_start(self.__progress) self.connect('response', Updater.__response_cb) # Stash urls and things self.__source = gio.File(url) self.__temp = gio.File(target.get_path() + '.tmp') self.__target = target self.__finished_cb = finished_cb # Throb for a while until the file is actively being downloaded. self.__pulse_timeout = 0 self.__start_pulsing() # Kick off the actual copying. Maemo 5's pygobject doesn't have # copy_async, so we use a thread to basically re-implement # copy_async... self.__cancellable = gio.Cancellable() self.__download_thread = threading.Thread(target=self.__download, name='Downloader') self.__download_thread.daemon = True self.__download_thread.start()
def testLoadContentsAsync(self): self._f.write("testing load_contents_async") self._f.seek(0) def callback(contents, result): try: cont, leng, etag = contents.load_contents_finish(result) self.assertEqual(cont, "testing load_contents_async") self.assertEqual(leng, 27) self.assertNotEqual(etag, '') finally: loop.quit() canc = gio.Cancellable() self.file.load_contents_async(callback, cancellable=canc) loop = glib.MainLoop() loop.run()
def testReplaceContentsAsync(self): def callback(contents, result): try: newetag = contents.replace_contents_finish(result) cont, leng, etag = self.file.load_contents() self.assertEqual(cont, "testing replace_contents_async") self.assertEqual(leng, 30) self.assertEqual(etag, newetag) self.assertNotEqual(newetag, '') finally: loop.quit() canc = gio.Cancellable() self.file.replace_contents_async("testing replace_contents_async", callback, cancellable=canc) loop = glib.MainLoop() loop.run()
def __init__(self, vfs_uri): assert isinstance(vfs_uri, VfsUri) gobject.GObject.__init__(self) self.vfs_uri = vfs_uri # FIXME: this might be unnecessary self.type = vfs_uri.as_uri().query_file_type(0, gio.Cancellable()) ''' if type == gio.FILE_TYPE_DIRECTORY: self.monitor_type = gnomevfs.MONITOR_DIRECTORY elif type == gio.FILE_TYPE_REGULAR: self.monitor_type = gnomevfs.MONITOR_FILE else: raise RuntimeError("Not Supported") ''' try: self.monitor = self.vfs_uri.as_uri().monitor() self.monitor.connect("changed", self._monitor_cb) except Exception: return None
def __init__(self, file_uris, destination_uri, title, text=_("Saved %(count)s of %(total)s."), parent=None): self.file_uris = file_uris self.destination_uri = destination_uri self.cancel = gio.Cancellable() self.is_copying = False gtk.Dialog.__init__(self, title, parent) self.count = 0 self.total = len(file_uris) self.text = text self.overwrite_response = None #self.set_modal(True) #self.set_decorated(False) self.set_resizable(False) #self.set_focus_on_map(False) vbox = self.get_content_area() vbox.set_spacing(12) vbox.set_border_width(12) self._label = gtk.Label() self._label.set_use_markup(True) self._label.set_markup(self.text % {'count': 0, 'total': self.total}) vbox.pack_start(self._label) self._progress = gtk.ProgressBar() self._progress.set_size_request(300, -1) vbox.pack_start(self._progress) self.show_all() # TODO: Make dialog cancelable #self.cancel_button.connect('activate', lambda *e: self.cancel.cancel() ) self.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
def is_dir(self): filetype = self._gfile.query_file_type(gio.FILE_MONITOR_NONE, gio.Cancellable()) return filetype == gio.FILE_TYPE_DIRECTORY
str_uri = audio_dl_uri.get_uri() in_progress.replace_contents(str_uri, None, False, flags=gio.FILE_CREATE_PRIVATE | gio.FILE_CREATE_REPLACE_DESTINATION) shell = self.get_property('shell') manager = shell.get_player().get_property('ui-manager') manager.get_action("/MagnatuneSourceViewPopup/MagnatuneCancelDownload" ).set_sensitive(True) self.__downloads[str_uri] = (0, 0) # (current, total) cancel = gio.Cancellable() self.__cancellables[str_uri] = cancel try: # For some reason, gio.FILE_COPY_OVERWRITE doesn't work for copy_async dest.delete() except: pass # no way to resume downloads, sadly audio_dl_uri.copy_async(dest, download_finished, progress_callback=download_progress, flags=gio.FILE_COPY_OVERWRITE, cancellable=cancel) def cancel_downloads(self):
def __init__(self, onResult, onClear=None, onFinish=None): self._onResult = onResult self._onClear = onClear self._onFinish = onFinish self._enumerate = gio.Cancellable() self._userData = None
def _create(self): uri = self.backend_uri.as_uri() try: uri.make_directory_with_parents(gio.Cancellable()) except gio.Error: pass
def __init__(self, src, dst, actions): self.__progress = None self.cancel = False self.txt_operation = _("Copying files") self.label_under = None self.num_items = 1 # force copying of non-local objects if (actions & gtk.gdk.ACTION_LINK): if src[0].get_path() is None: actions = gtk.gdk.ACTION_COPY if not (actions & gtk.gdk.ACTION_LINK): if (actions & gtk.gdk.ACTION_MOVE): self.txt_operation = _("Moving files") elif (actions == 0): self.txt_operation = _("Deleting files") self.dialog = gtk.Dialog(title=self.txt_operation, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT)) self.dialog.set_border_width(12) self.dialog.set_has_separator(False) self.dialog.vbox.set_spacing(2) hbox_copy = gtk.HBox(False, 0) label_copy = gtk.Label("") label_copy.set_markup("<big><b>%s</b></big>\n" % self.txt_operation) hbox_copy.pack_start(label_copy, False, False, 0) self.dialog.vbox.add(hbox_copy) hbox_info = gtk.HBox(False, 0) label_fromto = gtk.Label("") label_fromto.set_justify(gtk.JUSTIFY_RIGHT) hbox_info.pack_start(label_fromto, False, False, 0) srcdir = src[0].get_parent().get_uri() if len(dst) > 0: label_fromto.set_markup("<b>From:</b>\n<b>To:</b>") dstdir = dst[0].get_parent().get_uri() else: dstdir = "" label_fromto.set_markup("<b>From:</b>\n") label_srcdst = gtk.Label("") label_srcdst.set_alignment(0.0, 0.5) label_srcdst.set_ellipsize(pango.ELLIPSIZE_START) label_srcdst.set_markup("%s\n%s" % (srcdir, dstdir)) hbox_info.pack_start(label_srcdst, True, True, 4) self.dialog.vbox.add(hbox_info) self.progress_bar = gtk.ProgressBar() self.dialog.vbox.add(self.progress_bar) hbox_under = gtk.HBox(False, 0) self.label_under = gtk.Label("") self.label_under.set_justify(gtk.JUSTIFY_LEFT) self.label_under.set_ellipsize(pango.ELLIPSIZE_MIDDLE) self.label_under.xalign = 0.0 hbox_under.pack_start(self.label_under, True, True, 0) self.dialog.vbox.add(hbox_under) self.status_label = gtk.Label() self.dialog.vbox.add(self.status_label) self.dialog.set_size_request(400, -1) self.dialog.connect("response", self._dialog_response) self.cancellable = gio.Cancellable() def _copy_callback(file, result, items): try: if file is None or file.copy_finish(result): if len(items) > 0: source, dest = items.pop() self.label_under.set_markup( "<i>%s %s</i>" % (self.txt_operation, str(source.get_basename()))) self.progress_bar.set_text( self.txt_operation + " " + _("%d of %d") % (self.num_items - len(items), self.num_items)) source.copy_async(dest, _copy_callback, _copy_progress, cancellable=self.cancellable, user_data=items) else: self._finish() else: print "copy failed" except gio.Error: self._finish() def _copy_progress(current, total): if self.dialog: if current > 0 and total > 0: fraction = float(current) / total self.progress_bar.set_fraction(fraction) def _delete_cb(src, result): try: en = src.enumerate_children_finish(result) fi = en.next_file(None) while fi != None: child = en.get_container().get_child(fi.get_name()) child.trash(None) fi = en.next_file(None) en.close(None) self._finish() except gio.Error: self._finish() if actions == 0: # remove the contents of directory print "We're deleting %s" % src[0] src[0].enumerate_children_async("standard::name", _delete_cb, cancellable=self.cancellable) elif (actions & gtk.gdk.ACTION_MOVE): # why the hell isn't there gio.File.move_async() ?? for item in zip(src, dst): source, dest = item source.move(dest, cancellable=self.cancellable) self._finish() elif (actions & gtk.gdk.ACTION_LINK): for item in zip(src, dst): source, dest = item dest.make_symbolic_link(source.get_path()) self._finish() else: # gtk.gdk.ACTION_COPY items = zip(src, dst) items.reverse() self.num_items = len(items) _copy_callback(None, None, items) # show dialog after 1 sec gobject.timeout_add(1000, self._dialog_show)
def __init__(self, source, dest): self._source = source._file self._dest = gio.File(dest) self._cancel_func = lambda: False self._cancellable = gio.Cancellable()
def make_directory_with_parents(self): self._gfile.make_directory_with_parents(gio.Cancellable())