Line 1: |
Line 1: |
− | {{Sugar Almanac TOC}} | + | {{Almanac}} |
| + | {{Almanac TOC}} |
| The sugar.activity.activity package includes several important classes that are needed to run a basic activity. | | The sugar.activity.activity package includes several important classes that are needed to run a basic activity. |
| | | |
Line 39: |
Line 40: |
| #print out the name of this activity | | #print out the name of this activity |
| print activity.get_bundle_name() | | print activity.get_bundle_name() |
| + | |
| + | === How do I get the version number of my activity? === |
| + | |
| + | #print out the version number of this activity |
| + | print os.environ['SUGAR_BUNDLE_VERSION'] |
| | | |
| = Class: Activity = | | = Class: Activity = |
Line 53: |
Line 59: |
| self._top_canvas_box = gtk.VBox() | | self._top_canvas_box = gtk.VBox() |
| self.set_canvas(self._top_canvas_box) | | self.set_canvas(self._top_canvas_box) |
| + | |
| + | === How do I ensure the canvas is of maximum size and how do I get the size the canvas? === |
| | | |
| === What are activity id's? How do I obtain the activity id for an instance of my activity? === | | === What are activity id's? How do I obtain the activity id for an instance of my activity? === |
Line 142: |
Line 150: |
| </pre> | | </pre> |
| | | |
− | = Class: ActivityToolbox ([[Sugar.graphics.toolbox|Toolbox]])= | + | === How do I run a specific block of code (eg. for cleanup) when my activity is closed? === |
| + | |
| + | The activity.Activity class has a can_close() method that you should override in any of your own activities where you want to run some specific code when an activity is closed. |
| + | |
| + | <pre> |
| + | from sugar.activity import activity |
| + | ... |
| + | class AnnotateActivity(activity.Activity): |
| + | def __init__(self, handle): |
| + | # Put activity initialization code in here |
| + | ... |
| + | |
| + | #Override activity.Activity's can_close method |
| + | def can_close(self): |
| + | ... |
| + | # PUT CLOSING CODE HERE |
| + | ... |
| + | |
| + | #Return True at the end to ensure activity does close properly |
| + | return True |
| + | </pre> |
| + | |
| + | It is important to note that the can_close method will only be called when the activity is cleanly closed using the "Stop" button on the Activity Toolbar. In cases where an activity is terminated using a kill signal or through some other OS-driven method, then the can_close() method will not necessarily be called. |
| + | |
| + | === How do I programmatically make my activity available for sharing? === |
| + | |
| + | Use the share() method that is included in the Activity class. This method takes a boolean argument that specifies whether the sharing should be private (only visible to those buddies that are invited to share) or public (visible to the entire neighborhood). |
| + | |
| + | The following code shows how to share the activity with all buddies in your neighborhood: |
| + | |
| + | <pre> |
| + | from sugar.activity import activity |
| + | ... |
| + | class AnnotateActivity(activity.Activity): |
| + | #### Method: __init__, initialize this AnnotateActivity instance |
| + | def __init__(self, handle): |
| + | ... |
| + | self.share(private=False) |
| + | ... |
| + | </pre> |
| + | |
| + | If you wanted to just share the activity with a particular buddy, then first you would need to have access to your buddy's key and then you can send him a personal invite (without inviting everyone else in your neighborhood). |
| + | |
| + | The code below shows how personal invites can be achieved. First, the _populate_buddy_box gives UI controls for inviting specific buddies through the activation of toggle buttons. Each toggle button corresponds to a specific buddy and is connected to a callback (_buddy_toggled_cb) which then personally invites a buddy. Notice that we pass buddy.get_property("key") to our callback. This is needed because personal invites require a buddy key (as used in sugar.presence.buddy) in order to identify who you are inviting. |
| + | |
| + | <pre> |
| + | #### Method: _populate_buddy_box, which updates the list of buddies available for this |
| + | # XO and then updates the _buddy_box UI widget accordingly. |
| + | def _populate_buddy_box(self): |
| + | buddies = self._ps.get_buddies() |
| + | self._buddy_box = gtk.VBox() |
| + | for buddy in buddies: |
| + | tb = gtk.ToggleButton(buddy.get_property("nick")) |
| + | tb.connect("toggled", self._buddy_toggled_cb, tb, buddy.get_property("key")) |
| + | self._buddy_box.pack_start(tb, expand=False, fill=False, padding=10) |
| + | |
| + | |
| + | #### Method: _buddy_toggled_cb, which is called when a togglebutton for a buddy |
| + | # has its state changed (indicating user wants a specific buddy to join or exit |
| + | # the conversation). |
| + | def _buddy_toggled_cb(self, widget, tb, buddy_key): |
| + | if tb.get_active(): |
| + | self.invite(buddy_key) |
| + | </pre> |
| + | |
| + | === How do I know if my activity is being started from the Journal? === |
| + | |
| + | You can check handle.object_id in the constructor of your Activity class: |
| + | |
| + | <pre> |
| + | from sugar.activity import activity |
| + | ... |
| + | class AnnotateActivity(activity.Activity): |
| + | #### Method: __init__, initialize this AnnotateActivity instance |
| + | def __init__(self, handle): |
| + | ... |
| + | if handle.object_id is None: |
| + | print "Activity is started anew (from the home view)" |
| + | else: |
| + | print "Activity is started from the journal and the object id is %s" % handle.object_id |
| + | ... |
| + | </pre> |
| + | |
| + | = Class: ActivityToolbox ([[Development Team/Almanac/sugar.graphics.toolbox|Toolbox]])= |
| | | |
| === What is the standard toolbox needed in most activities and how do I create it? === | | === What is the standard toolbox needed in most activities and how do I create it? === |