From a26a959f1bdb14986ddeb8d8a6f5ac6235cd6175 Mon Sep 17 00:00:00 2001 From: Gonzalo Odiard Date: Wed, 13 Mar 2013 12:53:46 +0000 Subject: First version of the activity displaying a webview With code copied from Read and Wikipedia, just to start. Signed-off-by: Gonzalo Odiard --- (limited to 'activity.py') diff --git a/activity.py b/activity.py index ac180ff..0862ba1 100644 --- a/activity.py +++ b/activity.py @@ -14,20 +14,63 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +import logging +from threading import Thread + +from gi.repository import GObject from gi.repository import Gtk +from gi.repository import WebKit from sugar3.activity import activity from sugar3.activity.widgets import ActivityToolbarButton from sugar3.activity.widgets import StopButton from sugar3.graphics.toolbarbox import ToolbarBox +from sugar3 import network + + +class JournalHTTPRequestHandler(network.ChunkedGlibHTTPRequestHandler): + """HTTP Request Handler to send data to the webview. + + RequestHandler class that integrates with Glib mainloop. It writes + the specified file to the client in chunks, returning control to the + mainloop between chunks. + + """ + def do_HEAD(self): + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + + def do_GET(self): + """Respond to a GET request.""" + logging.error('inside do_get dir(self) %s', dir(self)) + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + self.wfile.write("Title goes here.") + self.wfile.write("

This is a test.

") + # If someone went to "http://something.somewhere.net/foo/bar/", + # then s.path equals "/foo/bar/". + + #self.wfile.write("

You accessed path: %s

" % self.path) + self.wfile.write("") + + +class JournalHTTPServer(network.GlibTCPServer): + """HTTP Server for transferring document while collaborating.""" + + def __init__(self, server_address): + """Set up the GlibTCPServer with the JournalHTTPRequestHandler. + """ + network.GlibTCPServer.__init__(self, server_address, + JournalHTTPRequestHandler) class JournalShare(activity.Activity): def __init__(self, handle): - activity.Activity.__init__(self, handle) - self.max_participants = 1 + activity.Activity.__init__(self, handle) toolbar_box = ToolbarBox() @@ -37,9 +80,9 @@ class JournalShare(activity.Activity): separator = Gtk.SeparatorToolItem() separator.props.draw = False - separator.props.expand = True - toolbar_box.toolbar.insert(separator, -1) + separator.set_expand(True) separator.show() + toolbar_box.toolbar.insert(separator, -1) stopbutton = StopButton(self) toolbar_box.toolbar.insert(stopbutton, -1) @@ -48,4 +91,28 @@ class JournalShare(activity.Activity): self.set_toolbar_box(toolbar_box) toolbar_box.show() + activity_path = activity.get_bundle_path() + self.view = WebKit.WebView() + #self.view.load_uri('file://%s/web/index.html' % activity_path) + self.view.load_uri('http://localhost:2500') + self.view.show() + self.set_canvas(self.view) + + # TODO: set the port in a more inteligent way + self.port = 2500 + self._server = JournalHTTPServer(("", self.port)) + server = Thread(target=self._server.serve_forever) + server.setDaemon(True) + logging.debug("Before start server") + server.start() + logging.debug("After start server") + + def read_file(self, file_path): + pass + + def write_file(self, file_path): + pass + def can_close(self): + self._server.shutdown() + return True -- cgit v0.9.1