Fixing non-ascii support
[theonering] / hand_tests / tp_presence.py
1 #!/usr/bin/env python
2
3 import sys
4
5 import gobject
6 # Begin Example 2-1
7 import dbus.mainloop.glib
8 dbus.mainloop.glib.DBusGMainLoop(set_as_default = True)
9 # End Example 2-1
10
11 import telepathy
12 import telepathy.client
13 from telepathy.interfaces import CONNECTION_MANAGER, \
14                                                                  CONNECTION, \
15                                                                  CONNECTION_INTERFACE_REQUESTS, \
16                                                                  CONNECTION_INTERFACE_ALIASING, \
17                                                                  CONNECTION_INTERFACE_SIMPLE_PRESENCE, \
18                                                                  CONNECTION_INTERFACE_CONTACTS, \
19                                                                  CHANNEL, \
20                                                                  CHANNEL_TYPE_CONTACT_LIST, \
21                                                                  CHANNEL_INTERFACE_GROUP
22 from telepathy.constants import CONNECTION_STATUS_CONNECTED, \
23                                                                 CONNECTION_STATUS_DISCONNECTED, \
24                                                                 HANDLE_TYPE_LIST, \
25                                                                 HANDLE_TYPE_GROUP
26
27 DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
28
29
30 class Example(object):
31
32         def __init__ (self, account, password, forward):
33                 """e.g. account  = 'bob@example.com/test'
34                                 password = 'bigbob'
35                 """
36                 self.conn = None
37
38                 reg = telepathy.client.ManagerRegistry()
39                 reg.LoadManagers()
40
41                 # get the gabble Connection Manager
42                 self.cm = cm = reg.GetManager('theonering')
43
44                 # get the parameters required to make a Jabber connection
45                 # Begin Example 2-3
46                 cm[CONNECTION_MANAGER].RequestConnection('gv',
47                         {
48                                 'account':  account,
49                                 'forward':  forward,
50                                 'password': password,
51                         },
52                         reply_handler = self.request_connection_cb,
53                         error_handler = self.error_cb)
54                 # End Example 2-3
55
56         def run(self):
57                 self.loop = gobject.MainLoop()
58                 try:
59                         self.loop.run()
60                 except KeyboardInterrupt:
61                         print "Terminating connection..."
62                         self.disconnect()
63                         # reengage the mainloop so that we can disconnect cleanly
64                         self.loop.run()
65
66         def generic_reply (self, *args): pass
67
68         def error_cb (self, error):
69                 print "Error:", error
70                 self.disconnect()
71
72         def disconnect (self):
73                 if self.conn is None:
74                         return
75                 self.conn[CONNECTION].Disconnect(reply_handler = self.generic_reply,
76                                                                                  error_handler = self.error_cb)
77
78         # Begin Example 2-4
79         def request_connection_cb (self, bus_name, object_path):
80                 print bus_name, object_path
81                 # End Example 2-4
82                 self.conn = conn = telepathy.client.Connection(bus_name, object_path)
83
84                 conn[CONNECTION].connect_to_signal('StatusChanged',
85                         self.status_changed_cb)
86
87                 print "Establishing connection..."
88                 conn[CONNECTION].Connect(reply_handler = self.generic_reply,
89                                                                  error_handler = self.error_cb)
90
91         def status_changed_cb (self, status, reason):
92                 conn = self.conn
93
94                 if status == CONNECTION_STATUS_DISCONNECTED:
95                         print "Disconnected!"
96                         self.loop.quit()
97
98                 if status != CONNECTION_STATUS_CONNECTED: return
99
100                 print 'Carrier Detected' # remember dialup modems?
101                 print 'Ctrl-C to disconnect'
102
103                 # get a list of interfaces on this connection
104                 conn[CONNECTION].GetInterfaces(reply_handler = self.get_interfaces_cb,
105                                                                            error_handler = self.error_cb)
106
107         def request_contact_list (self, *groups):
108                 conn = self.conn
109
110                 class ensure_channel_cb (object):
111                         def __init__ (self, parent, group):
112                                 self.parent = parent
113                                 self.group = group
114
115                         def __call__ (self, yours, path, properties):
116                                 print "got channel for %s -> %s, yours = %s" % (
117                                         self.group, path, yours)
118
119                                 channel = telepathy.client.Channel(conn.service_name, path)
120                                 self.channel = channel
121
122                                 # Begin Example 6-8
123                                 # request the list of members
124                                 channel[DBUS_PROPERTIES].Get(CHANNEL_INTERFACE_GROUP,
125                                                                                  'Members',
126                                                                                  reply_handler = self.members_cb,
127                                                                                  error_handler = self.parent.error_cb)
128
129                         def members_cb (self, handles):
130                                 # request information for this list of handles using the
131                                 # Contacts interface
132                                 conn[CONNECTION_INTERFACE_CONTACTS].GetContactAttributes(
133                                         handles, [
134                                                 CONNECTION,
135                                                 CONNECTION_INTERFACE_ALIASING,
136                                                 CONNECTION_INTERFACE_SIMPLE_PRESENCE,
137                                         ],
138                                         False,
139                                         reply_handler = self.get_contact_attributes_cb,
140                                         error_handler = self.parent.error_cb)
141                                 # End Example 6-8
142
143                         def get_contact_attributes_cb (self, attributes):
144                                 return # DEBUG
145
146                                 print '-' * 78
147                                 print self.group
148                                 print '-' * 78
149                                 for member in attributes.values():
150                                         for key, value in member.iteritems():
151                                                 print '%s: %s' % (key, value)
152                                         print
153                                 print '-' * 78
154
155                 def no_channel_available (error):
156                         print error
157
158                 # we can either use TargetID if we know a name, or TargetHandle
159                 # if we already have a handle
160                 for group in groups:
161                         # Begin Example 6-6
162                         print "Ensuring channel to %s..." % group
163                         conn[CONNECTION_INTERFACE_REQUESTS].EnsureChannel({
164                                 CHANNEL + '.ChannelType'         : CHANNEL_TYPE_CONTACT_LIST,
165                                 CHANNEL + '.TargetHandleType': HANDLE_TYPE_LIST,
166                                 CHANNEL + '.TargetID'           : group,
167                                 },
168                                 reply_handler = ensure_channel_cb(self, group),
169                                 error_handler = no_channel_available)
170                         # End Example 6-6
171
172         def get_interfaces_cb (self, interfaces):
173                 conn = self.conn
174
175                 print "Interfaces:"
176                 for interface in interfaces:
177                         print " - %s" % interface
178
179                 if CONNECTION_INTERFACE_REQUESTS in interfaces:
180                         self.request_contact_list('subscribe',
181                                                                           'publish',
182                                                                           'hide',
183                                                                           'allow',
184                                                                           'deny',
185                                                                           'known')
186
187                         # get the open channelsA
188                         # Begin Example 2-6
189                         conn[CONNECTION_INTERFACE_REQUESTS].connect_to_signal(
190                                                                         'NewChannels',
191                                                                         self.get_channels_cb)
192                         # End Example 2-6
193                         # Begin Example 2-5
194                         conn[DBUS_PROPERTIES].Get(CONNECTION_INTERFACE_REQUESTS,
195                                                                         'Channels',
196                                                                         reply_handler = self.get_channels_cb,
197                                                                         error_handler = self.error_cb)
198                         # End Example 2-5
199
200                 if CONNECTION_INTERFACE_SIMPLE_PRESENCE in interfaces:
201                         # Begin Example 5-4
202                         # request the statuses
203                         print 'Requesting statuses...'
204                         conn[DBUS_PROPERTIES].Get(CONNECTION_INTERFACE_SIMPLE_PRESENCE,
205                                                                         'Statuses',
206                                                                         reply_handler = self.get_statuses_cb,
207                                                                         error_handler = self.error_cb)
208
209                         # set our presence
210                         #print 'Setting presence...'
211                         #conn[CONNECTION_INTERFACE_SIMPLE_PRESENCE].SetPresence(
212                         #                                               'away',
213                         #                                               'At the Movies',
214                         #                                               reply_handler = self.generic_reply,
215                         #                                               error_handler = self.error_cb)
216                         # End Example 5-4
217
218                 if CONNECTION_INTERFACE_CONTACTS in interfaces:
219                         print 'Requesting contact attribute interfaces...'
220                         conn[DBUS_PROPERTIES].Get(CONNECTION_INTERFACE_CONTACTS,
221                                                                         'ContactAttributeInterfaces',
222                                                                         reply_handler = self.get_contact_ifaces_cb,
223                                                                         error_handler = self.error_cb)
224
225         def get_channels_cb (self, channels):
226                 for channel, props in channels:
227                         # find the group channels
228                         if props[CHANNEL + '.ChannelType'] != CHANNEL_TYPE_CONTACT_LIST or \
229                            props[CHANNEL + '.TargetHandleType'] != HANDLE_TYPE_GROUP:
230                                 continue
231
232                         print 'GROUP: Got channel %s: %s' % (channel, props[CHANNEL + '.TargetID'])
233
234         def get_statuses_cb (self, value):
235                 print "Statuses:"
236
237                 for (key, value) in value.iteritems():
238                         print " - %s" % key
239
240         def get_contact_ifaces_cb (self, interfaces):
241                 print "Contact Attribute Interfaces:"
242                 for interface in interfaces:
243                         print " - %s" % interface
244
245 if __name__ == '__main__':
246         username = sys.argv[1]
247         password = sys.argv[2]
248         forward = sys.argv[3]
249         e = Example(username, password, forward)
250         e.run()