changeset 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 a676cb07c1cb
children ee4d7e923e92
files cagou/plugins/plugin_wid_contact_list.kv cagou/plugins/plugin_wid_contact_list.py
diffstat 2 files changed, 76 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/plugins/plugin_wid_contact_list.kv	Tue Jun 26 20:26:21 2018 +0200
+++ b/cagou/plugins/plugin_wid_contact_list.kv	Tue Jun 26 20:27:23 2018 +0200
@@ -15,6 +15,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 #:import _ sat.core.i18n._
+#:import e kivy.utils.escape_markup
 
 <AddContactMenu>:
     padding: dp(20)
@@ -22,7 +23,7 @@
     Label:
         size_hint: 1, None
         color: 1, 1, 1, 1
-        text: root.instructions
+        text: _("Please enter new contact JID")
         text_size: root.width, None
         size: self.texture_size
         halign: "center"
@@ -40,6 +41,39 @@
     Widget:
 
 
+<DelContactMenu>:
+    padding: dp(20)
+    spacing: dp(10)
+    Avatar:
+        id: avatar
+        size_hint: 1, None
+        height: dp(60)
+        source: root.contact_item.data.get('avatar', app.default_avatar)
+        allow_stretch: True
+    Label:
+        size_hint: 1, None
+        color: 1, 1, 1, 1
+        text: _("Are you sure you wand to remove [b]{name}[/b] from your contact list?").format(name=e(root.contact_item.jid))
+        markup: True
+        text_size: root.width, None
+        size: self.texture_size
+        halign: "center"
+    BoxLayout:
+        Button:
+            background_color: 1, 0, 0, 1
+            size_hint: 0.5, None
+            height: sp(50)
+            text: _("yes, remove it")
+            bold: True
+            on_release: root.do_delete_contact()
+        Button:
+            size_hint: 0.5, None
+            height: sp(50)
+            text: _("no, keep it")
+            on_release: root.hide()
+    Widget:
+
+
 <ContactItem>:
     size_hint: None, None
     width: self.base_width
--- 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: