Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/activity/Activity.py
blob: 44c5fdbee87742da05d099ed3408e018fe44c7c1 (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
import os
import logging

import dbus
import dbus.service
import gtk
import gobject

from sugar.presence.PresenceService import PresenceService
from sugar import activity
import sugar.util

ACTIVITY_SERVICE_NAME = "org.laptop.Activity"
ACTIVITY_SERVICE_PATH = "/org/laptop/Activity"
ACTIVITY_INTERFACE = "org.laptop.Activity"

def get_service_name(xid):
	return ACTIVITY_SERVICE_NAME + '%d' % xid

def get_object_path(xid):
	return ACTIVITY_SERVICE_PATH + "/%s" % xid 

class ActivityDbusService(dbus.service.Object):
	"""Base dbus service object that each Activity uses to export dbus methods.
	
	The dbus service is separate from the actual Activity object so that we can
	tightly control what stuff passes through the dbus python bindings."""

	def start(self, pservice, activity):
		self._activity = activity
		self._pservice = pservice		

	@dbus.service.method(ACTIVITY_INTERFACE)
	def share(self):
		"""Called by the shell to request the activity to share itself on the network."""
		self._activity.share()

	@dbus.service.method(ACTIVITY_INTERFACE)
	def join(self, activity_ps_path):
		"""Join the activity specified by its presence service path"""
		activity_ps = self._pservice.get(activity_ps_path)
		return self._activity.join(activity_ps)

	@dbus.service.method(ACTIVITY_INTERFACE)
	def get_id(self):
		"""Get the activity identifier"""
		return self._activity.get_id()

	@dbus.service.method(ACTIVITY_INTERFACE)
	def get_type(self):
		"""Get the activity type"""
		return self._activity.get_type()

	@dbus.service.method(ACTIVITY_INTERFACE)
	def get_shared(self):
		"""Returns True if the activity is shared on the mesh."""
		return self._activity.get_shared()

	@dbus.service.method(ACTIVITY_INTERFACE,
						 in_signature="sas", out_signature="")
	def execute(self, command, args):
		self._activity.execute(command, args)

class Activity(gtk.Window):
	"""Base Activity class that all other Activities derive from."""

	def __init__(self):
		gtk.Window.__init__(self)

		self.connect('destroy', self.__destroy_cb)

		self._shared = False
		self._activity_id = None
		self._default_type = None
		self._service = None
		self._pservice = PresenceService()

		self.present()
	
		group = gtk.Window()
		group.realize()
		self.window.set_group(group.window)

		bus = dbus.SessionBus()
		xid = self.window.xid
		bus_name = dbus.service.BusName(get_service_name(xid), bus=bus)
		self._bus = ActivityDbusService(bus_name, get_object_path(xid))
		self._bus.start(self._pservice, self)

	def set_type(self, activity_type):
		"""Sets the activity type."""
		self._activity_type = activity_type
		self._default_type = activity.get_default_type(activity_type)

	def get_type(self):
		"""Gets the activity type."""
		return self._activity_type

	def get_shared(self):
		"""Returns TRUE if the activity is shared on the mesh."""
		return self._shared

	def get_id(self):
		"""Get the unique activity identifier."""
		if self._activity_id == None:
			self._activity_id = sugar.util.unique_id()
		return self._activity_id

	def join(self, activity_ps):
		"""Join an activity shared on the network."""
		self._shared = True
		self._activity_id = activity_ps.get_id()

		# Publish the default service, it's a copy of
		# one of those we found on the network.
		services = activity_ps.get_services_of_type(self._default_type)
		if len(services) > 0:
			service = services[0]
			addr = service.get_address()
			port = service.get_port()
			properties = service.get_published_values()
			self._service = self._pservice.share_activity(
							self, self._default_type, properties, addr, port)
		else:
			logging.error('Cannot join the activity')

	def share(self):
		"""Share the activity on the network."""
		logging.debug('Share activity %s on the network.' % self.get_id())

		self._service = self._pservice.share_activity(self, self._default_type)
		self._shared = True

	def execute(self, command, args):
		"""Execute the given command with args"""
		pass

	def __destroy_cb(self, window):
		if self._bus:
			del self._bus
			self._bus = None
		if self._service:
			self._pservice.unregister_service(self._service)