comparison src/browser/libervia_main.py @ 616:1c0d5a87c554 frontends_multi_profiles

browser_side: add and use method displayWidget to harmonize widget's management in Libervia (not completely done, there are some issues)
author souliane <souliane@mailoo.org>
date Wed, 11 Feb 2015 11:18:13 +0100
parents 70872a83ef15
children ac5881d683d3 e287a4b431c1
comparison
equal deleted inserted replaced
615:70872a83ef15 616:1c0d5a87c554
24 from sat.core.log import getLogger 24 from sat.core.log import getLogger
25 log = getLogger(__name__) 25 log = getLogger(__name__)
26 ### 26 ###
27 27
28 from sat_frontends.quick_frontend.quick_app import QuickApp 28 from sat_frontends.quick_frontend.quick_app import QuickApp
29 from sat_frontends.quick_frontend.quick_widgets import WidgetAlreadyExistsError
29 30
30 from sat_frontends.tools.misc import InputHistory 31 from sat_frontends.tools.misc import InputHistory
31 from sat_frontends.tools import strings 32 from sat_frontends.tools import strings
32 from sat_frontends.tools import jid 33 from sat_frontends.tools import jid
33 from sat.core.i18n import _ 34 from sat.core.i18n import _
41 from sat_browser import json 42 from sat_browser import json
42 from sat_browser import register 43 from sat_browser import register
43 from sat_browser.contact_list import ContactList 44 from sat_browser.contact_list import ContactList
44 from sat_browser import base_widget 45 from sat_browser import base_widget
45 from sat_browser import panels 46 from sat_browser import panels
46 from sat_browser import chat
47 from sat_browser import blog 47 from sat_browser import blog
48 from sat_browser import dialog 48 from sat_browser import dialog
49 from sat_browser import xmlui 49 from sat_browser import xmlui
50 from sat_browser import html_tools 50 from sat_browser import html_tools
51 from sat_browser import notification 51 from sat_browser import notification
167 return 167 return
168 168
169 if selected: 169 if selected:
170 selected.removeStyleName('selected_widget') 170 selected.removeStyleName('selected_widget')
171 171
172 # FIXME: check that widget is in the current WidgetsPanel
172 widgets_panel.selected = widget 173 widgets_panel.selected = widget
173 self.selected_widget = widget 174 self.selected_widget = widget
174 175
175 if widget: 176 if widget:
176 widgets_panel.selected.addStyleName('selected_widget') 177 widgets_panel.selected.addStyleName('selected_widget')
231 self.panel.refresh() 232 self.panel.refresh()
232 for lib_wid in self.libervia_widgets: 233 for lib_wid in self.libervia_widgets:
233 lib_wid.refresh() 234 lib_wid.refresh()
234 self.resize() 235 self.resize()
235 236
236 def addTab(self, label, wid, select=True): 237 def addTab(self, label, wid=None, select=False):
237 """Create a new tab and eventually add a widget in 238 """Create a new tab and eventually add a widget to it.
238 @param label: label of the tab 239
239 @param wid: LiberviaWidget to add 240 @param label (unicode): label of the tab
240 @param select: True to select the added tab 241 @param wid (LiberviaWidget): optional widget to add
242 @param select (bool): True to select the added tab
243 @return: WidgetsPanel
241 """ 244 """
242 widgets_panel = base_widget.WidgetsPanel(self) 245 widgets_panel = base_widget.WidgetsPanel(self)
243 self.tab_panel.add(widgets_panel, label) 246 self.tab_panel.add(widgets_panel, label)
244 widgets_panel.addWidget(wid) 247 tab_index = self.tab_panel.getWidgetCount() - 1
248 if wid is not None:
249 self.addWidget(wid, tab_index)
245 if select: 250 if select:
246 self.tab_panel.selectTab(self.tab_panel.getWidgetCount() - 1) 251 self.tab_panel.selectTab(tab_index)
252 if wid is not None:
253 self.setSelected(wid)
247 return widgets_panel 254 return widgets_panel
248 255
249 def addWidget(self, wid, tab_index=None): 256 def addWidget(self, wid, tab_index=None):
250 """ Add a widget at the bottom of the current or specified tab 257 """ Add a widget at the bottom of the current or specified tab
251 258
253 @param tab_index: index of the tab to add the widget to 260 @param tab_index: index of the tab to add the widget to
254 """ 261 """
255 if tab_index is None or tab_index < 0 or tab_index >= self.tab_panel.getWidgetCount(): 262 if tab_index is None or tab_index < 0 or tab_index >= self.tab_panel.getWidgetCount():
256 panel = self.tab_panel.getCurrentPanel() 263 panel = self.tab_panel.getCurrentPanel()
257 else: 264 else:
258 panel = self.tab_panel.tabBar.getTabWidget(tab_index) 265 panel = self.tab_panel.deck.getWidget(tab_index)
259 panel.addWidget(wid) 266 panel.addWidget(wid)
260 267
261 def displayNotification(self, title, body): 268 def displayNotification(self, title, body):
262 self.notification.notify(title, body) 269 self.notification.notify(title, body)
263 270
330 def domain_eb(value): 337 def domain_eb(value):
331 self._defaultDomain = "libervia.org" 338 self._defaultDomain = "libervia.org"
332 339
333 self.bridge.getNewAccountDomain(callback=domain_cb, errback=domain_eb) 340 self.bridge.getNewAccountDomain(callback=domain_cb, errback=domain_eb)
334 self.plug_profiles([C.PROF_KEY_NONE]) # XXX: None was used intitially, but pyjamas bug when using variable arguments and None is the only arg. 341 self.plug_profiles([C.PROF_KEY_NONE]) # XXX: None was used intitially, but pyjamas bug when using variable arguments and None is the only arg.
335 microblog_widget = self.widgets.getOrCreateWidget(blog.MicroblogPanel, (), profile=C.PROF_KEY_NONE) 342 microblog_widget = self.displayWidget(blog.MicroblogPanel, ())
336 self.setSelected(microblog_widget) 343 self.setSelected(microblog_widget)
337 # self.discuss_panel.addWidget(panels.MicroblogPanel(self, [])) 344 # self.discuss_panel.addWidget(panels.MicroblogPanel(self, []))
338 345
339 # # get cached params and refresh the display 346 # # get cached params and refresh the display
340 # def param_cb(cat, name, count): 347 # def param_cb(cat, name, count):
402 ui = xmlui.create(self, xml_data=data['xmlui']) 409 ui = xmlui.create(self, xml_data=data['xmlui'])
403 ui.show() 410 ui.show()
404 elif "public_blog" in data: 411 elif "public_blog" in data:
405 # TODO: use the bare instead of node when all blogs can be retrieved 412 # TODO: use the bare instead of node when all blogs can be retrieved
406 node = jid.JID(data['public_blog']).node 413 node = jid.JID(data['public_blog']).node
407 web_widget = self.widgets.getOrCreateWidget(panels.WebPanel, "/blog/{}".format(node), show_url=False, profile=C.PROF_KEY_NONE, on_new_widget=None, on_existing_widget=C.WIDGET_RECREATE) # FIXME: won't work with unicode nodes 414 # FIXME: "/blog/{}" won't work with unicode nodes
408 self.addTab("{}'s blog".format(unicode(node)), web_widget) 415 self.displayWidget(panels.WebPanel, "/blog/{}".format(node), show_url=False, new_tab="{}'s blog".format(unicode(node)))
409 else: 416 else:
410 dialog.InfoDialog("Error", 417 dialog.InfoDialog("Error",
411 "Unmanaged action result", Width="400px").center() 418 "Unmanaged action result", Width="400px").center()
412 419
413 def _actionEb(self, err_data): 420 def _actionEb(self, err_data):
543 mblog_panel.addEntryIfAccepted(self.whoami.bare, *cache_entry) 550 mblog_panel.addEntryIfAccepted(self.whoami.bare, *cache_entry)
544 551
545 def getEntityMBlog(self, entity): 552 def getEntityMBlog(self, entity):
546 log.info("geting mblog for entity [%s]" % (entity,)) 553 log.info("geting mblog for entity [%s]" % (entity,))
547 for lib_wid in self.libervia_widgets: 554 for lib_wid in self.libervia_widgets:
548 if isinstance(lib_wid, panels.MicroblogPanel): 555 if isinstance(lib_wid, blog.MicroblogPanel):
549 if lib_wid.isJidAccepted(entity): 556 if lib_wid.isJidAccepted(entity):
550 self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'JID', [entity], 10) 557 self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'JID', [entity], 10)
551 558
552 # def getLiberviaWidget(self, class_, entity, ignoreOtherTabs=True): 559 # def getLiberviaWidget(self, class_, entity, ignoreOtherTabs=True):
553 # """Get the corresponding panel if it exists. 560 # """Get the corresponding panel if it exists.
569 # return lib_wid 576 # return lib_wid
570 # except AttributeError as e: 577 # except AttributeError as e:
571 # e.stack_list() 578 # e.stack_list()
572 # return None 579 # return None
573 # return None 580 # return None
581
582 def displayWidget(self, class_, target, dropped=False, new_tab=None, *args, **kwargs):
583 """Get or create a LiberviaWidget and select it. When the user dropped
584 something, a new widget is always created, otherwise we look for an
585 existing widget and re-use it if it's in the current tab.
586
587 @arg class_(class): see sat_frontends.quick_frontend.quick_widgets.getOrCreateWidget
588 @arg target: see sat_frontends.quick_frontend.quick_widgets.getOrCreateWidget
589 @arg dropped(bool): if True, assume the widget has been dropped
590 @arg new_tab(unicode): if not None, it holds the name of a new tab to
591 open for the widget. If None, use the default behavior.
592 @param args(list): optional args to create a new instance of class_
593 @param kwargs(list): optional kwargs to create a new instance of class_
594 @return: the widget
595 """
596 kwargs['profile'] = C.PROF_KEY_NONE
597
598 if dropped:
599 kwargs['on_new_widget'] = None
600 kwargs['on_existing_widget'] = C.WIDGET_RECREATE
601 wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
602 self.setSelected(wid)
603 return wid
604
605 if new_tab:
606 kwargs['on_new_widget'] = None
607 kwargs['on_existing_widget'] = C.WIDGET_RECREATE
608 wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
609 self.addTab(new_tab, wid)
610 return wid
611
612 kwargs['on_existing_widget'] = C.WIDGET_RAISE
613 try:
614 wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
615 except WidgetAlreadyExistsError:
616 kwargs['on_existing_widget'] = C.WIDGET_KEEP
617 wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
618 if wid.getWidgetsPanel() != self.tab_panel.getCurrentPanel():
619 kwargs['on_existing_widget'] = C.WIDGET_RECREATE
620 wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
621 self.addWidget(wid)
622 self.setSelected(wid)
623 return wid
624
574 625
575 # def getOrCreateLiberviaWidget(self, class_, entity, select=True, new_tab=None): 626 # def getOrCreateLiberviaWidget(self, class_, entity, select=True, new_tab=None):
576 # """Get the matching LiberviaWidget if it exists, or create a new one. 627 # """Get the matching LiberviaWidget if it exists, or create a new one.
577 # @param class_ (class): class of the panel (ChatPanel, MicroblogPanel...) 628 # @param class_ (class): class of the panel (ChatPanel, MicroblogPanel...)
578 # @param entity (dict): dictionnary to define the entity. 629 # @param entity (dict): dictionnary to define the entity.
804 avatar = '/' + C.AVATARS_DIR + value 855 avatar = '/' + C.AVATARS_DIR + value
805 self.avatars_cache[entity_jid_s] = avatar 856 self.avatars_cache[entity_jid_s] = avatar
806 self.contact_panel.updateAvatar(entity_jid_s, avatar) 857 self.contact_panel.updateAvatar(entity_jid_s, avatar)
807 858
808 for lib_wid in self.libervia_widgets: 859 for lib_wid in self.libervia_widgets:
809 if isinstance(lib_wid, panels.MicroblogPanel): 860 if isinstance(lib_wid, blog.MicroblogPanel):
810 if lib_wid.isJidAccepted(entity_jid_s) or (self.whoami and entity_jid_s == self.whoami.bare): 861 if lib_wid.isJidAccepted(entity_jid_s) or (self.whoami and entity_jid_s == self.whoami.bare):
811 lib_wid.updateValue('avatar', entity_jid_s, avatar) 862 lib_wid.updateValue('avatar', entity_jid_s, avatar)
812 863
813 # def _chatStateReceivedCb(self, from_jid_s, state): 864 # def _chatStateReceivedCb(self, from_jid_s, state):
814 # """Callback when a new chat state is received. 865 # """Callback when a new chat state is received.