Ticket #394: 0001-Cancel-a-download-if-space-is-very-tight-SL-394.2.patch

File 0001-Cancel-a-download-if-space-is-very-tight-SL-394.2.patch, 2.7 KB (added by humitos, 12 years ago)

v2 - improved

  • downloadmanager.py

    From 8a05c24f2eb0b219f7ba24763c178d9796b78cd2 Mon Sep 17 00:00:00 2001
    From: Manuel Kaufmann <humitos@gmail.com>
    Date: Tue, 11 Sep 2012 15:36:55 -0300
    Subject: [PATCH Browse] Cancel a download if space is very tight SL #394
    
    It checks if there will be more than MIN_DISKFREE_AFTER_DOWNLOAD (50Mb)
    after downloading the file. If not, Browse will cancel the download
    process before starting it and an Alert will be shown to the user to
    inform this situation.
    
    Signed-off-by: Manuel Kaufmann <humitos@gmail.com>
    ---
     downloadmanager.py | 28 ++++++++++++++++++++++++++--
     1 file changed, 26 insertions(+), 2 deletions(-)
    
    diff --git a/downloadmanager.py b/downloadmanager.py
    index 9950c16..0240941 100644
    a b DS_DBUS_SERVICE = 'org.laptop.sugar.DataStore' 
    3535DS_DBUS_INTERFACE = 'org.laptop.sugar.DataStore'
    3636DS_DBUS_PATH = '/org/laptop/sugar/DataStore'
    3737
     38MIN_DISKFREE_AFTER_DOWNLOAD = 50 * 1024 * 1024
     39
    3840_active_downloads = []
    3941_dest_to_window = {}
    4042
    def remove_all_downloads(): 
    5557        download.cleanup()
    5658
    5759
     60def free_space(path):
     61    s = os.statvfs(path)
     62    return s.f_bavail * s.f_frsize
     63
     64
    5865class Download(object):
    5966    def __init__(self, download, browser):
    6067        self._download = download
    class Download(object): 
    8491        os.close(fd)
    8592        logging.debug('Download destination path: %s' % self._dest_path)
    8693
    87         self._download.set_destination_uri('file://' + self._dest_path)
    88         self._download.start()
     94        # Check free space and if the downloaded file plus an extra
     95        # space will fit on the disk. If not, cancel the download.
     96        total_size = self._download.get_total_size()
     97        logging.debug('Total size of the file: %s', total_size)
     98        if free_space('/') - total_size < MIN_DISKFREE_AFTER_DOWNLOAD:
     99            logging.debug('Download canceled because of Disk Space')
     100            self._canceled_alert = Alert()
     101            self._canceled_alert.props.title = _('Download canceled '
     102                                                  'because of Disk Space')
     103            self._canceled_alert.props.msg = \
     104                _('%s' % self._download.get_suggested_filename())
     105            ok_icon = Icon(icon_name='dialog-ok')
     106            self._canceled_alert.add_button(Gtk.ResponseType.OK,
     107                                             _('Ok'), ok_icon)
     108            ok_icon.show()
     109            self._canceled_alert.connect('response', self.__stop_response_cb)
     110            self._activity.add_alert(self._canceled_alert)
     111        else:
     112            self._download.set_destination_uri('file://' + self._dest_path)
    89113
    90114    def __progress_change_cb(self, download, something):
    91115        progress = self._download.get_progress()