changeset 1093:11e2bb20e896

core, frontends (menus): MENU_ROOM and MENU_SINGLE are now managed
author Goffi <goffi@goffi.org>
date Thu, 26 Jun 2014 00:05:20 +0200
parents 0eefc74c346b
children 4286a19e9e8a
files frontends/src/constants.py frontends/src/primitivus/chat.py frontends/src/primitivus/primitivus frontends/src/quick_frontend/quick_app.py frontends/src/wix/chat.py frontends/src/wix/main_window.py
diffstat 6 files changed, 66 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/constants.py	Wed Jun 25 20:54:15 2014 +0200
+++ b/frontends/src/constants.py	Thu Jun 26 00:05:20 2014 +0200
@@ -55,8 +55,6 @@
 
     PRESENCE = getPresence()
 
-    MENU_NORMAL = "NORMAL"
-
     # from plugin_misc_text_syntaxes
     SYNTAX_XHTML = "XHTML"
     SYNTAX_CURRENT = "@CURRENT@"
--- a/frontends/src/primitivus/chat.py	Wed Jun 25 20:54:15 2014 +0200
+++ b/frontends/src/primitivus/chat.py	Thu Jun 26 00:05:20 2014 +0200
@@ -132,11 +132,11 @@
         """Return Menu bar"""
         menu = sat_widgets.Menu(self.host.loop)
         if self.type == 'group':
+            self.host.addMenus(menu, C.MENU_ROOM, {'room_jid': self.target.bare})
             game = _("Game")
-            muc = _("MUC")
             menu.addMenu(game, "Tarot", self.onTarotRequest)
-            menu.addMenu(muc, _("Configure room"), self.onConfigureRoom)
         elif self.type == 'one2one':
+            self.host.addMenus(menu, C.MENU_SINGLE, {'jid': self.target})
             menu.addMenu(_("Action"), _("Send file"), self.onSendFileRequest)
         return menu
 
@@ -345,6 +345,7 @@
 
     #MENU EVENTS#
     def onTarotRequest(self, menu):
+        # TODO: move this to plugin_misc_tarot with dynamic menu
         if len(self.occupants) != 4:
             self.host.showPopUp(sat_widgets.Alert(_("Can't start game"), _("You need to be exactly 4 peoples in the room to start a Tarot game"), ok_cb=self.host.removePopUp))
         else:
@@ -358,6 +359,7 @@
         self.host.bridge.configureRoom(self.id, self.host.profile, callback=gotUI, errback=configureError)
 
     def onSendFileRequest(self, menu):
+        # TODO: move this to core with dynamic menus
         dialog = FileDialog(ok_cb=self.onFileSelected, cancel_cb=self.host.removePopUp)
         self.host.showPopUp(dialog, 80, 80)
 
--- a/frontends/src/primitivus/primitivus	Wed Jun 25 20:54:15 2014 +0200
+++ b/frontends/src/primitivus/primitivus	Thu Jun 26 00:05:20 2014 +0200
@@ -282,6 +282,22 @@
         except AttributeError:
             return input
 
+    def addMenus(self, menu, type_, menu_data=None):
+        """Add cached menus to instance
+        @param menu: sat_widgets.Menu instance
+        @param type_: menu type like is sat.core.sat_main.importMenu
+        @param menu_data: data to send with these menus
+
+        """
+        menus = self.profiles[self.profile]['menus'].get(type_,[])
+        def add_menu_cb(callback_id):
+            self.launchAction(callback_id, menu_data, profile_key = self.profile)
+        for id_, path, path_i18n  in menus:
+            if len(path) != 2:
+                raise NotImplementedError("Menu with a path != 2 are not implemented yet")
+            menu.addMenu(path_i18n[0], path_i18n[1], lambda dummy,id_=id_: add_menu_cb(id_))
+
+
     def _buildMenuRoller(self):
         menu = sat_widgets.Menu(self.loop)
         general = _("General")
@@ -296,14 +312,7 @@
         menu.addMenu(communication, _("Join room"), self.onJoinRoomRequest, 'meta j')
         #additionals menus
         #FIXME: do this in a more generic way (in quickapp)
-        add_menus = self.bridge.getMenus('', C.NO_SECURITY_LIMIT)
-        def add_menu_cb(callback_id):
-            self.launchAction(callback_id, None, profile_key = self.profile)
-        for id_, type_, path, path_i18n  in add_menus:
-            assert(type_=="NORMAL") #TODO: manage other types
-            if len(path) != 2:
-                raise NotImplementedError("Menu with a path != 2 are not implemented yet")
-            menu.addMenu(path_i18n[0], path_i18n[1], lambda menu,id_=id_: add_menu_cb(id_))
+        self.addMenus(menu, C.MENU_GLOBAL)
 
         menu_roller = sat_widgets.MenuRoller([(_('Main menu'),menu)])
         return menu_roller
--- a/frontends/src/quick_frontend/quick_app.py	Wed Jun 25 20:54:15 2014 +0200
+++ b/frontends/src/quick_frontend/quick_app.py	Thu Jun 26 00:05:20 2014 +0200
@@ -133,8 +133,13 @@
             return
         self.profiles[profile] = {}
         if self.single_profile:
-            self.profile = profile
-
+            self.profile = profile # FIXME: must be refactored (multi profiles are not managed correclty)
+        raw_menus = self.bridge.getMenus("", C.NO_SECURITY_LIMIT )
+        menus = self.profiles[profile]['menus'] = {}
+        for raw_menu in raw_menus:
+            id_, type_, path, path_i18n  = raw_menu
+            menus_data = menus.setdefault(type_, [])
+            menus_data.append((id_, path, path_i18n))
         self.launchAction(C.AUTHENTICATE_PROFILE_ID, {'caller': 'plug_profile'}, profile_key=profile)
 
     def plug_profile_1(self, profile):
--- a/frontends/src/wix/chat.py	Wed Jun 25 20:54:15 2014 +0200
+++ b/frontends/src/wix/chat.py	Thu Jun 26 00:05:20 2014 +0200
@@ -19,6 +19,7 @@
 
 
 from sat.core.i18n import _
+from sat_frontends.wix.constants import Const as C
 import wx
 import os.path
 import time
@@ -173,6 +174,7 @@
         actionMenu = wx.Menu()
         actionMenu.Append(idSEND, _("&SendFile	CTRL-s"),_(" Send a file to contact"))
         menuBar.Append(actionMenu,_("&Action"))
+        self.host.addMenus(menuBar, C.MENU_SINGLE, {'jid': self.target})
 
         #events
         wx.EVT_MENU(self, idSEND, self.onSendFile)
@@ -185,6 +187,7 @@
         actionMenu = wx.Menu()
         actionMenu.Append(idTAROT, _("Start &Tarot game	CTRL-t"),_(" Start a Tarot card game")) #tmp
         menuBar.Append(actionMenu,_("&Games"))
+        self.host.addMenus(menuBar, C.MENU_ROOM, {'room_jid': self.target.bare})
 
         #events
         wx.EVT_MENU(self, idTAROT, self.onStartTarot)
--- a/frontends/src/wix/main_window.py	Wed Jun 25 20:54:15 2014 +0200
+++ b/frontends/src/wix/main_window.py	Thu Jun 26 00:05:20 2014 +0200
@@ -19,6 +19,7 @@
 
 
 from sat.core.i18n import _
+from sat_frontends.wix.constants import Const as C
 from sat_frontends.quick_frontend.quick_chat_list import QuickChatList
 from sat_frontends.quick_frontend.quick_app import QuickApp
 import wx
@@ -90,10 +91,6 @@
         #events
         self.Bind(wx.EVT_CLOSE, self.onClose, self)
 
-        #menus
-        self.createMenus()
-        for i in range(self.menuBar.GetMenuCount()):
-            self.menuBar.EnableTop(i, False)
 
         #profile panel
         self.profile_pan = ProfileManager(self)
@@ -109,9 +106,40 @@
         self.profile_pan.Hide()
         self.contact_list.Show()
         self.sizer.Layout()
-        for i in range(self.menuBar.GetMenuCount()):
-            self.menuBar.EnableTop(i, True)
         super(MainWindow, self).plug_profile_1(profile_key)
+        #menus
+        self.createMenus()
+
+    def addMenus(self, menubar, type_, menu_data=None):
+        """Add cached menus to instance
+        @param menu: wx.MenuBar instance
+        @param type_: menu type like is sat.core.sat_main.importMenu
+        @param menu_data: data to send with these menus
+
+        """
+        menus = self.profiles[self.profile]['menus'].get(type_,[])
+        for id_, path, path_i18n  in menus:
+            if len(path) != 2:
+                raise NotImplementedError("Menu with a path != 2 are not implemented yet")
+            category = path_i18n[0] # TODO: manage path with more than 2 levels
+            name = path_i18n[1]
+            menu_idx = menubar.FindMenu(category)
+            current_menu = None
+            if menu_idx == wx.NOT_FOUND:
+                #the menu is new, we create it
+                current_menu = wx.Menu()
+                menubar.Append(current_menu, category)
+            else:
+                current_menu = menubar.GetMenu(menu_idx)
+            assert(current_menu != None)
+            item_id = wx.NewId()
+            help_string = self.bridge.getMenuHelp(id_, '')
+            current_menu.Append(item_id, name, help=help_string)
+            #now we register the event
+            def event_answer(e, id_=id_):
+                self.launchAction(id_, menu_data, profile_key = self.profile)
+
+            wx.EVT_MENU(menubar.Parent, item_id, event_answer)
 
     def createMenus(self):
         log.info(_("Creating menus"))
@@ -133,30 +161,7 @@
 
         #additionals menus
         #FIXME: do this in a more generic way (in quickapp)
-        add_menus = self.bridge.getMenus('', Const.NO_SECURITY_LIMIT)
-        for id_, type_, path, path_i18n  in add_menus:
-            assert(type_=="NORMAL") #TODO: manage other types
-            if len(path) != 2:
-                raise NotImplementedError("Menu with a path != 2 are not implemented yet")
-            category = path_i18n[0] # TODO: manage path with more than 2 levels
-            name = path_i18n[1]
-            menu_idx = self.menuBar.FindMenu(category)
-            current_menu = None
-            if menu_idx == wx.NOT_FOUND:
-                #the menu is new, we create it
-                current_menu = wx.Menu()
-                self.menuBar.Append(current_menu, category)
-            else:
-                current_menu = self.menuBar.GetMenu(menu_idx)
-            assert(current_menu != None)
-            item_id = wx.NewId()
-            help_string = self.bridge.getMenuHelp(id_, '')
-            current_menu.Append(item_id, name, help=help_string)
-            #now we register the event
-            def event_answer(e, id_=id_):
-                self.launchAction(id_, None, profile_key = self.profile)
-
-            wx.EVT_MENU(self, item_id, event_answer)
+        self.addMenus(self.menuBar, C.MENU_GLOBAL)
 
         # menu items that should be displayed after the automatically added ones
         contactMenu.AppendSeparator()