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' |
35 | 35 | DS_DBUS_INTERFACE = 'org.laptop.sugar.DataStore' |
36 | 36 | DS_DBUS_PATH = '/org/laptop/sugar/DataStore' |
37 | 37 | |
| 38 | MIN_DISKFREE_AFTER_DOWNLOAD = 50 * 1024 * 1024 |
| 39 | |
38 | 40 | _active_downloads = [] |
39 | 41 | _dest_to_window = {} |
40 | 42 | |
… |
… |
def remove_all_downloads(): |
55 | 57 | download.cleanup() |
56 | 58 | |
57 | 59 | |
| 60 | def free_space(path): |
| 61 | s = os.statvfs(path) |
| 62 | return s.f_bavail * s.f_frsize |
| 63 | |
| 64 | |
58 | 65 | class Download(object): |
59 | 66 | def __init__(self, download, browser): |
60 | 67 | self._download = download |
… |
… |
class Download(object): |
84 | 91 | os.close(fd) |
85 | 92 | logging.debug('Download destination path: %s' % self._dest_path) |
86 | 93 | |
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) |
89 | 113 | |
90 | 114 | def __progress_change_cb(self, download, something): |
91 | 115 | progress = self._download.get_progress() |