Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Williams <dcbw@redhat.com>2007-05-03 05:15:19 (GMT)
committer Dan Williams <dcbw@redhat.com>2007-05-03 05:15:19 (GMT)
commit4b26ecde8c99d710021083c02ddc9a92333ac709 (patch)
tree2572c71a87f6cafe995ca00563ef46b60fc017ac
parentbfd7507cd27e1c5c3ae2cdb101d12a9593b1140d (diff)
When joining, pull activity document from a peer
-rw-r--r--XbookActivity.py74
1 files changed, 73 insertions, 1 deletions
diff --git a/XbookActivity.py b/XbookActivity.py
index 4653de6..4203a79 100644
--- a/XbookActivity.py
+++ b/XbookActivity.py
@@ -17,12 +17,13 @@
import logging
from gettext import gettext as _
-import gtk
+import gtk, gobject
import evince
import hippo
import os
from sugar.activity import activity
+from sugar.p2p import network
from xbooktoolbar import XbookToolbar
@@ -58,6 +59,77 @@ class XbookActivity(activity.Activity):
if handle.uri:
self._load_document(handle.uri)
+ elif self._shared_activity:
+ self._tried_buddies = []
+ if self._shared_activity.props.joined:
+ # Already joined for some reason, just get the document
+ self._fetch_document()
+ else:
+ # Wait for a successful join before trying to get the document
+ self.connect("joined", self._joined_cb)
+
+ def _download_result_cb(self, getter, tempfile, suggested_name, buddy):
+ del self._tried_buddies
+ logging.debug("Got document %s (%s) from %s (%s)" % (tempfile, suggested_name, buddy.props.nick, buddy.props.ip4_address))
+ import shutil
+ dest = os.path.join(os.path.expanduser("~"), suggested_name)
+ shutil.copyfile(tempfile, dest)
+ os.remove(tempfile)
+ self._load_document("file://%s" % dest)
+
+ def _download_error_cb(self, getter, err, buddy):
+ logging.debug("Error getting document from %s (%s): %s" % (buddy.props.nick, buddy.props.ip4_address, err))
+ self._tried_buddies.append(buddy)
+ gobject.idle_add(self._get_document)
+
+ def _download_document(self, buddy):
+ getter = network.GlibURLDownloader("http://%s:8867/document" % buddy.props.ip4_address)
+ getter.connect("finished", self._download_result_cb, buddy)
+ getter.connect("error", self._download_error_cb, buddy)
+ logging.debug("Starting download...")
+ getter.start()
+ return False
+
+ def _have_file_reply_handler(self, have_it, buddy):
+ if not have_it:
+ # Try again
+ logging.debug("Error: %s (%s) didn't have it" % (buddy.props.nick, buddy.props.ip4_address))
+ self._tried_buddies.append(buddy)
+ gobject.idle_add(self._get_document)
+ return
+ logging.debug("Trying to download document from %s (%s)" % (buddy.props.nick, buddy.props.ip4_address))
+ gobject.idle_add(self._download_document, buddy)
+
+ def _have_file_error_handler(self, err, buddy):
+ logging.debug("Failed to get document from %s (%s)" % (buddy.props.nick, buddy.props.ip4_address))
+ # Try again
+ self._tried_buddies.append(buddy)
+ gobject.idle_add(self._get_document)
+
+ def _get_document(self):
+ next_buddy = None
+ # Find the next untried buddy with an IP4 address we can try to
+ # download the document from
+ for buddy in self._shared_activity.get_joined_buddies():
+ if not buddy in self._tried_buddies:
+ if buddy.props.ip4_address:
+ next_buddy = buddy
+ break
+
+ if not next_buddy:
+ logging.debug("Couldn't find a buddy to get the document from.")
+ return False
+
+ logging.debug("Will try to get document from %s (%s)" % (buddy.props.nick, buddy.props.ip4_address))
+ proxy = network.GlibServerProxy("http://%s:8868" % buddy.props.ip4_address)
+ proxy.have_file(reply_handler=self._have_file_reply_handler,
+ error_handler=self._have_file_error_handler,
+ user_data=buddy)
+
+ return False
+
+ def _joined_cb(self, activity):
+ gobject.idle_add(self._get_document)
def _load_document(self, filename):
if self._document: