comparison src/browser/libervia_main.py @ 584:0a06cf833f5a

browser_side: various improvements, especially for MUC
author souliane <souliane@mailoo.org>
date Wed, 22 Oct 2014 19:03:55 +0200
parents 1c1dbe03d3c6
children 3eb3a2c0c011 a5019e62c3e9
comparison
equal deleted inserted replaced
583:4c6c61696ba0 584:0a06cf833f5a
720 # for example to scroll to the bottom 720 # for example to scroll to the bottom
721 self.setSelected(lib_wid) 721 self.setSelected(lib_wid)
722 lib_wid.refresh() 722 lib_wid.refresh()
723 return lib_wid 723 return lib_wid
724 724
725 def getRoomWidget(self, target):
726 """Get the MUC widget for the given target.
727
728 @param target (jid.JID): BARE jid of the MUC
729 @return: panels.ChatPanel instance or None
730 """
731 entity = {'item': target, 'type_': 'group'}
732 if target.full() in self.room_list or target in self.room_list: # as JID is a string-based class, we don't know what will please Pyjamas...
733 return self.getLiberviaWidget(panels.ChatPanel, entity, ignoreOtherTabs=False)
734 return None
735
736 def getOrCreateRoomWidget(self, target):
737 """Get the MUC widget for the given target, create it if necessary.
738
739 @param target (jid.JID): BARE jid of the MUC
740 @return: panels.ChatPanel instance
741 """
742 lib_wid = self.getRoomWidget(target)
743 if lib_wid:
744 return lib_wid
745
746 # XXX: target.node.startwith(...) raises an error "startswith is not a function"
747 # This happens when node a is property defined in the JID class
748 # FIXME: pyjamas doesn't handle the properties well
749 node = target.node
750
751 # XXX: it's not really beautiful, but it works :)
752 if node.startswith('sat_tarot_'):
753 tab_name = "Tarot"
754 elif node.startswith('sat_radiocol_'):
755 tab_name = "Radio collective"
756 else:
757 tab_name = target.node
758
759 self.room_list.append(target)
760 entity = {'item': target, 'type_': 'group'}
761 return self.getOrCreateLiberviaWidget(panels.ChatPanel, entity, new_tab=tab_name)
762
725 def _newMessageCb(self, from_jid_s, msg, msg_type, to_jid_s, extra): 763 def _newMessageCb(self, from_jid_s, msg, msg_type, to_jid_s, extra):
726 from_jid = jid.JID(from_jid_s) 764 from_jid = jid.JID(from_jid_s)
727 to_jid = jid.JID(to_jid_s) 765 to_jid = jid.JID(to_jid_s)
728 for plugin in self.plugins.values(): 766 for plugin in self.plugins.values():
729 if hasattr(plugin, 'messageReceivedTrigger'): 767 if hasattr(plugin, 'messageReceivedTrigger'):
764 assert(isinstance(self.status_panel, panels.PresenceStatusPanel)) 802 assert(isinstance(self.status_panel, panels.PresenceStatusPanel))
765 self.status_panel.setPresence(show) # pylint: disable=E1103 803 self.status_panel.setPresence(show) # pylint: disable=E1103
766 if statuses: 804 if statuses:
767 self.status_panel.setStatus(statuses.values()[0]) # pylint: disable=E1103 805 self.status_panel.setStatus(statuses.values()[0]) # pylint: disable=E1103
768 else: 806 else:
769 if entity_jid.bare in self.room_list: 807 bare_jid = entity_jid.bareJID()
770 wid = self.getLiberviaWidget(panels.ChatPanel, {'item': entity_jid, 'type_': 'group'}, ignoreOtherTabs=False) 808 if bare_jid.full() in self.room_list or bare_jid in self.room_list: # as JID is a string-based class, we don't know what will please Pyjamas...
809 wid = self.getRoomWidget(bare_jid)
771 else: 810 else:
772 wid = self.contact_panel 811 wid = self.contact_panel
773 if show == 'unavailable': # XXX: save some resources as for now we only need 'unavailable' 812 if show == 'unavailable': # XXX: save some resources as for now we only need 'unavailable'
774 for plugin in self.plugins.values(): 813 for plugin in self.plugins.values():
775 if hasattr(plugin, 'presenceReceivedTrigger'): 814 if hasattr(plugin, 'presenceReceivedTrigger'):
776 plugin.presenceReceivedTrigger(entity_jid, show, priority, statuses) 815 plugin.presenceReceivedTrigger(entity_jid, show, priority, statuses)
777 if wid: 816 if wid:
778 wid.setConnected(entity_jid.bare, entity_jid.resource, show, priority, statuses) 817 wid.setConnected(entity_jid.bare, entity_jid.resource, show, priority, statuses)
779 818
780 def _roomJoinedCb(self, room_jid, room_nicks, user_nick): 819 def _roomJoinedCb(self, room_jid_s, room_nicks, user_nick):
781 _target = jid.JID(room_jid) 820 chat_panel = self.getOrCreateRoomWidget(jid.JID(room_jid_s))
782
783 # XXX: _target.node.startwith(...) raises an error "startswith is not a function"
784 # This happens when node a is property defined in the JID class
785 # FIXME: pyjamas doesn't handle the properties well
786 node = _target.node
787
788 if _target not in self.room_list:
789 self.room_list.append(_target)
790
791 # XXX: it's not really beautiful, but it works :)
792 if node.startswith('sat_tarot_'):
793 tab_name = "Tarot"
794 elif node.startswith('sat_radiocol_'):
795 tab_name = "Radio collective"
796 else:
797 tab_name = _target.node
798
799 chat_panel = self.getOrCreateLiberviaWidget(panels.ChatPanel, {'item': _target, 'type_': 'group'}, new_tab=tab_name)
800 chat_panel.setUserNick(user_nick) 821 chat_panel.setUserNick(user_nick)
801 chat_panel.setPresents(room_nicks) 822 chat_panel.setPresents(room_nicks)
802 chat_panel.historyPrint()
803 chat_panel.refresh() 823 chat_panel.refresh()
804 824
805 def _roomLeftCb(self, room_jid, room_nicks, user_nick): 825 def _roomLeftCb(self, room_jid_s, room_nicks, user_nick):
806 # FIXME: room_list contains jid.JID instances so why MUST we do
807 # 'remove(room_jid)' and not 'remove(jid.JID(room_jid))' ????!!
808 # This looks like a pyjamas bug --> check/report
809 try: 826 try:
810 self.room_list.remove(room_jid) 827 del self.room_list[room_jid_s]
811 except KeyError: 828 except KeyError:
812 pass 829 try: # as JID is a string-based class, we don't know what will please Pyjamas...
830 del self.room_list[jid.JID(room_jid_s)]
831 except KeyError:
832 pass
813 833
814 def _roomUserJoinedCb(self, room_jid_s, user_nick, user_data): 834 def _roomUserJoinedCb(self, room_jid_s, user_nick, user_data):
815 for lib_wid in self.libervia_widgets: 835 lib_wid = self.getOrCreateRoomWidget(jid.JID(room_jid_s))
816 if isinstance(lib_wid, panels.ChatPanel) and lib_wid.type == 'group' and lib_wid.target.bare == room_jid_s: 836 if lib_wid:
817 lib_wid.userJoined(user_nick, user_data) 837 lib_wid.userJoined(user_nick, user_data)
818 838
819 def _roomUserLeftCb(self, room_jid_s, user_nick, user_data): 839 def _roomUserLeftCb(self, room_jid_s, user_nick, user_data):
820 for lib_wid in self.libervia_widgets: 840 lib_wid = self.getRoomWidget(jid.JID(room_jid_s))
821 if isinstance(lib_wid, panels.ChatPanel) and lib_wid.type == 'group' and lib_wid.target.bare == room_jid_s: 841 if lib_wid:
822 lib_wid.userLeft(user_nick, user_data) 842 lib_wid.userLeft(user_nick, user_data)
823 843
824 def _roomUserChangedNickCb(self, room_jid_s, old_nick, new_nick): 844 def _roomUserChangedNickCb(self, room_jid_s, old_nick, new_nick):
825 """Called when an user joined a MUC room""" 845 """Called when an user joined a MUC room"""
826 for lib_wid in self.libervia_widgets: 846 lib_wid = self.getRoomWidget(jid.JID(room_jid_s))
827 if isinstance(lib_wid, panels.ChatPanel) and lib_wid.type == 'group' and lib_wid.target.bare == room_jid_s: 847 if lib_wid:
828 lib_wid.changeUserNick(old_nick, new_nick) 848 lib_wid.changeUserNick(old_nick, new_nick)
829 849
830 def _tarotGameStartedCb(self, waiting, room_jid_s, referee, players): 850 def _tarotGameStartedCb(self, waiting, room_jid_s, referee, players):
831 for lib_wid in self.libervia_widgets: 851 lib_wid = self.getRoomWidget(jid.JID(room_jid_s))
832 if isinstance(lib_wid, panels.ChatPanel) and lib_wid.type == 'group' and lib_wid.target.bare == room_jid_s: 852 if lib_wid:
833 lib_wid.startGame("Tarot", waiting, referee, players) 853 lib_wid.startGame("Tarot", waiting, referee, players)
834 854
835 def _tarotGameGenericCb(self, event_name, room_jid_s, args): 855 def _tarotGameGenericCb(self, event_name, room_jid_s, args):
836 for lib_wid in self.libervia_widgets: 856 lib_wid = self.getRoomWidget(jid.JID(room_jid_s))
837 if isinstance(lib_wid, panels.ChatPanel) and lib_wid.type == 'group' and lib_wid.target.bare == room_jid_s: 857 if lib_wid:
838 getattr(lib_wid.getGame("Tarot"), event_name)(*args) 858 getattr(lib_wid.getGame("Tarot"), event_name)(*args)
839 859
840 def _radioColStartedCb(self, waiting, room_jid_s, referee, players, queue_data): 860 def _radioColStartedCb(self, waiting, room_jid_s, referee, players, queue_data):
841 for lib_wid in self.libervia_widgets: 861 lib_wid = self.getRoomWidget(jid.JID(room_jid_s))
842 if isinstance(lib_wid, panels.ChatPanel) and lib_wid.type == 'group' and lib_wid.target.bare == room_jid_s: 862 if lib_wid:
843 lib_wid.startGame("RadioCol", waiting, referee, players, queue_data) 863 lib_wid.startGame("RadioCol", waiting, referee, players, queue_data)
844 864
845 def _radioColGenericCb(self, event_name, room_jid_s, args): 865 def _radioColGenericCb(self, event_name, room_jid_s, args):
846 for lib_wid in self.libervia_widgets: 866 lib_wid = self.getRoomWidget(jid.JID(room_jid_s))
847 if isinstance(lib_wid, panels.ChatPanel) and lib_wid.type == 'group' and lib_wid.target.bare == room_jid_s: 867 if lib_wid:
848 getattr(lib_wid.getGame("RadioCol"), event_name)(*args) 868 getattr(lib_wid.getGame("RadioCol"), event_name)(*args)
849 869
850 def _getPresenceStatusesCb(self, presence_data): 870 def _getPresenceStatusesCb(self, presence_data):
851 for entity in presence_data: 871 for entity in presence_data:
852 for resource in presence_data[entity]: 872 for resource in presence_data[entity]:
853 args = presence_data[entity][resource] 873 args = presence_data[entity][resource]
907 """Callback when a new chat state is received. 927 """Callback when a new chat state is received.
908 @param from_jid_s: JID of the contact who sent his state, or '@ALL@' 928 @param from_jid_s: JID of the contact who sent his state, or '@ALL@'
909 @param state: new state (string) 929 @param state: new state (string)
910 """ 930 """
911 if from_jid_s == '@ALL@': 931 if from_jid_s == '@ALL@':
912 target = '@ALL@' 932 for lib_wid in self.libervia_widgets:
913 nick = C.ALL_OCCUPANTS 933 if isinstance(lib_wid, panels.ChatPanel):
914 else: 934 lib_wid.setState(state, nick=C.ALL_OCCUPANTS)
915 from_jid = jid.JID(from_jid_s) 935 return
916 target = from_jid.bare 936 from_jid = jid.JID(from_jid_s)
917 nick = from_jid.resource 937 lib_wid = self.getLiberviaWidget(panels.ChatPanel, {'item': from_jid}, ignoreOtherTabs=False)
918 938 lib_wid.setState(state, nick=from_jid.resource)
919 for lib_wid in self.libervia_widgets:
920 if isinstance(lib_wid, panels.ChatPanel):
921 if target == '@ALL@' or lib_wid.matchEntity(target):
922 lib_wid.setState(state, nick=nick)
923 939
924 def _askConfirmation(self, confirmation_id, confirmation_type, data): 940 def _askConfirmation(self, confirmation_id, confirmation_type, data):
925 answer_data = {} 941 answer_data = {}
926 942
927 def confirm_cb(result): 943 def confirm_cb(result):