diff cagou/core/common.py @ 348:38fd457b2158

core (common, share_widget): new JidSelector widget: - A widget to select jid among a list is commonly needed, so it has been implemented in core.common, based on the code from ShareWidget - ShareWidget has been adapted accordingly - new ContactButton class in core.common
author Goffi <goffi@goffi.org>
date Fri, 17 Jan 2020 18:44:32 +0100
parents e2b51663d8b8
children 4d660b252487
line wrap: on
line diff
--- a/cagou/core/common.py	Sat Jan 04 16:24:57 2020 +0100
+++ b/cagou/core/common.py	Fri Jan 17 18:44:32 2020 +0100
@@ -1,5 +1,4 @@
-#!/usr//bin/env python2
-# -*- coding: utf-8 -*-
+#!/usr/bin/env python3
 
 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
 # Copyright (C) 2016-2019 Jérôme Poisson (goffi@goffi.org)
@@ -19,18 +18,21 @@
 
 """common simple widgets"""
 
+import json
+from functools import partial
 from sat.core.i18n import _
+from sat.core import log as logging
 from kivy.uix.image import Image
 from kivy.uix.label import Label
 from kivy.uix.behaviors import ButtonBehavior
 from kivy.uix.behaviors import ToggleButtonBehavior
 from kivy.uix.boxlayout import BoxLayout
+from kivy.uix.scrollview import ScrollView
+from kivy.event import EventDispatcher
 from kivy.metrics import dp
-from cagou.core.constants import Const as C
 from kivy import properties
+from cagou.core.constants import Const as C
 from cagou import G
-import json
-from sat.core import log as logging
 
 log = logging.getLogger(__name__)
 
@@ -46,12 +48,20 @@
 
 
 class ContactItem(BoxLayout):
+    """An item from ContactList
+
+    The item will drawn as an icon (JID avatar) with its jid below
+    """
     base_width = dp(150)
     profile = properties.StringProperty()
     data = properties.DictProperty()
     jid = properties.StringProperty('')
 
 
+class ContactButton(ButtonBehavior, ContactItem):
+    pass
+
+
 class JidItem(BoxLayout):
     bg_color = properties.ListProperty([0.2, 0.2, 0.2, 1])
     color = properties.ListProperty([1, 1, 1, 1])
@@ -144,3 +154,42 @@
         else:
             icon_wid = ActionSymbol(symbol=symbol)
             self.add_widget(icon_wid)
+
+
+class JidSelector(ScrollView, EventDispatcher):
+    layout = properties.ObjectProperty(None)
+
+    def __init__(self, **kwargs):
+        self.register_event_type('on_select')
+        super().__init__(**kwargs)
+
+    def on_kv_post(self, wid):
+        self.addRosterContacts()
+
+    def on_select(self, wid):
+        pass
+
+    def on_parent(self, wid, parent):
+        if parent is None:
+            log.debug("removing contactsFilled listener")
+            G.host.removeListener("contactsFilled", self.onContactsFilled)
+        else:
+            G.host.addListener("contactsFilled", self.onContactsFilled)
+
+    def onContactsFilled(self, profile):
+        log.debug("onContactsFilled event received")
+        self.addRosterContacts()
+
+    def addRosterContacts(self):
+        log.debug("starting addRosterContacts")
+        self.layout.clear_widgets()
+        for profile in G.host.profiles:
+            contact_list = G.host.contact_lists[profile]
+            for entity_jid in sorted(contact_list.roster):
+                item = ContactButton(
+                    jid=entity_jid,
+                    data=contact_list.getItem(entity_jid),
+                    profile=profile,
+                )
+                item.bind(on_press=partial(self.dispatch, 'on_select'))
+                self.layout.add_widget(item)