[Sugar-devel] [PATCH Browse 1/2] Cancel a download if space is very tight SL #394
Manuel Kaufmann
humitos at gmail.com
Tue Sep 18 08:37:13 EDT 2012
It checks for enough space (using Activity.enough_space) before
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 | 63 +++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 48 insertions(+), 15 deletions(-)
diff --git a/downloadmanager.py b/downloadmanager.py
index 9950c16..5ad1a6f 100644
--- a/downloadmanager.py
+++ b/downloadmanager.py
@@ -74,16 +74,18 @@ class Download(object):
self._stop_alert = None
# figure out download URI
- temp_path = os.path.join(activity.get_activity_root(), 'instance')
- if not os.path.exists(temp_path):
- os.makedirs(temp_path)
+ self.temp_path = os.path.join(activity.get_activity_root(), 'instance')
+ if not os.path.exists(self.temp_path):
+ os.makedirs(self.temp_path)
- fd, self._dest_path = tempfile.mkstemp(dir=temp_path,
+ fd, self._dest_path = tempfile.mkstemp(dir=self.temp_path,
suffix=download.get_suggested_filename(),
prefix='tmp')
os.close(fd)
logging.debug('Download destination path: %s' % self._dest_path)
+ # We have to start the download to get 'total-size'
+ # property. It not, 0 is returned
self._download.set_destination_uri('file://' + self._dest_path)
self._download.start()
@@ -95,17 +97,48 @@ class Download(object):
def __state_change_cb(self, download, gparamspec):
state = self._download.get_status()
if state == WebKit.DownloadStatus.STARTED:
- self._create_journal_object()
- self._object_id = self.dl_jobject.object_id
-
- alert = TimeoutAlert(9)
- alert.props.title = _('Download started')
- alert.props.msg = _('%s' % self._download.get_suggested_filename())
- self._activity.add_alert(alert)
- alert.connect('response', self.__start_response_cb)
- alert.show()
- global _active_downloads
- _active_downloads.append(self)
+ # Check free space and cancel the download if there is not enough.
+ total_size = self._download.get_total_size()
+ logging.debug('Total size of the file: %s', total_size)
+ enough_space = self._activity.enough_space(
+ total_size, path=self.temp_path)
+ if not enough_space:
+ logging.debug('Download canceled because of Disk Space')
+ self.cancel()
+
+ self._canceled_alert = Alert()
+ self._canceled_alert.props.title = _('Not enough space '
+ 'to download')
+
+ total_size_mb = total_size / 1024.0 ** 2
+ free_space_mb = datastore.free_available_space(
+ path=self.temp_path) - datastore.SPACE_THRESHOLD \
+ / 1024.0 ** 2
+ filename = self._download.get_suggested_filename()
+ self._canceled_alert.props.msg = \
+ _('Download "%s" requires %.2f MB of free space, only '
+ '%.2f MB is available' % (filename, total_size_mb,
+ free_space_mb))
+ 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._create_journal_object()
+ self._object_id = self.dl_jobject.object_id
+
+ alert = TimeoutAlert(9)
+ alert.props.title = _('Download started')
+ alert.props.msg = _('%s' %
+ self._download.get_suggested_filename())
+ self._activity.add_alert(alert)
+ alert.connect('response', self.__start_response_cb)
+ alert.show()
+ global _active_downloads
+ _active_downloads.append(self)
elif state == WebKit.DownloadStatus.FINISHED:
self._stop_alert = Alert()
--
1.7.11.4
More information about the Sugar-devel
mailing list