diff cagou/plugins/plugin_wid_contact_list.py @ 221:e1a385a791cc

plugin contact list: implemented contact addition
author Goffi <goffi@goffi.org>
date Tue, 26 Jun 2018 07:36:11 +0200
parents 9faccd140119
children 9e5f9f0cee48
line wrap: on
line diff
--- a/cagou/plugins/plugin_wid_contact_list.py	Tue Jun 26 07:11:16 2018 +0200
+++ b/cagou/plugins/plugin_wid_contact_list.py	Tue Jun 26 07:36:11 2018 +0200
@@ -27,12 +27,15 @@
 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 kivy.metrics import dp
 from kivy import properties
 from cagou.core import cagou_widget
 from cagou.core import image
 from cagou import G
+from functools import partial
 import bisect
+import re
 
 
 PLUGIN_INFO = {
@@ -43,12 +46,47 @@
 }
 
 
+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)
+
+    def __init__(self, **kwargs):
+        super(AddContactMenu, self).__init__(**kwargs)
+        if self.profile is None:
+            log.warning(_(u"profile not set in AddContactMenu"))
+            self.profile = next(iter(G.host.profiles))
+
+    def addContact(self, contact_jid):
+        """Actually add the contact
+
+        @param contact_jid(unicode): jid of the contact to add
+        """
+        contact_jid = contact_jid.strip()
+        # FIXME: trivial jid verification
+        if not contact_jid or not re.match(r"[^@ ]+@[^@ ]+", contact_jid):
+            return
+        contact_jid = jid.JID(contact_jid).bare
+        G.host.bridge.addContact(contact_jid,
+            self.profile,
+            callback=lambda: G.host.addNote(
+                _(u"contact request"),
+                _(u"a contact request has been sent to {contact_jid}").format(
+                    contact_jid=contact_jid)),
+            errback=partial(G.host.errback,
+                title=_(u"can't add contact"),
+                message=_(u"error while trying to add contact: {msg}")))
+        self.hide()
+
+
 class Avatar(image.Image):
     pass
 
 
 class ContactItem(ButtonBehavior, BoxLayout):
     base_width = dp(150)
+    profile = properties.StringProperty()
     data = properties.DictProperty()
     jid = properties.StringProperty('')
 
@@ -56,6 +94,7 @@
         super(ContactItem, self).__init__(**kwargs)
 
     def on_release(self):
+        assert self.profile
         # XXX: for now clicking on an item launch the corresponding Chat widget
         #      behaviour should change in the future
         try:
@@ -69,7 +108,7 @@
             factory = plg_infos['factory']
             G.host.switchWidget(self, factory(plg_infos,
                                               jid.JID(self.jid),
-                                              profiles=iter(G.host.profiles)))
+                                              profiles=[self.profile]))
 
 
 class ContactList(QuickContactList, cagou_widget.CagouWidget, FilterBehavior):
@@ -86,6 +125,12 @@
             raise NotImplementedError('multi profiles is not implemented yet')
         self.update(profile=next(iter(self.profiles)))
 
+    def addContactMenu(self):
+        """Show the "add a contact" menu"""
+        # FIXME: for now we add contact to the first profile we find
+        profile = next(iter(self.profiles))
+        AddContactMenu(profile=profile).show()
+
     def onHeaderInputComplete(self, wid, text):
         self.do_filter(self.layout.children,
                        text,
@@ -102,7 +147,7 @@
         @param profile(unicode): profile where the contact is
         """
         data = G.host.contact_lists[profile].getItem(bare_jid)
-        wid = ContactItem(data=data, jid=bare_jid)
+        wid = ContactItem(profile=profile, data=data, jid=bare_jid)
         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)
@@ -114,7 +159,7 @@
             log.debug("full contact list update")
             self.layout.clear_widgets()
             for bare_jid, data in self.items_sorted.iteritems():
-                wid = ContactItem(data=data, jid=bare_jid)
+                wid = ContactItem(profile=profile, data=data, jid=bare_jid)
                 self.layout.add_widget(wid)
                 self._wid_map[(profile, bare_jid)] = wid
         elif type_ == C.UPDATE_MODIFY: