comparison src/browser/sat_browser/contact_widget.py @ 671:2201ff543a05 frontends_multi_profiles

browser_side: replace ContactsPanel and ContactBox init "parent" attribute with "host" + menus bound to the avatar can be parameterized
author souliane <souliane@mailoo.org>
date Mon, 09 Mar 2015 16:30:06 +0100
parents 267761bf7f08
children 849ffb24d5bf
comparison
equal deleted inserted replaced
670:a80c13249f5d 671:2201ff543a05
36 36
37 class ContactLabel(HTML): 37 class ContactLabel(HTML):
38 """Display a contact in HTML, selecting best display (jid/nick/etc)""" 38 """Display a contact in HTML, selecting best display (jid/nick/etc)"""
39 39
40 def __init__(self, host, jid_, display=C.CONTACT_DEFAULT_DISPLAY): 40 def __init__(self, host, jid_, display=C.CONTACT_DEFAULT_DISPLAY):
41 """
42
43 @param host (SatWebFrontend): host instance
44 @param jid_ (jid.JID): contact JID
45 @param display (tuple): prioritize the display methods of the contact's
46 label with values in ("jid", "nick", "bare", "resource").
47 """
41 # TODO: add a listener for nick changes 48 # TODO: add a listener for nick changes
42 HTML.__init__(self) 49 HTML.__init__(self)
43 self.host = host 50 self.host = host
44 self.jid = jid_ 51 self.jid = jid_
45 if "nick" in display: 52 if "nick" in display:
105 self.items[0].setHTML('<img src="%s" />' % url) 112 self.items[0].setHTML('<img src="%s" />' % url)
106 113
107 114
108 class ContactBox(VerticalPanel, ClickHandler, libervia_widget.DragLabel): 115 class ContactBox(VerticalPanel, ClickHandler, libervia_widget.DragLabel):
109 116
110 def __init__(self, parent, jid_, style_name=None, display=C.CONTACT_DEFAULT_DISPLAY): 117 def __init__(self, host, jid_, style_name=None, menu_types=None, display=C.CONTACT_DEFAULT_DISPLAY):
111 """ 118 """
112 @param parent (ContactPanel): ContactPanel hosting this box 119 @param host (SatWebFrontend): host instance
113 @param jid_ (jid.JID): contact JID 120 @param jid_ (jid.JID): contact JID
121 @param style_name (unicode): CSS style name
122 @param menu_types (tuple): define the menu types that fit this
123 contact panel, with values from the menus type constants.
124 @param contacts_display (tuple): prioritize the display methods of the
125 contact's label with values in ("jid", "nick", "bare", "resource").
126
114 """ 127 """
115 VerticalPanel.__init__(self, StyleName=style_name or 'contactBox', VerticalAlignment='middle') 128 VerticalPanel.__init__(self, StyleName=style_name or 'contactBox', VerticalAlignment='middle')
116 ClickHandler.__init__(self) 129 ClickHandler.__init__(self)
117 libervia_widget.DragLabel.__init__(self, jid_, "CONTACT", parent.host) 130 libervia_widget.DragLabel.__init__(self, jid_, "CONTACT", host)
118 self.jid = jid_ 131 self.jid = jid_
119 self.label = ContactLabel(parent.host, self.jid, display=display) 132 self.label = ContactLabel(host, self.jid, display=display)
120 self.avatar = ContactMenuBar(self, parent.host) if parent.handle_menu else Image() 133 self.menu_types = menu_types
121 self.updateAvatar(parent.host.getAvatarURL(self.jid.bare)) 134 self.avatar = ContactMenuBar(self, host) if menu_types else Image()
135 try: # FIXME: dirty hack to force using an Image when the menu is actually empty
136 self.avatar.items[0]
137 except IndexError:
138 self.avatar = Image()
139 self.updateAvatar(host.getAvatarURL(self.jid.bare))
122 self.add(self.avatar) 140 self.add(self.avatar)
123 self.add(self.label) 141 self.add(self.label)
124 self.addClickListener(self) 142 self.addClickListener(self)
125 143
126 def addMenus(self, menu_bar): 144 def addMenus(self, menu_bar):
127 menu_bar.addCachedMenus(C.MENU_ROSTER_JID_CONTEXT, {'jid': unicode(self.jid.bare)}) 145 for menu_type in self.menu_types:
128 menu_bar.addCachedMenus(C.MENU_JID_CONTEXT, {'jid': unicode(self.jid.bare)}) 146 menu_bar.addCachedMenus(menu_type, {'jid': unicode(self.jid.bare)})
129 147
130 def setAlert(self, alert): 148 def setAlert(self, alert):
131 """Show a visual indicator 149 """Show a visual indicator
132 150
133 @param alert: True if alert indicator show be shown""" 151 @param alert: True if alert indicator show be shown"""
148 self.label.updateNick(new_nick) 166 self.label.updateNick(new_nick)
149 167
150 def onClick(self, sender): 168 def onClick(self, sender):
151 try: 169 try:
152 self.parent.onClick(self.jid.bare) 170 self.parent.onClick(self.jid.bare)
153 except AttributeError: 171 except (AttributeError, TypeError):
154 pass 172 pass
155 else: 173 else:
156 self.setAlert(False) 174 self.setAlert(False)