core, frontends (menus): MENU_ROOM and MENU_SINGLE are now managed

This commit is contained in:
Goffi 2014-06-26 00:05:20 +02:00
commit 08be0324a6
6 changed files with 66 additions and 44 deletions

View file

@ -55,8 +55,6 @@ class Const(constants.Const):
PRESENCE = getPresence()
MENU_NORMAL = "NORMAL"
# from plugin_misc_text_syntaxes
SYNTAX_XHTML = "XHTML"
SYNTAX_CURRENT = "@CURRENT@"

View file

@ -132,11 +132,11 @@ class Chat(urwid.WidgetWrap, QuickChat):
"""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 @@ class Chat(urwid.WidgetWrap, QuickChat):
#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 @@ class Chat(urwid.WidgetWrap, QuickChat):
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)

View file

@ -282,6 +282,22 @@ class PrimitivusApp(QuickApp, InputHistory):
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 @@ class PrimitivusApp(QuickApp, InputHistory):
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

View file

@ -133,8 +133,13 @@ class QuickApp(object):
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):

View file

@ -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 @@ class Chat(wx.Frame, QuickChat):
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 @@ class Chat(wx.Frame, QuickChat):
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)

View file

@ -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 @@ class MainWindow(wx.Frame, QuickApp):
#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 @@ class MainWindow(wx.Frame, QuickApp):
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 @@ class MainWindow(wx.Frame, QuickApp):
#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()