2006-06-18 04:06:40 +02:00
|
|
|
import logging
|
|
|
|
import xml.sax.saxutils
|
|
|
|
|
2006-06-01 00:01:24 +02:00
|
|
|
import gtk
|
|
|
|
import geckoembed
|
|
|
|
|
2006-06-21 22:05:52 +02:00
|
|
|
from sugar.activity.Activity import Activity
|
2006-06-15 23:05:44 +02:00
|
|
|
from sugar.presence.PresenceService import PresenceService
|
2006-06-16 19:21:28 +02:00
|
|
|
from sugar.p2p.model.LocalModel import LocalModel
|
2006-06-16 20:20:09 +02:00
|
|
|
from sugar.p2p.model.RemoteModel import RemoteModel
|
2006-06-01 00:01:24 +02:00
|
|
|
|
2006-06-21 20:23:18 +02:00
|
|
|
from NotificationBar import NotificationBar
|
|
|
|
from NavigationToolbar import NavigationToolbar
|
|
|
|
|
2006-06-15 23:05:44 +02:00
|
|
|
_SERVICE_URI_TAG = "URI"
|
|
|
|
_SERVICE_TITLE_TAG = "Title"
|
2006-06-15 17:29:00 +02:00
|
|
|
|
2006-06-21 22:05:52 +02:00
|
|
|
class BrowserActivity(Activity):
|
2006-06-01 00:01:24 +02:00
|
|
|
SOLO = 1
|
|
|
|
FOLLOWING = 2
|
|
|
|
LEADING = 3
|
|
|
|
|
2006-07-14 16:40:45 +02:00
|
|
|
def __init__(self, service, args):
|
|
|
|
Activity.__init__(self, service)
|
2006-07-06 15:59:48 +02:00
|
|
|
|
2006-07-08 15:47:51 +02:00
|
|
|
if len(args) > 0:
|
|
|
|
self.uri = args[0]
|
|
|
|
else:
|
|
|
|
self.uri = 'http://www.google.com'
|
2006-06-18 04:06:40 +02:00
|
|
|
|
2006-07-06 15:59:48 +02:00
|
|
|
self._mode = BrowserActivity.SOLO
|
2006-06-15 23:05:44 +02:00
|
|
|
self._share_service = None
|
2006-06-17 06:33:08 +02:00
|
|
|
self._model_service = None
|
|
|
|
self._notif_service = None
|
2006-06-15 23:05:44 +02:00
|
|
|
self._model = None
|
2006-07-09 17:37:54 +02:00
|
|
|
|
|
|
|
self.set_title("Web Page")
|
|
|
|
|
|
|
|
vbox = gtk.VBox()
|
|
|
|
|
|
|
|
self._notif_bar = NotificationBar()
|
|
|
|
vbox.pack_start(self._notif_bar, False)
|
|
|
|
self._notif_bar.connect('action', self.__notif_bar_action_cb)
|
|
|
|
|
|
|
|
self.embed = geckoembed.Embed()
|
|
|
|
self.embed.connect("title", self.__title_cb)
|
|
|
|
vbox.pack_start(self.embed)
|
|
|
|
|
|
|
|
self.embed.show()
|
|
|
|
self.embed.load_address(self.uri)
|
|
|
|
|
|
|
|
nav_toolbar = NavigationToolbar(self)
|
|
|
|
vbox.pack_start(nav_toolbar, False)
|
|
|
|
nav_toolbar.show()
|
|
|
|
|
|
|
|
self.add(vbox)
|
|
|
|
vbox.show()
|
|
|
|
|
2006-07-20 10:12:42 +02:00
|
|
|
self._pservice = PresenceService()
|
2006-07-23 16:21:00 +02:00
|
|
|
self._pservice.connect('service-appeared', self._service_appeared_cb)
|
2006-07-09 17:37:54 +02:00
|
|
|
|
|
|
|
# Join the shared activity if we were started from one
|
|
|
|
if self._initial_service:
|
|
|
|
logging.debug("BrowserActivity joining shared activity %s" %
|
|
|
|
self._initial_service.get_activity_id())
|
|
|
|
self._pservice.join_shared_activity(self._initial_service)
|
2006-06-16 07:43:42 +02:00
|
|
|
|
2006-07-26 02:04:15 +02:00
|
|
|
def _service_appeared_cb(self, pservice, service):
|
2006-06-19 15:48:45 +02:00
|
|
|
# Make sure the service is for our activity
|
2006-06-22 20:05:38 +02:00
|
|
|
if service.get_activity_id() != self._activity_id:
|
2006-06-19 15:48:45 +02:00
|
|
|
return
|
|
|
|
|
2006-07-15 12:31:06 +02:00
|
|
|
if service.get_type() == self._default_type:
|
2006-06-17 06:33:08 +02:00
|
|
|
self._notif_service = service
|
|
|
|
elif service.get_type() == LocalModel.SERVICE_TYPE:
|
2006-06-18 04:06:40 +02:00
|
|
|
if self._mode != BrowserActivity.LEADING:
|
|
|
|
self._model_service = service
|
2006-06-17 06:33:08 +02:00
|
|
|
|
2006-06-23 19:13:28 +02:00
|
|
|
if not self._model and self._notif_service and self._model_service:
|
2006-06-17 06:33:08 +02:00
|
|
|
self._model = RemoteModel(self._model_service, self._notif_service)
|
|
|
|
self._model.add_listener(self.__shared_location_changed_cb)
|
|
|
|
|
2006-06-01 00:01:24 +02:00
|
|
|
def _update_shared_location(self):
|
|
|
|
address = self.embed.get_address()
|
|
|
|
self._model.set_value('address', address)
|
|
|
|
title = self.embed.get_title()
|
|
|
|
self._model.set_value('title', title)
|
|
|
|
|
|
|
|
def __notif_bar_action_cb(self, bar, action_id):
|
|
|
|
if action_id == 'set_shared_location':
|
|
|
|
self._update_shared_location()
|
|
|
|
elif action_id == 'goto_shared_location':
|
|
|
|
address = self._model.get_value("address")
|
|
|
|
self.embed.load_address(address)
|
|
|
|
self._notif_bar.hide()
|
|
|
|
|
2006-06-16 20:20:09 +02:00
|
|
|
def set_mode(self, mode):
|
|
|
|
self._mode = mode
|
|
|
|
if mode == BrowserActivity.LEADING:
|
|
|
|
self._notif_bar.set_text('Share this page with the group.')
|
|
|
|
self._notif_bar.set_action('set_shared_location', 'Share')
|
|
|
|
self._notif_bar.set_icon('stock_shared-by-me')
|
|
|
|
self._notif_bar.show()
|
|
|
|
|
2006-06-01 00:01:24 +02:00
|
|
|
def get_embed(self):
|
|
|
|
return self.embed
|
|
|
|
|
2006-07-26 00:17:05 +02:00
|
|
|
def share(self):
|
2006-06-17 04:23:19 +02:00
|
|
|
escaped_title = xml.sax.saxutils.escape(self.embed.get_title())
|
|
|
|
escaped_url = xml.sax.saxutils.escape(self.embed.get_address())
|
2006-06-15 23:05:44 +02:00
|
|
|
|
2006-07-26 00:17:05 +02:00
|
|
|
# Share this activity with others
|
2006-06-15 23:05:44 +02:00
|
|
|
properties = {_SERVICE_URI_TAG: escaped_url, _SERVICE_TITLE_TAG: escaped_title}
|
|
|
|
self._share_service = self._pservice.share_activity(self,
|
2006-07-14 16:40:45 +02:00
|
|
|
stype=self._default_type, properties=properties)
|
2006-06-15 23:05:44 +02:00
|
|
|
|
2006-06-16 00:27:50 +02:00
|
|
|
# Create our activity-specific browser sharing service
|
2006-06-16 20:20:09 +02:00
|
|
|
self._model = LocalModel(self, self._pservice, self._share_service)
|
2006-07-24 17:57:21 +02:00
|
|
|
self._model.set_value('owner', self._pservice.get_owner().get_name())
|
2006-06-16 19:06:11 +02:00
|
|
|
self._update_shared_location()
|
2006-06-16 20:20:09 +02:00
|
|
|
|
2006-06-16 19:06:11 +02:00
|
|
|
self.set_mode(BrowserActivity.LEADING)
|
|
|
|
|
2006-06-01 00:01:24 +02:00
|
|
|
def __title_cb(self, embed):
|
2006-07-10 13:42:34 +02:00
|
|
|
self.set_title(embed.get_title())
|
2006-06-01 00:01:24 +02:00
|
|
|
|
|
|
|
def __shared_location_changed_cb(self, model, key):
|
2006-06-02 20:52:20 +02:00
|
|
|
self.set_has_changes(True)
|
2006-06-01 00:01:24 +02:00
|
|
|
self._notify_shared_location_change()
|
|
|
|
|
|
|
|
def _notify_shared_location_change(self):
|
|
|
|
owner = self._model.get_value('owner')
|
|
|
|
title = self._model.get_value('title')
|
|
|
|
|
|
|
|
text = '<b>' + owner + '</b> is reading <i>' + title + '</i>'
|
|
|
|
self._notif_bar.set_text(text)
|
|
|
|
self._notif_bar.set_action('goto_shared_location', 'Go There')
|
|
|
|
self._notif_bar.set_icon('stock_right')
|
|
|
|
self._notif_bar.show()
|