Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/shell/StartPage.py
blob: 6d71871a41b94b60a2368fcefb78b4e61d1153ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import pygtk
pygtk.require('2.0')
import gtk
import pango
import dbus
import cgi
import urllib
import re

import google
from sugar.presence.PresenceService import PresenceService
from sugar.presence import Service
from sugar.browser import BrowserActivity

class ActivitiesModel(gtk.ListStore):
	def __init__(self):
		gtk.ListStore.__init__(self, str, str, str, str)

	def add_web_page(self, title, address):
		self.append([ title, address, None, None ])
	
	def add_activity(self, buddy, service):
		uid = service.get_activity_uid()
		title = urllib.unquote(service.get_one_property('Title'))
		address = urllib.unquote(service.get_one_property('URI'))
		subtitle = 'Shared by %s' % buddy.get_nick_name()
		self.append([ title, address, subtitle, uid ])

class ActivitiesView(gtk.TreeView):
	def __init__(self):
		gtk.TreeView.__init__(self, model = ActivitiesModel())
		
		self.set_headers_visible(False)
		
		column = gtk.TreeViewColumn('')
		self.append_column(column)

		cell = gtk.CellRendererText()
		column.pack_start(cell, True)
		column.set_cell_data_func(cell, self._cell_data_func)
		
		self.connect('row-activated', self._row_activated_cb)
	
	def _cell_data_func(self, column, cell, model, it):
		title = model.get_value(it, 0)
		subtitle = model.get_value(it, 2)
		if subtitle is None:
			subtitle = model.get_value(it, 1)
				
		markup = '<big><b>' + cgi.escape(title) + '</b></big>' 
		markup += '\n' + cgi.escape(subtitle)
		
		cell.set_property('markup', markup)
		cell.set_property('ellipsize', pango.ELLIPSIZE_END)
			
	def _row_activated_cb(self, treeview, path, column):	
		bus = dbus.SessionBus()
		proxy_obj = bus.get_object('com.redhat.Sugar.Browser', '/com/redhat/Sugar/Browser')
		browser_shell = dbus.Interface(proxy_obj, 'com.redhat.Sugar.BrowserShell')

		model = self.get_model() 
		address = model.get_value(model.get_iter(path), 1)
		activity_id = model.get_value(model.get_iter(path), 3)

		print 'Activated row %s %s' % (address, activity_id)

		if activity_id is None:
			browser_shell.open_browser(address)
		else:
			browser_shell.open_browser_with_id(address, activity_id)
			
class StartPage(gtk.HBox):
	def __init__(self, ac_signal_object):
		gtk.HBox.__init__(self)

		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)

		self._pservice = PresenceService.get_instance()
		self._pservice.connect("activity-announced", self._on_activity_announced_cb)
		self._pservice.start()
		self._pservice.track_service_type(BrowserActivity._BROWSER_ACTIVITY_TYPE)

		vbox = gtk.VBox()

		search_box = gtk.HBox(False, 6)
		search_box.set_border_width(24)
		
		self._search_entry = gtk.Entry()
		self._search_entry.connect('activate', self._search_entry_activate_cb)
		search_box.pack_start(self._search_entry)
		self._search_entry.show()
		
		search_button = gtk.Button("Search")
		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()

		sw = gtk.ScrolledWindow()
		sw.set_size_request(320, -1)
		sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)

		self._activities = ActivitiesView()
		sw.add(self._activities)
		self._activities.show()

		self.pack_start(sw)	
		sw.show()

	def _on_local_activity_started_cb(self, helper, activity_container, activity_id):
		self._pservice.track_activity(activity_id)
		print "new local activity %s" % activity_id

	def _on_local_activity_ended_cb(self, helper, activity_container, activity_id):
		self._pservice.untrack_activity(activity_id)

	def _on_activity_announced_cb(self, pservice, service, buddy):
		self._activities.get_model().add_activity(buddy, service)

	def _search_entry_activate_cb(self, entry):
		self._search()
		
	def _search_button_clicked_cb(self, button):
		self._search()
	
	def _search(self):
		text = self._search_entry.get_text()
		self._search_entry.set_text('')
	
		google.LICENSE_KEY = '1As9KaJQFHIJ1L0W5EZPl6vBOFvh/Vaf'
		data = google.doGoogleSearch(text)
		
		model = ActivitiesModel()
		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('&quot;', '"')
			title = title.replace('&amp;', '&')

			model.add_web_page(title, result.URL)
		self._activities.set_model(model)