comparison src/browser/sat_browser/contact_panel.py @ 673:e489218886d7 frontends_multi_profiles

browser_side: add attribute "merge_resources" to ContactsPanel to display the MUC occupants + override Chat.replaceUser and Chat.removeUser
author souliane <souliane@mailoo.org>
date Wed, 11 Mar 2015 19:01:27 +0100
parents 2201ff543a05
children 849ffb24d5bf
comparison
equal deleted inserted replaced
672:b39a9eddfe56 673:e489218886d7
80 """ContactList graphic representation 80 """ContactList graphic representation
81 81
82 Special features like popup menu panel or changing the contact states must be done in a sub-class. 82 Special features like popup menu panel or changing the contact states must be done in a sub-class.
83 """ 83 """
84 84
85 def __init__(self, host, contacts_click=None, contacts_style=None, contacts_menus=True, contacts_display=C.CONTACT_DEFAULT_DISPLAY): 85 def __init__(self, host, merge_resources=True, contacts_click=None,
86 contacts_style=None, contacts_menus=True,
87 contacts_display=C.CONTACT_DEFAULT_DISPLAY):
86 """ 88 """
87 89
88 @param host (SatWebFrontend): host instance 90 @param host (SatWebFrontend): host instance
91 @param merge_resources (bool): if True, the entities sharing the same
92 bare JID will also share the same contact box.
89 @param contacts_click (callable): click callback for the contact boxes 93 @param contacts_click (callable): click callback for the contact boxes
90 @param contacts_style (unicode): CSS style name for the contact boxes 94 @param contacts_style (unicode): CSS style name for the contact boxes
91 @param contacts_menus (tuple): define the menu types that fit this 95 @param contacts_menus (tuple): define the menu types that fit this
92 contact panel, with values from the menus type constants. 96 contact panel, with values from the menus type constants.
93 @param contacts_display (tuple): prioritize the display methods of the 97 @param contacts_display (tuple): prioritize the display methods of the
94 contact's label with values in ("jid", "nick", "bare", "resource") 98 contact's label with values in ("jid", "nick", "bare", "resource")
95 """ 99 """
96 VerticalPanel.__init__(self) 100 VerticalPanel.__init__(self)
97 self.host = host 101 self.host = host
102 self.merge_resources = merge_resources
98 self._contacts = {} # entity jid to ContactBox map 103 self._contacts = {} # entity jid to ContactBox map
99 self.click_listener = None 104 self.click_listener = None
100 105
101 if contacts_click is not None: 106 if contacts_click is not None:
102 self.onClick = contacts_click 107 self.onClick = contacts_click
103 108
104 self.contacts_style = contacts_style 109 self.contacts_style = contacts_style
105 self.contacts_menus = contacts_menus 110 self.contacts_menus = contacts_menus
106 self.contacts_display = contacts_display 111 self.contacts_display = contacts_display
112
113 def _key(self, contact_jid):
114 """Return internal key for this contact.
115
116 @param contact_jid (jid.JID): contact JID
117 @return: jid.JID
118 """
119 return contact_jid.bare if self.merge_resources else contact_jid
107 120
108 def setList(self, jids): 121 def setList(self, jids):
109 """set all contacts in the list in one shot. 122 """set all contacts in the list in one shot.
110 123
111 @param jids (list[jid.JID]): jids to display (the order is kept) 124 @param jids (list[jid.JID]): jids to display (the order is kept)
115 current = [box.jid for box in self.children if isinstance(box, contact_widget.ContactBox)] 128 current = [box.jid for box in self.children if isinstance(box, contact_widget.ContactBox)]
116 if current == jids: 129 if current == jids:
117 # the display doesn't change 130 # the display doesn't change
118 return 131 return
119 self.clear() 132 self.clear()
120 for jid_ in jids: 133 for contact_jid in jids:
121 assert isinstance(jid_, jid.JID) 134 assert isinstance(contact_jid, jid.JID)
122 box = self.getContactBox(jid_) 135 self.addContact(contact_jid)
123 VerticalPanel.append(self, box)
124
125 def isContactPresent(self, contact_jid):
126 """Return True if a contact is present in the panel"""
127 return contact_jid in self._contacts
128
129 def getContacts(self):
130 return self._contacts
131 136
132 def getContactBox(self, contact_jid): 137 def getContactBox(self, contact_jid):
133 """get the Contactbox of a contact 138 """Get a contact box for a contact, add it if it doesn't exist yet.
134 139
135 if the Contactbox doesn't exists, it will be created 140 @param contact_jid (jid.JID): contact JID
136 @param contact_jid (jid.JID): the contact 141 @return: ContactBox
137 @return: ContactBox instance 142 """
138 """ 143 try:
139 try: 144 return self._contacts[self._key(contact_jid)]
140 return self._contacts[contact_jid.bare]
141 except KeyError: 145 except KeyError:
142 box = contact_widget.ContactBox(self.host, contact_jid, 146 box = contact_widget.ContactBox(self.host, contact_jid,
143 style_name=self.contacts_style, 147 style_name=self.contacts_style,
144 menu_types=self.contacts_menus, 148 menu_types=self.contacts_menus,
145 display=self.contacts_display) 149 display=self.contacts_display)
146 self._contacts[contact_jid.bare] = box 150 self._contacts[self._key(contact_jid)] = box
147 return box 151 return box
148 152
149 def updateAvatar(self, jid_, url): 153 def addContact(self, contact_jid):
154 """Add a contact to the list.
155
156 @param contact_jid (jid.JID): contact JID
157 """
158 box = self.getContactBox(contact_jid)
159 if box not in self.children:
160 VerticalPanel.append(self, box)
161
162 def removeContact(self, contact_jid):
163 """Remove a contact from the list.
164
165 @param contact_jid (jid.JID): contact JID
166 """
167 box = self._contacts.pop(self._key(contact_jid))
168 VerticalPanel.remove(self, box)
169
170 def updateAvatar(self, contact_jid, url):
150 """Update the avatar of the given contact 171 """Update the avatar of the given contact
151 172
152 @param jid_ (jid.JID): contact jid 173 @param contact_jid (jid.JID): contact JID
153 @param url (unicode): image url 174 @param url (unicode): image url
154 """ 175 """
155 try: 176 try:
156 self.getContactBox(jid_).updateAvatar(url) 177 self.getContactBox(contact_jid).updateAvatar(url)
157 except TypeError: 178 except TypeError:
158 pass 179 pass
159 180
160 def updateNick(self, jid_, new_nick): 181 def updateNick(self, contact_jid, new_nick):
161 """Update the avatar of the given contact 182 """Update the avatar of the given contact.
162 183
163 @param jid_ (jid.JID): contact jid 184 @param contact_jid (jid.JID): contact JID
164 @param new_nick (unicode): new nick of the contact 185 @param new_nick (unicode): new nick of the contact
165 """ 186 """
166 try: 187 try:
167 self.getContactBox(jid_).updateNick(new_nick) 188 self.getContactBox(contact_jid).updateNick(new_nick)
168 except TypeError: 189 except TypeError:
169 pass 190 pass
170 191
171 192
172 193