diff src/core/sat_main.py @ 341:9eebdc655b8b

code: added asyncConnect
author Goffi <goffi@goffi.org>
date Thu, 26 May 2011 16:49:47 +0200
parents 953536246d9d
children ca3a041fed30
line wrap: on
line diff
--- a/src/core/sat_main.py	Thu May 26 16:49:07 2011 +0200
+++ b/src/core/sat_main.py	Thu May 26 16:49:47 2011 +0200
@@ -112,6 +112,7 @@
         self.bridge.register("deleteProfile", self.memory.deleteProfile)
         self.bridge.register("registerNewAccount", self.registerNewAccount)
         self.bridge.register("connect", self.connect)
+        self.bridge.register("asyncConnect", self.asyncConnect)
         self.bridge.register("disconnect", self.disconnect)
         self.bridge.register("getContacts", self.memory.getContacts)
         self.bridge.register("getPresenceStatus", self.memory.getPresenceStatus)
@@ -181,8 +182,16 @@
             self.plugins[import_name].is_handler = False
         #TODO: test xmppclient presence and register handler parent
 
+    
     def connect(self, profile_key = '@DEFAULT@'):
         """Connect to jabber server"""
+        self.asyncConnect(profile_key)
+    
+    def asyncConnect(self, profile_key = '@DEFAULT@', callback=None, errback=None):
+        """Connect to jabber server with asynchronous reply
+        @param profile_key: %(doc_profile)s
+        @param callback: called when the profile is connected
+        @param errback: called is the connection fail"""
 
         profile = self.memory.getProfileName(profile_key)
         if not profile:
@@ -191,12 +200,17 @@
         
         if (self.isConnected(profile)):
             info(_("already connected !"))
+            if callback:
+                callback()
             return
         current = self.profiles[profile] = xmpp.SatXMPPClient(self, profile,
             jid.JID(self.memory.getParamA("JabberID", "Connection", profile_key = profile), profile),
             self.memory.getParamA("Password", "Connection", profile_key = profile),
             self.memory.getParamA("Server", "Connection", profile_key = profile), 5222)
 
+        if callback and errback:
+            current.getConnectionDeferred().addCallbacks(lambda x:callback(), errback)
+
         current.messageProt = xmpp.SatMessageProtocol(self)
         current.messageProt.setHandlerParent(current)
         
@@ -220,7 +234,7 @@
                 plugin[1].getHandler(profile).setHandlerParent(current)
 
         current.startService()
-    
+
     def disconnect(self, profile_key='@DEFAULT@'):
         """disconnect from jabber server"""
         if (not self.isConnected(profile_key)):