diff options
author | Gonzalo Odiard <godiard@gmail.com> | 2013-03-13 12:53:46 (GMT) |
---|---|---|
committer | Gonzalo Odiard <godiard@gmail.com> | 2013-03-13 12:53:46 (GMT) |
commit | a26a959f1bdb14986ddeb8d8a6f5ac6235cd6175 (patch) | |
tree | 46e129443f015a0fa20e65925edccce0ec0ea358 | |
parent | 28dd53c6bb2d0c7d69aede38291e5b8e7e6b8879 (diff) |
First version of the activity displaying a webview
With code copied from Read and Wikipedia, just to start.
Signed-off-by: Gonzalo Odiard <gonzalo@laptop.org>
-rw-r--r-- | activity.py | 75 |
1 files changed, 71 insertions, 4 deletions
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("<html><head><title>Title goes here.</title></head>") + self.wfile.write("<body><p>This is a test.</p>") + # If someone went to "http://something.somewhere.net/foo/bar/", + # then s.path equals "/foo/bar/". + + #self.wfile.write("<p>You accessed path: %s</p>" % self.path) + self.wfile.write("</body></html>") + + +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 |