Changes

m
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 84: Line 92:  
Most basic activities can be frozen in the datastore by implementing the write_file() method within the main activity class. This method is then called by sugar to save the activity when it is closed or when user or program commands request that the activity be saved to the datastore. Once saved, the activity can be accessed and reloaded from the journal.  
 
Most basic activities can be frozen in the datastore by implementing the write_file() method within the main activity class. This method is then called by sugar to save the activity when it is closed or when user or program commands request that the activity be saved to the datastore. Once saved, the activity can be accessed and reloaded from the journal.  
   −
The following simple write_file() method shows how both metadata and files are written. Currently, write_file() will throw an error unless somewhere you actually write an actual file to the file_path that is passed to write_file. The code below writes a dummy file within the body of write_file itself (you can do this elsewhere as long as you have a handle on the file_path variable used by write_file).  
+
The following simple write_file() method shows how both metadata and files are written. write_file() must point to a created or existing journal entry.
 +
 
 +
# Your program can explicitly create a file in write_file() and write to it. Then, the journal entry can be created from scratch.
 +
# You can download or save a file to a particular location and then change the file path for yoru datastore object to point to this file.
 +
# If your Activity is always resumed from an existing Journal entry using the Journal entry's MIME type you can use the write_file() method to save metadata without actually writing a file.  An example of this is the Read activity, which is always resumed from a Journal entry that contains a PDF.  It uses write_file() to save metadata like the page the user was reading, the zoom level of the text, etc. but does not need to write the PDF it is using back to the journal.  The existing PDF is preserved.  
 +
 
 +
The code below creates a small text file (with "hello world" written in to it) within the body of write_file itself.  
    
   class AnnotateActivity(activity.Activity):
 
   class AnnotateActivity(activity.Activity):
Line 122: Line 136:  
         #call any code that will actually initialize the activity with  
 
         #call any code that will actually initialize the activity with  
 
         #data that is read from datastore
 
         #data that is read from datastore
         self._create_ui()
+
         self._create_ui(data)
 
          
 
          
   Line 136: 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? ===