From 645aa93e50288547dda88c156795839178426df9 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Fri, 15 Sep 2006 12:40:22 +0200 Subject: [PATCH] Split shell in model/view, cleanup things a lot --- shell/ActivityHost.py | 3 + shell/FriendIcon.py | 8 +-- shell/Owner.py | 10 ---- shell/Session.py | 8 +-- shell/Shell.py | 116 ++++--------------------------------- shell/frame/BottomPanel.py | 18 +++--- shell/frame/Frame.py | 8 ++- shell/frame/RightPanel.py | 12 ++-- shell/frame/TopPanel.py | 3 +- shell/home/FriendsGroup.py | 10 ++-- shell/home/HomeGroup.py | 17 +++--- shell/home/HomeWindow.py | 13 ++--- shell/home/MeshGroup.py | 3 +- 13 files changed, 63 insertions(+), 166 deletions(-) diff --git a/shell/ActivityHost.py b/shell/ActivityHost.py index b3703e7a..456406bc 100644 --- a/shell/ActivityHost.py +++ b/shell/ActivityHost.py @@ -31,6 +31,9 @@ class ActivityHost: def get_id(self): return self._id + def get_xid(self): + return self._xid + def get_icon_name(self): return self._icon_name diff --git a/shell/FriendIcon.py b/shell/FriendIcon.py index 0c6b1e67..49eecfbb 100644 --- a/shell/FriendIcon.py +++ b/shell/FriendIcon.py @@ -14,11 +14,11 @@ class _PopupShell: class FriendIcon(IconItem): _popup_shell = _PopupShell() - def __init__(self, shell, friend): + def __init__(self, shell_model, friend): IconItem.__init__(self, icon_name='stock-buddy', color=friend.get_color(), size=96) - self._shell = shell + self._shell_model = shell_model self._friend = friend self._popup = None self._popup_distance = 0 @@ -83,10 +83,10 @@ class FriendIcon(IconItem): return if action == FriendPopup.ACTION_INVITE: - activity = self._shell.get_current_activity() + activity = self._shell_model.get_current_activity() activity.invite(buddy) elif action == FriendPopup.ACTION_MAKE_FRIEND: - friends = self._shell.get_owner().get_friends() + friends = self._shell_model.get_friends() friends.add_buddy(buddy) def _popdown_cb(self, friend): diff --git a/shell/Owner.py b/shell/Owner.py index b6590589..7dec04a7 100644 --- a/shell/Owner.py +++ b/shell/Owner.py @@ -6,8 +6,6 @@ import conf from sugar import env from sugar.p2p import Stream from sugar.presence import PresenceService -from Friends import Friends -from Invites import Invites PRESENCE_SERVICE_TYPE = "_presence_olpc._tcp" @@ -31,14 +29,6 @@ class ShellOwner(object): break self._pservice = PresenceService.get_instance() - self._friends = Friends() - self._invites = Invites() - - def get_friends(self): - return self._friends - - def get_invites(self): - return self._invites def announce(self): # Create and announce our presence diff --git a/shell/Session.py b/shell/Session.py index 07337d68..0bf3fa4f 100644 --- a/shell/Session.py +++ b/shell/Session.py @@ -2,6 +2,7 @@ import os import gtk from Shell import Shell +from ShellModel import ShellModel from ConsoleWindow import ConsoleWindow from sugar import env from sugar import logger @@ -47,11 +48,8 @@ class Session: dbm = DBusMonitorProcess() dbm.start() - console = ConsoleWindow() - logger.start('Shell', console) - - shell = Shell() - shell.set_console(console) + model = ShellModel() + shell = Shell(model) from sugar import TracebackUtils tbh = TracebackUtils.TracebackHelper() diff --git a/shell/Shell.py b/shell/Shell.py index 92baaa23..30a79576 100755 --- a/shell/Shell.py +++ b/shell/Shell.py @@ -1,51 +1,19 @@ -import os -import logging - -import dbus -import dbus.glib import gtk import gobject import wnck from home.HomeWindow import HomeWindow -from Owner import ShellOwner -from sugar.presence import PresenceService from ActivityHost import ActivityHost -from sugar.activity import ActivityFactory -from sugar.activity import Activity from frame.Frame import Frame from globalkeys import KeyGrabber -import conf import sugar -class ShellDbusService(dbus.service.Object): - def __init__(self, shell, bus_name): - dbus.service.Object.__init__(self, bus_name, '/com/redhat/Sugar/Shell') - self._shell = shell - - def __show_console_idle(self): - self._shell.show_console() - - @dbus.service.method('com.redhat.Sugar.Shell') - def show_console(self): - gobject.idle_add(self.__show_console_idle) - class Shell(gobject.GObject): - __gsignals__ = { - 'activity-opened': (gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])), - 'activity-changed': (gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])), - 'activity-closed': (gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])) - } - - def __init__(self): + def __init__(self, model): gobject.GObject.__init__(self) + self._model = model self._screen = wnck.screen_get_default() - self._hosts = {} - self._current_window = None self._key_grabber = KeyGrabber() self._key_grabber.connect('key-pressed', self.__global_key_pressed_cb) @@ -56,7 +24,7 @@ class Shell(gobject.GObject): self._key_grabber.grab('F5') self._key_grabber.grab('F6') - self._home_window = HomeWindow(self) + self._home_window = HomeWindow(self.get_model()) self._home_window.show() self.set_zoom_level(sugar.ZOOM_HOME) @@ -65,24 +33,9 @@ class Shell(gobject.GObject): self._screen.connect('active-window-changed', self.__active_window_changed_cb) - session_bus = dbus.SessionBus() - bus_name = dbus.service.BusName('com.redhat.Sugar.Shell', bus=session_bus) - ShellDbusService(self, bus_name) - - PresenceService.start() - self._pservice = PresenceService.get_instance() - - self._owner = ShellOwner() - self._owner.announce() - - self._home_window.set_owner(self._owner) - - self._frame = Frame(self, self._owner) + self._frame = Frame(self) self._frame.show_and_hide(10) - def get_owner(self): - return self._owner - def __global_key_pressed_cb(self, grabber, key): if key == 'F1': self.set_zoom_level(sugar.ZOOM_ACTIVITY) @@ -95,72 +48,23 @@ class Shell(gobject.GObject): elif key == 'F5': self._frame.toggle_visibility() elif key == 'F6': - ActivityFactory.create('org.sugar.Terminal') - - def set_console(self, console): - self._console = console + self._model.start_activity('org.sugar.Terminal') def __window_opened_cb(self, screen, window): if window.get_window_type() == wnck.WINDOW_NORMAL: - host = ActivityHost(self, window) - self._hosts[window.get_xid()] = host - self.emit('activity-opened', host) + self._model.add_activity(ActivityHost(self, window)) def __active_window_changed_cb(self, screen): window = screen.get_active_window() if window and window.get_window_type() == wnck.WINDOW_NORMAL: - if self._current_window != window: - self._current_window = window - self.emit('activity-changed', self.get_current_activity()) + self._model.set_current_activity(window.get_xid()) def __window_closed_cb(self, screen, window): if window.get_window_type() == wnck.WINDOW_NORMAL: - xid = window.get_xid() - if self._hosts.has_key(xid): - host = self._hosts[xid] - self.emit('activity-closed', host) + self._model.remove_activity(window.get_xid()) - del self._hosts[xid] - - def get_activity(self, activity_id): - for host in self._hosts.values(): - if host.get_id() == activity_id: - return host - return None - - def get_current_activity(self): - if self._current_window != None: - xid = self._current_window.get_xid() - return self._hosts[xid] - else: - return None - - def show_console(self): - self._console.show() - - activity = self.get_current_activity() - if activity: - registry = conf.get_activity_registry() - module = registry.get_activity(activity.get_type()) - self._console.set_page(module.get_id()) - - def join_activity(self, bundle_id, activity_id): - activity = self.get_activity(activity_id) - if activity: - activity.present() - else: - activity_ps = self._pservice.get_activity(activity_id) - - if activity_ps: - activity = ActivityFactory.create(bundle_id) - activity.join(activity_ps.object_path()) - else: - logging.error('Cannot start activity.') - - def start_activity(self, activity_type): - activity = ActivityFactory.create(activity_type) - activity.execute('test', []) - return activity + def get_model(self): + return self._model def set_zoom_level(self, level): if level == sugar.ZOOM_ACTIVITY: diff --git a/shell/frame/BottomPanel.py b/shell/frame/BottomPanel.py index 2da51e77..d43160db 100644 --- a/shell/frame/BottomPanel.py +++ b/shell/frame/BottomPanel.py @@ -33,30 +33,30 @@ class InviteItem(IconItem): return self._invite class BottomPanel(CanvasBox): - def __init__(self, grid, shell, invites): + def __init__(self, grid, shell_model): CanvasBox.__init__(self, grid, CanvasBox.HORIZONTAL, 1) - self._shell = shell + self._shell_model = shell_model self._invite_to_item = {} - self._invites = invites + self._invites = shell_model.get_invites() registry = conf.get_activity_registry() for activity in registry.list_activities(): if activity.get_show_launcher(): self.add_activity(activity) - for invite in invites: + for invite in self._invites: self.add_invite(invite) - invites.connect('invite-added', self.__invite_added_cb) - invites.connect('invite-removed', self.__invite_removed_cb) + self._invites.connect('invite-added', self.__invite_added_cb) + self._invites.connect('invite-removed', self.__invite_removed_cb) def __activity_clicked_cb(self, icon): - self._shell.start_activity(icon.get_bundle_id()) + self._shell_model.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()) + self._shell_model.join_activity(icon.get_bundle_id(), + icon.get_activity_id()) def __invite_added_cb(self, invites, invite): self.add_invite(invite) diff --git a/shell/frame/Frame.py b/shell/frame/Frame.py index 1da50f0e..826295f1 100644 --- a/shell/frame/Frame.py +++ b/shell/frame/Frame.py @@ -9,9 +9,11 @@ from frame.PanelWindow import PanelWindow from sugar.canvas.Grid import Grid class Frame: - def __init__(self, shell, owner): + def __init__(self, shell): self._windows = [] + shell_model = shell.get_model() + model = goocanvas.CanvasModelSimple() root = model.get_root_item() @@ -21,7 +23,7 @@ class Frame: grid.set_constraints(bg, 0, 0, 80, 60) root.add_child(bg) - panel = BottomPanel(grid, shell, owner.get_invites()) + panel = BottomPanel(grid, shell_model) grid.set_constraints(panel, 5, 55) root.add_child(panel) @@ -34,7 +36,7 @@ class Frame: panel_window = PanelWindow(grid, model, 0, 0, 80, 5) self._windows.append(panel_window) - panel = RightPanel(grid, shell, owner.get_friends()) + panel = RightPanel(grid, shell_model) grid.set_constraints(panel, 75, 5) root.add_child(panel) diff --git a/shell/frame/RightPanel.py b/shell/frame/RightPanel.py index be55abf2..1bc27f5c 100644 --- a/shell/frame/RightPanel.py +++ b/shell/frame/RightPanel.py @@ -8,10 +8,10 @@ from FriendIcon import FriendIcon from Friends import Friend class RightPanel(CanvasBox): - def __init__(self, grid, shell, friends): + def __init__(self, grid, shell_model): CanvasBox.__init__(self, grid, CanvasBox.VERTICAL, 1) - self._shell = shell - self._friends = friends + self._shell_model = shell_model + self._friends = shell_model.get_friends() self._activity_ps = None self._joined_hid = -1 self._left_hid = -1 @@ -21,11 +21,11 @@ class RightPanel(CanvasBox): self._pservice.connect('activity-appeared', self.__activity_appeared_cb) - shell.connect('activity-changed', self.__activity_changed_cb) + shell_model.connect('activity-changed', self.__activity_changed_cb) def add(self, buddy): friend = Friend(buddy.get_name(), buddy.get_color()) - icon = FriendIcon(self._shell, friend) + icon = FriendIcon(self._shell_model, friend) icon.set_popup_distance(1) self.set_constraints(icon, 3, 3) self.add_child(icon) @@ -42,7 +42,7 @@ class RightPanel(CanvasBox): self._buddies = {} def __activity_appeared_cb(self, pservice, activity_ps): - activity = self._shell.get_current_activity() + activity = self._shell_model.get_current_activity() if activity and activity_ps.get_id() == activity.get_id(): self._set_activity_ps(activity_ps) diff --git a/shell/frame/TopPanel.py b/shell/frame/TopPanel.py index 14c0fb7c..1409d854 100644 --- a/shell/frame/TopPanel.py +++ b/shell/frame/TopPanel.py @@ -58,7 +58,8 @@ class TopPanel(goocanvas.Group): self._shell.set_zoom_level(level) def __share_clicked_cb(self, item): - activity = self._shell.get_current_activity() + shell_model = self._shell.get_model() + activity = shell_model.get_current_activity() if activity != None: activity.share() diff --git a/shell/home/FriendsGroup.py b/shell/home/FriendsGroup.py index ed87015b..adb3de8d 100644 --- a/shell/home/FriendsGroup.py +++ b/shell/home/FriendsGroup.py @@ -7,12 +7,12 @@ from home.MyIcon import MyIcon from FriendIcon import FriendIcon class FriendsGroup(goocanvas.Group): - def __init__(self, shell, friends): + def __init__(self, shell_model): goocanvas.Group.__init__(self) - self._shell = shell + self._shell_model = shell_model self._icon_layout = IconLayout(1200, 900) - self._friends = friends + self._friends = shell_model.get_friends() me = MyIcon(100) me.translate(600 - (me.get_property('size') / 2), @@ -22,10 +22,10 @@ class FriendsGroup(goocanvas.Group): for friend in self._friends: self.add_friend(friend) - friends.connect('friend-added', self._friend_added_cb) + self._friends.connect('friend-added', self._friend_added_cb) def add_friend(self, friend): - icon = FriendIcon(self._shell, friend) + icon = FriendIcon(self._shell_model, friend) self.add_child(icon) self._icon_layout.add_icon(icon) diff --git a/shell/home/HomeGroup.py b/shell/home/HomeGroup.py index 0f088132..3834bda6 100644 --- a/shell/home/HomeGroup.py +++ b/shell/home/HomeGroup.py @@ -4,19 +4,19 @@ from home.DonutItem import DonutItem from home.MyIcon import MyIcon class TasksItem(DonutItem): - def __init__(self, shell): + def __init__(self, shell_model): DonutItem.__init__(self, 250) self._items = {} - self._shell = shell - self._shell.connect('activity_opened', self.__activity_opened_cb) - self._shell.connect('activity_closed', self.__activity_closed_cb) + self._shell_model = shell_model + self._shell_model.connect('activity_opened', self.__activity_opened_cb) + self._shell_model.connect('activity_closed', self.__activity_closed_cb) - def __activity_opened_cb(self, shell, activity): + def __activity_opened_cb(self, model, activity): self._add(activity) - def __activity_closed_cb(self, shell, activity): + def __activity_closed_cb(self, model, activity): self._remove(activity) def _remove(self, activity): @@ -27,6 +27,7 @@ class TasksItem(DonutItem): def _add(self, activity): icon_name = activity.get_icon_name() icon_color = activity.get_icon_color() + print 'Add activity %s' % icon_color.to_string() item = self.add_piece(100 / 8, icon_name, icon_color) item.get_icon().connect('clicked', @@ -39,10 +40,10 @@ class TasksItem(DonutItem): activity.present() class HomeGroup(goocanvas.Group): - def __init__(self, shell): + def __init__(self, shell_model): goocanvas.Group.__init__(self) - tasks = TasksItem(shell) + tasks = TasksItem(shell_model) tasks.translate(600, 450) self.add_child(tasks) diff --git a/shell/home/HomeWindow.py b/shell/home/HomeWindow.py index 259ddc4f..3c895fab 100644 --- a/shell/home/HomeWindow.py +++ b/shell/home/HomeWindow.py @@ -9,9 +9,9 @@ from home.FriendsGroup import FriendsGroup import sugar class HomeWindow(gtk.Window): - def __init__(self, shell): + def __init__(self, shell_model): gtk.Window.__init__(self) - self._shell = shell + self._shell_model = shell_model self.realize() self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DESKTOP) @@ -23,6 +23,10 @@ class HomeWindow(gtk.Window): self.add(self._nb) self._nb.show() + self._add_page(HomeGroup(shell_model)) + self._add_page(FriendsGroup(shell_model)) + self._add_page(MeshGroup()) + def _add_page(self, group): view = CanvasView() self._nb.append_page(view) @@ -37,11 +41,6 @@ class HomeWindow(gtk.Window): root.add_child(bg) root.add_child(group) - def set_owner(self, owner): - self._add_page(HomeGroup(self._shell)) - self._add_page(FriendsGroup(self._shell, owner.get_friends())) - self._add_page(MeshGroup(self._shell)) - def set_zoom_level(self, level): if level == sugar.ZOOM_HOME: self._nb.set_current_page(0) diff --git a/shell/home/MeshGroup.py b/shell/home/MeshGroup.py index f7bc1c99..3247b734 100644 --- a/shell/home/MeshGroup.py +++ b/shell/home/MeshGroup.py @@ -32,9 +32,8 @@ class ActivityItem(IconItem): return self._service class MeshGroup(goocanvas.Group): - def __init__(self, shell): + def __init__(self): goocanvas.Group.__init__(self) - self._shell = shell self._icon_layout = IconLayout(1200, 900) self._activities = {}