diff frontends/src/jp/base.py @ 1627:5a641e7b858a

jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
author Goffi <goffi@goffi.org>
date Thu, 19 Nov 2015 18:13:26 +0100
parents ec48b35309dc
children 44a14f83e64b
line wrap: on
line diff
--- a/frontends/src/jp/base.py	Thu Nov 19 18:13:26 2015 +0100
+++ b/frontends/src/jp/base.py	Thu Nov 19 18:13:26 2015 +0100
@@ -105,9 +105,6 @@
         # progress attributes
         self._progress_id = None # TODO: manage several progress ids
         self.quit_on_progress_end = True
-        self.progress_started = lambda dummy: self.disp(_(u"Operation started"), 2)
-        self.progress_success = lambda dummy: self.disp(_(u"Operation successfully finished"), 2)
-        self.progress_failure = lambda dummy: self.disp(_(u"Error while doing operation"), error=True)
 
     @property
     def version(self):
@@ -379,35 +376,7 @@
                 return "%s/%s" % (_jid.bare, main_resource)
         return param_jid
 
-    def _onProgressStarted(self, uid, profile):
-        if profile != self.profile:
-            return
-        self.progress_started(None)
-        if self.watch_progress and self.progress_id and uid == self.progress_id:
-            GLib.timeout_add(PROGRESS_DELAY, self._progress_cb)
-
-    def _onProgressFinished(self, uid, profile):
-        if profile != self.profile:
-            return
-        if uid == self.progress_id:
-            try:
-                self.pbar.finish()
-            except AttributeError:
-                pass
-            self.progress_success(None)
-            if self.quit_on_progress_end:
-                self.quit()
-
-    def _onProgressError(self, uid, profile):
-        if profile != self.profile:
-            return
-        if uid == self.progress_id:
-            self.disp('') # progress is not finished, so we skip a line
-            if self.quit_on_progress_end:
-                self.progress_failure(None)
-                self.quitFromSignal(1)
-
-    def _progress_cb(self):
+    def progressUpdate(self):
         """This method is continualy called to update the progress bar"""
         data = self.bridge.progressGet(self.progress_id, self.profile)
         if data:
@@ -505,6 +474,43 @@
     def progress_id(self, value):
         self.host.progress_id = value
 
+    def progressStartedHandler(self, uid, metadata, profile):
+        if profile != self.profile:
+            return
+        self.onProgressStarted(metadata)
+        if self.host.watch_progress and self.progress_id and uid == self.progress_id:
+            GLib.timeout_add(PROGRESS_DELAY, self.host.progressUpdate)
+
+    def progressFinishedHandler(self, uid, metadata, profile):
+        if profile != self.profile:
+            return
+        if uid == self.progress_id:
+            try:
+                self.host.pbar.finish()
+            except AttributeError:
+                pass
+            self.onProgressFinished(metadata)
+            if self.host.quit_on_progress_end:
+                self.host.quit()
+
+    def progressErrorHandler(self, uid, message, profile):
+        if profile != self.profile:
+            return
+        if uid == self.progress_id:
+            self.disp('') # progress is not finished, so we skip a line
+            if self.host.quit_on_progress_end:
+                self.onProgressError(message)
+                self.host.quitFromSignal(1)
+
+    def onProgressStarted(self, metadata):
+        self.disp(_(u"Operation started"), 2)
+
+    def onProgressFinished(self, metadata):
+        self.disp(_(u"Operation successfully finished"), 2)
+
+    def onProgressError(self, error_msg):
+        self.disp(_(u"Error while doing operation: {}").format(error_msg), error=True)
+
     def disp(self, msg, verbosity=0, error=False):
         return self.host.disp(msg, verbosity, error)
 
@@ -538,9 +544,9 @@
             if show_progress:
                 self.host.watch_progress = True
             # we need to register the following signal even if we don't display the progress bas
-            self.host.bridge.register("progressStarted", self.host._onProgressStarted)
-            self.host.bridge.register("progressFinished", self.host._onProgressFinished)
-            self.host.bridge.register("progressError", self.host._onProgressError)
+            self.host.bridge.register("progressStarted", self.progressStartedHandler)
+            self.host.bridge.register("progressFinished", self.progressFinishedHandler)
+            self.host.bridge.register("progressError", self.progressErrorHandler)
 
     def connected(self):
         if not self.need_loop: