comparison cagou/core/common.py @ 491:203755bbe0fe

massive refactoring from camelCase -> snake_case. See backend commit log for more details
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:44:32 +0200
parents 3c9ba4a694ef
children
comparison
equal deleted inserted replaced
490:962d17c4078c 491:203755bbe0fe
51 class Avatar(Image): 51 class Avatar(Image):
52 data = properties.DictProperty(allownone=True) 52 data = properties.DictProperty(allownone=True)
53 53
54 def on_kv_post(self, __): 54 def on_kv_post(self, __):
55 if not self.source: 55 if not self.source:
56 self.source = G.host.getDefaultAvatar() 56 self.source = G.host.get_default_avatar()
57 57
58 def on_data(self, __, data): 58 def on_data(self, __, data):
59 if data is None: 59 if data is None:
60 self.source = G.host.getDefaultAvatar() 60 self.source = G.host.get_default_avatar()
61 else: 61 else:
62 self.source = data['path'] 62 self.source = data['path']
63 63
64 64
65 class NotifLabel(Label): 65 class NotifLabel(Label):
82 jid = properties.StringProperty('') 82 jid = properties.StringProperty('')
83 83
84 def on_kv_post(self, __): 84 def on_kv_post(self, __):
85 if ((self.profile and self.jid and self.data is not None 85 if ((self.profile and self.jid and self.data is not None
86 and ('avatar' not in self.data or 'nicknames' not in self.data))): 86 and ('avatar' not in self.data or 'nicknames' not in self.data))):
87 G.host.bridge.identityGet( 87 G.host.bridge.identity_get(
88 self.jid, ['avatar', 'nicknames'], True, self.profile, 88 self.jid, ['avatar', 'nicknames'], True, self.profile,
89 callback=self._identityGetCb, 89 callback=self._identity_get_cb,
90 errback=partial( 90 errback=partial(
91 G.host.errback, 91 G.host.errback,
92 message=_("Can't retrieve identity for {jid}: {{msg}}").format( 92 message=_("Can't retrieve identity for {jid}: {{msg}}").format(
93 jid=self.jid) 93 jid=self.jid)
94 ) 94 )
95 ) 95 )
96 96
97 def _identityGetCb(self, identity_raw): 97 def _identity_get_cb(self, identity_raw):
98 identity_data = data_format.deserialise(identity_raw) 98 identity_data = data_format.deserialise(identity_raw)
99 self.data.update(identity_data) 99 self.data.update(identity_data)
100 100
101 def on_badge_text(self, wid, text): 101 def on_badge_text(self, wid, text):
102 if text: 102 if text:
129 nick = properties.StringProperty() 129 nick = properties.StringProperty()
130 avatar = properties.ObjectProperty() 130 avatar = properties.ObjectProperty()
131 131
132 def on_avatar(self, wid, jid_): 132 def on_avatar(self, wid, jid_):
133 if self.jid and self.profile: 133 if self.jid and self.profile:
134 self.getImage() 134 self.get_image()
135 135
136 def on_jid(self, wid, jid_): 136 def on_jid(self, wid, jid_):
137 if self.profile and self.avatar: 137 if self.profile and self.avatar:
138 self.getImage() 138 self.get_image()
139 139
140 def on_profile(self, wid, profile): 140 def on_profile(self, wid, profile):
141 if self.jid and self.avatar: 141 if self.jid and self.avatar:
142 self.getImage() 142 self.get_image()
143 143
144 def getImage(self): 144 def get_image(self):
145 host = G.host 145 host = G.host
146 if host.contact_lists[self.profile].isRoom(self.jid.bare): 146 if host.contact_lists[self.profile].is_room(self.jid.bare):
147 self.avatar.opacity = 0 147 self.avatar.opacity = 0
148 self.avatar.source = "" 148 self.avatar.source = ""
149 else: 149 else:
150 self.avatar.source = ( 150 self.avatar.source = (
151 host.getAvatar(self.jid, profile=self.profile) 151 host.get_avatar(self.jid, profile=self.profile)
152 or host.getDefaultAvatar(self.jid) 152 or host.get_default_avatar(self.jid)
153 ) 153 )
154 154
155 155
156 class JidButton(ButtonBehavior, JidItem): 156 class JidButton(ButtonBehavior, JidItem):
157 pass 157 pass
313 pass 313 pass
314 314
315 def on_parent(self, wid, parent): 315 def on_parent(self, wid, parent):
316 if parent is None: 316 if parent is None:
317 log.debug("removing listeners") 317 log.debug("removing listeners")
318 G.host.removeListener("contactsFilled", self.onContactsFilled) 318 G.host.removeListener("contactsFilled", self.on_contacts_filled)
319 G.host.removeListener("notification", self.onNotification) 319 G.host.removeListener("notification", self.on_notification)
320 G.host.removeListener("notificationsClear", self.onNotificationsClear) 320 G.host.removeListener("notificationsClear", self.on_notifications_clear)
321 G.host.removeListener( 321 G.host.removeListener(
322 "widgetNew", self.onWidgetNew, ignore_missing=True) 322 "widgetNew", self.on_widget_new, ignore_missing=True)
323 G.host.removeListener( 323 G.host.removeListener(
324 "widgetDeleted", self.onWidgetDeleted, ignore_missing=True) 324 "widgetDeleted", self.on_widget_deleted, ignore_missing=True)
325 else: 325 else:
326 log.debug("adding listeners") 326 log.debug("adding listeners")
327 G.host.addListener("contactsFilled", self.onContactsFilled) 327 G.host.addListener("contactsFilled", self.on_contacts_filled)
328 G.host.addListener("notification", self.onNotification) 328 G.host.addListener("notification", self.on_notification)
329 G.host.addListener("notificationsClear", self.onNotificationsClear) 329 G.host.addListener("notificationsClear", self.on_notifications_clear)
330 330
331 def onContactsFilled(self, profile): 331 def on_contacts_filled(self, profile):
332 log.debug("onContactsFilled event received") 332 log.debug("on_contacts_filled event received")
333 self.update() 333 self.update()
334 334
335 def onNotification(self, entity, notification_data, profile): 335 def on_notification(self, entity, notification_data, profile):
336 for item in self.items_map.get(entity.bare, []): 336 for item in self.items_map.get(entity.bare, []):
337 notifs = list(G.host.getNotifs(entity.bare, profile=profile)) 337 notifs = list(G.host.get_notifs(entity.bare, profile=profile))
338 item.badge_text = str(len(notifs)) 338 item.badge_text = str(len(notifs))
339 339
340 def onNotificationsClear(self, entity, type_, profile): 340 def on_notifications_clear(self, entity, type_, profile):
341 for item in self.items_map.get(entity.bare, []): 341 for item in self.items_map.get(entity.bare, []):
342 item.badge_text = '' 342 item.badge_text = ''
343 343
344 def onWidgetNew(self, wid): 344 def on_widget_new(self, wid):
345 if not isinstance(wid, quick_chat.QuickChat): 345 if not isinstance(wid, quick_chat.QuickChat):
346 return 346 return
347 item = self.getItemFromWid(wid) 347 item = self.get_item_from_wid(wid)
348 if item is None: 348 if item is None:
349 return 349 return
350 idx = 0 350 idx = 0
351 for child in self.opened_chats.children: 351 for child in self.opened_chats.children:
352 if isinstance(child, self.item_class) and child < item: 352 if isinstance(child, self.item_class) and child < item:
353 break 353 break
354 idx+=1 354 idx+=1
355 self.opened_chats.add_widget(item, index=idx) 355 self.opened_chats.add_widget(item, index=idx)
356 356
357 def onWidgetDeleted(self, wid): 357 def on_widget_deleted(self, wid):
358 if not isinstance(wid, quick_chat.QuickChat): 358 if not isinstance(wid, quick_chat.QuickChat):
359 return 359 return
360 360
361 for child in self.opened_chats.children: 361 for child in self.opened_chats.children:
362 if not isinstance(child, self.item_class): 362 if not isinstance(child, self.item_class):
363 continue 363 continue
364 if child.jid.bare == wid.target.bare: 364 if child.jid.bare == wid.target.bare:
365 self.opened_chats.remove_widget(child) 365 self.opened_chats.remove_widget(child)
366 break 366 break
367 367
368 def _createItem(self, **kwargs): 368 def _create_item(self, **kwargs):
369 item = self.item_class(**kwargs) 369 item = self.item_class(**kwargs)
370 jid = kwargs['jid'] 370 jid = kwargs['jid']
371 self.items_map.setdefault(jid, []).append(item) 371 self.items_map.setdefault(jid, []).append(item)
372 return item 372 return item
373 373
375 log.debug("starting update") 375 log.debug("starting update")
376 self.layout.clear_widgets() 376 self.layout.clear_widgets()
377 for item in self.to_show: 377 for item in self.to_show:
378 if isinstance(item, str): 378 if isinstance(item, str):
379 if item == 'roster': 379 if item == 'roster':
380 self.addRosterItems() 380 self.add_roster_items()
381 elif item == 'bookmarks': 381 elif item == 'bookmarks':
382 self.addBookmarksItems() 382 self.add_bookmarks_items()
383 elif item == 'opened_chats': 383 elif item == 'opened_chats':
384 self.addOpenedChatsItems() 384 self.add_opened_chats_items()
385 else: 385 else:
386 log.error(f'unknown "to_show" magic string {item!r}') 386 log.error(f'unknown "to_show" magic string {item!r}')
387 elif isinstance(item, Widget): 387 elif isinstance(item, Widget):
388 self.layout.add_widget(item) 388 self.layout.add_widget(item)
389 elif callable(item): 389 elif callable(item):
390 items_kwargs = item() 390 items_kwargs = item()
391 for item_kwargs in items_kwargs: 391 for item_kwargs in items_kwargs:
392 item = self._createItem(**items_kwargs) 392 item = self._create_item(**items_kwargs)
393 item.bind(on_press=partial(self.dispatch, 'on_select')) 393 item.bind(on_press=partial(self.dispatch, 'on_select'))
394 self.layout.add_widget(item) 394 self.layout.add_widget(item)
395 else: 395 else:
396 log.error(f"unmanaged to_show item type: {item!r}") 396 log.error(f"unmanaged to_show item type: {item!r}")
397 397
398 def addCategoryLayout(self, label=None): 398 def add_category_layout(self, label=None):
399 category_layout = JidSelectorCategoryLayout() 399 category_layout = JidSelectorCategoryLayout()
400 400
401 if label and self.add_separators: 401 if label and self.add_separators:
402 category_layout.add_widget(CategorySeparator(text=label)) 402 category_layout.add_widget(CategorySeparator(text=label))
403 403
404 self.layout.add_widget(category_layout) 404 self.layout.add_widget(category_layout)
405 self.items_layouts.append(category_layout) 405 self.items_layouts.append(category_layout)
406 return category_layout 406 return category_layout
407 407
408 def getItemFromWid(self, wid): 408 def get_item_from_wid(self, wid):
409 """create JidSelector item from QuickChat widget""" 409 """create JidSelector item from QuickChat widget"""
410 contact_list = G.host.contact_lists[wid.profile] 410 contact_list = G.host.contact_lists[wid.profile]
411 try: 411 try:
412 data=contact_list.getItem(wid.target) 412 data=contact_list.get_item(wid.target)
413 except KeyError: 413 except KeyError:
414 log.warning(f"Can't find item data for {wid.target}") 414 log.warning(f"Can't find item data for {wid.target}")
415 data={} 415 data={}
416 try: 416 try:
417 item = self._createItem( 417 item = self._create_item(
418 jid=wid.target, 418 jid=wid.target,
419 data=data, 419 data=data,
420 profile=wid.profile, 420 profile=wid.profile,
421 ) 421 )
422 except Exception as e: 422 except Exception as e:
423 log.warning(f"Can't add contact {wid.target}: {e}") 423 log.warning(f"Can't add contact {wid.target}: {e}")
424 return 424 return
425 notifs = list(G.host.getNotifs(wid.target, profile=wid.profile)) 425 notifs = list(G.host.get_notifs(wid.target, profile=wid.profile))
426 if notifs: 426 if notifs:
427 item.badge_text = str(len(notifs)) 427 item.badge_text = str(len(notifs))
428 item.bind(on_press=partial(self.dispatch, 'on_select')) 428 item.bind(on_press=partial(self.dispatch, 'on_select'))
429 return item 429 return item
430 430
431 def addOpenedChatsItems(self): 431 def add_opened_chats_items(self):
432 G.host.addListener("widgetNew", self.onWidgetNew) 432 G.host.addListener("widgetNew", self.on_widget_new)
433 G.host.addListener("widgetDeleted", self.onWidgetDeleted) 433 G.host.addListener("widgetDeleted", self.on_widget_deleted)
434 self.opened_chats = category_layout = self.addCategoryLayout(_("Opened chats")) 434 self.opened_chats = category_layout = self.add_category_layout(_("Opened chats"))
435 widgets = sorted(G.host.widgets.getWidgets( 435 widgets = sorted(G.host.widgets.get_widgets(
436 quick_chat.QuickChat, 436 quick_chat.QuickChat,
437 profiles = G.host.profiles, 437 profiles = G.host.profiles,
438 with_duplicates=False)) 438 with_duplicates=False))
439 439
440 for wid in widgets: 440 for wid in widgets:
441 item = self.getItemFromWid(wid) 441 item = self.get_item_from_wid(wid)
442 if item is None: 442 if item is None:
443 continue 443 continue
444 category_layout.add_widget(item) 444 category_layout.add_widget(item)
445 445
446 def addRosterItems(self): 446 def add_roster_items(self):
447 self.roster = category_layout = self.addCategoryLayout(_("Your contacts")) 447 self.roster = category_layout = self.add_category_layout(_("Your contacts"))
448 for profile in G.host.profiles: 448 for profile in G.host.profiles:
449 contact_list = G.host.contact_lists[profile] 449 contact_list = G.host.contact_lists[profile]
450 for entity_jid in sorted(contact_list.roster): 450 for entity_jid in sorted(contact_list.roster):
451 item = self._createItem( 451 item = self._create_item(
452 jid=entity_jid, 452 jid=entity_jid,
453 data=contact_list.getItem(entity_jid), 453 data=contact_list.get_item(entity_jid),
454 profile=profile, 454 profile=profile,
455 ) 455 )
456 item.bind(on_press=partial(self.dispatch, 'on_select')) 456 item.bind(on_press=partial(self.dispatch, 'on_select'))
457 category_layout.add_widget(item) 457 category_layout.add_widget(item)
458 458
459 def addBookmarksItems(self): 459 def add_bookmarks_items(self):
460 self.bookmarks = category_layout = self.addCategoryLayout(_("Your chat rooms")) 460 self.bookmarks = category_layout = self.add_category_layout(_("Your chat rooms"))
461 for profile in G.host.profiles: 461 for profile in G.host.profiles:
462 profile_manager = G.host.profiles[profile] 462 profile_manager = G.host.profiles[profile]
463 try: 463 try:
464 bookmarks = profile_manager._bookmarks 464 bookmarks = profile_manager._bookmarks
465 except AttributeError: 465 except AttributeError:
467 continue 467 continue
468 468
469 contact_list = G.host.contact_lists[profile] 469 contact_list = G.host.contact_lists[profile]
470 for entity_jid in bookmarks: 470 for entity_jid in bookmarks:
471 try: 471 try:
472 cache = contact_list.getItem(entity_jid) 472 cache = contact_list.get_item(entity_jid)
473 except KeyError: 473 except KeyError:
474 cache = {} 474 cache = {}
475 item = self._createItem( 475 item = self._create_item(
476 jid=entity_jid, 476 jid=entity_jid,
477 data=cache, 477 data=cache,
478 profile=profile, 478 profile=profile,
479 ) 479 )
480 item.bind(on_press=partial(self.dispatch, 'on_select')) 480 item.bind(on_press=partial(self.dispatch, 'on_select'))