comparison 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
comparison
equal deleted inserted replaced
340:2572351d875a 341:9eebdc655b8b
110 self.bridge.register("getProfilesList", self.memory.getProfilesList) 110 self.bridge.register("getProfilesList", self.memory.getProfilesList)
111 self.bridge.register("createProfile", self.memory.createProfile) 111 self.bridge.register("createProfile", self.memory.createProfile)
112 self.bridge.register("deleteProfile", self.memory.deleteProfile) 112 self.bridge.register("deleteProfile", self.memory.deleteProfile)
113 self.bridge.register("registerNewAccount", self.registerNewAccount) 113 self.bridge.register("registerNewAccount", self.registerNewAccount)
114 self.bridge.register("connect", self.connect) 114 self.bridge.register("connect", self.connect)
115 self.bridge.register("asyncConnect", self.asyncConnect)
115 self.bridge.register("disconnect", self.disconnect) 116 self.bridge.register("disconnect", self.disconnect)
116 self.bridge.register("getContacts", self.memory.getContacts) 117 self.bridge.register("getContacts", self.memory.getContacts)
117 self.bridge.register("getPresenceStatus", self.memory.getPresenceStatus) 118 self.bridge.register("getPresenceStatus", self.memory.getPresenceStatus)
118 self.bridge.register("getWaitingSub", self.memory.getWaitingSub) 119 self.bridge.register("getWaitingSub", self.memory.getWaitingSub)
119 self.bridge.register("sendMessage", self.sendMessage) 120 self.bridge.register("sendMessage", self.sendMessage)
179 self.plugins[import_name].is_handler = True 180 self.plugins[import_name].is_handler = True
180 else: 181 else:
181 self.plugins[import_name].is_handler = False 182 self.plugins[import_name].is_handler = False
182 #TODO: test xmppclient presence and register handler parent 183 #TODO: test xmppclient presence and register handler parent
183 184
185
184 def connect(self, profile_key = '@DEFAULT@'): 186 def connect(self, profile_key = '@DEFAULT@'):
185 """Connect to jabber server""" 187 """Connect to jabber server"""
188 self.asyncConnect(profile_key)
189
190 def asyncConnect(self, profile_key = '@DEFAULT@', callback=None, errback=None):
191 """Connect to jabber server with asynchronous reply
192 @param profile_key: %(doc_profile)s
193 @param callback: called when the profile is connected
194 @param errback: called is the connection fail"""
186 195
187 profile = self.memory.getProfileName(profile_key) 196 profile = self.memory.getProfileName(profile_key)
188 if not profile: 197 if not profile:
189 error (_('Trying to connect a non-exsitant profile')) 198 error (_('Trying to connect a non-exsitant profile'))
190 return 199 return
191 200
192 if (self.isConnected(profile)): 201 if (self.isConnected(profile)):
193 info(_("already connected !")) 202 info(_("already connected !"))
203 if callback:
204 callback()
194 return 205 return
195 current = self.profiles[profile] = xmpp.SatXMPPClient(self, profile, 206 current = self.profiles[profile] = xmpp.SatXMPPClient(self, profile,
196 jid.JID(self.memory.getParamA("JabberID", "Connection", profile_key = profile), profile), 207 jid.JID(self.memory.getParamA("JabberID", "Connection", profile_key = profile), profile),
197 self.memory.getParamA("Password", "Connection", profile_key = profile), 208 self.memory.getParamA("Password", "Connection", profile_key = profile),
198 self.memory.getParamA("Server", "Connection", profile_key = profile), 5222) 209 self.memory.getParamA("Server", "Connection", profile_key = profile), 5222)
199 210
211 if callback and errback:
212 current.getConnectionDeferred().addCallbacks(lambda x:callback(), errback)
213
200 current.messageProt = xmpp.SatMessageProtocol(self) 214 current.messageProt = xmpp.SatMessageProtocol(self)
201 current.messageProt.setHandlerParent(current) 215 current.messageProt.setHandlerParent(current)
202 216
203 current.roster = xmpp.SatRosterProtocol(self) 217 current.roster = xmpp.SatRosterProtocol(self)
204 current.roster.setHandlerParent(current) 218 current.roster.setHandlerParent(current)
218 for plugin in self.plugins.iteritems(): 232 for plugin in self.plugins.iteritems():
219 if plugin[1].is_handler: 233 if plugin[1].is_handler:
220 plugin[1].getHandler(profile).setHandlerParent(current) 234 plugin[1].getHandler(profile).setHandlerParent(current)
221 235
222 current.startService() 236 current.startService()
223 237
224 def disconnect(self, profile_key='@DEFAULT@'): 238 def disconnect(self, profile_key='@DEFAULT@'):
225 """disconnect from jabber server""" 239 """disconnect from jabber server"""
226 if (not self.isConnected(profile_key)): 240 if (not self.isConnected(profile_key)):
227 info(_("not connected !")) 241 info(_("not connected !"))
228 return 242 return