[Sugar-devel] [PATCH [v2] Browse] Cancel a download if space is very tight SL #394
Manuel Kaufmann
humitos at gmail.com
Mon Sep 10 17:47:29 EDT 2012
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 at 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/downloadmanager.py
+++ b/downloadmanager.py
@@ -55,6 +55,12 @@ def remove_all_downloads():
download.cleanup()
+# http://stackoverflow.com/questions/787776/find-free-disk-space-in-python-on-os-x
+def free_space(path):
+ s = os.statvfs(path)
+ return (s.f_bavail * s.f_frsize) / 1024 / 1024
+
+
class Download(object):
def __init__(self, download, browser):
self._download = download
@@ -88,8 +94,26 @@ class Download(object):
def __progress_change_cb(self, download, something):
progress = self._download.get_progress()
- self.dl_jobject.metadata['progress'] = str(int(progress * 100))
- datastore.write(self.dl_jobject)
+
+ # Check free space and if there is less than 50Mb, then cancel
+ # the download
+ if free_space('/') < 50:
+ self.cancel()
+
+ self._canceled_alert = Alert()
+ self._canceled_alert.props.title = _('Download canceled '
+ 'because of Disk Space')
+ self._canceled_alert.props.msg = \
+ _('%s' % self._download.get_suggested_filename())
+ ok_icon = Icon(icon_name='dialog-ok')
+ self._canceled_alert.add_button(Gtk.ResponseType.OK,
+ _('Ok'), ok_icon)
+ ok_icon.show()
+ self._canceled_alert.connect('response', self.__stop_response_cb)
+ self._activity.add_alert(self._canceled_alert)
+ else:
+ self.dl_jobject.metadata['progress'] = str(int(progress * 100))
+ datastore.write(self.dl_jobject)
def __state_change_cb(self, download, gparamspec):
state = self._download.get_status()
--
1.7.11.4
More information about the Sugar-devel
mailing list