Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/conf/ActivityRegistry.py
blob: 64173a9d1536d56320d01aa7b68da4a396af3255 (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
# Copyright (C) 2006, Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import logging
import os
from ConfigParser import ConfigParser
from ConfigParser import NoOptionError

from sugar import activity
from sugar import env
import sugar

class ActivityModule:
	"""Info about an activity module. Wraps a .activity file."""
	
	def __init__(self, name, activity_id, directory):
		self._name = name
		self._icon = None
		self._id = activity_id
		self._directory = directory
		self._show_launcher = False	

	def get_name(self):
		"""Get the activity user visible name."""
		return self._name

	def get_id(self):
		"""Get the activity identifier"""
		return self._id

	def get_icon(self):
		"""Get the activity icon name"""
		return self._icon

	def set_icon(self, icon):
		"""Set the activity icon name"""
		self._icon = icon

	def get_directory(self):
		"""Get the path to activity directory."""
		return self._directory
		
	def get_default_type(self):
		"""Get the the type of the default activity service."""
		return activity.get_default_type(self._id)

	def set_default_type(self, default_type):
		"""Set the the type of the default activity service."""
		self._default_type = default_type

	def get_show_launcher(self):
		"""Get whether there should be a visible launcher for the activity"""
		return self._show_launcher

	def set_show_launcher(self, show_launcher):
		"""Set whether there should be a visible launcher for the activity"""
		self._show_launcher = show_launcher

class _ActivityRegistry:
	"""Service that tracks the available activities"""

	def __init__(self):
		self._activities = []
		self.scan_directory(env.get_activities_dir())

	def get_activity(self, activity_id):
		"""Returns an activity given his identifier"""
		for activity in self._activities:
			if activity.get_id() == activity_id:
				return activity
		return None
	
	def get_activity_from_type(self, default_type):
		"""Returns an activity given his default type"""
		for activity in self._activities:
			if activity.get_default_type() == default_type:
				return activity
		return None
	
	def scan_directory(self, path):
		"""Scan a directory for activities and add them to the registry.""" 
		if os.path.isdir(path):
			for f in os.listdir(path):
				if f.endswith(".activity"):
					self.add(os.path.join(path, f))

	def add(self, path):
		"""Add an activity to the registry. The path points to a .activity file."""
		cp = ConfigParser()
		cp.read([path])

		directory = os.path.dirname(path)

		try:
			activity_id = cp.get('Activity', 'id')
		except NoOptionError:
			logging.error('%s miss the required id option' % (path))
			return False

		try:
			name = cp.get('Activity', 'name')
		except NoOptionError:
			logging.error('%s miss the required name option' % (path))
			return False

		module = ActivityModule(name, activity_id, directory)
		self._activities.append(module)

		if cp.has_option('Activity', 'show_launcher'):
			if cp.get('Activity', 'show_launcher') == 'yes':
				module.set_show_launcher(True)

		if cp.has_option('Activity', 'icon'):
			module.set_icon(cp.get('Activity', 'icon'))

		return True

	def list_activities(self):
		"""Enumerate the registered activities as an ActivityModule list."""
		return self._activities