<br><br><div class="gmail_quote">On Sat, May 21, 2011 at 10:24 AM, Sascha Silbe <span dir="ltr"><<a href="mailto:silbe@activitycentral.com">silbe@activitycentral.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

If downloads were cancelled, Browse could leave a (potentially large)<br>
temporary file behind. Now we delete them whenever they get cancelled<br>
(but not if Browse crashes).<br>
<br>
Based on patches by Rafael Ortiz <<a href="mailto:rafael@activitycentral.com">rafael@activitycentral.com</a>> and<br>
Sebastian Silva <<a href="mailto:sebastian@somosazucar.org">sebastian@somosazucar.org</a>>.<br>
<br>
Signed-off-by: Sascha Silbe <<a href="mailto:silbe@activitycentral.com">silbe@activitycentral.com</a>><br>
---<br>
 downloadmanager.py |   37 ++++++++++++++++++++++---------------<br>
 1 files changed, 22 insertions(+), 15 deletions(-)<br>
<br>
diff --git a/downloadmanager.py b/downloadmanager.py<br>
index 934693f..a88389f 100644<br>
--- a/downloadmanager.py<br>
+++ b/downloadmanager.py<br>
@@ -38,7 +38,6 @@ from sugar.graphics.alert import Alert, TimeoutAlert<br>
 from sugar.graphics.icon import Icon<br>
 from sugar.graphics import style<br>
 from sugar.activity import activity<br>
-<br>
 # #3903 - this constant can be removed and assumed to be 1 when dbus-python<br>
 # 0.82.3 is the only version used<br>
 import dbus<br>
@@ -69,9 +68,8 @@ def remove_all_downloads():<br>
     for download in _active_downloads:<br>
         download.cancelable.cancel(NS_ERROR_FAILURE)<br>
         if download.dl_jobject is not None:<br>
-            download.datastore_deleted_handler.remove()<br>
             datastore.delete(download.dl_jobject.object_id)<br>
-            download.cleanup_datastore_write()<br>
+        download.cleanup()<br>
<br>
<br>
 class HelperAppLauncherDialog:<br>
@@ -170,6 +168,7 @@ class Download:<br>
         elif state_flags & interfaces.nsIWebProgressListener.STATE_STOP:<br>
             if NS_FAILED(status):<br>
                 # download cancelled<br>
+                self.cleanup()<br>
                 return<br>
<br>
             self._stop_alert = Alert()<br>
@@ -252,14 +251,13 @@ class Download:<br>
         global _active_downloads<br>
         if response_id is gtk.RESPONSE_CANCEL:<br>
             logging.debug('Download Canceled')<br>
+            logging.debug('target_path=%r', self._target_file.path)<br>
             self.cancelable.cancel(NS_ERROR_FAILURE)<br>
             try:<br>
-                self.datastore_deleted_handler.remove()<br>
                 datastore.delete(self._object_id)<br>
             except Exception:<br>
                 logging.exception('Object has been deleted already')<br>
-            if self.dl_jobject is not None:<br>
-                self.cleanup_datastore_write()<br>
+            self.cleanup()<br>
             if self._stop_alert is not None:<br>
                 self._activity.remove_alert(self._stop_alert)<br>
<br>
@@ -272,21 +270,30 @@ class Download:<br>
             activity.show_object_in_journal(self._object_id)<br>
         self._activity.remove_alert(alert)<br>
<br>
-    def cleanup_datastore_write(self):<br>
+    def cleanup(self):<br>
         global _active_downloads<br>
-        _active_downloads.remove(self)<br>
+        if self in _active_downloads:<br>
+            _active_downloads.remove(self)<br>
<br>
-        if os.path.isfile(self.dl_jobject.file_path):<br>
-            os.remove(self.dl_jobject.file_path)<br>
-        self.dl_jobject.destroy()<br>
-        self.dl_jobject = None<br>
+        if self.datastore_deleted_handler is not None:<br>
+            self.datastore_deleted_handler.remove()<br>
+            self.datastore_deleted_handler = None<br>
+<br>
+        if os.path.isfile(self._target_file.path):<br>
+            os.remove(self._target_file.path)<br>
+        if os.path.isfile(self._target_file.path + '.part'):<br>
+            os.remove(self._target_file.path + '.part')<br>
+<br>
+        if self.dl_jobject is not None:<br>
+            self.dl_jobject.destroy()<br>
+            self.dl_jobject = None<br>
<br>
     def _internal_save_cb(self):<br>
-        self.cleanup_datastore_write()<br>
+        self.cleanup()<br>
<br>
     def _internal_save_error_cb(self, err):<br>
         logging.error('Error saving activity object to datastore: %s', err)<br>
-        self.cleanup_datastore_write()<br>
+        self.cleanup()<br>
<br>
     def onProgressChange64(self, web_progress, request, cur_self_progress,<br>
                            max_self_progress, cur_total_progress,<br>
@@ -348,7 +355,7 @@ class Download:<br>
         if self in _active_downloads:<br>
             # TODO: Use NS_BINDING_ABORTED instead of NS_ERROR_FAILURE.<br>
             self.cancelable.cancel(NS_ERROR_FAILURE)<br>
-            _active_downloads.remove(self)<br>
+            self.cleanup()<br>
<br>
<br>
 components.registrar.registerFactory('{23c51569-e9a1-4a92-adeb-3723db82ef7c}',<br>
<font color="#888888">--<br>
1.7.4.1<br>
<br></font></blockquote><div><br></div><div>Tested and works as expected, we still need to address the case when browse crashes.  </div></div><br><div>can you apply to mainline ?. </div><div><br></div>