summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJonny Lamb <jonny.lamb@collabora.co.uk>2011-11-04 17:40:52 +0000
committerJonny Lamb <jonny.lamb@collabora.co.uk>2011-11-11 10:50:52 +0000
commitea6a2cbba50851f041603933be199461137a35a7 (patch)
tree8a7e0c5b7986377ee4ef30ba5ab6c54e0fa8c99f /examples
parent3044465ff3b5aee82d1112f0de6a56e748c75f66 (diff)
downloadtelepathy-glib-ea6a2cbba50851f041603933be199461137a35a7.tar.gz
python examples: update file transfer examples
Signed-off-by: Jonny Lamb <jonny.lamb@collabora.co.uk>
Diffstat (limited to 'examples')
-rw-r--r--examples/client/python/Makefile.am4
-rwxr-xr-xexamples/client/python/file-transfer.py32
-rw-r--r--examples/client/python/ft-handler.py67
3 files changed, 90 insertions, 13 deletions
diff --git a/examples/client/python/Makefile.am b/examples/client/python/Makefile.am
index 811a43879..5eacd52e7 100644
--- a/examples/client/python/Makefile.am
+++ b/examples/client/python/Makefile.am
@@ -1,4 +1,6 @@
EXTRA_DIST = \
contact-list.py \
ensure-channel.py \
- text-handler.py
+ text-handler.py \
+ file-transfer.py \
+ ft-handler.py
diff --git a/examples/client/python/file-transfer.py b/examples/client/python/file-transfer.py
index 7752d5115..e47bc4575 100755
--- a/examples/client/python/file-transfer.py
+++ b/examples/client/python/file-transfer.py
@@ -3,11 +3,10 @@ import sys
import os
import mimetypes
-import gobject
-gobject.threads_init()
-
-import magic
+from gi.repository import GObject
+GObject.threads_init()
+from gi.repository import Gio
from gi.repository import TelepathyGLib
def usage():
@@ -18,12 +17,23 @@ def usage():
sys.exit(1)
-def create_channel_cb(request, result, main_loop):
+def provide_file_cb(channel, result, data):
+ if not channel.provide_file_finish(result):
+ print 'failed to provide'
+
+def state_changed_cb(channel, pspec, data):
+ main_loop, file_path = data
+ state, _ = channel.get_state()
+ print 'state is now', state
+
+ if state == TelepathyGLib.FileTransferState.ACCEPTED:
+ file = Gio.File.new_for_path(file_path)
+ channel.provide_file_async(file, provide_file_cb, None)
+
+def create_channel_cb(request, result, data):
(chan, context) = request.create_and_handle_channel_finish(result)
- print chan
- print chan.get_description
- #main_loop.quit()
+ chan.connect('notify::state', state_changed_cb, data)
if __name__ == '__main__':
#TelepathyGLib.debug_set_flags("all")
@@ -69,10 +79,8 @@ if __name__ == '__main__':
request = TelepathyGLib.AccountChannelRequest.new(account, request_dict, 0)
- main_loop = gobject.MainLoop()
-
- print TelepathyGLib
+ main_loop = GObject.MainLoop()
- request.create_and_handle_channel_async(None, create_channel_cb, main_loop)
+ request.create_and_handle_channel_async(None, create_channel_cb, (main_loop, file_path))
main_loop.run()
diff --git a/examples/client/python/ft-handler.py b/examples/client/python/ft-handler.py
new file mode 100644
index 000000000..2e042f782
--- /dev/null
+++ b/examples/client/python/ft-handler.py
@@ -0,0 +1,67 @@
+#!/usr/bin/env python
+import sys
+
+from gi.repository import GObject
+GObject.threads_init()
+
+from gi.repository import Gio
+from gi.repository import TelepathyGLib
+
+import magic
+
+def usage():
+ print "%s FILE" % sys.argv[0]
+ print "FILE is a path to the location you want the file saved to"
+
+ sys.exit(1)
+
+def state_changed_cb(channel, pspec, data):
+ state, _ = channel.get_state()
+ print 'State is now:', state
+
+def accept_cb(channel, result, data):
+ if not channel.accept_file_finish(result):
+ print 'Failed to accept file'
+
+def handle_channels_cb(handler, account, connection, channels, requests,
+ user_action_time, context, filename):
+
+ for chan in channels:
+ if not isinstance(chan, TelepathyGLib.FileTransferChannel):
+ continue
+
+ chan.connect('notify::state', state_changed_cb, None)
+
+ print 'Handling FileTransfer channel:', chan.get_identifier()
+
+ file = Gio.File.new_for_path(filename)
+ chan.accept_file_async(file, 0, accept_cb, None)
+
+ context.accept()
+
+if __name__ == '__main__':
+ if len(sys.argv) != 2:
+ usage()
+
+ _, filename = sys.argv
+
+ #TelepathyGLib.debug_set_flags("all")
+
+ dbus = TelepathyGLib.DBusDaemon.dup()
+
+ handler = TelepathyGLib.SimpleHandler.new(dbus, False, False,
+ 'ExampleFTHandler', False, handle_channels_cb, filename)
+
+ handler.add_handler_filter({
+ TelepathyGLib.PROP_CHANNEL_CHANNEL_TYPE:
+ TelepathyGLib.IFACE_CHANNEL_TYPE_FILE_TRANSFER,
+ TelepathyGLib.PROP_CHANNEL_TARGET_HANDLE_TYPE:
+ int(TelepathyGLib.HandleType.CONTACT),
+ TelepathyGLib.PROP_CHANNEL_REQUESTED: False
+ })
+
+ handler.register()
+
+ main_loop = GObject.MainLoop()
+
+ main_loop.run()