Change Activity so that subclasses don't have to call Activity superclass methods from the shell callbacks. Makes subclasses simpler since they don't (necessarily) have to care about OO concepts like superclasses right away.

This commit is contained in:
Dan Williams 2006-06-05 12:35:00 -04:00
parent 93333ed5cf
commit 046a954de1
5 changed files with 81 additions and 26 deletions

View File

@ -13,8 +13,6 @@ class GMailActivity(activity.Activity):
activity.Activity.__init__(self) activity.Activity.__init__(self)
def on_connected_to_shell(self): def on_connected_to_shell(self):
activity.Activity.on_connected_to_shell(self)
profile_path = os.path.join(sugar.env.get_user_dir(), 'gmail') profile_path = os.path.join(sugar.env.get_user_dir(), 'gmail')
geckoembed.set_profile_path(profile_path) geckoembed.set_profile_path(profile_path)
self.set_tab_text("Mail") self.set_tab_text("Mail")
@ -32,7 +30,6 @@ class GMailActivity(activity.Activity):
embed.load_address("http://www.gmail.com") embed.load_address("http://www.gmail.com")
def on_disconnected_from_shell(self): def on_disconnected_from_shell(self):
activity.Activity.on_disconnected_from_shell()
gtk.main_quit() gtk.main_quit()
activity = GMailActivity() activity = GMailActivity()

View File

@ -53,8 +53,6 @@ class BrowserActivity(activity.Activity):
self._model.add_listener(self.__shared_location_changed_cb) self._model.add_listener(self.__shared_location_changed_cb)
def on_connected_to_shell(self): def on_connected_to_shell(self):
activity.Activity.on_connected_to_shell(self)
self.set_ellipsize_tab(True) self.set_ellipsize_tab(True)
self.set_can_close(True) self.set_can_close(True)
self.set_tab_text("Web Page") self.set_tab_text("Web Page")

View File

@ -25,8 +25,6 @@ class WebActivity(activity.Activity):
self._shell = shell self._shell = shell
def on_connected_to_shell(self): def on_connected_to_shell(self):
activity.Activity.on_connected_to_shell(self)
self.set_tab_text("Web") self.set_tab_text("Web")
self.set_tab_icon(name="web-browser") self.set_tab_icon(name="web-browser")
self.set_show_tab_icon(True) self.set_show_tab_icon(True)
@ -58,5 +56,4 @@ class WebActivity(activity.Activity):
return True return True
def on_disconnected_from_shell(self): def on_disconnected_from_shell(self):
activity.Activity.on_disconnected_from_shell(self)
gtk.main_quit() gtk.main_quit()

View File

@ -50,8 +50,6 @@ class Chat(activity.Activity):
self._browser_shell = dbus.Interface(proxy_obj, 'com.redhat.Sugar.BrowserShell') self._browser_shell = dbus.Interface(proxy_obj, 'com.redhat.Sugar.BrowserShell')
def on_connected_to_shell(self): def on_connected_to_shell(self):
activity.Activity.on_connected_to_shell(self)
self.set_tab_text(self._act_name) self.set_tab_text(self._act_name)
self._ui_setup(self._plug) self._ui_setup(self._plug)
self._plug.show_all() self._plug.show_all()

View File

@ -13,12 +13,23 @@ SHELL_SERVICE_PATH = "/com/redhat/Sugar/Shell"
ACTIVITY_SERVICE_NAME = "com.redhat.Sugar.Activity" ACTIVITY_SERVICE_NAME = "com.redhat.Sugar.Activity"
ACTIVITY_SERVICE_PATH = "/com/redhat/Sugar/Activity" ACTIVITY_SERVICE_PATH = "/com/redhat/Sugar/Activity"
ON_CONNECTED_TO_SHELL_CB = "connected_to_shell"
ON_DISCONNECTED_FROM_SHELL_CB = "disconnected_from_shell"
ON_RECONNECTED_TO_SHELL_CB = "reconnected_to_shell"
ON_CLOSE_FROM_USER_CB = "close_from_user"
ON_LOST_FOCUS_CB = "lost_focus"
ON_GOT_FOCUS_CB = "got_focus"
class ActivityDbusService(dbus.service.Object): class ActivityDbusService(dbus.service.Object):
"""Base dbus service object that each Activity uses to export dbus methods. """Base dbus service object that each Activity uses to export dbus methods.
The dbus service is separate from the actual Activity object so that we can The dbus service is separate from the actual Activity object so that we can
tightly control what stuff passes through the dbus python bindings.""" tightly control what stuff passes through the dbus python bindings."""
_ALLOWED_CALLBACKS = [ON_CONNECTED_TO_SHELL_CB, ON_DISCONNECTED_FROM_SHELL_CB, \
ON_RECONNECTED_TO_SHELL_CB, ON_CLOSE_FROM_USER_CB, ON_LOST_FOCUS_CB, \
ON_GOT_FOCUS_CB]
def __init__(self, activity): def __init__(self, activity):
self._activity = activity self._activity = activity
self._activity_id = None self._activity_id = None
@ -26,6 +37,9 @@ class ActivityDbusService(dbus.service.Object):
self._service = None self._service = None
self._bus = dbus.SessionBus() self._bus = dbus.SessionBus()
self._bus.add_signal_receiver(self.name_owner_changed, dbus_interface = "org.freedesktop.DBus", signal_name = "NameOwnerChanged") self._bus.add_signal_receiver(self.name_owner_changed, dbus_interface = "org.freedesktop.DBus", signal_name = "NameOwnerChanged")
self._callbacks = {}
for cb in self._ALLOWED_CALLBACKS:
self._callbacks[cb] = None
def __del__(self): def __del__(self):
if self._activity_id: if self._activity_id:
@ -36,6 +50,16 @@ class ActivityDbusService(dbus.service.Object):
self._bus.remove_signal_receiver(self.name_owner_changed, dbus_interface="org.freedesktop.DBus", signal_name="NameOwnerChanged") self._bus.remove_signal_receiver(self.name_owner_changed, dbus_interface="org.freedesktop.DBus", signal_name="NameOwnerChanged")
del self._bus del self._bus
def register_callback(self, name, callback):
if name not in self._ALLOWED_CALLBACKS:
print "ActivityDbusService: bad callback registration request for '%s'" % name
return
self._callbacks[name] = callback
def _call_callback(self, name, *args):
if name in self._ALLOWED_CALLBACKS and self._callbacks[name]:
self._callbacks[name](*args)
def connect_to_shell(self): def connect_to_shell(self):
"""Register with the shell via dbus, getting an activity ID and """Register with the shell via dbus, getting an activity ID and
and XEMBED window ID in which to display the Activity.""" and XEMBED window ID in which to display the Activity."""
@ -59,11 +83,12 @@ class ActivityDbusService(dbus.service.Object):
dbus.service.Object.__init__(self, self._service, self._peer_object_path) dbus.service.Object.__init__(self, self._service, self._peer_object_path)
self._activity_object.set_peer_service_name(self._peer_service_name, self._peer_object_path) self._activity_object.set_peer_service_name(self._peer_service_name, self._peer_object_path)
return (self._activity_object, self._activity_id)
self._call_callback(ON_CONNECTED_TO_SHELL_CB, self._activity_object, self._activity_id)
def _shutdown_reply_cb(self): def _shutdown_reply_cb(self):
"""Shutdown was successful, tell the Activity that we're disconnected.""" """Shutdown was successful, tell the Activity that we're disconnected."""
self._activity.on_disconnected_from_shell() self._call_callback(ON_DISCONNECTED_FROM_SHELL_CB)
def _shutdown_error_cb(self, error): def _shutdown_error_cb(self, error):
print "ActivityDbusService: error during shutdown - '%s'" % error print "ActivityDbusService: error during shutdown - '%s'" % error
@ -79,24 +104,24 @@ class ActivityDbusService(dbus.service.Object):
return return
if service_name == SHELL_SERVICE_NAME and not len(new_service_name): if service_name == SHELL_SERVICE_NAME and not len(new_service_name):
self._activity.on_disconnected_from_shell() self._call_callback(ON_DISCONNECTED_FROM_SHELL_CB)
elif service_name == SHELL_SERVICE_NAME and not len(old_service_name): elif service_name == SHELL_SERVICE_NAME and not len(old_service_name):
self._activity.on_reconnected_to_shell() self._call_callback(ON_RECONNECTED_TO_SHELL_CB)
@dbus.service.method(ACTIVITY_SERVICE_NAME) @dbus.service.method(ACTIVITY_SERVICE_NAME)
def lost_focus(self): def lost_focus(self):
"""Called by the shell to notify us that we've lost focus.""" """Called by the shell to notify us that we've lost focus."""
self._activity.on_lost_focus() self._call_callback(ON_LOST_FOCUS_CB)
@dbus.service.method(ACTIVITY_SERVICE_NAME) @dbus.service.method(ACTIVITY_SERVICE_NAME)
def got_focus(self): def got_focus(self):
"""Called by the shell to notify us that the user gave us focus.""" """Called by the shell to notify us that the user gave us focus."""
self._activity.on_got_focus() self._call_callback(ON_GOT_FOCUS_CB)
@dbus.service.method(ACTIVITY_SERVICE_NAME) @dbus.service.method(ACTIVITY_SERVICE_NAME)
def close_from_user(self): def close_from_user(self):
"""Called by the shell to notify us that the user closed us.""" """Called by the shell to notify us that the user closed us."""
self._activity.on_close_from_user() self._call_callback(ON_CLOSE_FROM_USER_CB)
class Activity(object): class Activity(object):
@ -104,6 +129,12 @@ class Activity(object):
def __init__(self): def __init__(self):
self._dbus_service = self._get_new_dbus_service() self._dbus_service = self._get_new_dbus_service()
self._dbus_service.register_callback(ON_CONNECTED_TO_SHELL_CB, self._internal_on_connected_to_shell_cb)
self._dbus_service.register_callback(ON_DISCONNECTED_FROM_SHELL_CB, self._internal_on_disconnected_from_shell_cb)
self._dbus_service.register_callback(ON_RECONNECTED_TO_SHELL_CB, self._internal_on_reconnected_to_shell_cb)
self._dbus_service.register_callback(ON_CLOSE_FROM_USER_CB, self._internal_on_close_from_user_cb)
self._dbus_service.register_callback(ON_LOST_FOCUS_CB, self._internal_on_lost_focus_cb)
self._dbus_service.register_callback(ON_GOT_FOCUS_CB, self._internal_on_got_focus_cb)
self._has_focus = False self._has_focus = False
self._plug = None self._plug = None
self._activity_object = None self._activity_object = None
@ -131,9 +162,42 @@ class Activity(object):
def connect_to_shell(self): def connect_to_shell(self):
"""Called by our controller to tell us to initialize and connect """Called by our controller to tell us to initialize and connect
to the shell.""" to the shell."""
(self._activity_object, self._activity_id) = self._dbus_service.connect_to_shell() self._dbus_service.connect_to_shell()
def _internal_on_connected_to_shell_cb(self, activity_object, activity_id):
"""Callback when the dbus service object has connected to the shell."""
self._activity_object = activity_object
self._activity_id = activity_id
self._window_id = self._activity_object.get_host_xembed_id()
print "Activity: XEMBED window ID is %d" % self._window_id
self._plug = gtk.Plug(self._window_id)
self.on_connected_to_shell() self.on_connected_to_shell()
def _internal_on_disconnected_from_shell_cb(self):
"""Callback when the dbus service object has disconnected from the shell."""
self._cleanup()
self.on_disconnected_from_shell()
def _internal_on_reconnected_to_shell_cb(self):
"""Callback when the dbus service object has reconnected to the shell."""
self.on_reconnected_to_shell()
def _internal_on_close_from_user_cb(self):
"""Callback when the dbus service object tells us the user has closed our activity."""
self.shutdown()
self.on_close_from_user()
def _internal_on_lost_focus_cb(self):
"""Callback when the dbus service object tells us we have lost focus."""
self._has_focus = False
self.on_lost_focus()
def _internal_on_got_focus_cb(self):
"""Callback when the dbus service object tells us we have gotten focus."""
self._has_focus = True
self.set_has_changes(False)
self.on_got_focus()
def gtk_plug(self): def gtk_plug(self):
"""Return our GtkPlug widget.""" """Return our GtkPlug widget."""
return self._plug return self._plug
@ -194,18 +258,21 @@ class Activity(object):
"""Disconnect from the shell and clean up.""" """Disconnect from the shell and clean up."""
self._dbus_service.shutdown() self._dbus_service.shutdown()
#############################################################
# Pure Virtual methods that subclasses may/may not implement
#############################################################
def on_lost_focus(self): def on_lost_focus(self):
"""Triggered when this Activity loses focus.""" """Triggered when this Activity loses focus."""
self._has_focus = False; pass
def on_got_focus(self): def on_got_focus(self):
"""Triggered when this Activity gets focus.""" """Triggered when this Activity gets focus."""
self._has_focus = True pass
self.set_has_changes(False)
def on_disconnected_from_shell(self): def on_disconnected_from_shell(self):
"""Triggered when we disconnect from the shell.""" """Triggered when we disconnect from the shell."""
self._cleanup() pass
def on_reconnected_to_shell(self): def on_reconnected_to_shell(self):
"""Triggered when the shell's service comes back.""" """Triggered when the shell's service comes back."""
@ -213,10 +280,8 @@ class Activity(object):
def on_connected_to_shell(self): def on_connected_to_shell(self):
"""Triggered when this Activity has successfully connected to the shell.""" """Triggered when this Activity has successfully connected to the shell."""
self._window_id = self._activity_object.get_host_xembed_id() pass
print "Activity: XEMBED window ID is %d" % self._window_id
self._plug = gtk.Plug(self._window_id)
def on_close_from_user(self): def on_close_from_user(self):
"""Triggered when this Activity is closed by the user.""" """Triggered when this Activity is closed by the user."""
self.shutdown() pass