diff options
Diffstat (limited to 'src/jarabe/model/filetransfer.py')
-rw-r--r-- | src/jarabe/model/filetransfer.py | 68 |
1 files changed, 30 insertions, 38 deletions
diff --git a/src/jarabe/model/filetransfer.py b/src/jarabe/model/filetransfer.py index 46b246d..710c3a4 100644 --- a/src/jarabe/model/filetransfer.py +++ b/src/jarabe/model/filetransfer.py @@ -31,6 +31,8 @@ from sugar.presence import presenceservice from sugar import dispatch from jarabe.util.telepathy import connection_watcher +from jarabe.model import neighborhood + FT_STATE_NONE = 0 FT_STATE_PENDING = 1 @@ -51,14 +53,18 @@ FT_REASON_REMOTE_ERROR = 6 CHANNEL_TYPE_FILE_TRANSFER = \ 'org.freedesktop.Telepathy.Channel.Type.FileTransfer' +new_file_transfer = dispatch.Signal() + + # TODO Move to use splice_async() in Sugar 0.88 class StreamSplicer(gobject.GObject): - _CHUNK_SIZE = 10240 # 10K + _CHUNK_SIZE = 10240 # 10K __gsignals__ = { 'finished': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])), } + def __init__(self, input_stream, output_stream): gobject.GObject.__init__(self) @@ -104,6 +110,7 @@ class StreamSplicer(gobject.GObject): gobject.PRIORITY_LOW, user_data=data) + class BaseFileTransfer(gobject.GObject): def __init__(self, connection): @@ -140,11 +147,7 @@ class BaseFileTransfer(gobject.GObject): self.mime_type = props['ContentType'] handle = channel_properties.Get(CHANNEL, 'TargetHandle') - presence_service = presenceservice.get_instance() - self.buddy = presence_service.get_buddy_by_telepathy_handle( - self._connection.service_name, - self._connection.object_path, - handle) + self.buddy = neighborhood.get_model().get_buddy_by_handle(handle) def __transferred_bytes_changed_cb(self, transferred_bytes): logging.debug('__transferred_bytes_changed_cb %r', transferred_bytes) @@ -179,6 +182,7 @@ class BaseFileTransfer(gobject.GObject): def cancel(self): self.channel[CHANNEL].Close() + class IncomingFileTransfer(BaseFileTransfer): def __init__(self, connection, object_path, props): BaseFileTransfer.__init__(self, connection) @@ -223,6 +227,7 @@ class IncomingFileTransfer(BaseFileTransfer): self._splicer = StreamSplicer(input_stream, output_stream) self._splicer.start() + class OutgoingFileTransfer(BaseFileTransfer): def __init__(self, buddy, file_name, title, description, mime_type): @@ -240,20 +245,18 @@ class OutgoingFileTransfer(BaseFileTransfer): self._splicer = None self._output_stream = None - self.buddy = buddy.get_buddy() + self.buddy = buddy self.title = title self.file_size = os.stat(file_name).st_size self.description = description self.mime_type = mime_type def __connection_ready_cb(self, connection): - handle = self._get_buddy_handle() - requests = connection[CONNECTION_INTERFACE_REQUESTS] object_path, properties_ = requests.CreateChannel({ CHANNEL + '.ChannelType': CHANNEL_TYPE_FILE_TRANSFER, CHANNEL + '.TargetHandleType': CONNECTION_HANDLE_TYPE_CONTACT, - CHANNEL + '.TargetHandle': handle, + CHANNEL + '.TargetHandle': self.buddy.handle, CHANNEL_TYPE_FILE_TRANSFER + '.ContentType': self.mime_type, CHANNEL_TYPE_FILE_TRANSFER + '.Filename': self.title, CHANNEL_TYPE_FILE_TRANSFER + '.Size': self.file_size, @@ -267,21 +270,6 @@ class OutgoingFileTransfer(BaseFileTransfer): SOCKET_ADDRESS_TYPE_UNIX, SOCKET_ACCESS_CONTROL_LOCALHOST, '', byte_arrays=True) - def _get_buddy_handle(self): - object_path = self.buddy.object_path() - - bus = dbus.SessionBus() - remote_object = bus.get_object('org.laptop.Sugar.Presence', object_path) - ps_buddy = dbus.Interface(remote_object, - 'org.laptop.Sugar.Presence.Buddy') - - handles = ps_buddy.GetTelepathyHandles() - logging.debug('_get_buddy_handle %r', handles) - - bus_name, object_path, handle = handles[0] - - return handle - def __notify_state_cb(self, file_transfer, pspec): logging.debug('__notify_state_cb %r', self.props.state) if self.props.state == FT_STATE_OPEN: @@ -303,6 +291,7 @@ class OutgoingFileTransfer(BaseFileTransfer): def cancel(self): self.channel[CHANNEL].Close() + def _new_channels_cb(connection, channels): for object_path, props in channels: if props[CHANNEL + '.ChannelType'] == CHANNEL_TYPE_FILE_TRANSFER and \ @@ -314,35 +303,39 @@ def _new_channels_cb(connection, channels): object_path, props) new_file_transfer.send(None, file_transfer=incoming_file_transfer) + def _monitor_connection(connection): logging.debug('connection added %r', connection) connection[CONNECTION_INTERFACE_REQUESTS].connect_to_signal('NewChannels', lambda channels: _new_channels_cb(connection, channels)) -def _connection_addded_cb(conn_watcher, connection): + +def _connection_added_cb(conn_watcher, connection): _monitor_connection(connection) + def _connection_removed_cb(conn_watcher, connection): logging.debug('connection removed %r', connection) -_conn_watcher = None def init(): - global _conn_watcher - _conn_watcher = connection_watcher.ConnectionWatcher() - _conn_watcher.connect('connection-added', _connection_addded_cb) - _conn_watcher.connect('connection-removed', _connection_removed_cb) + conn_watcher = connection_watcher.get_instance() + conn_watcher.connect('connection-added', _connection_added_cb) + conn_watcher.connect('connection-removed', _connection_removed_cb) - for connection in _conn_watcher.get_connections(): + for connection in conn_watcher.get_connections(): _monitor_connection(connection) + def start_transfer(buddy, file_name, title, description, mime_type): outgoing_file_transfer = OutgoingFileTransfer(buddy, file_name, title, description, mime_type) new_file_transfer.send(None, file_transfer=outgoing_file_transfer) + def file_transfer_available(): - for connection in _conn_watcher.get_connections(): + conn_watcher = connection_watcher.get_instance() + for connection in conn_watcher.get_connections(): properties_iface = connection[dbus.PROPERTIES_IFACE] properties = properties_iface.GetAll(CONNECTION_INTERFACE_REQUESTS) @@ -359,18 +352,17 @@ def file_transfer_available(): return False -new_file_transfer = dispatch.Signal() if __name__ == '__main__': import tempfile - input_stream = gio.File('/home/tomeu/isos/Soas2-200904031934.iso').read() - output_stream = gio.File(tempfile.mkstemp()[1]).append_to() + test_file_name = '/home/tomeu/isos/Soas2-200904031934.iso' + test_input_stream = gio.File(test_file_name).read() + test_output_stream = gio.File(tempfile.mkstemp()[1]).append_to() # TODO: Use splice_async when it gets implemented - splicer = StreamSplicer(input_stream, output_stream) + splicer = StreamSplicer(test_input_stream, test_output_stream) splicer.start() loop = gobject.MainLoop() loop.run() - |