diff cagou/plugins/plugin_wid_contact_list.py @ 223:9e5f9f0cee48

plugin contact list: use new TouchMenuBehaviour to implement contact deletion
author Goffi <goffi@goffi.org>
date Tue, 26 Jun 2018 20:27:23 +0200
parents e1a385a791cc
children ff1efdeff53f
line wrap: on
line diff
--- a/cagou/plugins/plugin_wid_contact_list.py	Tue Jun 26 20:26:21 2018 +0200
+++ b/cagou/plugins/plugin_wid_contact_list.py	Tue Jun 26 20:27:23 2018 +0200
@@ -25,9 +25,8 @@
 from sat_frontends.quick_frontend.quick_contact_list import QuickContactList
 from sat_frontends.tools import jid
 from kivy.uix.boxlayout import BoxLayout
-from kivy.uix.behaviors import ButtonBehavior
 from cagou.core.utils import FilterBehavior
-from cagou.core.menu import SideMenu
+from cagou.core.menu import SideMenu, TouchMenuBehaviour, TouchMenuItemBehaviour
 from kivy.metrics import dp
 from kivy import properties
 from cagou.core import cagou_widget
@@ -48,7 +47,6 @@
 
 class AddContactMenu(SideMenu):
     profile = properties.StringProperty()
-    instructions = properties.StringProperty(_(u"Please enter new contact JID"))
     size_hint_close = (1, 0)
     size_hint_open = (1, 0.5)
 
@@ -63,6 +61,7 @@
 
         @param contact_jid(unicode): jid of the contact to add
         """
+        self.hide()
         contact_jid = contact_jid.strip()
         # FIXME: trivial jid verification
         if not contact_jid or not re.match(r"[^@ ]+@[^@ ]+", contact_jid):
@@ -77,14 +76,35 @@
             errback=partial(G.host.errback,
                 title=_(u"can't add contact"),
                 message=_(u"error while trying to add contact: {msg}")))
+
+
+class DelContactMenu(SideMenu):
+    size_hint_close = (1, 0)
+    size_hint_open = (1, 0.5)
+
+    def __init__(self, contact_item, **kwargs):
+        self.contact_item = contact_item
+        super(DelContactMenu, self).__init__(**kwargs)
+
+    def do_delete_contact(self):
         self.hide()
+        G.host.bridge.delContact(self.contact_item.jid.bare,
+        self.contact_item.profile,
+        callback=lambda: G.host.addNote(
+            _(u"contact removed"),
+            _(u"{contact_jid} has been removed from your contacts list").format(
+                contact_jid=self.contact_item.jid.bare)),
+        errback=partial(G.host.errback,
+            title=_(u"can't remove contact"),
+            message=_(u"error while trying to remove contact: {msg}")))
+
 
 
 class Avatar(image.Image):
     pass
 
 
-class ContactItem(ButtonBehavior, BoxLayout):
+class ContactItem(TouchMenuItemBehaviour, BoxLayout):
     base_width = dp(150)
     profile = properties.StringProperty()
     data = properties.DictProperty()
@@ -93,7 +113,7 @@
     def __init__(self, **kwargs):
         super(ContactItem, self).__init__(**kwargs)
 
-    def on_release(self):
+    def do_item_action(self, touch):
         assert self.profile
         # XXX: for now clicking on an item launch the corresponding Chat widget
         #      behaviour should change in the future
@@ -110,8 +130,16 @@
                                               jid.JID(self.jid),
                                               profiles=[self.profile]))
 
+    def getMenuChoices(self):
+        choices = []
+        choices.append(dict(text=_(u'delete'),
+                            index=len(choices)+1,
+                            callback=self.main_wid.removeContact))
+        return choices
 
-class ContactList(QuickContactList, cagou_widget.CagouWidget, FilterBehavior):
+
+class ContactList(QuickContactList, cagou_widget.CagouWidget, FilterBehavior,
+                  TouchMenuBehaviour):
     float_layout = properties.ObjectProperty()
     layout = properties.ObjectProperty()
 
@@ -131,6 +159,11 @@
         profile = next(iter(self.profiles))
         AddContactMenu(profile=profile).show()
 
+    def removeContact(self, menu_label):
+        item = self.menu_item
+        self.clear_menu()
+        DelContactMenu(contact_item=item).show()
+
     def onHeaderInputComplete(self, wid, text):
         self.do_filter(self.layout.children,
                        text,
@@ -147,7 +180,7 @@
         @param profile(unicode): profile where the contact is
         """
         data = G.host.contact_lists[profile].getItem(bare_jid)
-        wid = ContactItem(profile=profile, data=data, jid=bare_jid)
+        wid = ContactItem(profile=profile, data=data, jid=bare_jid, main_wid=self)
         child_jids = [c.jid for c in reversed(self.layout.children)]
         idx = bisect.bisect_right(child_jids, bare_jid)
         self.layout.add_widget(wid, -idx)
@@ -159,7 +192,7 @@
             log.debug("full contact list update")
             self.layout.clear_widgets()
             for bare_jid, data in self.items_sorted.iteritems():
-                wid = ContactItem(profile=profile, data=data, jid=bare_jid)
+                wid = ContactItem(profile=profile, data=data, jid=bare_jid, main_wid=self)
                 self.layout.add_widget(wid)
                 self._wid_map[(profile, bare_jid)] = wid
         elif type_ == C.UPDATE_MODIFY: