comparison browser/libervia_main.py @ 1124:28e3eb3bb217

files reorganisation and installation rework: - files have been reorganised to follow other SàT projects and usual Python organisation (no more "/src" directory) - VERSION file is now used, as for other SàT projects - replace the overcomplicated setup.py be a more sane one. Pyjamas part is not compiled anymore by setup.py, it must be done separatly - removed check for data_dir if it's empty - installation tested working in virtual env - libervia launching script is now in bin/libervia
author Goffi <goffi@goffi.org>
date Sat, 25 Aug 2018 17:59:48 +0200
parents src/browser/libervia_main.py@63a4b8fe9782
children 2af117bfe6cc
comparison
equal deleted inserted replaced
1123:63a4b8fe9782 1124:28e3eb3bb217
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Libervia: a Salut à Toi frontend
5 # Copyright (C) 2011-2018 Jérôme Poisson <goffi@goffi.org>
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 ### logging configuration ###
22 from sat_browser import logging
23 logging.configure()
24 from sat.core.log import getLogger
25 log = getLogger(__name__)
26 ###
27
28 from sat.core.i18n import D_
29
30 from sat_frontends.quick_frontend.quick_app import QuickApp
31 from sat_frontends.quick_frontend import quick_widgets
32 from sat_frontends.quick_frontend import quick_menus
33
34 from sat_frontends.tools.misc import InputHistory
35 from sat_browser import strings
36 from sat_frontends.tools import jid
37 from sat_frontends.tools import host_listener
38 from sat.core.i18n import _
39
40 from pyjamas.ui.RootPanel import RootPanel
41 # from pyjamas.ui.HTML import HTML
42 from pyjamas.ui.KeyboardListener import KEY_ESCAPE
43 from pyjamas.Timer import Timer
44 from pyjamas import Window, DOM
45
46 from sat_browser import json
47 from sat_browser import register
48 from sat_browser.contact_list import ContactList
49 from sat_browser import main_panel
50 # from sat_browser import chat
51 from sat_browser import blog
52 from sat_browser import xmlui
53 from sat_browser import dialog
54 from sat_browser import html_tools
55 from sat_browser import notification
56 from sat_browser import libervia_widget
57 from sat_browser import web_widget
58 assert web_widget # XXX: just here to avoid pyflakes warning
59
60 from sat_browser.constants import Const as C
61
62
63 try:
64 # FIXME: import plugin dynamically
65 from sat_browser import plugin_sec_otr
66 except ImportError:
67 pass
68
69
70 unicode = str # FIXME: pyjamas workaround
71
72
73 # MAX_MBLOG_CACHE = 500 # Max microblog entries kept in memories # FIXME
74
75
76 class SatWebFrontend(InputHistory, QuickApp):
77 ENCRYPTION_HANDLERS = False # e2e encryption is handled directly by Libervia,
78 # not backend
79
80 def onModuleLoad(self):
81 log.info("============ onModuleLoad ==============")
82 self.bridge_signals = json.BridgeSignals(self)
83 QuickApp.__init__(self, json.BridgeCall, xmlui=xmlui, connect_bridge=False)
84 self.connectBridge()
85 self._profile_plugged = False
86 self.signals_cache[C.PROF_KEY_NONE] = []
87 self.panel = main_panel.MainPanel(self)
88 self.tab_panel = self.panel.tab_panel
89 self.tab_panel.addTabListener(self)
90 self._register_box = None
91 RootPanel().add(self.panel)
92
93 self.alerts_counter = notification.FaviconCounter()
94 self.notification = notification.Notification(self.alerts_counter)
95 DOM.addEventPreview(self)
96 self.importPlugins()
97 self._register = json.RegisterCall()
98 self._register.call('menusGet', self.gotMenus)
99 self._register.call('registerParams', None)
100 self._register.call('getSessionMetadata', self._getSessionMetadataCB)
101 self.initialised = False
102 self.init_cache = [] # used to cache events until initialisation is done
103 self.cached_params = {}
104 self.next_rsm_index = 0
105
106 #FIXME: microblog cache should be managed directly in blog module
107 self.mblog_cache = [] # used to keep our own blog entries in memory, to show them in new mblog panel
108
109 self._versions={} # SàT and Libervia versions cache
110
111 @property
112 def whoami(self):
113 # XXX: works because Libervia is mono-profile
114 # if one day Libervia manage several profiles at once, this must be deleted
115 return self.profiles[C.PROF_KEY_NONE].whoami
116
117 @property
118 def contact_list(self):
119 return self.contact_lists[C.PROF_KEY_NONE]
120
121 @property
122 def visible_widgets(self):
123 widgets_panel = self.tab_panel.getCurrentPanel()
124 return [wid for wid in widgets_panel.widgets if isinstance(wid, quick_widgets.QuickWidget)]
125
126 @property
127 def base_location(self):
128 """Return absolute base url of this Libervia instance"""
129 url = Window.getLocation().getHref()
130 if url.endswith(C.LIBERVIA_MAIN_PAGE):
131 url = url[:-len(C.LIBERVIA_MAIN_PAGE)]
132 if url.endswith("/"):
133 url = url[:-1]
134 return url
135
136 @property
137 def sat_version(self):
138 return self._versions["sat"]
139
140 @property
141 def libervia_version(self):
142 return self._versions["libervia"]
143
144 def getVersions(self, callback=None):
145 """Ask libervia server for SàT and Libervia version and fill local cache
146
147 @param callback: method to call when both versions have been received
148 """
149 def gotVersion():
150 if len(self._versions) == 2 and callback is not None:
151 callback()
152
153 if len(self._versions) == 2:
154 # we already have versions in cache
155 gotVersion()
156 return
157
158 def gotSat(version):
159 self._versions["sat"] = version
160 gotVersion()
161
162 def gotLibervia(version):
163 self._versions["libervia"] = version
164 gotVersion()
165
166 self.bridge.getVersion(callback=gotSat, profile=None)
167 self.bridge.getLiberviaVersion(callback=gotLibervia, profile=None) # XXX: bridge direct call expect a profile, even for method with no profile needed
168
169 def registerSignal(self, functionName, handler=None, iface="core", with_profile=True):
170 if handler is None:
171 callback = getattr(self, "{}{}".format(functionName, "Handler"))
172 else:
173 callback = handler
174
175 self.bridge_signals.register_signal(functionName, callback, with_profile=with_profile)
176
177 def importPlugins(self):
178 self.plugins = {}
179 try:
180 self.plugins['otr'] = plugin_sec_otr.OTR(self)
181 except TypeError: # plugin_sec_otr has not been imported
182 pass
183
184 def getSelected(self):
185 wid = self.tab_panel.getCurrentPanel()
186 if not isinstance(wid, libervia_widget.WidgetsPanel):
187 log.error("Tab widget is not a WidgetsPanel, can't get selected widget")
188 return None
189 return wid.selected
190
191 def setSelected(self, widget):
192 """Define the selected widget"""
193 widgets_panel = self.tab_panel.getCurrentPanel()
194 if not isinstance(widgets_panel, libervia_widget.WidgetsPanel):
195 return
196
197 selected = widgets_panel.selected
198
199 if selected == widget:
200 return
201
202 if selected:
203 selected.removeStyleName('selected_widget')
204
205 # FIXME: check that widget is in the current WidgetsPanel
206 widgets_panel.selected = widget
207 self.selected_widget = widget
208
209 if widget:
210 widgets_panel.selected.addStyleName('selected_widget')
211
212 def resize(self):
213 """Resize elements"""
214 Window.onResize()
215
216 def onBeforeTabSelected(self, sender, tab_index):
217 return True
218
219 def onTabSelected(self, sender, tab_index):
220 pass
221 # def onTabSelected(self, sender, tab_index):
222 # for widget in self.tab_panel.getCurrentPanel().widgets:
223 # if isinstance(widget, chat.Chat):
224 # clist = self.contact_list
225 # clist.removeAlerts(widget.current_target, True)
226
227 def onEventPreview(self, event):
228 if event.type in ["keydown", "keypress", "keyup"] and event.keyCode == KEY_ESCAPE:
229 #needed to prevent request cancellation in Firefox
230 event.preventDefault()
231 return True
232
233 def getAvatarURL(self, jid_):
234 """Return avatar of a jid if in cache, else ask for it.
235
236 @param jid_ (jid.JID): JID of the contact
237 @return: the URL to the avatar (unicode)
238 """
239 return self.getAvatar(jid_) or self.getDefaultAvatar()
240
241 def getDefaultAvatar(self):
242 return C.DEFAULT_AVATAR_URL
243
244 def registerWidget(self, wid):
245 log.debug(u"Registering %s" % wid.getDebugName())
246 self.libervia_widgets.add(wid)
247
248 def unregisterWidget(self, wid):
249 try:
250 self.libervia_widgets.remove(wid)
251 except KeyError:
252 log.warning(u'trying to remove a non registered Widget: %s' % wid.getDebugName())
253
254 def refresh(self):
255 """Refresh the general display."""
256 self.contact_list.refresh()
257 for lib_wid in self.libervia_widgets:
258 lib_wid.refresh()
259 self.resize()
260
261 def addWidget(self, wid, tab_index=None):
262 """ Add a widget at the bottom of the current or specified tab
263
264 @param wid: LiberviaWidget to add
265 @param tab_index: index of the tab to add the widget to
266 """
267 if tab_index is None or tab_index < 0 or tab_index >= self.tab_panel.getWidgetCount():
268 panel = self.tab_panel.getCurrentPanel()
269 else:
270 panel = self.tab_panel.deck.getWidget(tab_index)
271 panel.addWidget(wid)
272
273 def gotMenus(self, backend_menus):
274 """Put the menus data in cache and build the main menu bar
275
276 @param backend_menus (list[tuple]): menu data from backend
277 """
278 main_menu = self.panel.menu # most of global menu callbacks are in main_menu
279
280 # Categories (with icons)
281 self.menus.addCategory(C.MENU_GLOBAL, [D_(u"General")], extra={'icon': 'home'})
282 self.menus.addCategory(C.MENU_GLOBAL, [D_(u"Contacts")], extra={'icon': 'social'})
283 self.menus.addCategory(C.MENU_GLOBAL, [D_(u"Groups")], extra={'icon': 'social'})
284 #self.menus.addCategory(C.MENU_GLOBAL, [D_(u"Games")], extra={'icon': 'games'})
285
286 # menus to have before backend menus
287 self.menus.addMenu(C.MENU_GLOBAL, (D_(u"Groups"), D_(u"Discussion")), callback=main_menu.onJoinRoom)
288
289 # menus added by the backend/plugins (include other types than C.MENU_GLOBAL)
290 self.menus.addMenus(backend_menus, top_extra={'icon': 'plugins'})
291
292 # menus to have under backend menus
293 self.menus.addMenu(C.MENU_GLOBAL, (D_(u"Contacts"), D_(u"Manage contact groups")), callback=main_menu.onManageContactGroups)
294
295 # separator and right hand menus
296 self.menus.addMenuItem(C.MENU_GLOBAL, [], quick_menus.MenuSeparator())
297
298 self.menus.addMenu(C.MENU_GLOBAL, (D_(u"Help"), D_("Official chat room")), top_extra={'icon': 'help'}, callback=main_menu.onOfficialChatRoom)
299 self.menus.addMenu(C.MENU_GLOBAL, (D_(u"Help"), D_("Social contract")), top_extra={'icon': 'help'}, callback=main_menu.onSocialContract)
300 self.menus.addMenu(C.MENU_GLOBAL, (D_(u"Help"), D_("About")), callback=main_menu.onAbout)
301 self.menus.addMenu(C.MENU_GLOBAL, (D_(u"Settings"), D_("Account")), top_extra={'icon': 'settings'}, callback=main_menu.onAccount)
302 self.menus.addMenu(C.MENU_GLOBAL, (D_(u"Settings"), D_("Parameters")), callback=main_menu.onParameters)
303 # XXX: temporary, will change when a full profile will be managed in SàT
304 self.menus.addMenu(C.MENU_GLOBAL, (D_(u"Settings"), D_("Upload avatar")), callback=main_menu.onAvatarUpload)
305
306 # we call listener to have menu added by local classes/plugins
307 self.callListeners('gotMenus') # FIXME: to be done another way or moved to quick_app
308
309 # and finally the menus which must appear at the bottom
310 self.menus.addMenu(C.MENU_GLOBAL, (D_(u"General"), D_(u"Disconnect")), callback=main_menu.onDisconnect)
311
312 # we can now display all the menus
313 main_menu.update(C.MENU_GLOBAL)
314
315 # XXX: temp, will be reworked in the backed static blog plugin
316 self.menus.addMenu(C.MENU_JID_CONTEXT, (D_(u"User"), D_("Public blog")), callback=main_menu.onPublicBlog)
317
318 def removeListener(self, type_, callback):
319 """Remove a callback from listeners
320
321 @param type_: same as for [addListener]
322 @param callback: callback to remove
323 """
324 # FIXME: workaround for pyjamas
325 # check KeyError issue
326 assert type_ in C.LISTENERS
327 try:
328 self._listeners[type_].pop(callback)
329 except KeyError:
330 pass
331
332 def _getSessionMetadataCB(self, metadata):
333 if not metadata['plugged']:
334 warning = metadata.get("warning")
335 self.panel.setStyleAttribute("opacity", "0.25") # set background transparency
336 self._register_box = register.RegisterBox(self.logged, metadata)
337 self._register_box.centerBox()
338 self._register_box.show()
339 if warning:
340 dialog.InfoDialog(_('Security warning'), warning).show()
341 self._tryAutoConnect(skip_validation=not not warning)
342 else:
343 self._register.call('isConnected', self._isConnectedCB)
344
345 def _isConnectedCB(self, connected):
346 if not connected:
347 self._register.call('connect', lambda x: self.logged())
348 else:
349 self.logged()
350
351 def logged(self):
352 self.panel.setStyleAttribute("opacity", "1") # background becomes foreground
353 if self._register_box:
354 self._register_box.hide()
355 del self._register_box # don't work if self._register_box is None
356
357 # display the presence status panel and tab bar
358 self.presence_status_panel = main_panel.PresenceStatusPanel(self)
359 self.panel.addPresenceStatusPanel(self.presence_status_panel)
360 self.panel.tab_panel.getTabBar().setVisible(True)
361
362 self.bridge_signals.getSignals(callback=self.bridge_signals.signalHandler, profile=None)
363
364 def domain_cb(value):
365 self._defaultDomain = value
366 log.info(u"new account domain: %s" % value)
367
368 def domain_eb(value):
369 self._defaultDomain = "libervia.org"
370
371 self.bridge.getNewAccountDomain(callback=domain_cb, errback=domain_eb)
372 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.
373
374 def profilePlugged(self, dummy):
375 self._profile_plugged = True
376 QuickApp.profilePlugged(self, C.PROF_KEY_NONE)
377 contact_list = self.widgets.getOrCreateWidget(ContactList, None, on_new_widget=None, profile=C.PROF_KEY_NONE)
378 self.contact_list_widget = contact_list
379 self.panel.addContactList(contact_list)
380
381 # FIXME: the contact list height has to be set manually the first time
382 self.resize()
383
384 # XXX: as contact_list.update() is slow and it's called a lot of time
385 # during profile plugging, we prevent it before it's plugged
386 # and do all at once now
387 contact_list.update()
388
389 try:
390 self.mblog_available = C.bool(self.features['XEP-0277']['available'])
391 except KeyError:
392 self.mblog_available = False
393
394 try:
395 self.groupblog_available = C.bool(self.features['GROUPBLOG']['available'])
396 except KeyError:
397 self.groupblog_available = False
398
399 blog_widget = self.displayWidget(blog.Blog, ())
400 self.setSelected(blog_widget)
401
402 if self.mblog_available:
403 if not self.groupblog_available:
404 dialog.InfoDialog(_(u"Group blogging not available"), _(u"Your server can manage (micro)blogging, but not fine permissions.<br />You'll only be able to blog publicly.")).show()
405
406 else:
407 dialog.InfoDialog(_(u"Blogging not available"), _(u"Your server can't handle (micro)blogging.<br />You'll be able to see your contacts (micro)blogs, but not to post yourself.")).show()
408
409 # we fill the panels already here
410 # for wid in self.widgets.getWidgets(blog.MicroblogPanel):
411 # if wid.accept_all():
412 # self.bridge.getMassiveMblogs('ALL', (), None, profile=C.PROF_KEY_NONE, callback=wid.massiveInsert)
413 # else:
414 # self.bridge.getMassiveMblogs('GROUP', list(wid.accepted_groups), None, profile=C.PROF_KEY_NONE, callback=wid.massiveInsert)
415
416 #we ask for our own microblogs:
417 # self.loadOurMainEntries()
418
419 def gotDefaultMUC(default_muc):
420 self.default_muc = default_muc
421 self.bridge.mucGetDefaultService(profile=None, callback=gotDefaultMUC)
422
423 def newWidget(self, wid):
424 log.debug(u"newWidget: {}".format(wid))
425 self.addWidget(wid)
426
427 def newMessageHandler(self, from_jid_s, msg, type_, to_jid_s, extra, profile=C.PROF_KEY_NONE):
428 if type_ == C.MESS_TYPE_HEADLINE:
429 from_jid = jid.JID(from_jid_s)
430 if from_jid.domain == self._defaultDomain:
431 # we display announcement from the server in a dialog for better visibility
432 try:
433 title = extra['subject']
434 except KeyError:
435 title = _('Announcement from %s') % from_jid
436 msg = strings.addURLToText(html_tools.XHTML2Text(msg))
437 dialog.InfoDialog(title, msg).show()
438 return
439 QuickApp.newMessageHandler(self, from_jid_s, msg, type_, to_jid_s, extra, profile)
440
441 def disconnectedHandler(self, profile):
442 QuickApp.disconnectedHandler(self, profile)
443 Window.getLocation().reload()
444
445 def setPresenceStatus(self, show='', status=None, profile=C.PROF_KEY_NONE):
446 self.presence_status_panel.setPresence(show)
447 if status is not None:
448 self.presence_status_panel.setStatus(status)
449
450 def _tryAutoConnect(self, skip_validation=False):
451 """This method retrieve the eventual URL parameters to auto-connect the user.
452 @param skip_validation: if True, set the form values but do not validate it
453 """
454 params = strings.getURLParams(Window.getLocation().getSearch())
455 if "login" in params:
456 self._register_box._form.right_side.showStack(0)
457 self._register_box._form.login_box.setText(params["login"])
458 self._register_box._form.login_pass_box.setFocus(True)
459 if "passwd" in params:
460 # try to connect
461 self._register_box._form.login_pass_box.setText(params["passwd"])
462 if not skip_validation:
463 self._register_box._form.onLogin(None)
464 return True
465 else:
466 # this would eventually set the browser saved password
467 Timer(5, lambda: self._register_box._form.login_pass_box.setFocus(True))
468
469 def _actionManagerUnknownError(self):
470 dialog.InfoDialog("Error",
471 "Unmanaged action result", Width="400px").center()
472
473 # def _ownBlogsFills(self, mblogs, mblog_panel=None):
474 # """Put our own microblogs in cache, then fill the panels with them.
475
476 # @param mblogs (dict): dictionary mapping a publisher JID to blogs data.
477 # @param mblog_panel (MicroblogPanel): the panel to fill, or all if None.
478 # """
479 # cache = []
480 # for publisher in mblogs:
481 # for mblog in mblogs[publisher][0]:
482 # if 'content' not in mblog:
483 # log.warning(u"No content found in microblog [%s]" % mblog)
484 # continue
485 # if 'groups' in mblog:
486 # _groups = set(mblog['groups'].split() if mblog['groups'] else [])
487 # else:
488 # _groups = None
489 # mblog_entry = blog.MicroblogItem(mblog)
490 # cache.append((_groups, mblog_entry))
491
492 # self.mblog_cache.extend(cache)
493 # if len(self.mblog_cache) > MAX_MBLOG_CACHE:
494 # del self.mblog_cache[0:len(self.mblog_cache - MAX_MBLOG_CACHE)]
495
496 # widget_list = [mblog_panel] if mblog_panel else self.widgets.getWidgets(blog.MicroblogPanel)
497
498 # for wid in widget_list:
499 # self.fillMicroblogPanel(wid, cache)
500
501 # # FIXME
502
503 # if self.initialised:
504 # return
505 # self.initialised = True # initialisation phase is finished here
506 # for event_data in self.init_cache: # so we have to send all the cached events
507 # self.personalEventHandler(*event_data)
508 # del self.init_cache
509
510 # def loadOurMainEntries(self, index=0, mblog_panel=None):
511 # """Load a page of our own blogs from the cache or ask them to the
512 # backend. Then fill the panels with them.
513
514 # @param index (int): starting index of the blog page to retrieve.
515 # @param mblog_panel (MicroblogPanel): the panel to fill, or all if None.
516 # """
517 # delta = index - self.next_rsm_index
518 # if delta < 0:
519 # assert mblog_panel is not None
520 # self.fillMicroblogPanel(mblog_panel, self.mblog_cache[index:index + C.RSM_MAX_ITEMS])
521 # return
522
523 # def cb(result):
524 # self._ownBlogsFills(result, mblog_panel)
525
526 # rsm = {'max_': str(delta + C.RSM_MAX_ITEMS), 'index': str(self.next_rsm_index)}
527 # self.bridge.getMassiveMblogs('JID', [unicode(self.whoami.bare)], rsm, callback=cb, profile=C.PROF_KEY_NONE)
528 # self.next_rsm_index = index + C.RSM_MAX_ITEMS
529
530 ## Signals callbacks ##
531
532 # def personalEventHandler(self, sender, event_type, data):
533 # elif event_type == 'MICROBLOG_DELETE':
534 # for wid in self.widgets.getWidgets(blog.MicroblogPanel):
535 # wid.removeEntry(data['type'], data['id'])
536
537 # if sender == self.whoami.bare and data['type'] == 'main_item':
538 # for index in xrange(0, len(self.mblog_cache)):
539 # entry = self.mblog_cache[index]
540 # if entry[1].id == data['id']:
541 # self.mblog_cache.remove(entry)
542 # break
543
544 # def fillMicroblogPanel(self, mblog_panel, mblogs):
545 # """Fill a microblog panel with entries in cache
546
547 # @param mblog_panel: MicroblogPanel instance
548 # """
549 # #XXX: only our own entries are cached
550 # for cache_entry in mblogs:
551 # _groups, mblog_entry = cache_entry
552 # mblog_panel.addEntryIfAccepted(self.whoami.bare, *cache_entry)
553
554 # def getEntityMBlog(self, entity):
555 # # FIXME: call this after a contact has been added to roster
556 # log.info(u"geting mblog for entity [%s]" % (entity,))
557 # for lib_wid in self.libervia_widgets:
558 # if isinstance(lib_wid, blog.MicroblogPanel):
559 # if lib_wid.isJidAccepted(entity):
560 # self.bridge.call('getMassiveMblogs', lib_wid.massiveInsert, 'JID', [unicode(entity)])
561
562 def displayWidget(self, class_, target, dropped=False, new_tab=None, *args, **kwargs):
563 """Get or create a LiberviaWidget and select it. When the user dropped
564 something, a new widget is always created, otherwise we look for an
565 existing widget and re-use it if it's in the current tab.
566
567 @arg class_(class): see quick_widgets.getOrCreateWidget
568 @arg target: see quick_widgets.getOrCreateWidget
569 @arg dropped(bool): if True, assume the widget has been dropped
570 @arg new_tab(unicode): if not None, it holds the name of a new tab to
571 open for the widget. If None, use the default behavior.
572 @param args(list): optional args to create a new instance of class_
573 @param kwargs(list): optional kwargs to create a new instance of class_
574 @return: the widget
575 """
576 kwargs['profile'] = C.PROF_KEY_NONE
577
578 if dropped:
579 kwargs['on_new_widget'] = None
580 kwargs['on_existing_widget'] = C.WIDGET_RECREATE
581 wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
582 self.setSelected(wid)
583 return wid
584
585 if new_tab:
586 kwargs['on_new_widget'] = None
587 kwargs['on_existing_widget'] = C.WIDGET_RECREATE
588 wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
589 self.tab_panel.addWidgetsTab(new_tab)
590 self.addWidget(wid, tab_index=self.tab_panel.getWidgetCount() - 1)
591 return wid
592
593 kwargs['on_existing_widget'] = C.WIDGET_RAISE
594 try:
595 wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
596 except quick_widgets.WidgetAlreadyExistsError:
597 kwargs['on_existing_widget'] = C.WIDGET_KEEP
598 wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
599 widgets_panel = wid.getParent(libervia_widget.WidgetsPanel, expect=False)
600 if widgets_panel is None:
601 # The widget exists but is hidden
602 self.addWidget(wid)
603 elif widgets_panel != self.tab_panel.getCurrentPanel():
604 # the widget is on an other tab, so we add a new one here
605 kwargs['on_existing_widget'] = C.WIDGET_RECREATE
606 wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
607 self.addWidget(wid)
608 self.setSelected(wid)
609 return wid
610
611 def isHidden(self):
612 """Tells if the frontend window is hidden.
613
614 @return bool
615 """
616 return self.notification.isHidden()
617
618 def updateAlertsCounter(self, extra_inc=0):
619 """Update the over whole alerts counter
620
621 @param extra_inc (int): extra counter
622 """
623 extra = self.alerts_counter.extra + extra_inc
624 self.alerts_counter.update(self.alerts_count, extra=extra)
625
626 def _paramUpdate(self, name, value, category, refresh=True):
627 """This is called when the paramUpdate signal is received, but also
628 during initialization when the UI parameters values are retrieved.
629 @param refresh: set to True to refresh the general UI
630 """
631 for param_cat, param_name in C.CACHED_PARAMS:
632 if name == param_name and category == param_cat:
633 self.cached_params[(category, name)] = value
634 if refresh:
635 self.refresh()
636 break
637
638 def getCachedParam(self, category, name):
639 """Return a parameter cached value (e.g for refreshing the UI)
640
641 @param category (unicode): the parameter category
642 @pram name (unicode): the parameter name
643 """
644 return self.cached_params[(category, name)] if (category, name) in self.cached_params else None
645
646 def sendError(self, errorData):
647 dialog.InfoDialog("Error while sending message",
648 "Your message can't be sent", Width="400px").center()
649 log.error("sendError: %s" % unicode(errorData))
650
651 def showWarning(self, type_=None, msg=None):
652 """Display a popup information message, e.g. to notify the recipient of a message being composed.
653 If type_ is None, a popup being currently displayed will be hidden.
654 @type_: a type determining the CSS style to be applied (see WarningPopup.showWarning)
655 @msg: message to be displayed
656 """
657 if not hasattr(self, "warning_popup"):
658 self.warning_popup = main_panel.WarningPopup()
659 self.warning_popup.showWarning(type_, msg)
660
661 def showDialog(self, message, title="", type_="info", answer_cb=None, answer_data=None):
662 if type_ == 'info':
663 popup = dialog.InfoDialog(unicode(title), unicode(message), callback=answer_cb)
664 elif type_ == 'error':
665 popup = dialog.InfoDialog(unicode(title), unicode(message), callback=answer_cb)
666 elif type_ == 'yes/no':
667 popup = dialog.ConfirmDialog(lambda answer: answer_cb(answer, answer_data),
668 text=unicode(message), title=unicode(title))
669 popup.cancel_button.setText(_("No"))
670 else:
671 popup = dialog.InfoDialog(unicode(title), unicode(message), callback=answer_cb)
672 log.error(_('unmanaged dialog type: %s'), type_)
673 popup.show()
674
675 def dialogFailure(self, failure):
676 dialog.InfoDialog("Error",
677 unicode(failure), Width="400px").center()
678
679 def showFailure(self, err_data, msg=''):
680 """Show a failure that has been returned by an asynchronous bridge method.
681
682 @param failure (defer.Failure): Failure instance
683 @param msg (unicode): message to display
684 """
685 # FIXME: message is lost by JSON, we hardcode it for now... remove msg argument when possible
686 err_code, err_obj = err_data
687 title = err_obj['message']['faultString'] if isinstance(err_obj['message'], dict) else err_obj['message']
688 self.showDialog(msg, title, 'error')
689
690 def onJoinMUCFailure(self, err_data):
691 """Show a failure that has been returned when trying to join a room.
692
693 @param failure (defer.Failure): Failure instance
694 """
695 # FIXME: remove asap, see self.showFailure
696 err_code, err_obj = err_data
697 if err_obj["data"] == "AlreadyJoinedRoom":
698 msg = _(u"The room has already been joined.")
699 err_obj["message"] = _(u"Information")
700 else:
701 msg = _(u"Invalid room identifier. Please give a room short or full identifier like 'room' or '%s'.") % self.default_muc
702 err_obj["message"] = _(u"Error")
703 self.showFailure(err_data, msg)
704
705
706 if __name__ == '__main__':
707 app = SatWebFrontend()
708 app.onModuleLoad()
709 host_listener.callListeners(app)