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

Source Code for Module Skype4Py.callchannel

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