From 03097f32e67f961cb099c9dcd0b543f65ecfda1e Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Fri, 11 Feb 2011 23:58:13 +0000 Subject: add Enable collaboration menu item; pep8 cleanup --- diff --git a/extra/collaborationplugin.py b/extra/collaborationplugin.py index 56fcae5..8f8486f 100644 --- a/extra/collaborationplugin.py +++ b/extra/collaborationplugin.py @@ -1,3 +1,24 @@ +#!/usr/bin/env python +#Copyright (c) 2011 Walter Bender +#Copyright (c) 2011 Collabora Ltd. + +#Permission is hereby granted, free of charge, to any person obtaining a copy +#of this software and associated documentation files (the "Software"), to deal +#in the Software without restriction, including without limitation the rights +#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +#copies of the Software, and to permit persons to whom the Software is +#furnished to do so, subject to the following conditions: + +#The above copyright notice and this permission notice shall be included in +#all copies or substantial portions of the Software. + +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +#THE SOFTWARE. import sys sys.path.append("..") @@ -22,6 +43,7 @@ from TurtleArt.tacollaboration import Collaboration CONNECTION_INTERFACE_ACTIVITY_PROPERTIES = \ 'org.laptop.Telepathy.ActivityProperties' + class CollaborationPlugin(Plugin): __gsignals__ = { @@ -31,7 +53,7 @@ class CollaborationPlugin(Plugin): ()), } - # Using activity here is kinda misleading cause the + # Using activity here is kinda misleading cause the # obj we are receiving is not a Sugar activity. def __init__(self, activity, config_file_path): Plugin.__init__(self) @@ -40,24 +62,32 @@ class CollaborationPlugin(Plugin): self._neighborhood = None self._title = "My Turtle Art session" self._bundle_id = "org.laptop.TurtleArt" - self._activity_id = "1234567" # This could be hashed from the file path (if resuming) + # This could be hashed from the file path (if resuming) + self._activity_id = "1234567" self._nick = "" self._config_file_path = config_file_path self._collaboration_config_values = ConfigFile(self._config_file_path) - valid_config_values = { - "nick" : { "type" : "text"}, - "account_id" : { "type" : "text"}, - "password" : { "type" : "text"}, - "server" : { "type" : "text"}, - "port" : { "type" : "integer"}, - "register": { "type" : "boolean"}, - "turtle_color" : { "type" : "text"}, - "colors" : { "type" : "text"} + self._valid_config_values = { + "nick": {"type": "text"}, + "account_id": {"type": "text"}, + "password": {"type": "text"}, + "server": {"type": "text"}, + "port": {"type": "integer"}, + "register": {"type": "boolean"}, + "turtle_color": {"type": "text"}, + "colors": {"type": "text"} } - self._collaboration_config_values.set_valid_keys(valid_config_values) - self._collaboration_config_values.connect("configuration-loaded", self._connect_to_neighborhood) - self._collaboration_config_values.connect("configuration-saved", self._connect_to_neighborhood) + + def _connect_cb(self, button): + """ Enable connection """ + self._collaboration_config_values.set_valid_keys( + self._valid_config_values) + self._collaboration_config_values.connect( + "configuration-loaded", self._connect_to_neighborhood) + self._collaboration_config_values.connect( + "configuration-saved", self._connect_to_neighborhood) self._collaboration_config_values.load() + self.setup() def setup(self): self._collaboration = Collaboration(self.tw, self) @@ -70,17 +100,23 @@ class CollaborationPlugin(Plugin): def get_menu(self): menu = gtk.Menu() + MenuBuilder.make_menu_item(menu, _('Enable collaboration'), + self._connect_cb) + self._activities_submenu = gtk.Menu() - activities_menu = MenuBuilder.make_sub_menu(self._activities_submenu, _('Activities')) + activities_menu = MenuBuilder.make_sub_menu(self._activities_submenu, + _('Activities')) menu.append(activities_menu) self._buddies_submenu = gtk.Menu() - buddies_menu = MenuBuilder.make_sub_menu(self._buddies_submenu, _('Buddies')) + buddies_menu = MenuBuilder.make_sub_menu(self._buddies_submenu, + _('Buddies')) menu.append(buddies_menu) - + MenuBuilder.make_menu_item(menu, _('Share'), self._share_cb) - MenuBuilder.make_menu_item(menu, _('Configuration'), self._config_neighborhood_cb) - + MenuBuilder.make_menu_item(menu, _('Configuration'), + self._config_neighborhood_cb) + neighborhood_menu = MenuBuilder.make_sub_menu(menu, _('Neighborhood')) return neighborhood_menu @@ -99,7 +135,7 @@ class CollaborationPlugin(Plugin): def _get_title(self): return self._title - + def _get_turtle_color(self): return self._turtle_color @@ -111,7 +147,8 @@ class CollaborationPlugin(Plugin): params = {} params["nickname"] = self._collaboration_config_values.get("nick") - params["account_id"] = self._collaboration_config_values.get("account_id") + params["account_id"] = self._collaboration_config_values.get( + "account_id") params["server"] = self._collaboration_config_values.get("server") params["port"] = self._collaboration_config_values.get("port") params["password"] = self._collaboration_config_values.get("password") @@ -121,18 +158,20 @@ class CollaborationPlugin(Plugin): self._nick = self._collaboration_config_values.get("nick") self._colors = self._collaboration_config_values.get("colors") - self._turtle_color = self._collaboration_config_values.get("turtle_color") + self._turtle_color = self._collaboration_config_values.get( + "turtle_color") self._activities = {} self._buddies = {} print "connecting to the neighborhood" dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) - + self._client_handler = telepathyclient.get_instance() self._neighborhood = get_neighborhood(params) self._neighborhood.connect('activity-added', self._activity_added_cb) - self._neighborhood.connect('activity-removed', self._activity_removed_cb) + self._neighborhood.connect('activity-removed', + self._activity_removed_cb) self._neighborhood.connect('buddy-added', self._buddy_added_cb) self._neighborhood.connect('buddy-removed', self._buddy_removed_cb) @@ -162,7 +201,7 @@ class CollaborationPlugin(Plugin): print "Couldn't remove buddy %s" % buddy.get_key() self._recreate_available_buddies_menu() - # TODO: we should have a list of available actions over + # TODO: we should have a list of available actions over # a given buddy. I.e.: a) chat with him b) make friend # c) invite to current activity # @@ -175,24 +214,26 @@ class CollaborationPlugin(Plugin): if key is None: key = "" n = buddy.get_nick() + "|" + key[0:15] - MenuBuilder.make_menu_item(self._buddies_submenu, n, self._buddy_actions_cb, buddy) + MenuBuilder.make_menu_item(self._buddies_submenu, n, + self._buddy_actions_cb, buddy) def _buddy_actions_cb(self, widget, buddy): print "do something with %s" % buddy.get_nick() - # TODO - # - we need an extra menu branch with a) 'Join' button b) List of buddies + # TODO: + # we need an extra menu branch with a) 'Join' button b) List of buddies def _recreate_available_activities_menu(self): for child in self._activities_submenu.get_children(): self._activities_submenu.remove(child) for activity in self._activities.values(): n = activity.props.name - MenuBuilder.make_menu_item(self._activities_submenu, n, self._join_activity_cb, activity) + MenuBuilder.make_menu_item(self._activities_submenu, n, + self._join_activity_cb, activity) def _join_activity_cb(self, widget, activity): print "Lets try to join..." - + connection_manager = get_connection_manager() account_path, connection = \ connection_manager.get_preferred_connection() @@ -208,10 +249,10 @@ class CollaborationPlugin(Plugin): try: room_handle = connection.GetActivity(activity.activity_id, - dbus_interface=CONNECTION_INTERFACE_ACTIVITY_PROPERTIES) + dbus_interface=CONNECTION_INTERFACE_ACTIVITY_PROPERTIES) print("room_handle = %s" % str(room_handle)) - self._joined_activity = Activity(account_path, connection, room_handle, - properties=properties) + self._joined_activity = Activity( + account_path, connection, room_handle, properties=properties) # FIXME: this should be unified, no need to keep 2 references self._shared_activity = self._joined_activity except: @@ -224,21 +265,29 @@ class CollaborationPlugin(Plugin): _join_id = self._joined_activity.connect('joined', self.__joined_cb) self._joined_activity.join() - def __joined_cb(self,activity, success, err): + def __joined_cb(self, activity, success, err): print "We've joined an activity" self.emit('joined') def _config_neighborhood_cb(self, widget): config_w = ConfigWizard(self._config_file_path) config_items = [ - {"item_label" : _("Nickname"), "item_type" : "text", "item_name" : "nick" }, - { "item_label" : _("Account ID"), "item_type" : "text", "item_name" : "account_id" }, - { "item_label" : _("Server"), "item_type" : "text", "item_name" : "server" }, - { "item_label" : _("Port"), "item_type" : "text", "item_name" : "port" }, - { "item_label" : _("Password"), "item_type" : "text", "item_name" : "password" }, - { "item_label" : _("Register"), "item_type" : "boolean", "item_name" : "register" }, - { "item_label" : _("Colors"), "item_type" : "text", "item_name" : "colors" }, - { "item_label" : _("Turtle Color"), "item_type" : "text", "item_name" : "turtle_color" } + {"item_label": _("Nickname"), "item_type": "text", + "item_name": "nick"}, + {"item_label": _("Account ID"), "item_type": "text", + "item_name": "account_id"}, + {"item_label": _("Server"), "item_type": "text", + "item_name": "server"}, + {"item_label": _("Port"), "item_type": "text", + "item_name": "port"}, + {"item_label": _("Password"), "item_type": "text", + "item_name": "password"}, + {"item_label": _("Register"), "item_type": "boolean", + "item_name": "register"}, + {"item_label": _("Colors"), "item_type": "text", + "item_name": "colors"}, + {"item_label": _("Turtle Color"), "item_type": "text", + "item_name": "turtle_color"} ] config_w.set_config_items(config_items) config_w.set_config_file_obj(self._collaboration_config_values) @@ -261,8 +310,9 @@ class CollaborationPlugin(Plugin): return try: - self._activity._shared_activity = Activity(account_path, connection, - properties=properties) + self._activity._shared_activity = Activity(account_path, + connection, + properties=properties) # FIXME: this should be unified, no need to keep 2 references self._shared_activity = self._activity._shared_activity except: @@ -280,10 +330,8 @@ class CollaborationPlugin(Plugin): self.emit('shared') def __share_activity_error_cb(self, activity, error): - """Notify with GObject event of unsuccessful sharing of activity - """ + """Notify with GObject event of unsuccessful sharing of activity""" print "%s got error: %s" % (activity, error) if __name__ == "__main__": print "testing collaboration" - -- cgit v0.9.1