[Pkg-sugar-commit] [sugar-browse-activity] 02/05: Add timer to update progress in intervals of 3 seconds

Jonas Smedegaard dr at jones.dk
Mon Apr 20 09:47:54 UTC 2015


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag v149.4
in repository sugar-browse-activity.

commit 890de598bea714651c2a750f4312a1ab39790e2a
Author: Manuel Quiñones <manuq at laptop.org>
Date:   Thu Sep 5 09:22:09 2013 -0300

    Add timer to update progress in intervals of 3 seconds
---
 downloadmanager.py | 40 ++++++++++++++++++++++++++++------------
 1 file changed, 28 insertions(+), 12 deletions(-)

diff --git a/downloadmanager.py b/downloadmanager.py
index cf72a2c..9a3f079 100644
--- a/downloadmanager.py
+++ b/downloadmanager.py
@@ -27,6 +27,7 @@ from gi.repository import Gtk
 from gi.repository import Gdk
 from gi.repository import WebKit
 from gi.repository import GdkPixbuf
+from gi.repository import GObject
 
 from sugar3.datastore import datastore
 from sugar3 import profile
@@ -43,6 +44,7 @@ DS_DBUS_PATH = '/org/laptop/sugar/DataStore'
 _active_downloads = []
 _dest_to_window = {}
 
+PROGRESS_TIMEOUT = 3000
 SPACE_THRESHOLD = 52428800  # 50 Mb
 
 
@@ -75,10 +77,12 @@ class Download(object):
 
         self.dl_jobject = None
         self._object_id = None
-        self._last_update_time = 0
-        self._last_update_percent = 0
         self._stop_alert = None
 
+        self._progress = 0
+        self._last_update_progress = 0
+        self._progress_sid = None
+
         # figure out download URI
         self.temp_path = os.path.join(activity.get_activity_root(), 'instance')
         if not os.path.exists(self.temp_path):
@@ -95,21 +99,30 @@ class Download(object):
         self._download.set_destination_uri('file://' + self._dest_path)
         self._download.start()
 
-    def __progress_change_cb(self, download, something):
-        progress = int(self._download.get_progress() * 100)
-        if progress > self._last_update_percent:
-            self._last_update_percent = progress
-            self.dl_jobject.metadata['progress'] = str(progress)
+    def _update_progress(self):
+        if self._progress > self._last_update_progress:
+            self._last_update_progress = self._progress
+            self.dl_jobject.metadata['progress'] = str(self._progress)
             datastore.write(self.dl_jobject)
 
+        self._progress_sid = None
+        return False
+
+    def __progress_change_cb(self, download, something):
+        self._progress = int(self._download.get_progress() * 100)
+
+        if self._progress_sid is None:
+            self._progress_sid = GObject.timeout_add(
+                PROGRESS_TIMEOUT, self._update_progress)
+
     def __current_size_changed_cb(self, download, something):
         current_size = self._download.get_current_size()
         total_size = self._download.get_total_size()
-        progress = int(current_size * 100 / total_size)
-        if progress > self._last_update_percent:
-            self._last_update_percent = progress
-            self.dl_jobject.metadata['progress'] = str(progress)
-            datastore.write(self.dl_jobject)
+        self._progress = int(current_size * 100 / total_size)
+
+        if self._progress_sid is None:
+            self._progress_sid = GObject.timeout_add(
+                PROGRESS_TIMEOUT, self._update_progress)
 
     def __state_change_cb(self, download, gparamspec):
         state = self._download.get_status()
@@ -179,6 +192,9 @@ class Download(object):
             self._stop_alert.connect('response', self.__stop_response_cb)
             self._stop_alert.show()
 
+            if self._progress_sid is not None:
+                GObject.source_remove(self._progress_sid)
+
             self.dl_jobject.metadata['title'] = \
                 self._download.get_suggested_filename()
             self.dl_jobject.metadata['description'] = _('From: %s') \

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-sugar/sugar-browse-activity.git



More information about the pkg-sugar-commit mailing list