All pastes #1853249 Raw Edit

Unnamed

public python v1 · immutable
#1853249 ·published 2010-03-26 19:35 UTC
rendered paste body
#!/usr/bin/env pythonimport dbusimport dbus.glibimport gobjectimport datetimeimport telepathyfrom telepathy.interfaces import *class ExampleObserver(telepathy.server.Observer,                      telepathy.server.DBusProperties):    def __init__(self, client_name):        bus_name = '.'.join ([CLIENT, client_name])        object_path = '/' + bus_name.replace('.', '/')        bus_name = dbus.service.BusName(bus_name, bus=dbus.SessionBus())        telepathy.server.Observer.__init__(self, bus_name, object_path)        telepathy.server.DBusProperties.__init__(self)        self._implement_property_get(CLIENT, {            'Interfaces': lambda: [ CLIENT_OBSERVER ],          })        self._implement_property_get(CLIENT_OBSERVER, {            'ObserverChannelFilter': lambda: dbus.Array([                    dbus.Dictionary({                        CHANNEL + '.ChannelType': CHANNEL_TYPE_TEXT,                    }, signature='sv')                ], signature='a{sv}')          })    def ObserveChannels(self, account_path, connection_path, channels,                        dispatch_operation, requests_satisfied, observer_info):# Callback functions for getting contact information        @dbus.service.method(CHANNEL_TYPE_TEXT,            in_signature='a{sv}', out_signature='oa{sv}',            async_callbacks=('_success', '_error'))        def get_pending_messages(messages):            print "lol"            print messages            properties = channel.get_props()            _success(channel._object_path, properties)        def get_contact_attributes(attributes):            if attributes:                for handle, properties in attributes.items():                    print "Identifier: %s" % \                        properties[CONNECTION + '/contact-id']                    print "Alias: %s" % \                        properties[CONNECTION_INTERFACE_ALIASING + '/alias']            else:                print "No attributes known"        def get_locations(locations):            if locations:                for key, value in locations.items():                    print key, value            else:                print "Location: unknown"        def get_protocol(protocol):            print "Protocol:", protocol#TODO: more specific errors        def error(*args):            print "error", args        def message_sent(timestamp, contenttype, content):            time = datetime.datetime.fromtimestamp(timestamp)            print time, contenttype, content        def message_received(identification, timestamp, sender, contenttype,                flags, content):            time = datetime.datetime.fromtimestamp(timestamp)#            print identification, time, sender, contenttype, flags, content            print "id:", identification            print "time:", time            print "sender", sender            print "type:", contenttype            print "flags:", flags            print "content:", content        def channel_closed():            print "Channel closed"# Once a connection is ready, query some methods for immediate information,# then connect to relevant signals        def connection_ready(connection):            print "Connection ready:", connection            handles = [ properties[CHANNEL + '.TargetHandle'] for                        channel, properties in channels ]# Get pending messages on startup            if CHANNEL_TYPE_TEXT in connection:                connection[CHANNEL_TYPE_TEXT].ListPendingMessages(                    reply_handler=list_pending_messages,                    error_handler=error)# Get contact name and handle            if CONNECTION_INTERFACE_CONTACTS in connection:                connection[CONNECTION_INTERFACE_CONTACTS].GetContactAttributes(                    handles,                    # assuming this is available is technically wrong                    [ CONNECTION_INTERFACE_ALIASING ],                    False,                    reply_handler=get_contact_attributes,                    error_handler=error)# Get contact location (rarely available...yet)            if CONNECTION_INTERFACE_LOCATION in connection:                connection[CONNECTION_INTERFACE_LOCATION].GetLocations(                    handles,                    reply_handler=get_locations,                    error_handler=error)            else:                print "Location: unsupported"# Get conversation protocol            if CONNECTION in connection:                connection[CONNECTION].GetProtocol(                    reply_handler=get_protocol,                    error_handler=error)        def channel_ready(channel):            print "Channel ready", channel            channel[CHANNEL_TYPE_TEXT].connect_to_signal('Sent',                    message_sent)            channel[CHANNEL_TYPE_TEXT].connect_to_signal('Received',                    message_received)            channel[CHANNEL].connect_to_signal('Closed', channel_closed)        bus_name = connection_path.replace('/', '.')[1:]        connection = telepathy.client.Connection(bus_name, connection_path,                ready_handler=connection_ready)        textchannels = []        for channel_path, properties in channels:            print properties[CHANNEL + '.TargetHandle']            textchannels.append(telepathy.client.Channel(bus_name, channel_path,                    ready_handler=channel_ready))        def start():    ExampleObserver("ExampleObserver")    return Falseif __name__ == '__main__':    gobject.timeout_add(0, start)    loop = gobject.MainLoop()    loop.run()