comparison src/browser/libervia_main.py @ 521:69bffcf37ce3

browser_side: add minimal requirements for generic plugins handling
author souliane <souliane@mailoo.org>
date Tue, 02 Sep 2014 21:26:32 +0200
parents d58d4dd0cefe
children 1735aaeac652
comparison
equal deleted inserted replaced
520:d58d4dd0cefe 521:69bffcf37ce3
47 from sat_browser import html_tools 47 from sat_browser import html_tools
48 from sat_browser import notification 48 from sat_browser import notification
49 49
50 from sat_browser.constants import Const as C 50 from sat_browser.constants import Const as C
51 51
52 # FIXME: import plugin dynamically
53 from sat_browser import plugin_sec_otr
52 54
53 MAX_MBLOG_CACHE = 500 # Max microblog entries kept in memories 55 MAX_MBLOG_CACHE = 500 # Max microblog entries kept in memories
54 56
55 # Set to true to not create a new LiberviaWidget when a similar one 57 # Set to true to not create a new LiberviaWidget when a similar one
56 # already exist (i.e. a chat panel with the same target). Instead 58 # already exist (i.e. a chat panel with the same target). Instead
188 self.avatars_cache = {} # keep track of jid's avatar hash (key=jid, value=file) 190 self.avatars_cache = {} # keep track of jid's avatar hash (key=jid, value=file)
189 self._register_box = None 191 self._register_box = None
190 RootPanel().add(self.panel) 192 RootPanel().add(self.panel)
191 self.notification = notification.Notification() 193 self.notification = notification.Notification()
192 DOM.addEventPreview(self) 194 DOM.addEventPreview(self)
195 self.importPlugins()
193 self._register = RegisterCall() 196 self._register = RegisterCall()
194 self._register.call('getMenus', self.gotMenus) 197 self._register.call('getMenus', self.gotMenus)
195 self._register.call('registerParams', None) 198 self._register.call('registerParams', None)
196 self._register.call('isRegistered', self._isRegisteredCB) 199 self._register.call('isRegistered', self._isRegisteredCB)
197 self.initialised = False 200 self.initialised = False
198 self.init_cache = [] # used to cache events until initialisation is done 201 self.init_cache = [] # used to cache events until initialisation is done
199 self.cached_params = {} 202 self.cached_params = {}
200 203
204 def importPlugins(self):
205 self.plugins = {}
206 # FIXME: plugins import should be dynamic and generic like in sat
207 self.plugins['otr'] = plugin_sec_otr.OTR(self)
208
201 def addSelectedListener(self, callback): 209 def addSelectedListener(self, callback):
202 self._selected_listeners.add(callback) 210 self._selected_listeners.add(callback)
203 211
204 def getSelected(self): 212 def getSelected(self):
205 wid = self.tab_panel.getCurrentPanel() 213 wid = self.tab_panel.getCurrentPanel()
319 def gotMenus(self, menus): 327 def gotMenus(self, menus):
320 """Put the menus data in cache and build the main menu bar 328 """Put the menus data in cache and build the main menu bar
321 329
322 @param menus (list[tuple]): menu data 330 @param menus (list[tuple]): menu data
323 """ 331 """
332 def process(menus, inhibited=None):
333 for raw_menu in menus:
334 id_, type_, path, path_i18n = raw_menu
335 if inhibited and path[0] in inhibited:
336 continue
337 menus_data = self.menus.setdefault(type_, [])
338 menus_data.append((id_, path, path_i18n))
339
324 self.menus = {} 340 self.menus = {}
325 for raw_menu in menus: 341 inhibited = set()
326 id_, type_, path, path_i18n = raw_menu 342 extras = []
327 menus_data = self.menus.setdefault(type_, []) 343 for plugin in self.plugins.values():
328 menus_data.append((id_, path, path_i18n)) 344 if hasattr(plugin, "inhibitMenus"):
345 inhibited.update(plugin.inhibitMenus())
346 if hasattr(plugin, "extraMenus"):
347 extras.extend(plugin.extraMenus())
348 process(menus, inhibited)
349 process(extras)
329 self.panel.menu.createMenus() 350 self.panel.menu.createMenus()
330 351
331 def _isRegisteredCB(self, result): 352 def _isRegisteredCB(self, result):
332 registered, warning = result 353 registered, warning = result
333 if not registered: 354 if not registered:
546 self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'GROUP', lib_wid.accepted_groups, 10) 567 self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'GROUP', lib_wid.accepted_groups, 10)
547 568
548 #we ask for our own microblogs: 569 #we ask for our own microblogs:
549 self.bridge.call('getMassiveLastMblogs', self._ownBlogsFills, 'JID', [self.whoami.bare], 10) 570 self.bridge.call('getMassiveLastMblogs', self._ownBlogsFills, 'JID', [self.whoami.bare], 10)
550 571
572 # initialize plugins which waited for the connection to be done
573 for plugin in self.plugins.values():
574 if hasattr(plugin, 'profileConnected'):
575 plugin.profileConnected()
576
551 ## Signals callbacks ## 577 ## Signals callbacks ##
552 578
553 def _personalEventCb(self, sender, event_type, data): 579 def _personalEventCb(self, sender, event_type, data):
554 if not self.initialised: 580 if not self.initialised:
555 self.init_cache.append((sender, event_type, data)) 581 self.init_cache.append((sender, event_type, data))
679 # for example to scroll to the bottom 705 # for example to scroll to the bottom
680 self.setSelected(lib_wid) 706 self.setSelected(lib_wid)
681 lib_wid.refresh() 707 lib_wid.refresh()
682 return lib_wid 708 return lib_wid
683 709
684 def _newMessageCb(self, from_jid, msg, msg_type, to_jid, extra): 710 def _newMessageCb(self, from_jid_s, msg, msg_type, to_jid_s, extra):
685 _from = jid.JID(from_jid) 711 from_jid = jid.JID(from_jid_s)
686 _to = jid.JID(to_jid) 712 to_jid = jid.JID(to_jid_s)
687 other = _to if _from.bare == self.whoami.bare else _from 713 for plugin in self.plugins.values():
714 if hasattr(plugin, 'messageReceivedTrigger'):
715 if not plugin.messageReceivedTrigger(from_jid, msg, msg_type, to_jid, extra):
716 return # plugin returned False to interrupt the process
717 self.newMessageCb(from_jid, msg, msg_type, to_jid, extra)
718
719 def newMessageCb(self, from_jid, msg, msg_type, to_jid, extra):
720 # FIXME: newMessage should manage system message, so they don't appear as coming from the contact
721 other = to_jid if from_jid.bare == self.whoami.bare else from_jid
688 lib_wid = self.getLiberviaWidget(panels.ChatPanel, other, ignoreOtherTabs=False) 722 lib_wid = self.getLiberviaWidget(panels.ChatPanel, other, ignoreOtherTabs=False)
689 self.displayNotification(_from, msg) 723 self.displayNotification(from_jid, msg)
690 if msg_type == 'headline' and from_jid == self._defaultDomain: 724 if msg_type == 'headline' and from_jid.full() == self._defaultDomain:
691 try: 725 try:
692 assert(extra['subject']) # subject is defined and not empty 726 assert(extra['subject']) # subject is defined and not empty
693 title = extra['subject'] 727 title = extra['subject']
694 except (KeyError, AssertionError): 728 except (KeyError, AssertionError):
695 title = _('Announcement from %s') % from_jid 729 title = _('Announcement from %s') % from_jid.full()
696 msg = strings.addURLToText(html_tools.XHTML2Text(msg)) 730 msg = strings.addURLToText(html_tools.XHTML2Text(msg))
697 dialog.InfoDialog(title, msg).show() 731 dialog.InfoDialog(title, msg).show()
698 return 732 return
699 if lib_wid is not None: 733 if lib_wid is not None:
700 lib_wid.printMessage(_from, msg, extra) 734 lib_wid.printMessage(from_jid, msg, extra)
701 else: 735 else:
702 if not self.contact_panel.isContactInRoster(other.bare): 736 if not self.contact_panel.isContactInRoster(other.bare):
703 self.contact_panel.updateContact(other.bare, {}, [C.GROUP_NOT_IN_ROSTER]) 737 self.contact_panel.updateContact(other.bare, {}, [C.GROUP_NOT_IN_ROSTER])
704 # The message has not been shown, we must indicate it 738 # The message has not been shown, we must indicate it
705 self.contact_panel.setContactMessageWaiting(other.bare, True) 739 self.contact_panel.setContactMessageWaiting(other.bare, True)
912 addresses.append((addr, entities)) 946 addresses.append((addr, entities))
913 else: 947 else:
914 log.error("Unknown target type") 948 log.error("Unknown target type")
915 if addresses: 949 if addresses:
916 if len(addresses) == 1 and addresses[0][0] == 'to': 950 if len(addresses) == 1 and addresses[0][0] == 'to':
917 self.bridge.call('sendMessage', (None, self.sendError), addresses[0][1], text, '', type_, extra) 951 to_jid_s = addresses[0][1]
952 for plugin in self.plugins.values():
953 if hasattr(plugin, 'sendMessageTrigger'):
954 if not plugin.sendMessageTrigger(jid.JID(to_jid_s), text, type_, extra):
955 return # plugin returned False to interrupt the process
956 self.bridge.call('sendMessage', (None, self.sendError), to_jid_s, text, '', type_, extra)
918 else: 957 else:
919 extra.update({'address': '\n'.join([('%s:%s' % entry) for entry in addresses])}) 958 extra.update({'address': '\n'.join([('%s:%s' % entry) for entry in addresses])})
920 self.bridge.call('sendMessage', (None, self.sendError), self.whoami.domain, text, '', type_, extra) 959 self.bridge.call('sendMessage', (None, self.sendError), self.whoami.domain, text, '', type_, extra)
921 960
922 def showWarning(self, type_=None, msg=None): 961 def showWarning(self, type_=None, msg=None):