First_Hack_At_API

danni on #telepathy

   1 import gobject
   2 
   3 from telepathy.client import Channel
   4 from telepathy.interfaces import (
   5         CONN_INTERFACE, CHANNEL_INTERFACE_GROUP,
   6         CHANNEL_TYPE_TEXT, CHANNEL_INTERFACE, CONNECTION_INTERFACE_REQUESTS,
   7         CHANNEL_INTERFACE_TUBE, CHANNEL_TYPE_DBUS_TUBE)
   8 from telepathy.constants import (
   9         CONNECTION_HANDLE_TYPE_CONTACT,
  10         CONNECTION_HANDLE_TYPE_ROOM, CONNECTION_STATUS_CONNECTED,
  11         CONNECTION_STATUS_DISCONNECTED, CONNECTION_STATUS_CONNECTING,
  12         SOCKET_ACCESS_CONTROL_CREDENTIALS,
  13         TUBE_CHANNEL_STATE_LOCAL_PENDING, TUBE_CHANNEL_STATE_REMOTE_PENDING,
  14         TUBE_CHANNEL_STATE_OPEN, TUBE_CHANNEL_STATE_NOT_OFFERED)
  15 
  16 class Connection(object):
  17     def __init__(self):
  18         self.object_path = "....."
  19 
  20 class Account(object):
  21     def __init__(self):
  22         self.object_path = "....."
  23         self.connection = Connection()
  24 
  25 class MessageTubeHandler(object):
  26     __gsignals__ = {'handle-channel' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
  27                               (gobject.TYPE_PYOBJECT,gobject.TYPE_PYOBJECT)),
  28                     }
  29     def __init__(self, client_name):
  30         bus_name = '.'.join ([CLIENT, client_name])
  31         object_path = '/' + bus_name.replace('.', '/')
  32 
  33         bus_name = dbus.service.BusName(bus_name, bus=dbus.SessionBus())
  34 
  35         telepathy.server.Handler.__init__(self, bus_name, object_path)
  36         telepathy.server.DBusProperties.__init__(self)
  37 
  38         self._implement_property_get(CLIENT, {
  39             'Interfaces': lambda: [ CLIENT_HANDLER ],
  40           })
  41         self._implement_property_get(CLIENT_HANDLER, {
  42             'HandlerChannelFilter': lambda: dbus.Array([
  43                     dbus.Dictionary({
  44                     }, signature='sv')
  45                 ], signature='a{sv}')
  46           })
  47 
  48     def HandleChannels(self, account, connection, channels, requests_satisfied,
  49                         user_action_time, handler_info):
  50         for channel in channels:
  51             self.emit('handle-channel', MessageTube(account, channel))
  52 
  53 class MessageTube(object):
  54     def __init__(self, account, channel_path=None, contact_id=None):
  55         self._account = account
  56         self._connection = account.connection
  57 
  58         if contact_id:
  59             request a channel
  60             
  61         if channel:
  62             self.tube = Channel(self.conn.dbus_proxy.bus_name, path)
  63             
  64             self.tube[CHANNEL_INTERFACE_TUBE].connect_to_signal(
  65                 "TubeChannelStateChanged", self.tube_channel_state_changed_cb)
  66 
  67                 self.tube[CHANNEL_INTERFACE].connect_to_signal("Closed", 
  68                                                         self.tube_closed_cb)
  69 
  70                 self._on_channel_created(props)
  71 
  72     def _on_channel_created(self, props):
  73         initiator_id = props[CHANNEL_INTERFACE + ".InitiatorID"]
  74         service = props[CHANNEL_TYPE_DBUS_TUBE + ".ServiceName"]
  75 
  76         state = self.tube[PROPERTIES_IFACE].Get(CHANNEL_INTERFACE_TUBE, 'State')
  77 
  78         print "new D-Bus tube offered by %s. Service: %s. State: %s" % (
  79             initiator_id, service, tube_state[state])
  80 
  81     def send_message(self, msg):
  82         pass
  83     
  84     
  85     def _on_message_received(self, dict):
  86         """on_message_received - internal functionm, do not call directly"""
  87         
  88         emit("message_recieved",self.contact, dict)     
  89 
  90     __gsignals__ = {'message-received' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
  91                               (gobject.TYPE_PYOBJECT,gobject.TYPE_PYOBJECT)),
  92                     }
  93 
  94 class Client(Boo):
  95     def __init__(self):
  96         self.tube_handler = MessageTubeHandler("tubey")
  97         self.tube_handler.connect("handle-channel", self.on_handle_channel)
  98 
  99     def on_handle_channel(self, tube):
 100         self.image_tube = tube
 101         tube.connect("message-received", self.on_message_received)
 102 
 103     def on_message_received(self, tube, msg):
 104         if "img_url" in msg:
 105             self._download_image(msg["img_url"])
 106         
 107     def on_contact_selected(self, contact, account):
 108         self.image_tube = tp.MessageTube(contact, account)
 109         msg = {}
 110         msg["note"] = self.label1.get_text()
 111         self.image_tube.send(msg)
 112         
 113     def on_tube_initiated(self, tube):
 114         if "kenvandine" in tube.contact.props.identifier:
 115                 return

http://wiki.laptop.org/go/Sugar.presence

Quickly/Telepathy/First_Hack_At_API (last edited 2010-05-13 12:49:48 by 217)