diff --git a/shell/example-activity/example-activity.py b/shell/example-activity/example-activity.py index 56b1a730..619f04d1 100755 --- a/shell/example-activity/example-activity.py +++ b/shell/example-activity/example-activity.py @@ -10,24 +10,152 @@ import pygtk pygtk.require('2.0') import gtk,sys +################################################################################ +# Code below will be moved to a module someday + +class Activity(dbus.service.Object): + + def __init__(self): + pass + + def name_owner_changed(self, service_name, old_service_name, new_service_name): + #print "in name_owner_changed: svc=%s oldsvc=%s newsvc=%s"%(service_name, old_service_name, new_service_name) + if service_name == "com.redhat.Sugar.Shell" and new_service_name == "": + self.activity_on_disconnected_from_shell() + #elif service_name == "com.redhat.Sugar.Shell" and old_service_name == "": + # self.activity_on_shell_reappeared() + + def activity_connect_to_shell(self): + self.__bus = dbus.SessionBus() + + self.__bus.add_signal_receiver(self.name_owner_changed, dbus_interface = "org.freedesktop.DBus", signal_name = "NameOwnerChanged") + + self.__activity_container_object = self.__bus.get_object("com.redhat.Sugar.Shell", \ + "/com/redhat/Sugar/Shell/ActivityContainer") + self.__activity_container = dbus.Interface(self.__activity_container_object, \ + "com.redhat.Sugar.Shell.ActivityContainer") + + self.__activity_id = self.__activity_container.add_activity("") + self.__object_path = "/com/redhat/Sugar/Shell/Activities/%d"%self.__activity_id + + print "object_path = %s"%self.__object_path + + self.__activity_object = dbus.Interface(self.__bus.get_object("com.redhat.Sugar.Shell", self.__object_path), \ + "com.redhat.Sugar.Shell.ActivityHost") + self.__window_id = self.__activity_object.get_host_xembed_id() + + print "XEMBED window_id = %d"%self.__window_id + + self.__plug = gtk.Plug(self.__window_id) + + # Now let the Activity register a peer service so the Shell can poke it + self.__peer_service_name = "com.redhat.Sugar.Activity%d"%self.__activity_id + self.__peer_object_name = "/com/redhat/Sugar/Activity/%d"%self.__activity_id + self.__service = dbus.service.BusName(self.__peer_service_name, bus=self.__bus) + dbus.service.Object.__init__(self, self.__service, self.__peer_object_name) + + self.__activity_object.set_peer_service_name(self.__peer_service_name, self.__peer_object_name) + + self.activity_on_connected_to_shell() + + def activity_get_gtk_plug(self): + return self.__plug + + def activity_set_tab_text(self, text): + self.__activity_object.set_tab_text(text) + + @dbus.service.method("com.redhat.Sugar.Activity", \ + in_signature="", \ + out_signature="") + def lost_focus(self): + self.activity_on_lost_focus() + + @dbus.service.method("com.redhat.Sugar.Activity", \ + in_signature="", \ + out_signature="") + def got_focus(self): + self.activity_on_got_focus() + + + @dbus.service.method("com.redhat.Sugar.Activity", \ + in_signature="", \ + out_signature="") + def close_from_user(self): + self.activity_on_close_from_user() + + def activity_get_id(self): + return self.__activity_id + + # pure virtual methods + + def activity_on_connected_to_shell(self): + print "act %d: you need to override activity_on_connected_to_shell"%self.activity_get_id() + + def activity_on_disconnected_from_shell(self): + print "act %d: you need to override activity_on_disconnected_to_shell"%self.activity_get_id() + + def activity_on_close_from_user(self): + print "act %d: you need to override activity_on_close_from_user"%self.activity_get_id() + + def activity_on_lost_focus(self): + print "act %d: you need to override activity_on_lost_focus"%self.activity_get_id() + + def activity_on_got_focus(self): + print "act %d: you need to override activity_on_got_focus"%self.activity_get_id() + +def my_exit(): + sys.exit(0) + +def deferred_exit(): + gobject.timeout_add(0, my_exit) + +################################################################################ + +class ExampleActivity(Activity): + + def __init__(self, name): + self.name = name + + def entry_changed(self, entry): + self.activity_set_tab_text(entry.get_text()) + + def activity_on_connected_to_shell(self): + print "act %d: in activity_on_connected_to_shell"%self.activity_get_id() + + self.activity_set_tab_text(self.name) + + plug = self.activity_get_gtk_plug() + self.entry = gtk.Entry() + self.entry.set_text(self.name) + self.entry.connect("changed", self.entry_changed) + plug.add(self.entry) + plug.show_all() + + def activity_on_disconnected_from_shell(self): + print "act %d: in activity_on_disconnected_to_shell"%self.activity_get_id() + print "act %d: Shell disappeared..."%self.activity_get_id() + deferred_exit() + + def activity_on_close_from_user(self): + print "act %d: in activity_on_close_from_user"%self.activity_get_id() + deferred_exit() + + def activity_on_lost_focus(self): + print "act %d: in activity_on_lost_focus"%self.activity_get_id() + + def activity_on_got_focus(self): + print "act %d: in activity_on_got_focus"%self.activity_get_id() + + if len(sys.argv) != 2: print "usage: example-activity.py " sys.exit(1) -bus = dbus.SessionBus() -activity_container_object = bus.get_object("com.redhat.Sugar.Shell", \ - "/com/redhat/Sugar/Shell/ActivityContainer") -activity_container = dbus.Interface(activity_container_object, \ - "com.redhat.Sugar.Shell.ActivityContainer") +example_activity = ExampleActivity(sys.argv[1]) +example_activity.activity_connect_to_shell() -window_id = activity_container.add_activity(sys.argv[1]) -print "got XEMBED window_id = %d"%window_id - -plug = gtk.Plug(window_id) -entry = gtk.Entry() -entry.set_text(sys.argv[1]) -plug.add(entry) -plug.show_all() +example_activity2 = ExampleActivity(sys.argv[1] + " (2nd)") +example_activity2.activity_connect_to_shell() gtk.main() diff --git a/shell/src/shell.py b/shell/src/shell.py index 6eb2c5fa..2b2b4141 100755 --- a/shell/src/shell.py +++ b/shell/src/shell.py @@ -13,70 +13,163 @@ import gtk activity_counter = 0 -class Activity(dbus.service.Object): +class ActivityHost(dbus.service.Object): def __init__(self, activity_container, activity_name): global activity_counter + self.activity_name = activity_name + self.activity_container = activity_container - self.activity_object_name = "/com/redhat/Sugar/Shell/Activity_%d"%activity_counter + self.activity_id = activity_counter activity_counter += 1 - print "object name = %s"%self.activity_object_name - dbus.service.Object.__init__(self, activity_container.service, self.activity_object_name) + self.dbus_object_name = "/com/redhat/Sugar/Shell/Activities/%d"%self.activity_id + print "object name = %s"%self.dbus_object_name + + dbus.service.Object.__init__(self, activity_container.service, self.dbus_object_name) self.socket = gtk.Socket() + self.socket.set_data("sugar-activity", self) self.socket.show() - tab_name = gtk.Label(activity_name) - activity_container.notebook.append_page(self.socket, tab_name) + hbox = gtk.HBox(); + self.tab_activity_image = gtk.Image() + self.tab_activity_image.set_from_stock(gtk.STOCK_CONVERT, gtk.ICON_SIZE_MENU) + self.tab_activity_image.show() + + self.tab_label = gtk.Label(self.activity_name) + self.tab_label.show() + + self.tab_close_button = gtk.Button() + close_image = gtk.Image() + close_image.set_from_stock (gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU) + close_image.show() + self.tab_close_button.add(close_image) + self.tab_close_button.set_relief(gtk.RELIEF_NONE) + self.tab_close_button.set_focus_on_click(gtk.FALSE) + self.tab_close_button.show() + self.tab_close_button.connect("clicked", self.tab_close_button_clicked) + + hbox.set_spacing(4) + hbox.pack_start(self.tab_activity_image) + hbox.pack_start(self.tab_label) + hbox.pack_start(self.tab_close_button) + hbox.show() + self.activity_container.notebook.append_page(self.socket, hbox) + + def tab_close_button_clicked(self, button): + self.peer_service.close_from_user() + + @dbus.service.method("com.redhat.Sugar.Shell.ActivityHost", \ + in_signature="", \ + out_signature="t") + def get_host_xembed_id(self): + window_id = self.socket.get_id() + print "window_id = %d"%window_id + return window_id + + @dbus.service.method("com.redhat.Sugar.Shell.ActivityHost", \ + in_signature="ss", \ + out_signature="") + def set_peer_service_name(self, peer_service_name, peer_object_name): + print "peer_service_name = %s, peer_object_name = %s"%(peer_service_name, peer_object_name) + self.__peer_service_name = peer_service_name + self.__peer_object_name = peer_object_name + self.peer_service = dbus.Interface(self.activity_container.bus.get_object( \ + self.__peer_service_name, self.__peer_object_name), \ + "com.redhat.Sugar.Activity") + + @dbus.service.method("com.redhat.Sugar.Shell.ActivityHost", \ + in_signature="s", \ + out_signature="") + def set_tab_text(self, text): + self.tab_label.set_text(text) + + def get_host_activity_id(self): + return self.activity_id + + def get_object_path(self): + return self.dbus_object_name class ActivityContainer(dbus.service.Object): - def __init__(self, service): + def __init__(self, service, bus): self.activities = [] + self.bus = bus self.service = service - dbus.service.Object.__init__(self, service, "/com/redhat/Sugar/Shell/ActivityContainer") + dbus.service.Object.__init__(self, self.service, "/com/redhat/Sugar/Shell/ActivityContainer") + bus.add_signal_receiver(self.name_owner_changed, dbus_interface = "org.freedesktop.DBus", signal_name = "NameOwnerChanged") self.window = gtk.Window() self.window.set_title("OLPC Sugar") self.window.resize(640, 480) - self.notebook = gtk.Notebook() - - tab_label = gtk.Label("Some tab") - empty_label = gtk.Label("This left intentionally blank") + tab_label = gtk.Label("My Laptop") + empty_label = gtk.Label("This activity could launch other activities / be a help page") empty_label.show() self.notebook.append_page(empty_label, tab_label) - self.notebook.show() + self.notebook.connect("switch-page", self.notebook_tab_changed) self.window.add(self.notebook) self.window.connect("destroy", lambda w: gtk.main_quit()) self.window.show() + self.current_activity = None + + + def notebook_tab_changed(self, notebook, page, page_number): + print "in notebook_tab_changed" + new_activity = notebook.get_nth_page(page_number).get_data("sugar-activity") + print " Current activity: ", self.current_activity + print " New activity: ", new_activity + + if self.current_activity != None: + self.current_activity.peer_service.lost_focus() + + self.current_activity = new_activity + + if self.current_activity != None: + self.current_activity.peer_service.got_focus() + + + def name_owner_changed(self, service_name, old_service_name, new_service_name): + print "in name_owner_changed: svc=%s oldsvc=%s newsvc=%s"%(service_name, old_service_name, new_service_name) + for owner, activity in self.activities[:]: + if owner == old_service_name: + self.activities.remove((owner, activity)) + self.__print_activities() + @dbus.service.method("com.redhat.Sugar.Shell.ActivityContainer", \ in_signature="s", \ - out_signature="t", \ + out_signature="i", \ sender_keyword="sender") def add_activity(self, activity_name, sender): print "hello world, activity_name = '%s', sender = '%s'"%(activity_name, sender) - activity = Activity(self, activity_name) - self.activities.append(activity) - window_id = activity.socket.get_id() - print "window_id = %d"%window_id - return window_id + activity = ActivityHost(self, activity_name) + self.activities.append((sender, activity)) + + self.__print_activities() + return activity.get_host_activity_id() + + def __print_activities(self): + print "__print_activities: %d activities registered"%len(self.activities) + i = 0 + for owner, activity in self.activities: + print " %d: owner=%s activity_object_name=%s"%(i, owner, activity.dbus_object_name) + i += 1 session_bus = dbus.SessionBus() service = dbus.service.BusName("com.redhat.Sugar.Shell", bus=session_bus) -activityContainer = ActivityContainer(service) +activityContainer = ActivityContainer(service, session_bus) gtk.main()