2006-04-19 21:30:49 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2006-04-20 18:10:42 +02:00
|
|
|
import dbus
|
|
|
|
import dbus.service
|
|
|
|
import dbus.glib
|
|
|
|
|
2006-04-19 21:30:49 +02:00
|
|
|
import pygtk
|
|
|
|
pygtk.require('2.0')
|
|
|
|
import gtk
|
2006-04-20 18:10:42 +02:00
|
|
|
|
2006-04-19 21:30:49 +02:00
|
|
|
import geckoembed
|
|
|
|
|
2006-04-21 22:35:02 +02:00
|
|
|
import sys
|
2006-04-27 21:05:50 +02:00
|
|
|
from sugar import activity
|
2006-04-21 04:02:51 +02:00
|
|
|
|
2006-04-26 06:18:21 +02:00
|
|
|
from sugar_globals import *
|
|
|
|
|
2006-04-19 21:30:49 +02:00
|
|
|
class AddressToolbar(gtk.Toolbar):
|
|
|
|
def __init__(self):
|
|
|
|
gtk.Toolbar.__init__(self)
|
|
|
|
|
|
|
|
address_item = AddressItem(self.__open_address_cb)
|
|
|
|
self.insert(address_item, 0)
|
|
|
|
address_item.show()
|
|
|
|
|
|
|
|
def __open_address_cb(self, address):
|
2006-04-26 21:58:25 +02:00
|
|
|
BrowserShell.get_instance().open_browser(address)
|
2006-04-19 21:30:49 +02:00
|
|
|
|
|
|
|
class AddressItem(gtk.ToolItem):
|
|
|
|
def __init__(self, callback):
|
|
|
|
gtk.ToolItem.__init__(self)
|
|
|
|
|
|
|
|
address_entry = AddressEntry(callback)
|
|
|
|
self.add(address_entry)
|
|
|
|
address_entry.show()
|
|
|
|
|
|
|
|
class AddressEntry(gtk.HBox):
|
|
|
|
def __init__(self, callback):
|
|
|
|
gtk.HBox.__init__(self)
|
|
|
|
|
|
|
|
self.callback = callback
|
|
|
|
self.folded = True
|
|
|
|
|
|
|
|
label = gtk.Label("Open")
|
|
|
|
self.pack_start(label, False)
|
|
|
|
label.show()
|
|
|
|
|
|
|
|
self.button = gtk.Button()
|
|
|
|
self.button.set_relief(gtk.RELIEF_NONE)
|
|
|
|
self.button.connect("clicked", self.__button_clicked_cb)
|
|
|
|
self.pack_start(self.button, False)
|
|
|
|
self.button.show()
|
|
|
|
|
|
|
|
self.entry = gtk.Entry()
|
|
|
|
self.entry.connect("activate", self.__activate_cb)
|
|
|
|
self.pack_start(self.entry, False)
|
|
|
|
self.entry.show()
|
|
|
|
|
|
|
|
self._update_folded_state()
|
|
|
|
|
|
|
|
def _update_folded_state(self):
|
|
|
|
if self.folded:
|
|
|
|
image = gtk.Image()
|
2006-04-26 06:18:21 +02:00
|
|
|
image.set_from_file(data_dir + "/unfold.png")
|
2006-04-19 21:30:49 +02:00
|
|
|
self.button.set_image(image)
|
|
|
|
image.show()
|
|
|
|
|
|
|
|
self.entry.hide()
|
|
|
|
else:
|
|
|
|
image = gtk.Image()
|
2006-04-26 06:18:21 +02:00
|
|
|
image.set_from_file(data_dir + "/fold.png")
|
2006-04-19 21:30:49 +02:00
|
|
|
self.button.set_image(image)
|
|
|
|
image.show()
|
|
|
|
|
|
|
|
self.entry.show()
|
2006-04-21 04:22:09 +02:00
|
|
|
self.entry.grab_focus()
|
|
|
|
|
|
|
|
def get_folded(self):
|
|
|
|
return self.folded
|
|
|
|
|
|
|
|
def set_folded(self, folded):
|
2006-04-20 17:43:33 +02:00
|
|
|
self.folded = not self.folded
|
2006-04-21 04:22:09 +02:00
|
|
|
self._update_folded_state()
|
|
|
|
|
|
|
|
def __button_clicked_cb(self, button):
|
|
|
|
self.set_folded(not self.get_folded())
|
2006-04-19 21:30:49 +02:00
|
|
|
|
|
|
|
def __activate_cb(self, entry):
|
|
|
|
self.callback(entry.get_text())
|
2006-04-21 04:22:09 +02:00
|
|
|
self.set_folded(True)
|
2006-04-19 21:30:49 +02:00
|
|
|
|
|
|
|
class NavigationToolbar(gtk.Toolbar):
|
|
|
|
def __init__(self, embed):
|
|
|
|
gtk.Toolbar.__init__(self)
|
|
|
|
self.embed = embed
|
|
|
|
|
|
|
|
self.set_style(gtk.TOOLBAR_ICONS)
|
|
|
|
|
|
|
|
self.back = gtk.ToolButton(gtk.STOCK_GO_BACK)
|
|
|
|
self.back.connect("clicked", self.__go_back_cb)
|
|
|
|
self.insert(self.back, -1)
|
|
|
|
self.back.show()
|
|
|
|
|
|
|
|
self.forward = gtk.ToolButton(gtk.STOCK_GO_FORWARD)
|
|
|
|
self.forward.connect("clicked", self.__go_forward_cb)
|
|
|
|
self.insert(self.forward, -1)
|
|
|
|
self.forward.show()
|
|
|
|
|
|
|
|
self.reload = gtk.ToolButton(gtk.STOCK_REFRESH)
|
|
|
|
self.reload.connect("clicked", self.__reload_cb)
|
|
|
|
self.insert(self.reload, -1)
|
|
|
|
self.reload.show()
|
|
|
|
|
|
|
|
separator = gtk.SeparatorToolItem()
|
|
|
|
self.insert(separator, -1)
|
|
|
|
separator.show()
|
|
|
|
|
|
|
|
address_item = AddressItem(self.__open_address_cb)
|
|
|
|
self.insert(address_item, -1)
|
|
|
|
address_item.show()
|
|
|
|
|
|
|
|
self._update_sensitivity()
|
|
|
|
|
|
|
|
self.embed.connect("location", self.__location_changed)
|
|
|
|
|
|
|
|
def _update_sensitivity(self):
|
|
|
|
self.back.set_sensitive(self.embed.can_go_back())
|
|
|
|
self.forward.set_sensitive(self.embed.can_go_forward())
|
|
|
|
|
|
|
|
def __go_back_cb(self, button):
|
|
|
|
self.embed.go_back()
|
|
|
|
|
|
|
|
def __go_forward_cb(self, button):
|
|
|
|
self.embed.go_forward()
|
|
|
|
|
|
|
|
def __reload_cb(self, button):
|
|
|
|
self.embed.reload()
|
|
|
|
|
|
|
|
def __location_changed(self, embed):
|
|
|
|
self._update_sensitivity()
|
|
|
|
|
|
|
|
def __open_address_cb(self, address):
|
2006-04-25 23:47:49 +02:00
|
|
|
self.embed.load_address(address)
|
2006-04-19 21:30:49 +02:00
|
|
|
|
2006-04-21 22:35:02 +02:00
|
|
|
class BrowserActivity(activity.Activity):
|
2006-04-19 21:30:49 +02:00
|
|
|
def __init__(self, uri):
|
2006-04-21 22:35:02 +02:00
|
|
|
activity.Activity.__init__(self)
|
2006-04-21 04:02:51 +02:00
|
|
|
self.uri = uri
|
|
|
|
|
|
|
|
def activity_on_connected_to_shell(self):
|
2006-04-22 00:34:41 +02:00
|
|
|
self.activity_set_ellipsize_tab(True)
|
2006-04-21 21:53:26 +02:00
|
|
|
self.activity_set_can_close(True)
|
2006-04-21 04:02:51 +02:00
|
|
|
self.activity_set_tab_text("Web Page")
|
2006-04-27 20:58:10 +02:00
|
|
|
self.activity_set_tab_icon_name("web-browser")
|
|
|
|
self.activity_show_icon(True)
|
2006-04-21 04:02:51 +02:00
|
|
|
|
|
|
|
vbox = gtk.VBox()
|
2006-04-19 21:30:49 +02:00
|
|
|
|
|
|
|
self.embed = geckoembed.Embed()
|
2006-04-21 04:02:51 +02:00
|
|
|
self.embed.connect("title", self.__title_cb)
|
|
|
|
vbox.pack_start(self.embed)
|
|
|
|
|
2006-04-19 21:30:49 +02:00
|
|
|
self.embed.show()
|
2006-04-25 23:47:49 +02:00
|
|
|
self.embed.load_address(self.uri)
|
2006-04-19 21:30:49 +02:00
|
|
|
|
|
|
|
nav_toolbar = NavigationToolbar(self.embed)
|
2006-04-21 04:02:51 +02:00
|
|
|
vbox.pack_start(nav_toolbar, False)
|
2006-04-19 21:30:49 +02:00
|
|
|
nav_toolbar.show()
|
|
|
|
|
2006-04-21 04:02:51 +02:00
|
|
|
plug = self.activity_get_gtk_plug()
|
|
|
|
plug.add(vbox)
|
|
|
|
plug.show()
|
|
|
|
|
|
|
|
vbox.show()
|
2006-04-25 23:47:49 +02:00
|
|
|
|
|
|
|
def get_embed(self):
|
|
|
|
return self.embed
|
|
|
|
|
2006-04-21 04:02:51 +02:00
|
|
|
def __title_cb(self, embed):
|
|
|
|
self.activity_set_tab_text(embed.get_title())
|
|
|
|
|
2006-04-21 21:27:20 +02:00
|
|
|
def activity_on_close_from_user(self):
|
|
|
|
self.activity_shutdown()
|
|
|
|
|
2006-04-21 22:35:02 +02:00
|
|
|
class WebActivity(activity.Activity):
|
2006-04-19 21:30:49 +02:00
|
|
|
def __init__(self):
|
2006-04-21 22:35:02 +02:00
|
|
|
activity.Activity.__init__(self)
|
2006-04-19 21:30:49 +02:00
|
|
|
|
2006-04-21 04:02:51 +02:00
|
|
|
def activity_on_connected_to_shell(self):
|
|
|
|
self.activity_set_tab_text("Web Browser")
|
2006-04-27 20:58:10 +02:00
|
|
|
self.activity_set_tab_icon_name("web-browser")
|
|
|
|
self.activity_show_icon(True)
|
2006-04-19 21:30:49 +02:00
|
|
|
|
2006-04-21 04:02:51 +02:00
|
|
|
vbox = gtk.VBox()
|
|
|
|
|
2006-04-19 21:30:49 +02:00
|
|
|
self.embed = geckoembed.Embed()
|
2006-04-21 04:02:51 +02:00
|
|
|
self.embed.connect("open-address", self.__open_address);
|
|
|
|
vbox.pack_start(self.embed)
|
2006-04-19 21:30:49 +02:00
|
|
|
self.embed.show()
|
|
|
|
|
|
|
|
address_toolbar = AddressToolbar()
|
2006-04-21 04:02:51 +02:00
|
|
|
vbox.pack_start(address_toolbar, False)
|
2006-04-19 21:30:49 +02:00
|
|
|
address_toolbar.show()
|
|
|
|
|
2006-04-21 04:02:51 +02:00
|
|
|
plug = self.activity_get_gtk_plug()
|
|
|
|
plug.add(vbox)
|
|
|
|
plug.show()
|
2006-04-19 21:30:49 +02:00
|
|
|
|
2006-04-21 04:02:51 +02:00
|
|
|
vbox.show()
|
|
|
|
|
2006-04-25 23:47:49 +02:00
|
|
|
self.embed.load_address("http://www.google.com")
|
2006-04-21 04:02:51 +02:00
|
|
|
|
2006-04-19 21:30:49 +02:00
|
|
|
def __open_address(self, embed, uri, data=None):
|
|
|
|
if uri.startswith("http://www.google.com"):
|
|
|
|
return False
|
|
|
|
else:
|
2006-04-26 21:58:25 +02:00
|
|
|
BrowserShell.get_instance().open_browser(uri)
|
2006-04-19 21:30:49 +02:00
|
|
|
return True
|
|
|
|
|
2006-04-26 22:35:19 +02:00
|
|
|
def activity_on_disconnected_from_shell(self):
|
|
|
|
gtk.main_quit()
|
|
|
|
gc.collect()
|
|
|
|
|
2006-04-25 23:47:49 +02:00
|
|
|
class BrowserShell(dbus.service.Object):
|
2006-04-26 21:58:25 +02:00
|
|
|
instance = None
|
|
|
|
|
|
|
|
def get_instance():
|
|
|
|
if not BrowserShell.instance:
|
|
|
|
BrowserShell.instance = BrowserShell()
|
|
|
|
return BrowserShell.instance
|
|
|
|
|
|
|
|
get_instance = staticmethod(get_instance)
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
session_bus = dbus.SessionBus()
|
|
|
|
bus_name = dbus.service.BusName('com.redhat.Sugar.Browser', bus=session_bus)
|
|
|
|
object_path = '/com/redhat/Sugar/Browser'
|
|
|
|
|
2006-04-25 23:47:49 +02:00
|
|
|
dbus.service.Object.__init__(self, bus_name, object_path)
|
2006-04-26 21:58:25 +02:00
|
|
|
|
2006-04-25 23:47:49 +02:00
|
|
|
self.__browsers = []
|
|
|
|
|
2006-04-26 21:58:25 +02:00
|
|
|
def open_web_activity(self):
|
|
|
|
web_activity = WebActivity()
|
|
|
|
web_activity.activity_connect_to_shell()
|
|
|
|
|
2006-04-25 23:47:49 +02:00
|
|
|
@dbus.service.method('com.redhat.Sugar.BrowserShell')
|
|
|
|
def get_links(self):
|
|
|
|
links = []
|
|
|
|
for browser in self.__browsers:
|
|
|
|
embed = browser.get_embed()
|
|
|
|
link = {}
|
|
|
|
link['title'] = embed.get_title()
|
|
|
|
link['address'] = embed.get_address()
|
|
|
|
links.append(link)
|
|
|
|
return links
|
|
|
|
|
|
|
|
@dbus.service.method('com.redhat.Sugar.BrowserShell')
|
|
|
|
def open_browser(self, uri):
|
|
|
|
browser = BrowserActivity(uri)
|
|
|
|
self.__browsers.append(browser)
|
|
|
|
browser.activity_connect_to_shell()
|
2006-04-26 06:18:21 +02:00
|
|
|
|
2006-04-26 21:58:25 +02:00
|
|
|
def main():
|
|
|
|
BrowserShell.get_instance().open_web_activity()
|
|
|
|
|
2006-04-26 06:18:21 +02:00
|
|
|
try:
|
|
|
|
gtk.main()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
2006-04-27 21:01:42 +02:00
|
|
|
|
|
|
|
if __name__=="__main__":
|
|
|
|
main()
|