2007-06-24 14:45:05 +02:00
|
|
|
# Copyright (C) 2006-2007 Red Hat, Inc.
|
2006-10-15 01:24:45 +02:00
|
|
|
#
|
|
|
|
# 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-10-02 01:50:43 +02:00
|
|
|
import hippo
|
2006-09-04 21:34:54 +02:00
|
|
|
import logging
|
2006-08-23 11:52:18 +02:00
|
|
|
|
2007-08-31 13:43:31 +02:00
|
|
|
from sugar.graphics.tray import TrayButton
|
2007-08-28 14:15:51 +02:00
|
|
|
from sugar.graphics.tray import HTray
|
2007-08-31 13:43:31 +02:00
|
|
|
from sugar.graphics.icon import Icon
|
2007-08-09 00:19:05 +02:00
|
|
|
from sugar.graphics import style
|
2007-01-09 21:25:51 +01:00
|
|
|
from sugar import profile
|
2007-08-09 18:10:16 +02:00
|
|
|
from sugar import activity
|
2007-09-22 03:37:31 +02:00
|
|
|
from sugar import env
|
2006-08-23 11:52:18 +02:00
|
|
|
|
2007-08-28 14:15:51 +02:00
|
|
|
from activitybutton import ActivityButton
|
2006-08-23 11:52:18 +02:00
|
|
|
|
2007-08-31 13:43:31 +02:00
|
|
|
class InviteButton(TrayButton):
|
2007-08-09 18:10:16 +02:00
|
|
|
def __init__(self, activity_model, invite):
|
2007-08-31 13:43:31 +02:00
|
|
|
TrayButton.__init__(self)
|
|
|
|
|
|
|
|
icon = Icon(file=activity_model.get_icon_name(),
|
|
|
|
xo_color=activity_model.get_color())
|
|
|
|
self.set_icon_widget(icon)
|
|
|
|
icon.show()
|
2006-10-23 21:28:19 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._invite = invite
|
2006-09-04 17:00:45 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def get_activity_id(self):
|
|
|
|
return self._invite.get_activity_id()
|
2006-09-04 17:00:45 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def get_bundle_id(self):
|
|
|
|
return self._invite.get_bundle_id()
|
2006-09-04 17:00:45 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def get_invite(self):
|
|
|
|
return self._invite
|
2006-09-09 14:37:11 +02:00
|
|
|
|
2007-09-03 23:28:49 +02:00
|
|
|
class ActivitiesTray(hippo.CanvasBox):
|
2007-07-01 12:55:10 +02:00
|
|
|
def __init__(self, shell):
|
2006-12-04 20:12:24 +01:00
|
|
|
hippo.CanvasBox.__init__(self, orientation=hippo.ORIENTATION_HORIZONTAL)
|
|
|
|
|
|
|
|
self._shell = shell
|
|
|
|
self._shell_model = self._shell.get_model()
|
|
|
|
self._invite_to_item = {}
|
|
|
|
self._invites = self._shell_model.get_invites()
|
2007-09-22 03:37:31 +02:00
|
|
|
self._config = self._load_config()
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-08-31 13:43:31 +02:00
|
|
|
self._tray = HTray()
|
|
|
|
self.append(hippo.CanvasWidget(widget=self._tray), hippo.PACK_EXPAND)
|
|
|
|
self._tray.show()
|
2007-08-28 14:15:51 +02:00
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
registry = activity.get_registry()
|
2007-08-27 21:47:58 +02:00
|
|
|
registry.get_activities_async(reply_handler=self._get_activities_cb)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
registry.connect('activity-added', self._activity_added_cb)
|
2007-10-04 21:59:23 +02:00
|
|
|
registry.connect('activity-removed', self._activity_removed_cb)
|
2007-01-27 12:54:56 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
for invite in self._invites:
|
|
|
|
self.add_invite(invite)
|
|
|
|
self._invites.connect('invite-added', self._invite_added_cb)
|
|
|
|
self._invites.connect('invite-removed', self._invite_removed_cb)
|
|
|
|
|
2007-09-22 03:37:31 +02:00
|
|
|
def _load_config(self):
|
|
|
|
config = []
|
|
|
|
|
|
|
|
f = open(env.get_data_path('activities.defaults'), 'r')
|
|
|
|
for line in f.readlines():
|
|
|
|
line = line.strip()
|
|
|
|
if line and not line.startswith('#'):
|
|
|
|
config.append(line)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
2007-08-27 21:47:58 +02:00
|
|
|
def _get_activities_cb(self, activity_list):
|
2007-09-22 03:37:31 +02:00
|
|
|
known_activities = []
|
|
|
|
unknown_activities = []
|
|
|
|
name_to_activity = {}
|
|
|
|
|
|
|
|
while activity_list:
|
|
|
|
info = activity_list.pop()
|
2007-10-09 13:15:06 +02:00
|
|
|
name_to_activity[info.bundle_id] = info
|
2007-09-22 03:37:31 +02:00
|
|
|
|
2007-10-09 13:15:06 +02:00
|
|
|
if info.bundle_id in self._config:
|
2007-09-22 03:37:31 +02:00
|
|
|
known_activities.append(info)
|
|
|
|
else:
|
|
|
|
unknown_activities.append(info)
|
|
|
|
|
|
|
|
sorted_activities = []
|
|
|
|
for name in self._config:
|
|
|
|
if name in name_to_activity:
|
|
|
|
sorted_activities.append(name_to_activity[name])
|
|
|
|
|
|
|
|
for info in sorted_activities + unknown_activities:
|
|
|
|
if info.show_launcher:
|
|
|
|
self.add_activity(info)
|
2007-08-27 21:47:58 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def _activity_clicked_cb(self, icon):
|
|
|
|
self._shell.start_activity(icon.get_bundle_id())
|
|
|
|
|
|
|
|
def _invite_clicked_cb(self, icon):
|
|
|
|
self._invites.remove_invite(icon.get_invite())
|
|
|
|
self._shell.join_activity(icon.get_bundle_id(),
|
|
|
|
icon.get_activity_id())
|
|
|
|
|
|
|
|
def _invite_added_cb(self, invites, invite):
|
|
|
|
self.add_invite(invite)
|
|
|
|
|
|
|
|
def _invite_removed_cb(self, invites, invite):
|
|
|
|
self.remove_invite(invite)
|
|
|
|
|
2007-10-04 21:59:23 +02:00
|
|
|
def _remove_activity_cb(self, item):
|
2007-08-31 13:43:31 +02:00
|
|
|
self._tray.remove_item(item)
|
2007-08-28 14:15:51 +02:00
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
def _activity_added_cb(self, activity_registry, activity_info):
|
|
|
|
self.add_activity(activity_info)
|
2007-01-27 12:54:56 +01:00
|
|
|
|
2007-10-04 21:59:23 +02:00
|
|
|
def _activity_removed_cb(self, activity_registry, activity_info):
|
|
|
|
for item in self._tray.get_children():
|
2007-12-18 13:43:34 +01:00
|
|
|
if item.get_bundle_id() == activity_info.bundle_id:
|
2007-10-04 21:59:23 +02:00
|
|
|
self._tray.remove_item(item)
|
|
|
|
return
|
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
def add_activity(self, activity_info):
|
|
|
|
item = ActivityButton(activity_info)
|
2007-08-28 14:15:51 +02:00
|
|
|
item.connect('clicked', self._activity_clicked_cb)
|
2007-10-04 21:59:23 +02:00
|
|
|
item.connect('remove_activity', self._remove_activity_cb)
|
2007-08-31 13:43:31 +02:00
|
|
|
self._tray.add_item(item, -1)
|
2007-08-28 14:15:51 +02:00
|
|
|
item.show()
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def add_invite(self, invite):
|
|
|
|
mesh = self._shell_model.get_mesh()
|
2007-08-09 18:10:16 +02:00
|
|
|
activity_model = mesh.get_activity(invite.get_activity_id())
|
2006-12-04 20:12:24 +01:00
|
|
|
if activity:
|
2007-08-09 18:10:16 +02:00
|
|
|
item = InviteButton(activity_model, invite)
|
2007-08-31 13:43:31 +02:00
|
|
|
item.connect('clicked', self._invite_clicked_cb)
|
|
|
|
self._tray.add_item(item, 0)
|
|
|
|
item.show()
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
self._invite_to_item[invite] = item
|
|
|
|
|
|
|
|
def remove_invite(self, invite):
|
2007-08-31 13:43:31 +02:00
|
|
|
self._tray.remove_item(self._invite_to_item[invite])
|
2006-12-04 20:12:24 +01:00
|
|
|
del self._invite_to_item[invite]
|