5 # Copyright (C) 2009 Olivier Le Thanh Duong
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 from dbus
.service
import method
, signal
, Object
27 from connection_watcher
import ConnectionWatcher
29 from telepathy
.interfaces
import CHANNEL_TYPE_CONTACT_LIST
, CONNECTION_INTERFACE_REQUESTS
,\
30 CONNECTION_INTERFACE_SIMPLE_PRESENCE
, CHANNEL_INTERFACE_GROUP
, CHANNEL_TYPE_TUBES
31 from telepathy
.constants
import HANDLE_TYPE_LIST
, HANDLE_TYPE_CONTACT
, CONNECTION_PRESENCE_TYPE_AVAILABLE
,\
32 CONNECTION_PRESENCE_TYPE_AWAY
, CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY
, CONNECTION_PRESENCE_TYPE_BUSY
,\
34 from telepathy
.client
.channel
import Channel
35 from telepathy
.client
.conn
import Connection
37 TUBE_SERVICE
= "be.staz.DTubetest"
38 dbus
.mainloop
.glib
.DBusGMainLoop(set_as_default
=True)
40 def tp_search_contact_from_id(contact
, conn
):
41 """Return the handle that match the id in given connexion"""
43 if CONNECTION_INTERFACE_REQUESTS
not in conn
:
44 print "CONNECTION_INTERFACE_REQUESTS not implemented in %s" % (conn
.service_name
)
48 yours
, path
, props
= conn
[CONNECTION_INTERFACE_REQUESTS
].EnsureChannel({
49 'org.freedesktop.Telepathy.Channel.ChannelType': CHANNEL_TYPE_CONTACT_LIST
,
50 'org.freedesktop.Telepathy.Channel.TargetHandleType': HANDLE_TYPE_LIST
,
51 'org.freedesktop.Telepathy.Channel.TargetID': 'subscribe'})
53 chan
= Channel(conn
.service_name
, path
)
54 contacts
= chan
[CHANNEL_INTERFACE_GROUP
].GetMembers()
56 ids
= conn
.InspectHandles(HANDLE_TYPE_CONTACT
, contacts
)
58 for handle
, id in zip(contacts
, ids
):
62 def tp_contact_is_online(handle
, conn
):
63 presences
= conn
[CONNECTION_INTERFACE_SIMPLE_PRESENCE
].GetPresences([handle
])
65 type, status
, msg
= presences
[handle
]
66 return type in [CONNECTION_PRESENCE_TYPE_AVAILABLE
, CONNECTION_PRESENCE_TYPE_AWAY
,
67 CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY
, CONNECTION_PRESENCE_TYPE_BUSY
]
69 def offer_tube(conn
, handle
):
70 # FIXME: check if contact supports our tube
72 # FIXME: use new tube API
73 yours
, path
, props
= conn
[CONNECTION_INTERFACE_REQUESTS
].EnsureChannel({
74 'org.freedesktop.Telepathy.Channel.ChannelType': CHANNEL_TYPE_TUBES
,
75 'org.freedesktop.Telepathy.Channel.TargetHandleType': HANDLE_TYPE_CONTACT
,
76 'org.freedesktop.Telepathy.Channel.TargetHandle': handle
})
78 def tube_state_changed_cb(id, state
):
79 print "tube state changed", state
80 if state
== TUBE_STATE_OPEN
:
83 address
= chan
[CHANNEL_TYPE_TUBES
].GetDBusTubeAddress(id)
84 tube_conn
= dbus
.connection
.Connection(address
)
85 #p = RemotePlayer(tube_conn, "/Player", UI.game, "White")
87 def tube_closed_cb(id):
91 chan
= Channel(conn
.service_name
, path
)
92 chan
[CHANNEL_TYPE_TUBES
].connect_to_signal('TubeStateChanged', tube_state_changed_cb
)
93 chan
[CHANNEL_TYPE_TUBES
].connect_to_signal('TubeClosed', tube_closed_cb
)
94 chan
[CHANNEL_TYPE_TUBES
].OfferDBusTube(TUBE_SERVICE
, {})
96 def offer_tube_to_contact_cb(watcher
, conn
, contact
):
97 #TODO : Remove itself when contact is found.
98 handle
= tp_search_contact_from_id(contact
, conn
)
100 print "Contact %s found in %s" % (contact
, conn
.service_name
)
101 if tp_contact_is_online(handle
, conn
):
102 print "contact online"
103 offer_tube(conn
, handle
)
106 print "Contact %s not found in %s" % (contact
, conn
.service_name
)
108 if __name__
== "__main__":
110 if sys
.argv
[1] == '--handler':
113 # create D-Bus handler service
114 class Handler(Object
):
115 @dbus.service
.method("org.gnome.Empathy.TubeHandler",
116 in_signature
='soouuu', out_signature
='')
117 def HandleTube(self
, bus_name
, connection
, channel
, handle_type
, handle
, tube_id
):
118 print "handling tube!"
119 # TODO: ask to the user if he wants to accept or decline the
122 conn
= Connection(bus_name
, connection
)
123 chan
= Channel(bus_name
, channel
)
126 address
= chan
[CHANNEL_TYPE_TUBES
].AcceptDBusTube(tube_id
)
127 tube_conn
= dbus
.connection
.Connection(address
)
128 #p = RemotePlayer(tube_conn, "/Player", UI.game, "Black")
130 bus
= dbus
.SessionBus()
131 name
= dbus
.service
.BusName("org.gnome.Empathy.DTubeHandler.%s" % TUBE_SERVICE
, bus
)
132 print "org.gnome.Empathy.DTubeHandler.%s" % TUBE_SERVICE
133 print "create handle service"
134 handler
= Handler(bus
, "/org/gnome/Empathy/DTubeHandler/%s" % TUBE_SERVICE
)
137 contact
= sys
.argv
[1]
139 if contact
is not None:
140 watcher
= ConnectionWatcher()
141 watcher
.connect('connection-added', offer_tube_to_contact_cb
, contact
)
143 loop
= gobject
.MainLoop()
144 #gobject.timeout_add(10, UI.runIteration)
147 # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: