comparison src/core/sat_main.py @ 755:e3ad48a2aab2

core, frontends: callMenu is now async and don't use callback_id anymore
author Goffi <goffi@goffi.org>
date Tue, 24 Dec 2013 15:18:31 +0100
parents f49945d728de
children 93bd868b8fb6
comparison
equal deleted inserted replaced
754:f021bf27a557 755:e3ad48a2aab2
107 107
108 self.__general_cb_map = {} # callback called for general reasons (key = name) 108 self.__general_cb_map = {} # callback called for general reasons (key = name)
109 self.__private_data = {} # used for internal callbacks (key = id) 109 self.__private_data = {} # used for internal callbacks (key = id)
110 self.profiles = {} 110 self.profiles = {}
111 self.plugins = {} 111 self.plugins = {}
112 self.menus = {} # used to know which new menus are wanted by plugins 112 self.menus = {} # dynamic menus. key: (type, category, name), value: menu data (dictionnary)
113 113
114 self.memory = Memory(self) 114 self.memory = Memory(self)
115 115
116 local_dir = self.memory.getConfig('', 'local_dir') 116 local_dir = self.memory.getConfig('', 'local_dir')
117 if not os.path.exists(local_dir): 117 if not os.path.exists(local_dir):
160 self.bridge.register("launchAction", self.launchAction) 160 self.bridge.register("launchAction", self.launchAction)
161 self.bridge.register("confirmationAnswer", self.confirmationAnswer) 161 self.bridge.register("confirmationAnswer", self.confirmationAnswer)
162 self.bridge.register("getProgress", self.getProgress) 162 self.bridge.register("getProgress", self.getProgress)
163 self.bridge.register("getMenus", self.getMenus) 163 self.bridge.register("getMenus", self.getMenus)
164 self.bridge.register("getMenuHelp", self.getMenuHelp) 164 self.bridge.register("getMenuHelp", self.getMenuHelp)
165 self.bridge.register("callMenu", self.callMenu) 165 self.bridge.register("asyncCallMenu", self.callMenu)
166 166
167 self.memory.initialized.addCallback(self._postMemoryInit) 167 self.memory.initialized.addCallback(self._postMemoryInit)
168 168
169 def _postMemoryInit(self, ignore): 169 def _postMemoryInit(self, ignore):
170 """Method called after memory initialization is done""" 170 """Method called after memory initialization is done"""
211 self.plugins[import_name].is_handler = True 211 self.plugins[import_name].is_handler = True
212 else: 212 else:
213 self.plugins[import_name].is_handler = False 213 self.plugins[import_name].is_handler = False
214 #TODO: test xmppclient presence and register handler parent 214 #TODO: test xmppclient presence and register handler parent
215 215
216 def connect(self, profile_key='@DEFAULT@'): 216 def connect(self, profile_key='@NONE@'):
217 """Connect to jabber server""" 217 """Connect to jabber server"""
218 self.asyncConnect(profile_key) 218 self.asyncConnect(profile_key)
219 219
220 def asyncConnect(self, profile_key='@DEFAULT@'): 220 def asyncConnect(self, profile_key='@NONE@'):
221 """Connect to jabber server with asynchronous reply 221 """Connect to jabber server with asynchronous reply
222 @param profile_key: %(doc_profile)s 222 @param profile_key: %(doc_profile)s
223 """ 223 """
224 224
225 profile = self.memory.getProfileName(profile_key) 225 profile = self.memory.getProfileName(profile_key)
374 profile = self.memory.getProfileName(profile_key) 374 profile = self.memory.getProfileName(profile_key)
375 if not profile: 375 if not profile:
376 return None 376 return None
377 return self.profiles[profile].getHostJid() 377 return self.profiles[profile].getHostJid()
378 378
379 def registerNewAccount(self, login, password, email, server, port=5222, id=None, profile_key='@DEFAULT@'): 379 def registerNewAccount(self, login, password, email, server, port=5222, id=None, profile_key='@NONE@'):
380 """Connect to a server and create a new account using in-band registration""" 380 """Connect to a server and create a new account using in-band registration"""
381 profile = self.memory.getProfileName(profile_key) 381 profile = self.memory.getProfileName(profile_key)
382 assert(profile) 382 assert(profile)
383 383
384 next_id = id or sat_next_id() # the id is used to send server's answer 384 next_id = id or sat_next_id() # the id is used to send server's answer
882 error(_("Trying to call unknown function (%s)") % name) 882 error(_("Trying to call unknown function (%s)") % name)
883 return None 883 return None
884 884
885 #Menus management 885 #Menus management
886 886
887 def importMenu(self, category, name, callback, help_string="", type="NORMAL"): 887 def importMenu(self, category, name, callback, callback_args=None, callback_kwargs=None, help_string="", type_="NORMAL"):
888 """register a new menu for frontends 888 """register a new menu for frontends
889 @param category: category of the menu 889 @param category: category of the menu
890 @param name: menu item entry 890 @param name: menu item entry
891 @param callback: method to be called when menuitem is selected""" 891 @param callback: method to be called when menuitem is selected
892 if (category, name) in self.menus: 892 @param callback_args: optional arguments to forward to callback
893 error("Want to register a menu which already existe") 893 @param callback_kwargs: optional keywords arguments to forward to callback
894 return 894 """
895 self.menus[(category, name, type)] = {'callback': callback, 'help_string': help_string, 'type': type} 895 # TODO: manage translations
896 if (type_, category, name) in self.menus:
897 raise exceptions.ConflictError("Menu already exists")
898 menu_data = {'callback': callback, 'help_string': help_string}
899 if callback_args is not None:
900 assert(isinstance(callback_args, list))
901 menu_data['callback_args'] = callback_args
902 if callback_kwargs is not None:
903 assert(isinstance(callback_kwargs, dict))
904 menu_data['callback_kwargs'] = callback_kwargs
905 self.menus[(type_, category, name)] = menu_data
896 906
897 def getMenus(self): 907 def getMenus(self):
898 """Return all menus registered""" 908 """Return all menus registered"""
909 # TODO: manage translations
899 return self.menus.keys() 910 return self.menus.keys()
900 911
901 def getMenuHelp(self, category, name, type="NORMAL"): 912 def getMenuHelp(self, category, name, type_="NORMAL"):
902 """return the help string of the menu""" 913 """return the help string of the menu"""
914 # TODO: manage translations
903 try: 915 try:
904 return self.menus[(category, name, type)]['help_string'] 916 return self.menus[(type_, category, name)]['help_string']
905 except KeyError: 917 except KeyError:
906 error(_("Trying to access an unknown menu")) 918 raise exceptions.DataError("Trying to access an unknown menu")
907 return "" 919
908 920 def callMenu(self, category, name, type_="NORMAL", profile_key='@NONE@'):
909 def callMenu(self, category, name, type="NORMAL", profile_key='@DEFAULT@'): 921 """ Call a dynamic menu
910 """return the id of the action""" 922 @param category: category of the menu to call
911 profile = self.memory.getProfileName(profile_key) 923 @param name: name of the menu to call
912 if not profile_key: 924 @param type_: type of the menu to call
913 error(_('Non-exsitant profile')) 925 @param profile_key: %(doc_profile_key)s
914 return "" 926 @return: XMLUI or empty string if it's a one shot menu
915 if (category, name, type) in self.menus: 927 """
916 id = self.get_next_id() 928 profile = self.memory.getProfileName(profile_key)
917 self.menus[(category, name, type)]['callback'](id, profile) 929 if not profile:
918 return id 930 raise exceptions.ProfileUnknownError
919 else: 931 menu_data = self.menus[(type_, category, name)]
920 error(_("Trying to access an unknown menu (%(category)s/%(name)s/%(type)s)") % {'category': category, 'name': name, 'type': type}) 932 callback = menu_data['callback']
921 return "" 933 args = menu_data.get('callback_args', ())
934 kwargs = menu_data.get('callback_kwargs', {}).copy()
935 kwargs["profile"] = profile
936 try:
937 return defer.maybeDeferred(callback, *args, **kwargs)
938 except KeyError:
939 raise exceptions.DataError("Trying to access an unknown menu (%(type)s/%(category)s/%(name)s)" % {'type': type_, 'category': category, 'name': name})