Sugar.activity.registry: Difference between revisions

From OLPC
Jump to navigation Jump to search
No edit summary
No edit summary
Line 36: Line 36:
print ai.name
print ai.name


</pre>

=== How do I get a list of all activities that handle a specific mime type? ===
Use the get_activities_for_type() method in ActivityRegistry, passing to it the mime type you are trying to open.

<pre>
from sugar.activity import registry
...
#Create a new ActivityRegistry object
ar = registry.ActivityRegistry()

# search for activities that handle gif images - will get a list of ActivityInfo objects
aiList = ar.get_activities_for_type('image/gif')
</pre>
</pre>

Revision as of 20:09, 30 June 2008

Class: ActivityInfo

Class: ActivityRegistry

The ActivityRegistry class can be used to help you get information about the activities that are installed on the current XO.

How do I get a list of all the activities that are available on an XO?

Use the get_activities() method in ActivityRegistry. This will return a list of ActivityInfo objects - one for each activity installed. You can then iterate through this list.

from sugar.activity import registry
...
        #Create a new ActivityRegistry object
        ar = registry.ActivityRegistry()

        # use get_activities() to get a list of ActivityInfo objects and iterate through each
        for ai in ar.get_activities():
            print ai.name


How do I find all the activities whose name matches a certain string?

The find_activity() method in ActivityRegistry takes a string and returns a list of ActivityInfo objects, each of which represents an activity whose name matches the string passed. You can pass the full name of an activity (eg. 'Terminal', or 'Web') or you can pass part of the name (eg. 'term'). Note that this method may find multiple activities that match your search, so make sure you check for how many have been returned if you are looking for a specific activity. The find_activity() method is not case sensitive.

from sugar.activity import registry
...
        #Create a new ActivityRegistry object
        ar = registry.ActivityRegistry()

        # search for activities with a name like 'annot'
        aiList = ar.find_activity('terminal')
        # for each ActivityInfo object (representing each activity) found, print out the activity name. 
        for ai in aiList:
            print ai.name

How do I get a list of all activities that handle a specific mime type?

Use the get_activities_for_type() method in ActivityRegistry, passing to it the mime type you are trying to open.

from sugar.activity import registry
...
        #Create a new ActivityRegistry object
        ar = registry.ActivityRegistry()

        # search for activities that handle gif images - will get a list of ActivityInfo objects
        aiList = ar.get_activities_for_type('image/gif')