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

File 0001-Cancel-a-download-if-space-is-very-tight-SL-394.patch, 2.3 KB (added by humitos, 10 years ago)
  • downloadmanager.py

    From 289b0c6b7e390b4e32c3718b1f0f8d33b6735e9a Mon Sep 17 00:00:00 2001
    From: Manuel Kaufmann <humitos@gmail.com>
    Date: Mon, 10 Sep 2012 18:32:06 -0300
    Subject: [PATCH [v2] Browse] Cancel a download if space is very tight SL #394
    
    If there are less than 50Mb free on the Hard Disk the downloading
    process is canceled and this is informed to the user via an Alert.
    
    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 ab9fec2..b63f0fe 100644
    a b def remove_all_downloads(): 
    5555        download.cleanup()
    5656
    5757
     58# http://stackoverflow.com/questions/787776/find-free-disk-space-in-python-on-os-x
     59def free_space(path):
     60    s = os.statvfs(path)
     61    return (s.f_bavail * s.f_frsize) / 1024 / 1024
     62
     63
    5864class Download(object):
    5965    def __init__(self, download, browser):
    6066        self._download = download
    class Download(object): 
    8894
    8995    def __progress_change_cb(self, download, something):
    9096        progress = self._download.get_progress()
    91         self.dl_jobject.metadata['progress'] = str(int(progress * 100))
    92         datastore.write(self.dl_jobject)
     97
     98        # Check free space and if there is less than 50Mb, then cancel
     99        # the download
     100        if free_space('/') < 50:
     101            self.cancel()
     102
     103            self._canceled_alert = Alert()
     104            self._canceled_alert.props.title = _('Download canceled '
     105                                                  'because of Disk Space')
     106            self._canceled_alert.props.msg = \
     107                _('%s' % self._download.get_suggested_filename())
     108            ok_icon = Icon(icon_name='dialog-ok')
     109            self._canceled_alert.add_button(Gtk.ResponseType.OK,
     110                                             _('Ok'), ok_icon)
     111            ok_icon.show()
     112            self._canceled_alert.connect('response', self.__stop_response_cb)
     113            self._activity.add_alert(self._canceled_alert)
     114        else:
     115            self.dl_jobject.metadata['progress'] = str(int(progress * 100))
     116            datastore.write(self.dl_jobject)
    93117
    94118    def __state_change_cb(self, download, gparamspec):
    95119        state = self._download.get_status()