2e69a5d0b90d3831b5bcebcb4dc3163656096fdb
[theonering] / hand_tests / generic.py
1 #!/usr/bin/env python
2
3 import sys
4
5 import gobject
6 import dbus.mainloop.glib
7 dbus.mainloop.glib.DBusGMainLoop(set_as_default = True)
8
9 import telepathy
10
11
12 DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
13
14
15 def get_registry():
16         reg = telepathy.client.ManagerRegistry()
17         reg.LoadManagers()
18         return reg
19
20
21 def get_connection_manager(reg):
22         cm = reg.GetManager('theonering')
23         return cm
24
25
26 class Action(object):
27
28         def __init__(self):
29                 self._action = None
30
31         def queue_action(self):
32                 pass
33
34         def append_action(self, action):
35                 assert self._action is None
36                 self._action = action
37
38         def _on_done(self):
39                 if self._action is None:
40                         return
41                 self._action.queue_action()
42
43         def _on_error(self, error):
44                 print error
45
46         def _on_generic_message(self, *args):
47                 pass
48
49
50 class DummyAction(Action):
51
52         def queue_action(self):
53                 gobject.idle_add(self._on_done)
54
55
56 class QuitLoop(Action):
57
58         def __init__(self, loop):
59                 super(QuitLoop, self).__init__()
60                 self._loop = loop
61
62         def queue_action(self):
63                 self._loop.quit()
64
65
66 class DisplayParams(Action):
67
68         def __init__(self, cm):
69                 super(DisplayParams, self).__init__()
70                 self._cm = cm
71
72         def queue_action(self):
73                 self._cm[telepathy.interfaces.CONN_MGR_INTERFACE].GetParameters(
74                         'sip',
75                         reply_handler = self._on_done,
76                         error_handler = self._on_error,
77                 )
78
79         def _on_done(self, params):
80                 print "Connection Parameters:"
81                 for name, flags, signature, default in params:
82                         print "\t%s (%s)" % (name, signature),
83
84                         if flags & telepathy.constants.CONN_MGR_PARAM_FLAG_REQUIRED:
85                                 print "required",
86                         if flags & telepathy.constants.CONN_MGR_PARAM_FLAG_REGISTER:
87                                 print "register",
88                         if flags & telepathy.constants.CONN_MGR_PARAM_FLAG_SECRET:
89                                 print "secret",
90                         if flags & telepathy.constants.CONN_MGR_PARAM_FLAG_DBUS_PROPERTY:
91                                 print "dbus-property",
92                         if flags & telepathy.constants.CONN_MGR_PARAM_FLAG_HAS_DEFAULT:
93                                 print "has-default(%s)" % default,
94
95                         print ""
96                 super(DisplayParams, self)._on_done()
97
98
99 class Connect(Action):
100
101         def __init__(self, cm, username, password, forward):
102                 super(Connect, self).__init__()
103                 self._cm = cm
104
105                 self._conn = None
106                 self._serviceName = None
107
108                 self._username = username
109                 self._password = password
110                 self._forward = forward
111
112         @property
113         def conn(self):
114                 return self._conn
115
116         @property
117         def serviceName(self):
118                 return self._serviceName
119
120         def queue_action(self):
121                 self._cm[telepathy.server.CONNECTION_MANAGER].RequestConnection(
122                         'sip',
123                         {
124                                 'username':  self._username,
125                                 'password': self._password,
126                                 'forward':  self._forward,
127                         },
128                         reply_handler = self._on_connection_requested,
129                         error_handler = self._on_error,
130                 )
131
132         def _on_connection_requested(self, busName, objectPath):
133                 self._serviceName = busName
134                 self._conn = telepathy.client.Connection(busName, objectPath)
135                 self._conn[telepathy.server.CONNECTION].connect_to_signal(
136                         'StatusChanged',
137                         self._on_change,
138                 )
139                 self._conn[telepathy.server.CONNECTION].Connect(
140                         reply_handler = self._on_generic_message,
141                         error_handler = self._on_error,
142                 )
143
144         def _on_done(self):
145                 super(Connect, self)._on_done()
146
147         def _on_change(self, status, reason):
148                 if status == telepathy.constants.CONNECTION_STATUS_DISCONNECTED:
149                         print "Disconnected!"
150                         self._conn = None
151                 elif status == telepathy.constants.CONNECTION_STATUS_CONNECTED:
152                         print "Connected"
153                         self._on_done()
154                 elif status == telepathy.constants.CONNECTION_STATUS_CONNECTING:
155                         print "Connecting"
156                 else:
157                         print "Status: %r" % status
158
159
160 class SimplePresenceOptions(Action):
161
162         def __init__(self, connAction):
163                 super(SimplePresenceOptions, self).__init__()
164                 self._connAction = connAction
165
166         def queue_action(self):
167                 self._connAction.conn[DBUS_PROPERTIES].Get(
168                         telepathy.server.CONNECTION_INTERFACE_SIMPLE_PRESENCE,
169                         'Statuses',
170                         reply_handler = self._on_done,
171                         error_handler = self._on_error,
172                 )
173
174         def _on_done(self, statuses):
175                 print "\tAvailable Statuses"
176                 for (key, value) in statuses.iteritems():
177                         print "\t\t - %s" % key
178                 super(SimplePresenceOptions, self)._on_done()
179
180
181 class NullHandle(object):
182
183         @property
184         def handle(self):
185                 return 0
186
187         @property
188         def handles(self):
189                 return []
190
191
192 class UserHandle(Action):
193
194         def __init__(self, connAction):
195                 super(UserHandle, self).__init__()
196                 self._connAction = connAction
197                 self._handle = None
198
199         @property
200         def handle(self):
201                 return self._handle
202
203         @property
204         def handles(self):
205                 return [self._handle]
206
207         def queue_action(self):
208                 self._connAction.conn[telepathy.server.CONNECTION].GetSelfHandle(
209                         reply_handler = self._on_done,
210                         error_handler = self._on_error,
211                 )
212
213         def _on_done(self, handle):
214                 self._handle = handle
215                 super(UserHandle, self)._on_done()
216
217
218 class RequestHandle(Action):
219
220         def __init__(self, connAction, handleType, handleNames):
221                 super(RequestHandle, self).__init__()
222                 self._connAction = connAction
223                 self._handle = None
224                 self._handleType = handleType
225                 self._handleNames = handleNames
226
227         @property
228         def handle(self):
229                 return self._handle
230
231         @property
232         def handles(self):
233                 return [self._handle]
234
235         def queue_action(self):
236                 self._connAction.conn[telepathy.server.CONNECTION].RequestHandles(
237                         self._handleType,
238                         self._handleNames,
239                         reply_handler = self._on_done,
240                         error_handler = self._on_error,
241                 )
242
243         def _on_done(self, handles):
244                 self._handle = handles[0]
245                 super(RequestHandle, self)._on_done()
246
247
248 class RequestChannel(Action):
249
250         def __init__(self, connAction, handleAction, channelType, handleType):
251                 super(RequestChannel, self).__init__()
252                 self._connAction = connAction
253                 self._handleAction = handleAction
254                 self._channel = None
255                 self._channelType = channelType
256                 self._handleType = handleType
257
258         @property
259         def channel(self):
260                 return self._channel
261
262         def queue_action(self):
263                 self._connAction.conn[telepathy.server.CONNECTION].RequestChannel(
264                         self._channelType,
265                         self._handleType,
266                         self._handleAction.handle,
267                         True,
268                         reply_handler = self._on_done,
269                         error_handler = self._on_error,
270                 )
271
272         def _on_done(self, channelObjectPath):
273                 self._channel = telepathy.client.Channel(self._connAction.serviceName, channelObjectPath)
274                 super(RequestChannel, self)._on_done()
275
276
277 class ContactHandles(Action):
278
279         def __init__(self, connAction, chanAction):
280                 super(ContactHandles, self).__init__()
281                 self._connAction = connAction
282                 self._chanAction = chanAction
283                 self._handles = []
284
285         @property
286         def handles(self):
287                 return self._handles
288
289         def queue_action(self):
290                 self._chanAction.channel[DBUS_PROPERTIES].Get(
291                         telepathy.server.CHANNEL_INTERFACE_GROUP,
292                         'Members',
293                         reply_handler = self._on_done,
294                         error_handler = self._on_error,
295                 )
296
297         def _on_done(self, handles):
298                 self._handles = list(handles)
299                 super(ContactHandles, self)._on_done()
300
301
302 class SimplePresenceStatus(Action):
303
304         def __init__(self, connAction, handleAction):
305                 super(SimplePresenceStatus, self).__init__()
306                 self._connAction = connAction
307                 self._handleAction = handleAction
308
309         def queue_action(self):
310                 self._connAction.conn[telepathy.server.CONNECTION_INTERFACE_SIMPLE_PRESENCE].GetPresences(
311                         self._handleAction.handles,
312                         reply_handler = self._on_done,
313                         error_handler = self._on_error,
314                 )
315
316         def _on_done(self, aliases):
317                 print "\tPresences:"
318                 for hid, (presenceType, presence, presenceMessage) in aliases.iteritems():
319                         print "\t\t%s:" % hid, presenceType, presence, presenceMessage
320                 super(SimplePresenceStatus, self)._on_done()
321
322
323 class SetSimplePresence(Action):
324
325         def __init__(self, connAction, status, message):
326                 super(SetSimplePresence, self).__init__()
327                 self._connAction = connAction
328                 self._status = status
329                 self._message = message
330
331         def queue_action(self):
332                 self._connAction.conn[telepathy.server.CONNECTION_INTERFACE_SIMPLE_PRESENCE].SetPresence(
333                         self._status,
334                         self._message,
335                         reply_handler = self._on_done,
336                         error_handler = self._on_error,
337                 )
338
339         def _on_done(self):
340                 super(SetSimplePresence, self)._on_done()
341
342
343 class Aliases(Action):
344
345         def __init__(self, connAction, handleAction):
346                 super(Aliases, self).__init__()
347                 self._connAction = connAction
348                 self._handleAction = handleAction
349
350         def queue_action(self):
351                 self._connAction.conn[telepathy.server.CONNECTION_INTERFACE_ALIASING].RequestAliases(
352                         self._handleAction.handles,
353                         reply_handler = self._on_done,
354                         error_handler = self._on_error,
355                 )
356
357         def _on_done(self, aliases):
358                 print "\tAliases:"
359                 for h, alias in zip(self._handleAction.handles, aliases):
360                         print "\t\t", h, alias
361                 super(Aliases, self)._on_done()
362
363
364 class Call(Action):
365
366         def __init__(self, connAction, chanAction, handleAction):
367                 super(Call, self).__init__()
368                 self._connAction = connAction
369                 self._chanAction = chanAction
370                 self._handleAction = handleAction
371
372         def queue_action(self):
373                 self._chanAction.channel[telepathy.server.CHANNEL_TYPE_STREAMED_MEDIA].RequestStreams(
374                         self._handleAction.handle,
375                         [telepathy.constants.MEDIA_STREAM_TYPE_AUDIO],
376                         reply_handler = self._on_done,
377                         error_handler = self._on_error,
378                 )
379
380         def _on_done(self, handle):
381                 print "Call started"
382                 super(Call, self)._on_done()
383
384
385 class SendText(Action):
386
387         def __init__(self, connAction, chanAction, handleAction, messageType, message):
388                 super(SendText, self).__init__()
389                 self._connAction = connAction
390                 self._chanAction = chanAction
391                 self._handleAction = handleAction
392                 self._messageType = messageType
393                 self._message = message
394
395         def queue_action(self):
396                 self._chanAction.channel[telepathy.server.CHANNEL_TYPE_TEXT].Send(
397                         self._messageType,
398                         self._message,
399                         reply_handler = self._on_done,
400                         error_handler = self._on_error,
401                 )
402
403         def _on_done(self,):
404                 print "Message sent"
405                 super(SendText, self)._on_done()
406
407
408 class Sleep(Action):
409
410         def __init__(self, length):
411                 super(Sleep, self).__init__()
412                 self._length = length
413
414         def queue_action(self):
415                 gobject.timeout_add(self._length, self._on_done)
416
417
418 class Block(Action):
419
420         def __init__(self):
421                 super(Block, self).__init__()
422
423         def queue_action(self):
424                 print "Blocking"
425
426         def _on_done(self):
427                 #super(SendText, self)._on_done()
428                 pass
429
430
431 class Disconnect(Action):
432
433         def __init__(self, connAction):
434                 super(Disconnect, self).__init__()
435                 self._connAction = connAction
436
437         def queue_action(self):
438                 self._connAction.conn[telepathy.server.CONNECTION].Disconnect(
439                         reply_handler = self._on_done,
440                         error_handler = self._on_error,
441                 )
442
443
444 if __name__ == '__main__':
445         loop = gobject.MainLoop()
446
447         reg = get_registry()
448         cm = get_connection_manager(reg)
449
450         nullHandle = NullHandle()
451
452         dummy = DummyAction()
453         firstAction = dummy
454         lastAction = dummy
455
456         if True:
457                 dp = DisplayParams(cm)
458                 lastAction.append_action(dp)
459                 lastAction = dp
460
461         if True:
462                 username = sys.argv[1]
463                 password = sys.argv[2]
464                 forward = sys.argv[3]
465                 con = Connect(cm, username, password, forward)
466                 lastAction.append_action(con)
467                 lastAction = con
468
469                 if True:
470                         spo = SimplePresenceOptions(con)
471                         lastAction.append_action(spo)
472                         lastAction = spo
473
474                 if True:
475                         uh = UserHandle(con)
476                         lastAction.append_action(uh)
477                         lastAction = uh
478
479                         ua = Aliases(con, uh)
480                         lastAction.append_action(ua)
481                         lastAction = ua
482
483                         sps = SimplePresenceStatus(con, uh)
484                         lastAction.append_action(sps)
485                         lastAction = sps
486
487                         if False:
488                                 setdnd = SetSimplePresence(con, "dnd", "")
489                                 lastAction.append_action(setdnd)
490                                 lastAction = setdnd
491
492                                 sps = SimplePresenceStatus(con, uh)
493                                 lastAction.append_action(sps)
494                                 lastAction = sps
495
496                                 setdnd = SetSimplePresence(con, "available", "")
497                                 lastAction.append_action(setdnd)
498                                 lastAction = setdnd
499
500                                 sps = SimplePresenceStatus(con, uh)
501                                 lastAction.append_action(sps)
502                                 lastAction = sps
503
504                 if True:
505                         sl = Sleep(10 * 1000)
506                         lastAction.append_action(sl)
507                         lastAction = sl
508
509                 if True:
510                         rclh = RequestHandle(con, telepathy.HANDLE_TYPE_LIST, ["subscribe"])
511                         lastAction.append_action(rclh)
512                         lastAction = rclh
513
514                         rclc = RequestChannel(
515                                 con,
516                                 rclh,
517                                 telepathy.CHANNEL_TYPE_CONTACT_LIST,
518                                 telepathy.HANDLE_TYPE_LIST,
519                         )
520                         lastAction.append_action(rclc)
521                         lastAction = rclc
522
523                         ch = ContactHandles(con, rclc)
524                         lastAction.append_action(ch)
525                         lastAction = ch
526
527                         ca = Aliases(con, ch)
528                         lastAction.append_action(ca)
529                         lastAction = ca
530
531                 if False:
532                         rch = RequestHandle(con, telepathy.HANDLE_TYPE_CONTACT, ["18005558355"]) #(1-800-555-TELL)
533                         lastAction.append_action(rch)
534                         lastAction = rch
535
536                         # making a phone call
537                         if True:
538                                 smHandle = rch
539                                 smHandleType = telepathy.HANDLE_TYPE_CONTACT
540                         else:
541                                 smHandle = nullHandle
542                                 smHandleType = telepathy.HANDLE_TYPE_NONE
543                         rsmc = RequestChannel(
544                                 con,
545                                 smHandle,
546                                 telepathy.CHANNEL_TYPE_STREAMED_MEDIA,
547                                 smHandleType,
548                         )
549                         lastAction.append_action(rsmc)
550                         lastAction = rsmc
551
552                         if False:
553                                 call = Call(con, rsmc, rch)
554                                 lastAction.append_action(call)
555                                 lastAction = call
556
557                         # sending a text
558                         rtc = RequestChannel(
559                                 con,
560                                 rch,
561                                 telepathy.CHANNEL_TYPE_TEXT,
562                                 smHandleType,
563                         )
564                         lastAction.append_action(rtc)
565                         lastAction = rtc
566
567                         if False:
568                                 sendtext = SendText(con, rtc, rch, telepathy.CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, "Boo!")
569                                 lastAction.append_action(sendtext)
570                                 lastAction = sendtext
571
572                 if False:
573                         bl = Block()
574                         lastAction.append_action(bl)
575                         lastAction = bl
576
577                 if True:
578                         sl = Sleep(30 * 1000)
579                         lastAction.append_action(sl)
580                         lastAction = sl
581
582                 dis = Disconnect(con)
583                 lastAction.append_action(dis)
584                 lastAction = dis
585
586         quitter = QuitLoop(loop)
587         lastAction.append_action(quitter)
588         lastAction = quitter
589
590         firstAction.queue_action()
591         loop.run()