2006-10-15 01:24:45 +02:00
|
|
|
# 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
|
|
|
|
|
2006-07-12 13:20:41 +02:00
|
|
|
import logging
|
2006-07-12 17:21:22 +02:00
|
|
|
import os
|
|
|
|
from ConfigParser import ConfigParser
|
|
|
|
from ConfigParser import NoOptionError
|
|
|
|
|
2006-09-04 21:34:54 +02:00
|
|
|
from sugar import activity
|
2006-08-26 13:35:03 +02:00
|
|
|
from sugar import env
|
2006-09-04 21:34:54 +02:00
|
|
|
import sugar
|
2006-09-02 10:54:34 +02:00
|
|
|
|
2006-07-12 13:20:41 +02:00
|
|
|
class ActivityModule:
|
|
|
|
"""Info about an activity module. Wraps a .activity file."""
|
|
|
|
|
2006-07-28 01:25:08 +02:00
|
|
|
def __init__(self, name, activity_id, directory):
|
2006-07-08 11:56:13 +02:00
|
|
|
self._name = name
|
2006-08-16 23:20:22 +02:00
|
|
|
self._icon = None
|
2006-07-12 13:20:41 +02:00
|
|
|
self._id = activity_id
|
|
|
|
self._directory = directory
|
2006-07-20 10:35:41 +02:00
|
|
|
self._show_launcher = False
|
|
|
|
|
2006-07-08 11:56:13 +02:00
|
|
|
def get_name(self):
|
2006-07-12 13:20:41 +02:00
|
|
|
"""Get the activity user visible name."""
|
2006-07-08 11:56:13 +02:00
|
|
|
return self._name
|
|
|
|
|
2006-07-12 13:20:41 +02:00
|
|
|
def get_id(self):
|
|
|
|
"""Get the activity identifier"""
|
|
|
|
return self._id
|
|
|
|
|
2006-08-16 23:20:22 +02:00
|
|
|
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
|
|
|
|
|
2006-07-12 13:20:41 +02:00
|
|
|
def get_directory(self):
|
|
|
|
"""Get the path to activity directory."""
|
|
|
|
return self._directory
|
2006-07-14 16:40:45 +02:00
|
|
|
|
|
|
|
def get_default_type(self):
|
|
|
|
"""Get the the type of the default activity service."""
|
2006-09-04 21:34:54 +02:00
|
|
|
return activity.get_default_type(self._id)
|
2006-07-14 16:40:45 +02:00
|
|
|
|
|
|
|
def set_default_type(self, default_type):
|
|
|
|
"""Set the the type of the default activity service."""
|
|
|
|
self._default_type = default_type
|
2006-07-12 13:20:41 +02:00
|
|
|
|
2006-07-20 10:35:41 +02:00
|
|
|
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
|
|
|
|
|
2006-09-02 10:54:34 +02:00
|
|
|
class _ActivityRegistry:
|
2006-07-12 13:20:41 +02:00
|
|
|
"""Service that tracks the available activities"""
|
2006-07-08 11:56:13 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._activities = []
|
2006-10-17 10:07:35 +02:00
|
|
|
self.scan_directory(env.get_activity_info_dir())
|
2006-08-09 12:57:42 +02:00
|
|
|
|
2006-09-02 10:54:34 +02:00
|
|
|
def get_activity(self, activity_id):
|
2006-08-09 12:57:42 +02:00
|
|
|
"""Returns an activity given his identifier"""
|
|
|
|
for activity in self._activities:
|
|
|
|
if activity.get_id() == activity_id:
|
|
|
|
return activity
|
|
|
|
return None
|
2006-07-08 11:56:13 +02:00
|
|
|
|
2006-09-02 10:54:34 +02:00
|
|
|
def get_activity_from_type(self, default_type):
|
2006-07-15 12:31:06 +02:00
|
|
|
"""Returns an activity given his default type"""
|
|
|
|
for activity in self._activities:
|
|
|
|
if activity.get_default_type() == default_type:
|
|
|
|
return activity
|
|
|
|
return None
|
|
|
|
|
2006-07-12 13:20:41 +02:00
|
|
|
def scan_directory(self, path):
|
|
|
|
"""Scan a directory for activities and add them to the registry."""
|
2006-07-12 17:21:22 +02:00
|
|
|
if os.path.isdir(path):
|
2006-07-28 01:25:08 +02:00
|
|
|
for f in os.listdir(path):
|
|
|
|
if f.endswith(".activity"):
|
|
|
|
self.add(os.path.join(path, f))
|
2006-07-12 13:20:41 +02:00
|
|
|
|
|
|
|
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')
|
2006-07-12 17:21:22 +02:00
|
|
|
except NoOptionError:
|
|
|
|
logging.error('%s miss the required id option' % (path))
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
2006-07-12 13:20:41 +02:00
|
|
|
name = cp.get('Activity', 'name')
|
|
|
|
except NoOptionError:
|
2006-07-12 17:21:22 +02:00
|
|
|
logging.error('%s miss the required name option' % (path))
|
|
|
|
return False
|
2006-07-12 13:20:41 +02:00
|
|
|
|
2006-07-28 01:25:08 +02:00
|
|
|
module = ActivityModule(name, activity_id, directory)
|
2006-07-12 13:20:41 +02:00
|
|
|
self._activities.append(module)
|
2006-07-08 11:56:13 +02:00
|
|
|
|
2006-07-20 10:35:41 +02:00
|
|
|
if cp.has_option('Activity', 'show_launcher'):
|
2006-10-13 11:17:35 +02:00
|
|
|
if cp.get('Activity', 'show_launcher') == 'yes':
|
|
|
|
module.set_show_launcher(True)
|
2006-07-20 10:35:41 +02:00
|
|
|
|
2006-08-16 23:20:22 +02:00
|
|
|
if cp.has_option('Activity', 'icon'):
|
|
|
|
module.set_icon(cp.get('Activity', 'icon'))
|
|
|
|
|
2006-07-12 13:20:41 +02:00
|
|
|
return True
|
2006-07-08 11:56:13 +02:00
|
|
|
|
|
|
|
def list_activities(self):
|
2006-07-12 13:20:41 +02:00
|
|
|
"""Enumerate the registered activities as an ActivityModule list."""
|
2006-07-08 11:56:13 +02:00
|
|
|
return self._activities
|