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