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

File 0001-Cancel-a-download-if-space-is-very-tight-SL-394.3.patch, 4.7 KB (added by humitos, 12 years ago)

This patch needs a patched version of sugar that implements "enough_space" method in Activity class

  • downloadmanager.py

    From 6c68cfdc1adfdace1efb785e13d05784ee2be61a Mon Sep 17 00:00:00 2001
    From: Manuel Kaufmann <humitos@gmail.com>
    Date: Mon, 17 Sep 2012 11:19:02 -0300
    Subject: [PATCH Browse 1/2] Cancel a download if space is very tight SL #394
    
    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@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 b class Download(object): 
    7474        self._stop_alert = None
    7575
    7676        # figure out download URI
    77         temp_path = os.path.join(activity.get_activity_root(), 'instance')
    78         if not os.path.exists(temp_path):
    79             os.makedirs(temp_path)
     77        self.temp_path = os.path.join(activity.get_activity_root(), 'instance')
     78        if not os.path.exists(self.temp_path):
     79            os.makedirs(self.temp_path)
    8080
    81         fd, self._dest_path = tempfile.mkstemp(dir=temp_path,
     81        fd, self._dest_path = tempfile.mkstemp(dir=self.temp_path,
    8282                                    suffix=download.get_suggested_filename(),
    8383                                    prefix='tmp')
    8484        os.close(fd)
    8585        logging.debug('Download destination path: %s' % self._dest_path)
    8686
     87        # We have to start the download to get 'total-size'
     88        # property. It not, 0 is returned
    8789        self._download.set_destination_uri('file://' + self._dest_path)
    8890        self._download.start()
    8991
    class Download(object): 
    9597    def __state_change_cb(self, download, gparamspec):
    9698        state = self._download.get_status()
    9799        if state == WebKit.DownloadStatus.STARTED:
    98             self._create_journal_object()
    99             self._object_id = self.dl_jobject.object_id
    100 
    101             alert = TimeoutAlert(9)
    102             alert.props.title = _('Download started')
    103             alert.props.msg = _('%s' % self._download.get_suggested_filename())
    104             self._activity.add_alert(alert)
    105             alert.connect('response', self.__start_response_cb)
    106             alert.show()
    107             global _active_downloads
    108             _active_downloads.append(self)
     100            # Check free space and cancel the download if there is not enough.
     101            total_size = self._download.get_total_size()
     102            logging.debug('Total size of the file: %s', total_size)
     103            enough_space = self._activity.enough_space(
     104                total_size, path=self.temp_path)
     105            if not enough_space:
     106                logging.debug('Download canceled because of Disk Space')
     107                self.cancel()
     108
     109                self._canceled_alert = Alert()
     110                self._canceled_alert.props.title = _('Not enough space '
     111                                                     'to download')
     112
     113                total_size_mb = total_size / 1024.0 ** 2
     114                free_space_mb = datastore.free_available_space(
     115                    path=self.temp_path) - datastore.SPACE_THRESHOLD \
     116                    / 1024.0 ** 2
     117                filename = self._download.get_suggested_filename()
     118                self._canceled_alert.props.msg = \
     119                    _('Download "%s" requires %.2f MB of free space, only '
     120                      '%.2f MB is available' % (filename, total_size_mb,
     121                                              free_space_mb))
     122                ok_icon = Icon(icon_name='dialog-ok')
     123                self._canceled_alert.add_button(Gtk.ResponseType.OK,
     124                                                _('Ok'), ok_icon)
     125                ok_icon.show()
     126                self._canceled_alert.connect('response',
     127                                             self.__stop_response_cb)
     128                self._activity.add_alert(self._canceled_alert)
     129            else:
     130                self._create_journal_object()
     131                self._object_id = self.dl_jobject.object_id
     132
     133                alert = TimeoutAlert(9)
     134                alert.props.title = _('Download started')
     135                alert.props.msg = _('%s' %
     136                                    self._download.get_suggested_filename())
     137                self._activity.add_alert(alert)
     138                alert.connect('response', self.__start_response_cb)
     139                alert.show()
     140                global _active_downloads
     141                _active_downloads.append(self)
    109142
    110143        elif state == WebKit.DownloadStatus.FINISHED:
    111144            self._stop_alert = Alert()