core, quick_frontend, primitivus, wix: Contacts List refactoring phase 1:

- QuickContactManagement is not used anymore and will be removed, ContactList + Core are used instead
	- disconnected contacts are now displayed in Primitivus (M-d to show/hide them)
	- avatars are temporary unavailable in wix
	- new bridge method: getContactsFromGroup
This commit is contained in:
Goffi 2012-09-25 00:58:34 +02:00
commit b44412ec0d
4 changed files with 207 additions and 130 deletions

View file

@ -262,11 +262,8 @@ class Chat(urwid.WidgetWrap, QuickChat):
def startGame(self, game_type, referee, players):
"""Configure the chat window to start a game"""
if game_type=="Tarot":
try:
self.tarot_wid = CardGame(self, referee, players, self.nick)
self.__appendGamePanel(self.tarot_wid)
except e:
self.host.debug()
self.tarot_wid = CardGame(self, referee, players, self.nick)
self.__appendGamePanel(self.tarot_wid)
def getGame(self, game_type):
"""Return class managing the game type"""
@ -288,6 +285,11 @@ class Chat(urwid.WidgetWrap, QuickChat):
#MISC EVENTS#
def onFileSelected(self, filepath):
self.host.removePopUp()
full_jid = self.host.CM.get_full(self.target)
#FIXME: check last_resource: what if self.target.resource exists ?
last_resource = self.host.bridge.getLastResource(unicode(self.target.short), self.host.profile)
if last_resource:
full_jid = JID("%s/%s" % (self.target.short, last_resource))
else:
full_jid = self.target
id = self.host.bridge.sendFile(full_jid, filepath, {}, self.host.profile)
self.host.addProgress(id,filepath)

View file

@ -54,6 +54,7 @@ __builtin__.__dict__['const_PALETTE'] = [
('warning', 'light red', 'default'),
('progress_normal', 'default', 'black'),
('progress_complete', 'default', 'light red'),
('show_disconnected', 'dark gray', 'default'),
('show_normal', 'default', 'default'),
('show_normal_focus', 'default, bold', 'default'),
('show_chat', 'dark green', 'default'),
@ -67,7 +68,8 @@ __builtin__.__dict__['const_PALETTE'] = [
('status', 'yellow', 'default'),
('status_focus', 'yellow, bold', 'default'),
]
__builtin__.__dict__['const_SHOW_ICON'] = {"": (u'✔', "show_normal"),
__builtin__.__dict__['const_SHOW_ICON'] = {"unavailable": (u'⨯', "show_disconnected"),
"": (u'✔', "show_normal"),
"chat": (u'✆', "show_chat"),
"away": (u'✈', "show_away"),
"dnd": (u'✖', "show_dnd"),

View file

@ -28,12 +28,13 @@ from sat.tools.jid import JID
class ContactList(urwid.WidgetWrap, QuickContactList):
signals = ['click','change']
def __init__(self, host, CM, on_click=None, on_change=None, user_data=None):
def __init__(self, host, on_click=None, on_change=None, user_data=None):
self.host = host
self.selected = None
self.groups={}
self.alert_jid=set()
self.show_status = False
self.show_disconnected = False
#we now build the widget
self.frame = urwid.Frame(self.__buildList())
@ -43,9 +44,9 @@ class ContactList(urwid.WidgetWrap, QuickContactList):
urwid.connect_signal(self, 'click', on_click, user_data)
if on_change:
urwid.connect_signal(self, 'change', on_change, user_data)
QuickContactList.__init__(self, CM)
QuickContactList.__init__(self)
def _update(self):
def update(self):
"""Update display, keep focus"""
widget, position = self.frame.body.get_focus()
self.frame.body = self.__buildList()
@ -53,10 +54,16 @@ class ContactList(urwid.WidgetWrap, QuickContactList):
self.frame.body.set_focus(position)
self.host.redraw()
def update_jid(self, jid):
self.update()
def keypress(self, size, key):
if key == "meta s": #user wants to (un)hide contacts' statuses
self.show_status = not self.show_status
self._update()
self.update()
elif key == "meta d": #user wants to (un)hide disconnected contacts
self.show_disconnected = not self.show_disconnected
self.update()
return super(ContactList, self).keypress(size, key)
def __contains__(self, jid):
@ -77,12 +84,12 @@ class ContactList(urwid.WidgetWrap, QuickContactList):
def putAlert(self, jid):
"""Put an alert on the jid to get attention from user (e.g. for new message)"""
self.alert_jid.add(jid.short)
self._update()
self.update()
def __groupClicked(self, group_wid):
group = self.groups[group_wid.getValue()]
group[0] = not group[0]
self._update()
self.update()
self.setFocus(group_wid.getValue())
def __contactClicked(self, contact_wid, selected):
@ -92,7 +99,7 @@ class ContactList(urwid.WidgetWrap, QuickContactList):
widget.setState(widget.data == self.selected, invisible=True)
if self.selected in self.alert_jid:
self.alert_jid.remove(self.selected)
self._update()
self.update()
self._emit('click')
def __buildContact(self, content, param_contacts):
@ -100,13 +107,20 @@ class ContactList(urwid.WidgetWrap, QuickContactList):
@param content: widget list, e.g. SimpleListWalker
@param contacts: list of JID"""
contacts = list(param_contacts)
contacts.sort()
widgets = [] #list of built widgets
for contact in contacts:
jid=JID(contact)
name = self.CM.getAttr(jid,'name')
nick = self.CM.getAttr(jid,'nick')
status = self.CM.getAttr(jid, 'status')
show = self.CM.getAttr(jid, 'show')
name = self.getCache(jid, 'name')
nick = self.getCache(jid, 'nick')
status = self.getCache(jid, 'status')
show = self.getCache(jid, 'show')
if show == None:
show = "unavailable"
if (not self.show_disconnected and show == "unavailable"
and not contact in self.alert_jid and contact != self.selected):
continue
show_icon, show_attr = const_SHOW_ICON.get(show,('','default'))
contact_disp = ('alert' if contact in self.alert_jid else show_attr, nick or name or jid.node or jid.short)
display = [ show_icon + " " , contact_disp]
@ -118,6 +132,12 @@ class ContactList(urwid.WidgetWrap, QuickContactList):
selected = contact==self.selected,
header=header)
widget.data = contact
widget.comp = contact_disp[1].lower() #value to use for sorting
widgets.append(widget)
widgets.sort(key=lambda widget: widget.comp)
for widget in widgets:
content.append(widget)
urwid.connect_signal(widget, 'change', self.__contactClicked)
@ -125,7 +145,7 @@ class ContactList(urwid.WidgetWrap, QuickContactList):
"""Build the main contact list widget"""
content = urwid.SimpleListWalker([])
group_keys = self.groups.keys()
group_keys.sort()
group_keys.sort(key = lambda x: x.lower() if x else x)
for key in group_keys:
unfolded = self.groups[key][0]
if key!=None:
@ -145,28 +165,30 @@ class ContactList(urwid.WidgetWrap, QuickContactList):
widget.setState(False, invisible=True)
def get_contact(self):
def getContact(self):
"""Return contact currently selected"""
return self.selected
def clear_contacts(self):
def clearContacts(self):
"""clear all the contact list"""
self.groups={}
self.selected = None
self.unselectAll()
self._update()
self.update()
def replace(self, jid, groups=[None]):
def replace(self, jid, groups=None, attributes=None):
"""add a contact to the list if doesn't exist, else update it"""
if not groups:
groups = [None]
if not attributes:
attributes={}
assert isinstance(groups, list)
assert isinstance(jid, JID)
if not groups:
groups=[None]
for group in groups:
if not self.groups.has_key(group):
self.groups[group] = [True,set()] #[unfold,list_of_contacts]
self.groups[group][1].add(jid.short)
self._update()
self.update()
"""contacts = self.list_wid.getAllValues()
@ -176,10 +198,6 @@ class ContactList(urwid.WidgetWrap, QuickContactList):
self.list_wid.changeValues(contacts)
self._emit('change')"""
def disconnect(self, jid):
"""mark a contact disconnected"""
self.remove(jid.short)
def remove(self, param_jid):
"""remove a contact from the list"""
groups_to_remove = []
@ -192,7 +210,7 @@ class ContactList(urwid.WidgetWrap, QuickContactList):
groups_to_remove.append(group)
for group in groups_to_remove:
del self.groups[group]
self._update()
self.update()
def add(self, jid, param_groups=[None]):
"""add a contact to the list"""

View file

@ -56,7 +56,6 @@ class ChatList(QuickChatList):
class PrimitivusApp(QuickApp):
def __init__(self):
self.CM = QuickContactManagement() #FIXME: not the best place
QuickApp.__init__(self)
## main loop setup ##
@ -141,12 +140,12 @@ class PrimitivusApp(QuickApp):
elif input == 'f2': #user wants to (un)hide the contact_list
try:
center_widgets = self.center_part.widget_list
if self.contactList in center_widgets:
if self.contact_list in center_widgets:
self.center_part.set_focus(0) #necessary as the focus change to the next object, we can go out of range if we are on the last object of self.center_part
center_widgets.remove(self.contactList)
center_widgets.remove(self.contact_list)
del self.center_part.column_types[0]
else:
center_widgets.insert(0, self.contactList)
center_widgets.insert(0, self.contact_list)
self.center_part.column_types.insert(0, ('weight', 2))
except AttributeError:
#The main widget is not built (probably in Profile Manager)
@ -196,9 +195,9 @@ class PrimitivusApp(QuickApp):
return menu_roller
def __buildMainWidget(self):
self.contactList = ContactList(self, self.CM, on_click = self.contactSelected, on_change=lambda w: self.redraw())
#self.center_part = urwid.Columns([('weight',2,self.contactList),('weight',8,Chat('',self))])
self.center_part = urwid.Columns([('weight',2,self.contactList), ('weight',8,urwid.Filler(urwid.Text('')))])
self.contact_list = ContactList(self, on_click = self.contactSelected, on_change=lambda w: self.redraw())
#self.center_part = urwid.Columns([('weight',2,self.contact_list),('weight',8,Chat('',self))])
self.center_part = urwid.Columns([('weight',2,self.contact_list), ('weight',8,urwid.Filler(urwid.Text('')))])
self.editBar = sat_widgets.AdvancedEdit(u'> ')
self.editBar.setCompletionMethod(self._nick_completion)
urwid.connect_signal(self.editBar,'click',self.onTextEntered)
@ -209,7 +208,7 @@ class PrimitivusApp(QuickApp):
def _nick_completion(self, text, completion_data):
"""Completion method which complete pseudo in group chat
for params, see AdvancedEdit"""
contact = self.contactList.get_contact() ###Based on the fact that there is currently only one contact selectable at once
contact = self.contact_list.getContact() ###Based on the fact that there is currently only one contact selectable at once
if contact:
chat = self.chat_wins[contact]
if chat.type != "group":
@ -266,7 +265,7 @@ class PrimitivusApp(QuickApp):
wid_idx = len(self.center_part.widget_list)-1
self.center_part.widget_list[wid_idx] = widget
self.menu_roller.removeMenu(_('Chat menu'))
self.contactList.unselectAll()
self.contact_list.unselectAll()
self.redraw()
def removeWindow(self):
@ -289,7 +288,7 @@ class PrimitivusApp(QuickApp):
self.notBar.setProgress(percentage)
def contactSelected(self, contact_list):
contact = contact_list.get_contact()
contact = contact_list.getContact()
if contact:
assert(len(self.center_part.widget_list)==2)
self.center_part.widget_list[1] = self.chat_wins[contact]
@ -297,7 +296,7 @@ class PrimitivusApp(QuickApp):
def onTextEntered(self, editBar):
"""Called when text is entered in the main edit bar"""
contact = self.contactList.get_contact() ###Based on the fact that there is currently only one contact selectableat once
contact = self.contact_list.getContact() ###Based on the fact that there is currently only one contact selectableat once
if contact:
chat = self.chat_wins[contact]
self.bridge.sendMessage(contact,
@ -311,8 +310,8 @@ class PrimitivusApp(QuickApp):
return
QuickApp.newMessage(self, from_jid, msg, type, to_jid, profile)
sender = JID(from_jid)
if JID(self.contactList.selected).short != sender.short:
self.contactList.putAlert(sender)
if JID(self.contact_list.selected).short != sender.short:
self.contact_list.putAlert(sender)
def _dialogOkCb(self, widget, data):
self.removePopUp()
@ -422,8 +421,8 @@ class PrimitivusApp(QuickApp):
def onRemoveContact(self, button):
self.removePopUp()
info(_("Unsubscribing %s presence"),self.contactList.get_contact())
self.bridge.delContact(self.contactList.get_contact(), profile_key=self.profile)
info(_("Unsubscribing %s presence"),self.contact_list.getContact())
self.bridge.delContact(self.contact_list.getContact(), profile_key=self.profile)
#MENU EVENTS#
def onConnectRequest(self, menu):
@ -460,7 +459,7 @@ class PrimitivusApp(QuickApp):
self.showPopUp(pop_up_widget)
def onRemoveContactRequest(self, menu):
contact = self.contactList.get_contact()
contact = self.contact_list.getContact()
if not contact:
self.showPopUp(sat_widgets.Alert(_("Error"), _("You have not selected any contact to delete !"), ok_cb=self.removePopUp))
else: