changeset 277:f5302d57fb09

chat: use the new progress_cb and progress_eb methods of actionManager: thanks to those arguments, progress management code in chat is not needed anymore, simplifying the code.
author Goffi <goffi@goffi.org>
date Wed, 20 Mar 2019 09:29:44 +0100
parents a0835f0212d8
children 444ba439530f
files cagou/plugins/plugin_wid_chat.py
diffstat 1 files changed, 7 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/plugins/plugin_wid_chat.py	Wed Mar 20 09:29:44 2019 +0100
+++ b/cagou/plugins/plugin_wid_chat.py	Wed Mar 20 09:29:44 2019 +0100
@@ -384,9 +384,6 @@
             self.encryption_btn = EncryptionMainButton(self)
             self.headerInputAddExtra(self.encryption_btn)
         self.header_input.hint_text = u"{}".format(target)
-        self.host.addListener('progressError', self.onProgressError, profiles)
-        self.host.addListener('progressFinished', self.onProgressFinished, profiles)
-        self._waiting_pids = {}  # waiting progress ids
         self.postInit()
 
     def __unicode__(self):
@@ -588,28 +585,13 @@
             )
         input_widget.text = ''
 
-    def onProgressFinished(self, progress_id, metadata, profile):
-        try:
-            callback, cleaning_cb = self._waiting_pids.pop(progress_id)
-        except KeyError:
-            return
+    def fileTransferEb(self, err_msg, cleaning_cb, profile):
         if cleaning_cb is not None:
             cleaning_cb()
-        callback(metadata, profile)
-
-    def onProgressError(self, progress_id, err_msg, profile):
-        try:
-            dummy, cleaning_cb = self._waiting_pids[progress_id]
-        except KeyError:
-            return
-        else:
-            del self._waiting_pids[progress_id]
-            if cleaning_cb is not None:
-                cleaning_cb()
         # TODO: display message to user
         log.warning(u"Can't transfer file: {}".format(err_msg))
 
-    def fileTransferDone(self, metadata, profile):
+    def fileTransferCb(self, metadata, cleaning_cb, profile):
         log.debug("file transfered: {}".format(metadata))
         extra = {}
 
@@ -629,16 +611,6 @@
             profile_key=profile
             )
 
-    def fileTransferCb(self, progress_data, cleaning_cb):
-        try:
-            progress_id = progress_data['progress']
-        except KeyError:
-            # FIXME: following code doesn't looks right, XMLUI should be created first
-            xml_ui = progress_data['xmlui']
-            G.host.showUI(xml_ui)
-        else:
-            self._waiting_pids[progress_id] = (self.fileTransferDone, cleaning_cb)
-
     def onTransferOK(self, file_path, cleaning_cb, transfer_type):
         if transfer_type == C.TRANSFER_UPLOAD:
             G.host.bridge.fileUpload(
@@ -647,9 +619,11 @@
                 "",
                 {"ignore_tls_errors": C.BOOL_TRUE},  # FIXME: should not be the default
                 self.profile,
-                callback = lambda progress_data: self.fileTransferCb(
-                    progress_data, cleaning_cb)
-                )
+                callback = partial(
+                    G.host.actionManager,
+                    progress_cb = partial(self.fileTransferCb, cleaning_cb=cleaning_cb),
+                    progress_eb = partial(self.fileTransferEb, cleaning_cb=cleaning_cb),               )
+            )
         elif transfer_type == C.TRANSFER_SEND:
             if self.type == C.CHAT_GROUP:
                 log.warning(u"P2P transfer is not possible for group chat")
@@ -686,11 +660,6 @@
     def _mucJoinEb(self, failure):
         log.warning(u"Can't join room: {}".format(failure))
 
-    def _onDelete(self):
-        self.host.removeListener('progressFinished', self.onProgressFinished)
-        self.host.removeListener('progressError', self.onProgressError)
-        return super(Chat, self).onDelete()
-
     def onOTRState(self, state, dest_jid, profile):
         assert profile in self.profiles
         if state in OTR_STATE_ENCRYPTION:
@@ -712,8 +681,6 @@
         # TODO: delete all widgets when chat is closed
         nb_instances = sum(1 for _ in self.host.widgets.getWidgetInstances(self))
         if nb_instances > 1:
-            self.host.removeListener('progressFinished', self.onProgressFinished)
-            self.host.removeListener('progressError', self.onProgressError)
             return super(Chat, self).onDelete()
         else:
             return False