changeset 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 63cef4dbf2a4
children e9936fcfaf91
files frontends/src/jp/base.py frontends/src/jp/cmd_file.py
diffstat 2 files changed, 69 insertions(+), 38 deletions(-) [+]
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:
--- a/frontends/src/jp/cmd_file.py	Thu Nov 19 18:13:26 2015 +0100
+++ b/frontends/src/jp/cmd_file.py	Thu Nov 19 18:13:26 2015 +0100
@@ -36,9 +36,6 @@
 class Send(base.CommandBase):
     def __init__(self, host):
         super(Send, self).__init__(host, 'send', use_progress=True, use_verbose=True, help=_('Send a file to a contact'))
-        self.host.progress_started = lambda dummy: self.disp(_(u'File copy started'),2)
-        self.host.progress_success = lambda dummy: self.disp(_(u'File copied successfully'),2)
-        self.host.progress_failure = lambda dummy: self.disp(_(u'Error while transfering file'),error=True)
 
     def add_parser_options(self):
         self.parser.add_argument("files", type=str, nargs = '+', help=_("A list of file"))
@@ -51,6 +48,15 @@
         super(Send, self).connected()
         self.send_files()
 
+    def onProgressStarted(self, metadata):
+        self.disp(_(u'File copy started'),2)
+
+    def onProgressFinished(self, metadata):
+        self.disp(_(u'File sent successfully'),2)
+
+    def onProgressError(self, error_msg):
+        self.disp(_(u'Error while sending file: {}').format(error_msg),error=True)
+
     def gotId(self, data, file_):
         """Called when a progress id has been received
 
@@ -112,6 +118,25 @@
         self.action_callbacks = {C.META_TYPE_FILE: self.onFileAction,
                                  C.META_TYPE_OVERWRITE: self.onOverwriteAction}
 
+    def onProgressStarted(self, metadata):
+        self.disp(_(u'File copy started'),2)
+
+    def onProgressFinished(self, metadata):
+        self.disp(_(u'File received successfully'),2)
+        if metadata.get('hash_verified', False):
+            try:
+                self.disp(_(u'hash checked: {algo}:{checksum}').format(
+                    algo=metadata['hash_algo'],
+                    checksum=metadata['hash']),
+                    1)
+            except KeyError:
+                self.disp(_(u'hash is checked but hash value is missing', 1), error=True)
+        else:
+            self.disp(_(u"hash can't be verified"), 1)
+
+    def onProgressError(self, error_msg):
+        self.disp(_(u'Error while receiving file: {}').format(error_msg),error=True)
+
     def getXmluiId(self, action_data):
         # FIXME: we temporarily use ElementTree, but a real XMLUI managing module
         #        should be available in the futur