[Sugar-devel] [PATCH Browse] Cancel a download if space is very tight SL #394

Manuel Kaufmann humitos at gmail.com
Tue Sep 11 14:58:35 EDT 2012


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 at 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/downloadmanager.py
+++ b/downloadmanager.py
@@ -35,6 +35,8 @@ DS_DBUS_SERVICE = 'org.laptop.sugar.DataStore'
 DS_DBUS_INTERFACE = 'org.laptop.sugar.DataStore'
 DS_DBUS_PATH = '/org/laptop/sugar/DataStore'
 
+MIN_DISKFREE_AFTER_DOWNLOAD = 50 * 1024 * 1024
+
 _active_downloads = []
 _dest_to_window = {}
 
@@ -55,6 +57,11 @@ def remove_all_downloads():
         download.cleanup()
 
 
+def free_space(path):
+    s = os.statvfs(path)
+    return s.f_bavail * s.f_frsize
+
+
 class Download(object):
     def __init__(self, download, browser):
         self._download = download
@@ -84,8 +91,25 @@ class Download(object):
         os.close(fd)
         logging.debug('Download destination path: %s' % self._dest_path)
 
-        self._download.set_destination_uri('file://' + self._dest_path)
-        self._download.start()
+        # Check free space and if the downloaded file plus an extra
+        # space will fit on the disk. If not, cancel the download.
+        total_size = self._download.get_total_size()
+        logging.debug('Total size of the file: %s', total_size)
+        if free_space('/') - total_size < MIN_DISKFREE_AFTER_DOWNLOAD:
+            logging.debug('Download canceled because of Disk Space')
+            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._download.set_destination_uri('file://' + self._dest_path)
 
     def __progress_change_cb(self, download, something):
         progress = self._download.get_progress()
-- 
1.7.11.4



More information about the Sugar-devel mailing list