diff --git a/activity/ViewSlides.svg b/activity/ViewSlides.svg index 2cbeb5d..a8490f2 100644 --- a/activity/ViewSlides.svg +++ b/activity/ViewSlides.svg @@ -1,7 +1,7 @@ -]> +]> 0: - _logger.debug("Downloaded %u of %u bytes from tube %u...", - bytes_downloaded, self._download_content_length, - tube_id) - else: - _logger.debug("Downloaded %u bytes from tube %u...", - bytes_downloaded, tube_id) - total = self._download_content_length - self.set_downloaded_bytes(bytes_downloaded, total) - Gdk.threads_enter() - while Gtk.events_pending(): - Gtk.main_iteration() - Gdk.threads_leave() - - def _download_error_cb(self, getter, err, tube_id): - self.progressbar.hide() - _logger.debug("Error getting document from tube %u: %s", - tube_id, err) - self._alert('Failure', 'Error getting document from tube') + def _buddy_joined_cb(self, collab, buddy): self._want_document = True - self._download_content_length = 0 - self._download_content_type = None - GLib.idle_add(self._get_document) - - def _download_document(self, tube_id, path): - # FIXME: should ideally have the CM listen on a Unix socket - # instead of IPv4 (might be more compatible with Rainbow) - chan = self._shared_activity.telepathy_tubes_chan - iface = chan[telepathy.CHANNEL_TYPE_TUBES] - addr = iface.AcceptStreamTube( - tube_id, - telepathy.SOCKET_ADDRESS_TYPE_IPV4, - telepathy.SOCKET_ACCESS_CONTROL_LOCALHOST, - 0, - utf8_strings=True) - _logger.debug('Accepted stream tube: listening address is %r', addr) - # SOCKET_ADDRESS_TYPE_IPV4 is defined to have addresses of type '(sq)' - assert isinstance(addr, dbus.Struct) - assert len(addr) == 2 - assert isinstance(addr[0], str) - assert isinstance(addr[1], (int, long)) - assert addr[1] > 0 and addr[1] < 65536 - port = int(addr[1]) - - getter = ReadURLDownloader("http://%s:%d/document" - % (addr[0], port)) - getter.connect("finished", self._download_result_cb, tube_id) - getter.connect("progress", self._download_progress_cb, tube_id) - getter.connect("error", self._download_error_cb, tube_id) - _logger.debug("Starting download to %s...", path) - getter.start(path) - self._download_content_length = getter.get_content_length() - self._download_content_type = getter.get_content_type() - return False + if buddy.nick in self.buddies.keys(): + return + self.buddies[buddy.nick] = buddy + + GLib.idle_add(self._share_document, buddy) + + def _buddy_left_cb(self, collab, buddy): + if buddy.nick not in self.buddies.keys(): + return + del self.buddies[buddy.nick] + + def _message_cb(self, collab, buddy, message): + action = message.get('action') + + if action == 'add-image': + self.add_image() + if action == 'remove-image': + self.remove_image() + if action == 'reload': + self.reload_journal_table() - def _get_document(self): + def _incoming_file_cb(self, collab, incoming_file, description): + _logger.debug("Accepting incoming file") if not self._want_document: return False - # Assign a file path to download if one doesn't exist yet - if not self._jobject.file_path: - path = os.path.join(self.get_activity_root(), 'instance', - 'tmp%i' % time.time()) + # Assign a file path to write incoming file to + fpath = os.path.join(self.get_activity_root(), 'instance', + 'activity-files') + + # Make sure the receiver has enough space on their + # computer to receive the sent file + def diskfree(path): + return os.statvfs(path).f_bsize * os.statvfs(path).f_bavail + + a_space = diskfree(os.path.dirname(fpath)) // (1024**2) + + if a_space > incoming_file.file_size: + incoming_file.accept_to_file(fpath) + incoming_file.ready.connect(ready_cb) + transfer_complete = False + + while not transfer_complete: + self.progressbar.show() + self.set_downloaded_bytes( + incoming_file.props.transferred_bytes, + incoming_file.props.file_size) else: - path = self._jobject.file_path + self._alert("No space on disk", + "Delete some files to create space") + self._want_document = True + incoming_file.cancel() - # Pick an arbitrary tube we can try to download the document from - try: - tube_id = self.unused_download_tubes.pop() - except (ValueError, KeyError) as e: - _logger.debug('No tubes to get the document from right now: %s', - e) - return False + def ready_cb(fpath): + transfer_complete = True + self._download_complete_cb(fpath) # Avoid trying to download the document multiple times at once self._want_document = False - self.progressbar.show() - GLib.idle_add(self._download_document, tube_id, path) - return False def _joined_cb(self, also_self): """Callback for when a shared activity is joined. Get the shared document from another participant. """ - self.watch_for_tubes() - GLib.idle_add(self._get_document) + _logger.debug('Activity has been joined') - def _share_document(self): + def _share_document(self, buddy): """Share the document.""" - # FIXME: should ideally have the fileserver listen on a Unix socket - # instead of IPv4 (might be more compatible with Rainbow) - - _logger.debug('Starting HTTP server on port %d', self.port) - self._fileserver = ReadHTTPServer(("", self.port), - self.tempfile) - - # Make a tube for it - chan = self._shared_activity.telepathy_tubes_chan - iface = chan[telepathy.CHANNEL_TYPE_TUBES] - self._fileserver_tube_id = iface.OfferStreamTube( - READ_STREAM_SERVICE, - {}, - telepathy.SOCKET_ADDRESS_TYPE_IPV4, - ('127.0.0.1', - dbus.UInt16( - self.port)), - telepathy.SOCKET_ACCESS_CONTROL_LOCALHOST, - 0) - - def watch_for_tubes(self): - """Watch for new tubes.""" - tubes_chan = self._shared_activity.telepathy_tubes_chan - - tubes_chan[telepathy.CHANNEL_TYPE_TUBES].connect_to_signal( - 'NewTube', self._new_tube_cb) - tubes_chan[telepathy.CHANNEL_TYPE_TUBES].ListTubes( - reply_handler=self._list_tubes_reply_cb, - error_handler=self._list_tubes_error_cb) - - def _new_tube_cb(self, tube_id, initiator, tube_type, service, params, - state): - """Callback when a new tube becomes available.""" - _logger.debug('New tube: ID=%d initator=%d type=%d service=%s ' - 'params=%r state=%d', tube_id, initiator, tube_type, - service, params, state) - if service == READ_STREAM_SERVICE: - _logger.debug('I could download from that tube') - self.unused_download_tubes.add(tube_id) - # if no download is in progress, let's fetch the document - if self._want_document: - GLib.idle_add(self._get_document) - - def _list_tubes_reply_cb(self, tubes): - """Callback when new tubes are available.""" - for tube_info in tubes: - self._new_tube_cb(*tube_info) - - def _list_tubes_error_cb(self, e): - """Handle ListTubes error by logging.""" - _logger.error('ListTubes() failed: %s', e) + if self._want_document: + path = os.path.abspath(self.activity_zip.filename) + self.collab.send_file_file( + buddy, + path, + "Share zip file to joined buddies") def _shared_cb(self, activityid): - """Callback when activity shared. - - Set up to share the document. - - """ + """Callback when activity shared.""" # We initiated this activity and have now shared it, so by # definition we have the file. _logger.debug('Activity became shared') - self.watch_for_tubes() - self._share_document() def _alert(self, title, text=None): alert = NotifyAlert(timeout=15)