comparison cagou/plugins/plugin_wid_chat.py @ 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
comparison
equal deleted inserted replaced
276:a0835f0212d8 277:f5302d57fb09
382 cagou_widget.CagouWidget.__init__(self) 382 cagou_widget.CagouWidget.__init__(self)
383 if type_ == C.CHAT_ONE2ONE: 383 if type_ == C.CHAT_ONE2ONE:
384 self.encryption_btn = EncryptionMainButton(self) 384 self.encryption_btn = EncryptionMainButton(self)
385 self.headerInputAddExtra(self.encryption_btn) 385 self.headerInputAddExtra(self.encryption_btn)
386 self.header_input.hint_text = u"{}".format(target) 386 self.header_input.hint_text = u"{}".format(target)
387 self.host.addListener('progressError', self.onProgressError, profiles)
388 self.host.addListener('progressFinished', self.onProgressFinished, profiles)
389 self._waiting_pids = {} # waiting progress ids
390 self.postInit() 387 self.postInit()
391 388
392 def __unicode__(self): 389 def __unicode__(self):
393 return u"Chat({})".format(self.target) 390 return u"Chat({})".format(self.target)
394 391
586 if self.type == C.CHAT_GROUP else C.MESS_TYPE_CHAT), # TODO: put this in QuickChat 583 if self.type == C.CHAT_GROUP else C.MESS_TYPE_CHAT), # TODO: put this in QuickChat
587 profile_key=self.profile 584 profile_key=self.profile
588 ) 585 )
589 input_widget.text = '' 586 input_widget.text = ''
590 587
591 def onProgressFinished(self, progress_id, metadata, profile): 588 def fileTransferEb(self, err_msg, cleaning_cb, profile):
592 try:
593 callback, cleaning_cb = self._waiting_pids.pop(progress_id)
594 except KeyError:
595 return
596 if cleaning_cb is not None: 589 if cleaning_cb is not None:
597 cleaning_cb() 590 cleaning_cb()
598 callback(metadata, profile)
599
600 def onProgressError(self, progress_id, err_msg, profile):
601 try:
602 dummy, cleaning_cb = self._waiting_pids[progress_id]
603 except KeyError:
604 return
605 else:
606 del self._waiting_pids[progress_id]
607 if cleaning_cb is not None:
608 cleaning_cb()
609 # TODO: display message to user 591 # TODO: display message to user
610 log.warning(u"Can't transfer file: {}".format(err_msg)) 592 log.warning(u"Can't transfer file: {}".format(err_msg))
611 593
612 def fileTransferDone(self, metadata, profile): 594 def fileTransferCb(self, metadata, cleaning_cb, profile):
613 log.debug("file transfered: {}".format(metadata)) 595 log.debug("file transfered: {}".format(metadata))
614 extra = {} 596 extra = {}
615 597
616 # FIXME: Q&D way of getting file type, upload plugins shouls give it 598 # FIXME: Q&D way of getting file type, upload plugins shouls give it
617 mime_type = mimetypes.guess_type(metadata['url'])[0] 599 mime_type = mimetypes.guess_type(metadata['url'])[0]
627 if self.type == C.CHAT_GROUP else C.MESS_TYPE_CHAT), 609 if self.type == C.CHAT_GROUP else C.MESS_TYPE_CHAT),
628 extra = extra, 610 extra = extra,
629 profile_key=profile 611 profile_key=profile
630 ) 612 )
631 613
632 def fileTransferCb(self, progress_data, cleaning_cb):
633 try:
634 progress_id = progress_data['progress']
635 except KeyError:
636 # FIXME: following code doesn't looks right, XMLUI should be created first
637 xml_ui = progress_data['xmlui']
638 G.host.showUI(xml_ui)
639 else:
640 self._waiting_pids[progress_id] = (self.fileTransferDone, cleaning_cb)
641
642 def onTransferOK(self, file_path, cleaning_cb, transfer_type): 614 def onTransferOK(self, file_path, cleaning_cb, transfer_type):
643 if transfer_type == C.TRANSFER_UPLOAD: 615 if transfer_type == C.TRANSFER_UPLOAD:
644 G.host.bridge.fileUpload( 616 G.host.bridge.fileUpload(
645 file_path, 617 file_path,
646 "", 618 "",
647 "", 619 "",
648 {"ignore_tls_errors": C.BOOL_TRUE}, # FIXME: should not be the default 620 {"ignore_tls_errors": C.BOOL_TRUE}, # FIXME: should not be the default
649 self.profile, 621 self.profile,
650 callback = lambda progress_data: self.fileTransferCb( 622 callback = partial(
651 progress_data, cleaning_cb) 623 G.host.actionManager,
652 ) 624 progress_cb = partial(self.fileTransferCb, cleaning_cb=cleaning_cb),
625 progress_eb = partial(self.fileTransferEb, cleaning_cb=cleaning_cb), )
626 )
653 elif transfer_type == C.TRANSFER_SEND: 627 elif transfer_type == C.TRANSFER_SEND:
654 if self.type == C.CHAT_GROUP: 628 if self.type == C.CHAT_GROUP:
655 log.warning(u"P2P transfer is not possible for group chat") 629 log.warning(u"P2P transfer is not possible for group chat")
656 # TODO: show an error dialog to user, or better hide the send button for 630 # TODO: show an error dialog to user, or better hide the send button for
657 # MUC 631 # MUC
684 self.changeWidget(jid_) 658 self.changeWidget(jid_)
685 659
686 def _mucJoinEb(self, failure): 660 def _mucJoinEb(self, failure):
687 log.warning(u"Can't join room: {}".format(failure)) 661 log.warning(u"Can't join room: {}".format(failure))
688 662
689 def _onDelete(self):
690 self.host.removeListener('progressFinished', self.onProgressFinished)
691 self.host.removeListener('progressError', self.onProgressError)
692 return super(Chat, self).onDelete()
693
694 def onOTRState(self, state, dest_jid, profile): 663 def onOTRState(self, state, dest_jid, profile):
695 assert profile in self.profiles 664 assert profile in self.profiles
696 if state in OTR_STATE_ENCRYPTION: 665 if state in OTR_STATE_ENCRYPTION:
697 self.otr_state_encryption = state 666 self.otr_state_encryption = state
698 elif state in OTR_STATE_TRUST: 667 elif state in OTR_STATE_TRUST:
710 def onDelete(self): 679 def onDelete(self):
711 # we always keep one widget, so it's available when swiping 680 # we always keep one widget, so it's available when swiping
712 # TODO: delete all widgets when chat is closed 681 # TODO: delete all widgets when chat is closed
713 nb_instances = sum(1 for _ in self.host.widgets.getWidgetInstances(self)) 682 nb_instances = sum(1 for _ in self.host.widgets.getWidgetInstances(self))
714 if nb_instances > 1: 683 if nb_instances > 1:
715 self.host.removeListener('progressFinished', self.onProgressFinished)
716 self.host.removeListener('progressError', self.onProgressError)
717 return super(Chat, self).onDelete() 684 return super(Chat, self).onDelete()
718 else: 685 else:
719 return False 686 return False
720 687
721 688