comparison cagou/core/common.py @ 406:03554ad70846

common (jidSelector): replace implitict_update mechanism by real-time update: instead of having the possibility to (de)activate implicit update, real time update of items is now done. For now only the notifications and opened chat items are updated in real time.
author Goffi <goffi@goffi.org>
date Wed, 12 Feb 2020 20:02:58 +0100
parents 84ff5c917064
children 364d2c8eb476
comparison
equal deleted inserted replaced
405:84ff5c917064 406:03554ad70846
209 # layout. 209 # layout.
210 # - a kivy Widget, which will be added to the layout (notable useful with 210 # - a kivy Widget, which will be added to the layout (notable useful with
211 # common_widgets.CategorySeparator) 211 # common_widgets.CategorySeparator)
212 # - a callable, which must return an iterable of kwargs for ContactButton 212 # - a callable, which must return an iterable of kwargs for ContactButton
213 to_show = properties.ListProperty(['roster']) 213 to_show = properties.ListProperty(['roster'])
214 # if True, update() is called automatically when widget is created 214
215 # if False, you'll have to call update() at least once manually 215 # TODO: roster and bookmarks must be updated in real time, like for opened_chats
216 implicit_update = properties.ObjectProperty(True) 216
217 217
218 def __init__(self, **kwargs): 218 def __init__(self, **kwargs):
219 self.register_event_type('on_select') 219 self.register_event_type('on_select')
220 # list of layouts containing items 220 # list of layouts containing items
221 self.items_layouts = [] 221 self.items_layouts = []
222 # jid to list of ContactButton instances map 222 # jid to list of ContactButton instances map
223 self.items_map = {} 223 self.items_map = {}
224 super().__init__(**kwargs) 224 super().__init__(**kwargs)
225 225
226 def on_kv_post(self, wid): 226 def on_kv_post(self, wid):
227 if self.implicit_update: 227 self.update()
228 self.update()
229 228
230 def on_select(self, wid): 229 def on_select(self, wid):
231 pass 230 pass
232 231
233 def on_parent(self, wid, parent): 232 def on_parent(self, wid, parent):
234 if parent is None: 233 if parent is None:
235 log.debug("removing contactsFilled listener") 234 log.debug("removing listeners")
236 G.host.removeListener("contactsFilled", self.onContactsFilled) 235 G.host.removeListener("contactsFilled", self.onContactsFilled)
237 else: 236 G.host.removeListener("notification", self.onNotification)
237 G.host.removeListener("notificationsClear", self.onNotificationsClear)
238 G.host.removeListener(
239 "widgetNew", self.onWidgetNew, ignore_missing=True)
240 G.host.removeListener(
241 "widgetDeleted", self.onWidgetDeleted, ignore_missing=True)
242 else:
243 log.debug("adding listeners")
238 G.host.addListener("contactsFilled", self.onContactsFilled) 244 G.host.addListener("contactsFilled", self.onContactsFilled)
245 G.host.addListener("notification", self.onNotification)
246 G.host.addListener("notificationsClear", self.onNotificationsClear)
239 247
240 def onContactsFilled(self, profile): 248 def onContactsFilled(self, profile):
241 log.debug("onContactsFilled event received") 249 log.debug("onContactsFilled event received")
242 self.update() 250 self.update()
243 251
252 def onNotification(self, entity, notification_data, profile):
253 for item in self.items_map.get(entity.bare, []):
254 notifs = list(G.host.getNotifs(entity.bare, profile=profile))
255 item.badge_text = str(len(notifs))
256
257 def onNotificationsClear(self, entity, type_, profile):
258 for item in self.items_map.get(entity.bare, []):
259 item.badge_text = ''
260
261 def onWidgetNew(self, wid):
262 if not isinstance(wid, quick_chat.QuickChat):
263 return
264 item = self.getItemFromWid(wid)
265 if item is None:
266 return
267 idx = 0
268 for child in self.opened_chats.children:
269 if isinstance(child, self.item_class) and child < item:
270 break
271 idx+=1
272 self.opened_chats.add_widget(item, index=idx)
273
274 def onWidgetDeleted(self, wid):
275 if not isinstance(wid, quick_chat.QuickChat):
276 return
277
278 for child in self.opened_chats.children:
279 if not isinstance(child, self.item_class):
280 continue
281 if child.jid.bare == wid.target.bare:
282 self.opened_chats.remove_widget(child)
283 break
244 284
245 def _createItem(self, **kwargs): 285 def _createItem(self, **kwargs):
246 item = self.item_class(**kwargs) 286 item = self.item_class(**kwargs)
247 jid = kwargs['jid'] 287 jid = kwargs['jid']
248 self.items_map.setdefault(jid, []).append(item) 288 self.items_map.setdefault(jid, []).append(item)
300 item.badge_text = str(len(notifs)) 340 item.badge_text = str(len(notifs))
301 item.bind(on_press=partial(self.dispatch, 'on_select')) 341 item.bind(on_press=partial(self.dispatch, 'on_select'))
302 return item 342 return item
303 343
304 def addOpenedChatsItems(self): 344 def addOpenedChatsItems(self):
345 G.host.addListener("widgetNew", self.onWidgetNew)
346 G.host.addListener("widgetDeleted", self.onWidgetDeleted)
305 self.opened_chats = category_layout = self.addCategoryLayout(_("Opened chats")) 347 self.opened_chats = category_layout = self.addCategoryLayout(_("Opened chats"))
306 widgets = sorted(G.host.widgets.getWidgets( 348 widgets = sorted(G.host.widgets.getWidgets(
307 quick_chat.QuickChat, 349 quick_chat.QuickChat,
308 profiles = G.host.profiles, 350 profiles = G.host.profiles,
309 with_duplicates=False)) 351 with_duplicates=False))