2006-06-14 21:01:17 +02:00
|
|
|
import pygtk
|
|
|
|
pygtk.require('2.0')
|
|
|
|
import gtk
|
2006-06-15 03:52:46 +02:00
|
|
|
import pango
|
2006-06-15 23:03:59 +02:00
|
|
|
import cgi
|
2006-06-17 04:24:12 +02:00
|
|
|
import xml.sax.saxutils
|
2006-06-18 07:31:55 +02:00
|
|
|
import gobject
|
2006-06-22 18:52:30 +02:00
|
|
|
import socket
|
2006-06-14 21:01:17 +02:00
|
|
|
|
2006-06-21 20:23:18 +02:00
|
|
|
from google import google
|
2006-06-15 23:41:49 +02:00
|
|
|
from sugar.presence.PresenceService import PresenceService
|
2006-07-06 15:59:48 +02:00
|
|
|
from sugar.activity import Activity
|
2006-06-21 20:23:18 +02:00
|
|
|
|
2006-07-05 17:00:11 +02:00
|
|
|
from gettext import gettext as _
|
|
|
|
|
2006-06-21 20:23:18 +02:00
|
|
|
_BROWSER_ACTIVITY_TYPE = "_web_olpc._udp"
|
2006-06-14 21:01:17 +02:00
|
|
|
|
2006-06-18 07:31:55 +02:00
|
|
|
_COLUMN_TITLE = 0
|
|
|
|
_COLUMN_ADDRESS = 1
|
|
|
|
_COLUMN_SUBTITLE = 2
|
|
|
|
_COLUMN_SERVICE = 3
|
|
|
|
|
2006-06-18 07:50:01 +02:00
|
|
|
class SearchHelper(object):
|
2006-06-22 20:05:38 +02:00
|
|
|
def __init__(self, activity_id):
|
|
|
|
self.search_id = activity_id
|
2006-06-18 07:50:01 +02:00
|
|
|
self.found = False
|
|
|
|
|
2006-06-19 06:39:24 +02:00
|
|
|
class SearchModel(gtk.ListStore):
|
|
|
|
def __init__(self, activities_model, search_text):
|
|
|
|
gtk.ListStore.__init__(self, gobject.TYPE_STRING, gobject.TYPE_STRING,
|
|
|
|
gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
|
2006-06-22 18:52:30 +02:00
|
|
|
success = False
|
2006-06-19 06:39:24 +02:00
|
|
|
|
|
|
|
for row in activities_model:
|
|
|
|
title = row[_COLUMN_TITLE]
|
|
|
|
address = row[_COLUMN_ADDRESS]
|
|
|
|
if title.find(search_text) >= 0 or address.find(search_text) >= 0:
|
|
|
|
self.append([ title, address, row[_COLUMN_SUBTITLE], row[_COLUMN_SERVICE] ])
|
|
|
|
|
|
|
|
google.LICENSE_KEY = '1As9KaJQFHIJ1L0W5EZPl6vBOFvh/Vaf'
|
2006-06-22 18:52:30 +02:00
|
|
|
try:
|
|
|
|
data = google.doGoogleSearch(search_text)
|
|
|
|
success = True
|
|
|
|
except socket.gaierror, exc:
|
|
|
|
if exc[0] == -3: # Temporary failure in name resolution
|
|
|
|
errdlg = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO,
|
|
|
|
gtk.BUTTONS_OK, "There appears to be no network connection.")
|
|
|
|
errdlg.connect("response", lambda d, e: d.destroy())
|
|
|
|
errdlg.connect("close", lambda d, e: d.destroy())
|
|
|
|
errdlg.show()
|
2006-06-19 06:39:24 +02:00
|
|
|
|
2006-06-22 18:52:30 +02:00
|
|
|
if success == True:
|
|
|
|
for result in data.results:
|
|
|
|
title = result.title
|
|
|
|
|
|
|
|
# FIXME what tags should we actually strip?
|
|
|
|
title = title.replace('<b>', '')
|
|
|
|
title = title.replace('</b>', '')
|
|
|
|
|
|
|
|
# FIXME I'm sure there is a better way to
|
|
|
|
# unescape these.
|
|
|
|
title = title.replace('"', '"')
|
|
|
|
title = title.replace('&', '&')
|
|
|
|
|
|
|
|
self.append([ title, result.URL, None, None ])
|
2006-06-19 06:39:24 +02:00
|
|
|
|
2006-06-14 21:01:17 +02:00
|
|
|
class ActivitiesModel(gtk.ListStore):
|
|
|
|
def __init__(self):
|
2006-06-18 07:31:55 +02:00
|
|
|
gtk.ListStore.__init__(self, gobject.TYPE_STRING, gobject.TYPE_STRING,
|
|
|
|
gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)
|
2006-06-14 21:01:17 +02:00
|
|
|
|
2006-06-18 07:50:01 +02:00
|
|
|
def _filter_dupe_activities(self, model, path, it, user_data):
|
|
|
|
"""Search the list of list rows for an existing service that
|
|
|
|
has the activity ID we're looking for."""
|
|
|
|
helper = user_data
|
|
|
|
(service, ) = model.get(it, _COLUMN_SERVICE)
|
|
|
|
if not service:
|
|
|
|
return False
|
2006-06-22 20:05:38 +02:00
|
|
|
if service.get_activity_id() == helper.search_id:
|
2006-06-18 07:50:01 +02:00
|
|
|
helper.found = True
|
|
|
|
return True
|
|
|
|
return False
|
2006-06-16 07:43:42 +02:00
|
|
|
|
|
|
|
def add_activity(self, buddy, service):
|
2006-06-17 04:24:12 +02:00
|
|
|
# Web Activity check
|
2006-06-22 20:05:38 +02:00
|
|
|
activity_id = service.get_activity_id()
|
|
|
|
if activity_id is None:
|
2006-06-18 07:50:01 +02:00
|
|
|
return
|
|
|
|
# Don't show dupes
|
2006-06-22 20:05:38 +02:00
|
|
|
helper = SearchHelper(activity_id)
|
2006-06-18 07:50:01 +02:00
|
|
|
self.foreach(self._filter_dupe_activities, helper)
|
|
|
|
if helper.found == True:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Only accept browser activities for now
|
2006-06-21 20:23:18 +02:00
|
|
|
if service.get_type() == _BROWSER_ACTIVITY_TYPE:
|
2006-06-17 04:24:12 +02:00
|
|
|
escaped_title = service.get_one_property('Title')
|
|
|
|
escaped_uri = service.get_one_property('URI')
|
|
|
|
if escaped_title and escaped_uri:
|
|
|
|
title = xml.sax.saxutils.unescape(escaped_title)
|
|
|
|
address = xml.sax.saxutils.unescape(escaped_uri)
|
|
|
|
subtitle = 'Shared by %s' % buddy.get_nick_name()
|
2006-06-18 07:31:55 +02:00
|
|
|
self.append([ title, address, subtitle, service ])
|
2006-06-14 21:01:17 +02:00
|
|
|
|
|
|
|
class ActivitiesView(gtk.TreeView):
|
2006-06-19 06:39:24 +02:00
|
|
|
def __init__(self, model):
|
|
|
|
gtk.TreeView.__init__(self, model)
|
2006-06-19 21:12:56 +02:00
|
|
|
|
|
|
|
self._owner = None
|
2006-06-14 21:01:17 +02:00
|
|
|
|
|
|
|
self.set_headers_visible(False)
|
|
|
|
|
2006-06-19 06:39:24 +02:00
|
|
|
theme = gtk.icon_theme_get_default()
|
|
|
|
size = 48
|
|
|
|
self._web_pixbuf = theme.load_icon('emblem-web', size, 0)
|
|
|
|
self._share_pixbuf = theme.load_icon('emblem-people', size, 0)
|
|
|
|
|
2006-06-14 21:01:17 +02:00
|
|
|
column = gtk.TreeViewColumn('')
|
|
|
|
self.append_column(column)
|
|
|
|
|
2006-06-19 06:39:24 +02:00
|
|
|
cell = gtk.CellRendererPixbuf()
|
|
|
|
column.pack_start(cell, False)
|
|
|
|
column.set_cell_data_func(cell, self._icon_cell_data_func)
|
|
|
|
|
2006-06-14 21:01:17 +02:00
|
|
|
cell = gtk.CellRendererText()
|
2006-06-19 06:39:24 +02:00
|
|
|
column.pack_start(cell)
|
2006-06-15 03:52:46 +02:00
|
|
|
column.set_cell_data_func(cell, self._cell_data_func)
|
2006-06-14 21:01:17 +02:00
|
|
|
|
|
|
|
self.connect('row-activated', self._row_activated_cb)
|
2006-06-19 06:39:24 +02:00
|
|
|
|
|
|
|
def _icon_cell_data_func(self, column, cell, model, it):
|
|
|
|
if model.get_value(it, _COLUMN_SERVICE) == None:
|
|
|
|
cell.set_property('pixbuf', self._web_pixbuf)
|
|
|
|
else:
|
|
|
|
cell.set_property('pixbuf', self._share_pixbuf)
|
2006-06-14 21:01:17 +02:00
|
|
|
|
2006-06-15 03:52:46 +02:00
|
|
|
def _cell_data_func(self, column, cell, model, it):
|
2006-06-18 07:31:55 +02:00
|
|
|
title = model.get_value(it, _COLUMN_TITLE)
|
|
|
|
subtitle = model.get_value(it, _COLUMN_SUBTITLE)
|
2006-06-16 07:43:42 +02:00
|
|
|
if subtitle is None:
|
2006-06-18 07:31:55 +02:00
|
|
|
subtitle = model.get_value(it, _COLUMN_ADDRESS)
|
|
|
|
|
2006-06-16 08:36:18 +02:00
|
|
|
markup = '<big><b>' + cgi.escape(title) + '</b></big>'
|
|
|
|
markup += '\n' + cgi.escape(subtitle)
|
2006-06-15 03:52:46 +02:00
|
|
|
|
|
|
|
cell.set_property('markup', markup)
|
|
|
|
cell.set_property('ellipsize', pango.ELLIPSIZE_END)
|
2006-06-19 21:12:56 +02:00
|
|
|
|
|
|
|
def set_owner(self, owner):
|
|
|
|
self._owner = owner
|
|
|
|
|
2006-06-16 07:43:42 +02:00
|
|
|
def _row_activated_cb(self, treeview, path, column):
|
2006-06-14 21:01:17 +02:00
|
|
|
model = self.get_model()
|
2006-06-18 07:31:55 +02:00
|
|
|
address = model.get_value(model.get_iter(path), _COLUMN_ADDRESS)
|
|
|
|
service = model.get_value(model.get_iter(path), _COLUMN_SERVICE)
|
2006-06-14 21:01:17 +02:00
|
|
|
|
2006-07-06 15:59:48 +02:00
|
|
|
Activity.create('com.redhat.Sugar.BrowserActivity', service, [ address ])
|
|
|
|
|
2006-06-14 21:01:17 +02:00
|
|
|
class StartPage(gtk.HBox):
|
2006-06-16 23:01:25 +02:00
|
|
|
def __init__(self, ac_signal_object):
|
2006-06-14 21:01:17 +02:00
|
|
|
gtk.HBox.__init__(self)
|
|
|
|
|
2006-06-16 23:01:25 +02:00
|
|
|
self._ac_signal_object = ac_signal_object
|
|
|
|
self._ac_signal_object.connect("local-activity-started",
|
|
|
|
self._on_local_activity_started_cb)
|
|
|
|
self._ac_signal_object.connect("local-activity-ended",
|
|
|
|
self._on_local_activity_ended_cb)
|
|
|
|
|
2006-06-15 23:41:49 +02:00
|
|
|
self._pservice = PresenceService.get_instance()
|
|
|
|
self._pservice.connect("activity-announced", self._on_activity_announced_cb)
|
2006-06-17 04:24:12 +02:00
|
|
|
self._pservice.connect("new-service-adv", self._on_new_service_adv_cb)
|
2006-06-19 21:12:56 +02:00
|
|
|
self._pservice.connect("buddy-appeared", self._on_buddy_appeared_cb)
|
|
|
|
self._pservice.connect("buddy-disappeared", self._on_buddy_disappeared_cb)
|
2006-06-15 23:41:49 +02:00
|
|
|
self._pservice.start()
|
2006-06-21 20:23:18 +02:00
|
|
|
self._pservice.track_service_type(_BROWSER_ACTIVITY_TYPE)
|
2006-06-20 03:16:30 +02:00
|
|
|
if self._pservice.get_owner():
|
2006-06-20 15:39:36 +02:00
|
|
|
self._on_buddy_appeared_cb(self._pservice, self._pservice.get_owner())
|
2006-06-15 23:41:49 +02:00
|
|
|
|
2006-06-14 21:01:17 +02:00
|
|
|
vbox = gtk.VBox()
|
|
|
|
|
|
|
|
search_box = gtk.HBox(False, 6)
|
|
|
|
search_box.set_border_width(24)
|
|
|
|
|
|
|
|
self._search_entry = gtk.Entry()
|
2006-06-15 03:52:46 +02:00
|
|
|
self._search_entry.connect('activate', self._search_entry_activate_cb)
|
2006-06-14 21:01:17 +02:00
|
|
|
search_box.pack_start(self._search_entry)
|
|
|
|
self._search_entry.show()
|
|
|
|
|
2006-07-05 17:00:11 +02:00
|
|
|
search_button = gtk.Button(_("Search"))
|
2006-06-14 21:01:17 +02:00
|
|
|
search_button.connect('clicked', self._search_button_clicked_cb)
|
|
|
|
search_box.pack_start(search_button, False)
|
|
|
|
search_button.show()
|
|
|
|
|
|
|
|
vbox.pack_start(search_box, False, True)
|
|
|
|
search_box.show()
|
|
|
|
|
|
|
|
exp_space = gtk.Label('')
|
|
|
|
vbox.pack_start(exp_space)
|
|
|
|
exp_space.show()
|
|
|
|
|
|
|
|
self.pack_start(vbox)
|
|
|
|
vbox.show()
|
|
|
|
|
2006-06-19 06:39:24 +02:00
|
|
|
vbox = gtk.VBox()
|
|
|
|
|
|
|
|
self._search_close_box = gtk.HBox()
|
|
|
|
|
|
|
|
self._search_close_label = gtk.Label()
|
|
|
|
self._search_close_label.set_alignment(0.0, 0.5)
|
|
|
|
self._search_close_box.pack_start(self._search_close_label)
|
|
|
|
self._search_close_label.show()
|
|
|
|
|
|
|
|
close_image = gtk.Image()
|
|
|
|
close_image.set_from_stock (gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
|
|
|
|
close_image.show()
|
|
|
|
|
|
|
|
search_close_button = gtk.Button()
|
|
|
|
rcstyle = gtk.RcStyle();
|
|
|
|
rcstyle.xthickness = rcstyle.ythickness = 0;
|
|
|
|
search_close_button.modify_style (rcstyle);
|
|
|
|
search_close_button.add(close_image)
|
|
|
|
search_close_button.set_relief(gtk.RELIEF_NONE)
|
|
|
|
search_close_button.set_focus_on_click(False)
|
|
|
|
search_close_button.connect("clicked", self.__search_close_button_clicked_cb)
|
|
|
|
|
|
|
|
self._search_close_box.pack_start(search_close_button, False)
|
|
|
|
search_close_button.show()
|
|
|
|
|
|
|
|
vbox.pack_start(self._search_close_box, False)
|
|
|
|
|
2006-06-15 03:52:46 +02:00
|
|
|
sw = gtk.ScrolledWindow()
|
|
|
|
sw.set_size_request(320, -1)
|
|
|
|
sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
|
2006-06-14 21:01:17 +02:00
|
|
|
|
2006-06-19 06:39:24 +02:00
|
|
|
self._activities_model = ActivitiesModel()
|
|
|
|
|
2006-06-19 21:12:56 +02:00
|
|
|
owner = self._pservice.get_owner()
|
2006-06-19 06:39:24 +02:00
|
|
|
self._activities = ActivitiesView(self._activities_model)
|
2006-06-15 03:52:46 +02:00
|
|
|
sw.add(self._activities)
|
|
|
|
self._activities.show()
|
2006-06-15 23:41:49 +02:00
|
|
|
|
2006-06-19 06:39:24 +02:00
|
|
|
vbox.pack_start(sw)
|
2006-06-15 03:52:46 +02:00
|
|
|
sw.show()
|
2006-06-19 06:39:24 +02:00
|
|
|
|
|
|
|
self.pack_start(vbox)
|
|
|
|
vbox.show()
|
|
|
|
|
|
|
|
def __search_close_button_clicked_cb(self, button):
|
|
|
|
self._search(None)
|
2006-06-15 03:52:46 +02:00
|
|
|
|
2006-06-16 23:01:25 +02:00
|
|
|
def _on_local_activity_started_cb(self, helper, activity_container, activity_id):
|
|
|
|
print "new local activity %s" % activity_id
|
|
|
|
|
|
|
|
def _on_local_activity_ended_cb(self, helper, activity_container, activity_id):
|
2006-06-19 15:49:57 +02:00
|
|
|
print "local activity %s disappeared" % activity_id
|
2006-06-16 23:01:25 +02:00
|
|
|
|
2006-06-17 04:24:12 +02:00
|
|
|
def _on_new_service_adv_cb(self, pservice, activity_id, short_stype):
|
|
|
|
if activity_id:
|
|
|
|
self._pservice.track_service_type(short_stype)
|
|
|
|
|
2006-06-19 21:12:56 +02:00
|
|
|
def _on_buddy_appeared_cb(self, pservice, buddy):
|
|
|
|
if buddy.is_owner():
|
|
|
|
self._activities.set_owner(buddy)
|
|
|
|
|
|
|
|
def _on_buddy_disappeared_cb(self, pservice, buddy):
|
2006-06-20 21:10:49 +02:00
|
|
|
if buddy.is_owner():
|
2006-06-19 21:12:56 +02:00
|
|
|
self._activities.set_owner(None)
|
|
|
|
|
2006-06-15 23:41:49 +02:00
|
|
|
def _on_activity_announced_cb(self, pservice, service, buddy):
|
2006-06-22 20:37:05 +02:00
|
|
|
print "Found new activity service (activity %s of type %s)" % (service.get_activity_id(), service.get_type())
|
2006-06-19 06:39:24 +02:00
|
|
|
self._activities_model.add_activity(buddy, service)
|
|
|
|
if self._activities.get_model() != self._activities_model:
|
|
|
|
self._search(self._last_search)
|
2006-06-15 23:41:49 +02:00
|
|
|
|
2006-06-15 03:52:46 +02:00
|
|
|
def _search_entry_activate_cb(self, entry):
|
|
|
|
self._search()
|
2006-06-19 06:39:24 +02:00
|
|
|
self._search_entry.set_text('')
|
2006-06-14 21:01:17 +02:00
|
|
|
|
|
|
|
def _search_button_clicked_cb(self, button):
|
2006-06-15 03:52:46 +02:00
|
|
|
self._search()
|
|
|
|
self._search_entry.set_text('')
|
2006-06-16 08:36:18 +02:00
|
|
|
|
2006-06-19 06:39:24 +02:00
|
|
|
def _search(self, text = None):
|
|
|
|
if text == None:
|
|
|
|
text = self._search_entry.get_text()
|
|
|
|
|
|
|
|
if text == None or len(text) == 0:
|
|
|
|
self._activities.set_model(self._activities_model)
|
|
|
|
self._search_close_box.hide()
|
|
|
|
else:
|
|
|
|
search_model = SearchModel(self._activities_model, text)
|
|
|
|
self._activities.set_model(search_model)
|
|
|
|
|
|
|
|
self._search_close_label.set_text('Search for %s' % (text))
|
|
|
|
self._search_close_box.show()
|
2006-06-16 08:36:18 +02:00
|
|
|
|
2006-06-19 06:39:24 +02:00
|
|
|
self._last_search = text
|