[Sugar-devel] [PATCH sugar] Example objects support
Martin Abente Lahaye
martin.abente.lahaye at gmail.com
Tue Aug 21 09:22:07 EDT 2012
Activities can include example objects in their
bundles.
No code is required for activity developers. All
they need to do is to include an examples folder
in the activity root directory.
I.E: ../activities/TurtleArt.activity/examples/
Whenever an activity with examples is opened, its
examples folder will be accessible through journal
and any objectchooser.
Only one folder will be shown per activity at the
same time.
Signed-off-by: Martin Abente Lahaye <tch at sugarlabs.org>
---
src/jarabe/journal/volumestoolbar.py | 86 ++++++++++++++++++++++++++++++++++++
1 file changed, 86 insertions(+)
diff --git a/src/jarabe/journal/volumestoolbar.py b/src/jarabe/journal/volumestoolbar.py
index 71b6ea8..58f5cde 100644
--- a/src/jarabe/journal/volumestoolbar.py
+++ b/src/jarabe/journal/volumestoolbar.py
@@ -30,11 +30,13 @@ import simplejson
import tempfile
import shutil
+from sugar.graphics.icon import Icon
from sugar.graphics.radiotoolbutton import RadioToolButton
from sugar.graphics.palette import Palette
from sugar.graphics.xocolor import XoColor
from sugar import env
+from jarabe.model import shell
from jarabe.journal import model
from jarabe.view.palettes import VolumePalette
@@ -174,6 +176,9 @@ class VolumesToolbar(gtk.Toolbar):
self._mount_added_hid = None
self._mount_removed_hid = None
+ self._activity_added_hid = None
+ self._activity_removed_hid = None
+
button = JournalButton()
button.connect('toggled', self._button_toggled_cb)
self.insert(button, 0)
@@ -191,6 +196,7 @@ class VolumesToolbar(gtk.Toolbar):
def _set_up_volumes(self):
self._set_up_documents_button()
+ self._set_up_activities_examples()
volume_monitor = gio.volume_monitor_get()
self._mount_added_hid = volume_monitor.connect('mount-added',
@@ -216,6 +222,75 @@ class VolumesToolbar(gtk.Toolbar):
self._volume_buttons.append(button)
self.show()
+ def _set_up_activities_examples(self):
+ home_model = shell.get_model()
+
+ self._activity_added_hid = home_model.connect('activity-added',
+ self.__activity_added_cb)
+ self._activity_removed_hid = home_model.connect('activity-removed',
+ self.__activity_removed_cb)
+
+ activities = []
+ for home_activity in home_model._activities:
+ if home_activity.is_journal():
+ continue
+
+ activity_name = home_activity.get_activity_name()
+ if activity_name not in activities:
+ self._add_activity_example(home_activity)
+ activities.append(activity_name)
+
+ def _proper_occurrence(self, home_activity):
+ if home_activity.is_journal():
+ return False
+
+ home_model = shell.get_model()
+ act_name = home_activity.get_activity_name()
+ activities = filter(lambda act: act_name == act.get_activity_name(),
+ home_model._activities)
+
+ return len(activities) <= 1
+
+ def __activity_added_cb(self, home_model, home_activity):
+ if self._proper_occurrence(home_activity):
+ self._add_activity_example(home_activity)
+
+ def _add_activity_example(self, home_activity):
+ examples_path = os.path.join(home_activity.get_bundle_path(),
+ 'examples')
+
+ if not os.path.exists(examples_path):
+ return
+
+ button = ExamplesButton(examples_path, home_activity.get_icon_path(),
+ home_activity.get_icon_color())
+ button.props.group = self._volume_buttons[0]
+ label = home_activity.get_activity_name()
+ button.set_palette(Palette(label))
+ button.connect('toggled', self._button_toggled_cb)
+ button.show()
+
+ position = self.get_item_index(self._volume_buttons[-1]) + 1
+ self.insert(button, position)
+ self._volume_buttons.append(button)
+ self.show()
+
+ def __activity_removed_cb(self, shell_model, home_activity):
+ if not self._proper_occurrence(home_activity):
+ return
+
+ examples_path = os.path.join(home_activity.get_bundle_path(),
+ 'examples')
+ activity_button = None
+ for button in self.get_children():
+ if button.mount_point == examples_path:
+ activity_button = button
+ break
+
+ self._volume_buttons.remove(activity_button)
+ self.remove(activity_button)
+ self.get_children()[0].props.active = True
+
def __mount_added_cb(self, volume_monitor, mount):
self._add_button(mount)
@@ -402,3 +477,14 @@ class DocumentsButton(BaseButton):
client = gconf.client_get_default()
color = XoColor(client.get_string('/desktop/sugar/user/color'))
self.props.xo_color = color
+
+class ExamplesButton(BaseButton):
+
+ def __init__(self, examples_path, icon_path, icon_color):
+ BaseButton.__init__(self, mount_point=examples_path)
+
+ icon = Icon(file=icon_path,
+ xo_color=icon_color,
+ icon_size=gtk.ICON_SIZE_LARGE_TOOLBAR)
+ self.set_icon_widget(icon)
+ icon.show()
--
1.7.11.2
More information about the Sugar-devel
mailing list