Package Skype4Py :: Module callchannel
[frames] | no frames]

Source Code for Module Skype4Py.callchannel

  1  '''Data channels for calls. 
  2  ''' 
  3   
  4  from utils import * 
  5  from enums import * 
  6  from errors import ISkypeError 
  7  import time 
  8   
  9   
10 -class ICallChannel(object):
11 '''Represents a call channel. 12 ''' 13
14 - def __init__(self, Manager, Call, Stream, Type):
15 '''__init__. 16 17 @param Manager: Manager 18 @type Manager: L{ICallChannelManager} 19 @param Call: Call 20 @type Call: L{ICall} 21 @param Stream: Stream 22 @type Stream: L{IApplicationStream} 23 @param Type: Type 24 @type Type: L{Call channel type<enums.cctUnknown>} 25 ''' 26 self._Manager = Manager 27 self._Call = Call 28 self._Stream = Stream 29 self._Type = Type
30
31 - def __repr__(self):
32 return '<%s with Manager=%s, Call=%s, Stream=%s>' % (object.__repr__(self)[1:-1], repr(self.Manager), repr(self.Call), repr(self.Stream))
33
34 - def SendTextMessage(self, Text):
35 '''Sends text message over channel. 36 37 @param Text: Text 38 @type Text: unicode 39 ''' 40 if self._Type == cctReliable: 41 self._Stream.Write(Text) 42 elif self._Type == cctDatagram: 43 self._Stream.SendDatagram(Text) 44 else: 45 raise ISkypeError(0, 'Cannot send using %s channel type' & repr(self._Type))
46
47 - def _GetCall(self):
48 return self._Call
49 50 Call = property(_GetCall, 51 doc='''Call. 52 53 @type: L{ICall} 54 ''') 55
56 - def _GetManager(self):
57 return self._Manager
58 59 Manager = property(_GetManager, 60 doc='''Manager. 61 62 @type: L{ICallChannelManager} 63 ''') 64
65 - def _GetStream(self):
66 return self._Stream
67 68 Stream = property(_GetStream, 69 doc='''Stream. 70 71 @type: L{IApplicationStream} 72 ''') 73
74 - def _GetType(self):
75 return self._Type
76 77 Type = property(_GetType, 78 doc='''Type. 79 80 @type: L{Call channel type<enums.cctUnknown>} 81 ''')
82 83
84 -class ICallChannelManager(EventHandlingBase):
85 '''Instatinate this class to create a call channel manager. A call channel manager will 86 automatically create a data channel for voice calls based on the APP2APP protocol. 87 88 1. Usage. 89 90 You should access this class using the alias at the package level:: 91 92 import Skype4Py 93 94 skype = Skype4Py.Skype() 95 96 ccm = Skype4Py.CallChannelManager() 97 ccm.Connect(skype) 98 99 For possible constructor arguments, read the L{ICallChannelManager.__init__} description. 100 101 2. Events. 102 103 This class provides events. 104 105 The events names and their arguments lists can be found in L{ICallChannelManagerEvents} class. 106 107 The usage of events is described in L{EventHandlingBase} class which is a superclass of 108 this class. Follow the link for more information. 109 110 @ivar OnChannels: Event handler for L{ICallChannelManagerEvents.Channels} event. See L{EventHandlingBase} for more information on events. 111 @type OnChannels: callable 112 113 @ivar OnMessage: Event handler for L{ICallChannelManagerEvents.Message} event. See L{EventHandlingBase} for more information on events. 114 @type OnMessage: callable 115 116 @ivar OnCreated: Event handler for L{ICallChannelManagerEvents.Created} event. See L{EventHandlingBase} for more information on events. 117 @type OnCreated: callable 118 ''' 119
120 - def __del__(self):
121 if self._Application: 122 self._Application.Delete() 123 self._Application = None 124 self._Skype.UnregisterEventHandler('ApplicationStreams', self._OnApplicationStreams) 125 self._Skype.UnregisterEventHandler('ApplicationReceiving', self._OnApplicationReceiving) 126 self._Skype.UnregisterEventHandler('ApplicationDatagram', self._OnApplicationDatagram)
127
128 - def __init__(self, Events=None):
129 '''__init__. 130 131 @param Events: Events 132 @type Events: An optional object with event handlers. See L{EventHandlingBase} for more information on events. 133 ''' 134 EventHandlingBase.__init__(self) 135 if Events: 136 self._SetEventHandlerObj(Events) 137 138 self._Skype = None 139 self._CallStatusEventHandler = None 140 self._ApplicationStreamsEventHandler = None 141 self._ApplicationReceivingEventHandler = None 142 self._ApplicationDatagramEventHandler = None 143 self._Application = None 144 self._Name = u'CallChannelManager' 145 self._ChannelType = cctReliable 146 self._Channels = []
147
148 - def _OnApplicationDatagram(self, pApp, pStream, Text):
149 if pApp == self._Application: 150 for ch in self_Channels: 151 if ch.Stream == pStream: 152 msg = ICallChannelMessage(Text) 153 self._CallEventHandler('Message', self, ch, msg) 154 break
155
156 - def _OnApplicationReceiving(self, pApp, pStreams):
157 if pApp == self._Application: 158 for ch in self._Channels: 159 if ch.Stream in pStreams: 160 msg = ICallChannelMessage(ch.Stream.Read()) 161 self._CallEventHandler('Message', self, ch, msg)
162
163 - def _OnApplicationStreams(self, pApp, pStreams):
164 if pApp == self._Application: 165 for ch in self._Channels: 166 if ch.Stream not in pStreams: 167 self._Channels.remove(ch) 168 self._CallEventHandler('Channels', self, tuple(self._Channels))
169
170 - def _OnCallStatus(self, pCall, Status):
171 if Status == clsRinging: 172 if self._Application == None: 173 self.CreateApplication() 174 self._Application.Connect(pCall.PartnerHandle, True) 175 for stream in self._Application.Streams: 176 if stream.PartnerHandle == pCall.PartnerHandle: 177 self._Channels.append(ICallChannel(self, pCall, stream, self._ChannelType)) 178 self._CallEventHandler('Channels', self, tuple(self._Channels)) 179 break 180 elif Status in (clsCancelled, clsFailed, clsFinished, clsRefused, clsMissed): 181 for ch in self._Channels: 182 if ch.Call == pCall: 183 self._Channels.remove(ch) 184 self._CallEventHandler('Channels', self, tuple(self._Channels)) 185 try: 186 ch.Stream.Disconnect() 187 except ISkypeError: 188 pass 189 break
190
191 - def Connect(self, Skype):
192 '''Connects this call channel manager instance to Skype. This is the first thing you should 193 do after creating this object. 194 195 @param Skype: Skype object 196 @type Skype: L{ISkype} 197 @see: L{Disconnect} 198 ''' 199 self._Skype = Skype 200 self._Skype.RegisterEventHandler('CallStatus', self._OnCallStatus)
201
202 - def CreateApplication(self, ApplicationName=None):
203 '''Creates an APP2APP application context. The application is automatically created using 204 L{IApplication.Create<application.IApplication.Create>}. 205 206 @param ApplicationName: Application name 207 @type ApplicationName: unicode 208 ''' 209 if ApplicationName != None: 210 self.Name = ApplicationName 211 self._Application = self._Skype.Application(self.Name) 212 self._Skype.RegisterEventHandler('ApplicationStreams', self._OnApplicationStreams) 213 self._Skype.RegisterEventHandler('ApplicationReceiving', self._OnApplicationReceiving) 214 self._Skype.RegisterEventHandler('ApplicationDatagram', self._OnApplicationDatagram) 215 self._Application.Create() 216 self._CallEventHandler('Created', self)
217
218 - def Disconnect(self):
219 '''Disconnects from Skype. 220 @see: L{Connect} 221 ''' 222 self._Skype.UnregisterEventHandler('CallStatus', self._OnCallStatus) 223 self._Skype = None
224
225 - def _GetChannels(self):
226 return tuple(self._Channels)
227 228 Channels = property(_GetChannels, 229 doc='''All call data channels. 230 231 @type: tuple of L{ICallChannel} 232 ''') 233
234 - def _GetChannelType(self):
235 return self._ChannelType
236
237 - def _SetChannelType(self, ChannelType):
238 self._ChannelType = ChannelType
239 240 ChannelType = property(_GetChannelType, _SetChannelType, 241 doc='''Queries/sets the default channel type. 242 243 @type: L{Call channel type<enums.cctUnknown>} 244 ''') 245
246 - def _GetCreated(self):
247 return bool(self._Application)
248 249 Created = property(_GetCreated, 250 doc='''Returns True if the application context has been created. 251 252 @type: bool 253 ''') 254
255 - def _GetName(self):
256 return self._Name
257
258 - def _SetName(self, Name):
259 self._Name = unicode(Name)
260 261 Name = property(_GetName, _SetName, 262 doc='''Queries/sets the application context name. 263 264 @type: unicode 265 ''')
266 267
268 -class ICallChannelManagerEvents(object):
269 '''Events defined in L{ICallChannelManager}. 270 271 See L{EventHandlingBase} for more information on events. 272 ''' 273
274 - def Channels(self, Manager, Channels):
275 '''This event is triggered when list of call channels changes. 276 277 @param Manager: Manager 278 @type Manager: L{ICallChannelManager} 279 @param Channels: Channels 280 @type Channels: tuple of L{ICallChannel} 281 '''
282
283 - def Created(self, Manager):
284 '''This event is triggered when the application context has successfuly been created. 285 286 @param Manager: Manager 287 @type Manager: L{ICallChannelManager} 288 '''
289
290 - def Message(self, Manager, Channel, Message):
291 '''This event is triggered when a call channel message has been received. 292 293 @param Manager: Manager 294 @type Manager: L{ICallChannelManager} 295 @param Channel: Channel 296 @type Channel: L{ICallChannel} 297 @param Message: Message 298 @type Message: L{ICallChannelMessage} 299 '''
300 301 302 ICallChannelManager._AddEvents(ICallChannelManagerEvents) 303 304
305 -class ICallChannelMessage(object):
306 '''Represents a call channel message. 307 ''' 308
309 - def __init__(self, Text):
310 '''__init__. 311 312 @param Text: Text 313 @type Text: unicode 314 ''' 315 self._Text = Text
316
317 - def _GetText(self):
318 return self._Text
319
320 - def _SetText(self, Text):
321 self._Text = Text
322 323 Text = property(_GetText, _SetText, 324 doc='''Queries/sets message text. 325 326 @type: unicode 327 ''')
328