From 687ebda03c7d7d34df418dd3a077e3eb33b1678d Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Wed, 30 Jan 2013 21:50:58 +0000 Subject: add fb plugin; consolidate gnome menus when possible --- diff --git a/gnome_plugins/fb_plugin.py b/gnome_plugins/fb_plugin.py new file mode 100644 index 0000000..d7b49ff --- /dev/null +++ b/gnome_plugins/fb_plugin.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python +# +# Copyright (c) 2012 Raul Gutierrez S. - rgs@itevenworks.net +# Copyright (c) 2013 Alan Aguiar alanjas@hotmail.com + +#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. + +# NOTE: This is a plugin to enable uploading of Turtle Art projects to +# Facebook. It currently works for the GTK3 version of Turtle Blocks and +# only from the GNOME desktop. +# USAGE: Download this file into the gnome_plugin directory; make sure the +# filename is fb_plugin.py (Mediawiki capitalizes the first letter of files). +# When you launch TA from the GNOME desktop by running turtleblocks.py +# you should see a new menu, Facebook, which allows you to upload your +# project. Please report any problems to rgs and walter. + + +import pycurl +import sys +import urlparse + +import gtk +import webkit +from plugin import Plugin +from util.menubuilder import MenuBuilder, MENUBAR +from gettext import gettext as _ + +class FbUploader(): + UPLOAD_URL = "https://graph.facebook.com/me/photos?access_token=%s" + + def __init__(self, image_path, access_token): + self._image_path = image_path + self._access_token = access_token + + def doit(self): + c = pycurl.Curl() + c.setopt(c.POST, 1) + c.setopt(c.URL, self._get_url()) + c.setopt(c.HTTPPOST, self._get_params(c)) + c.perform() + print c.getinfo(c.HTTP_CODE) + + def _get_url(self): + return self.UPLOAD_URL % (self._access_token) + + def _get_params(self, c): + params = [] + params.append(('source', (c.FORM_FILE, self._image_path))) + + return params + + +class Fb_plugin(Plugin): + APP_ID = "172917389475707" + REDIRECT_URI = "http://www.sugarlabs.org" + + def __init__(self, parent): + self._access_token = "" + self._auth_win = None + + def get_menu(self): + if _('Upload') in MENUBAR: + menu, upload_menu = MENUBAR[_('Upload')] + else: + upload_menu = None + menu = gtk.Menu() + MenuBuilder.make_menu_item(menu, _('Facebook wall post'), + self._post_menu_cb) + if upload_menu is not None: + return None # We don't have to add it since it already exists + else: + upload_menu = MenuBuilder.make_sub_menu(menu, _('Upload')) + return upload_menu + + def set_tw(self, turtleart_window): + self.tw = turtleart_window + + def enabled(self): + return True + + def _post_menu_cb(self, widget): + if self._access_token == "": + self._grab_fb_app_token() + return + + self._post_to_fb() + + def _grab_fb_app_token(self): + url = self._get_auth_url() + w = gtk.Window() + w.move(200, 200) + w.set_size_request(800, 400) + wkv = webkit.WebView() + wkv.load_uri(url) + wkv.grab_focus() + wkv.connect('navigation-policy-decision-requested', self._nav_policy_cb) + w.add(wkv) + w.show_all() + self._auth_win = w + + def _get_auth_url(self): + url = "http://www.facebook.com/dialog/oauth?client_id=%s" % (self.APP_ID) + url += "&redirect_uri=%s" % (self.REDIRECT_URI) + url += "&response_type=token&scope=publish_stream" + + return url + + def _nav_policy_cb(self, view, frame, req, action, param): + uri = req.get_uri() + if uri: + url_o = urlparse.urlparse(uri) + params = urlparse.parse_qs(url_o.fragment) + if params.has_key('access_token'): + self._access_token = params['access_token'][0] + self._auth_win.hide() + self._post_to_fb() + + def _post_to_fb(self): + ta_file, image_file = self.tw.save_for_upload("ta fb") + uploader = FbUploader(image_file, self._access_token) + uploader.doit() diff --git a/gnome_plugins/uploader_plugin.py b/gnome_plugins/uploader_plugin.py index 06224fa..3d3e04f 100644 --- a/gnome_plugins/uploader_plugin.py +++ b/gnome_plugins/uploader_plugin.py @@ -35,7 +35,7 @@ import os import gtk from plugin import Plugin -from util.menubuilder import MenuBuilder +from util.menubuilder import MenuBuilder, MENUBAR from gettext import gettext as _ @@ -60,11 +60,18 @@ class Uploader_plugin(Plugin): self.tw = turtleart_window def get_menu(self): - menu = gtk.Menu() + if _('Upload') in MENUBAR: + menu, upload_menu = MENUBAR[_('Upload')] + else: + upload_menu = None + menu = gtk.Menu() MenuBuilder.make_menu_item(menu, _('Upload to Web'), self.do_upload_to_web) - upload_menu = MenuBuilder.make_sub_menu(menu, _('Upload')) - return upload_menu + if upload_menu is not None: + return None # We don't have to add it since it already exists + else: + upload_menu = MenuBuilder.make_sub_menu(menu, _('Upload')) + return upload_menu def enabled(self): return _UPLOAD_AVAILABLE diff --git a/turtleblocks.py b/turtleblocks.py index 2e8fc8f..4526630 100755 --- a/turtleblocks.py +++ b/turtleblocks.py @@ -417,7 +417,9 @@ class TurtleMain(): # Add menus for plugins for p in self._gnome_plugins: - menu_bar.append(p.get_menu()) + menu_item = p.get_menu() + if menu_item is not None: + menu_bar.append(menu_item) menu_bar.append(help_menu) diff --git a/util/menubuilder.py b/util/menubuilder.py index 4ee4550..3c1d1af 100644 --- a/util/menubuilder.py +++ b/util/menubuilder.py @@ -17,12 +17,21 @@ import gtk +MENUBAR = {} class MenuBuilder(): @classmethod + def get_sub_menu_by_name(cls, name): + if name in MENUBAR: + return MENUBAR[name] + else: + return None + + @classmethod def make_sub_menu(cls, menu, name): """ add a new submenu to the toolbar """ sub_menu = gtk.MenuItem(name) + MENUBAR[name] = [menu, sub_menu] # Maintain a dictionary sub_menu.show() sub_menu.set_submenu(menu) return sub_menu -- cgit v0.9.1