~ubuntu-branches/ubuntu/quantal/gwibber/quantal

« back to all changes in this revision

Viewing changes to .pc/lp_38667.patch/gwibber/microblog/plugins/facebook/gtk/facebook/__init__.py

  • Committer: Package Import Robot
  • Author(s): Ken VanDine
  • Date: 2012-05-17 17:11:26 UTC
  • Revision ID: package-import@ubuntu.com-20120517171126-k8dvk1nempslz4jr
Tags: 3.4.1-0ubuntu3
* debian/patches/lp_38667.patch
  - facebook: Don't crash on unicode characters in a username (LP: #938667)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright (C) 2010 Canonical Ltd
 
3
#
 
4
# This program is free software: you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License version 2 as
 
6
# published by the Free Software Foundation.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
#
 
16
# Copyright (C) 2010 Ken VanDine <ken.vandine@canonical.com>
 
17
#
 
18
# facebook widgets for Gwibber
 
19
#
 
20
 
 
21
import urllib
 
22
import string
 
23
 
 
24
from gi.repository import Gdk, Gtk, WebKit, Pango
 
25
from gi.repository.Gtk import Builder
 
26
from gwibber.microblog.util import resources
 
27
from gwibber.microblog.util.const import *
 
28
from gwibber.microblog.util.keyring import get_from_keyring
 
29
# Try to import * from custom, install custom.py to include packaging 
 
30
# customizations like distro API keys, etc
 
31
try:
 
32
  from gwibber.microblog.util.custom import *
 
33
except:
 
34
  pass
 
35
 
 
36
import json, urlparse, uuid
 
37
import gettext
 
38
from gettext import gettext as _
 
39
if hasattr(gettext, 'bind_textdomain_codeset'):
 
40
    gettext.bind_textdomain_codeset('gwibber','UTF-8')
 
41
gettext.textdomain('gwibber')
 
42
 
 
43
"""
 
44
Gdk.threads_init()
 
45
 
 
46
APP_KEY = "71b85c6d8cb5bbb9f1a3f8bbdcdd4b05"
 
47
"""
 
48
 
 
49
class AccountWidget(Gtk.VBox):
 
50
  """AccountWidget: A widget that provides a user interface for configuring facebook accounts in Gwibber
 
51
  """
 
52
  
 
53
  def __init__(self, account=None, dialog=None):
 
54
    """Creates the account pane for configuring facebook accounts"""
 
55
    Gtk.VBox.__init__( self, False, 20 )
 
56
    self.ui = Gtk.Builder()
 
57
    self.ui.set_translation_domain ("gwibber")
 
58
    self.ui.add_from_file (resources.get_ui_asset("gwibber-accounts-facebook.ui"))
 
59
    self.ui.connect_signals(self)
 
60
    self.vbox_settings = self.ui.get_object("vbox_settings")
 
61
    self.pack_start(self.vbox_settings, False, False, 0)
 
62
    self.show_all()
 
63
    if account:
 
64
      self.account = account
 
65
    else:
 
66
      self.account = {}
 
67
    self.dialog = dialog
 
68
    self.window = dialog.dialog
 
69
    has_access_token = False
 
70
    if self.account.has_key("id"):
 
71
        has_secret_key = get_from_keyring(self.account['id'],
 
72
                                          'secret_token') is not None
 
73
 
 
74
    try:
 
75
      if self.account["access_token"] and self.account["username"] and has_access_token and not self.dialog.condition:
 
76
        self.ui.get_object("hbox_facebook_auth").hide()
 
77
        self.ui.get_object("fb_auth_done_label").set_label(_("%s has been authorized by Facebook") % str(self.account["username"]))
 
78
        self.ui.get_object("hbox_facebook_auth_done").show()
 
79
      else:
 
80
        self.ui.get_object("hbox_facebook_auth_done").hide()
 
81
        if self.dialog.ui:
 
82
          self.dialog.ui.get_object('vbox_create').hide()
 
83
    except:
 
84
      self.ui.get_object("hbox_facebook_auth_done").hide()
 
85
      if self.dialog.ui:
 
86
        self.dialog.ui.get_object("vbox_create").hide()
 
87
 
 
88
  def console_message_cb (self, *args):
 
89
    return True
 
90
 
 
91
  def on_facebook_auth_clicked(self, widget, data=None):
 
92
    self.winsize = self.window.get_size()
 
93
 
 
94
    web = WebKit.WebView()
 
95
    web.get_settings().set_property("enable-plugins", False)
 
96
    web.get_settings().set_property("enable-developer-extras", False)
 
97
    web.connect("console-message", self.console_message_cb)
 
98
    web.load_html_string(_("<p>Please wait...</p>"), "file:///")
 
99
 
 
100
    url = urllib.urlencode({
 
101
      "client_id": FB_APP_KEY,
 
102
      "display": "popup",
 
103
      "type": "user_agent",
 
104
      "scope": "publish_stream,read_stream,status_update,offline_access,user_photos,friends_photos",
 
105
      "redirect_uri": "http://www.facebook.com/connect/login_success.html",
 
106
    })
 
107
    web.load_uri("https://graph.facebook.com/oauth/authorize?" + url)
 
108
    web.connect("title-changed", self.on_facebook_auth_title_change)
 
109
 
 
110
    self.scroll = Gtk.ScrolledWindow()
 
111
    
 
112
    self.scroll.add(web)
 
113
    self.scroll.set_size_request(450, 340)
 
114
 
 
115
    self.pack_start(self.scroll, True, True, 0)
 
116
    self.show_all()
 
117
    self.dialog.infobar.hide()
 
118
    self.ui.get_object("vbox1").hide()
 
119
    self.ui.get_object("vbox_advanced").hide()
 
120
 
 
121
  def on_facebook_auth_title_change(self, web=None, title=None, data=None):
 
122
    saved = False
 
123
    if hasattr(self.dialog, "infobar_content_area"):
 
124
      for child in self.dialog.infobar_content_area.get_children(): child.destroy()
 
125
    self.dialog.infobar_content_area = self.dialog.infobar.get_content_area()
 
126
    self.dialog.infobar_content_area.show()
 
127
 
 
128
    url = web.get_main_frame().get_uri()
 
129
    if title.get_title() == "Success":
 
130
      try:
 
131
        self.account["access_token"] = str(urlparse.parse_qs(url.split("#", 1)[1])["access_token"][0])
 
132
      except:
 
133
        self.scroll.destroy()
 
134
        self.ui.get_object("vbox1").show()
 
135
        self.ui.get_object("vbox_advanced").show()
 
136
        self.window.resize(*self.winsize)
 
137
        self.dialog.select_account ()
 
138
        return
 
139
 
 
140
      message_label = Gtk.Label (_("Verifying"))
 
141
      message_label.set_use_markup(True)
 
142
      message_label.set_ellipsize(Pango.EllipsizeMode.END)
 
143
      self.dialog.infobar_content_area.add(message_label)
 
144
      self.dialog.infobar.show_all()
 
145
      self.scroll.destroy()
 
146
 
 
147
      data = json.loads(urllib.urlopen("https://graph.facebook.com/me?access_token=" + self.account["access_token"]).read())
 
148
      if isinstance(data, dict):
 
149
        if data.has_key("id") and data.has_key("name"):
 
150
          self.account["username"] = data["name"]
 
151
          self.account["uid"] = data["id"]
 
152
          saved = self.dialog.on_edit_account_save() 
 
153
      else:
 
154
        # Make a desparate attempt to guess the id from the url
 
155
        uid = url.split('-')[1].split('%7C')[0]
 
156
        if isinstance(uid, int) and len(uid) > 2:
 
157
          acct = json.loads(urllib.urlopen("https://graph.facebook.com/" + str(uid)).read())
 
158
          if isinstance(acct, dict):
 
159
            if acct.has_key("id") and acct.has_key("name"):
 
160
              self.account["uid"] = acct["id"]
 
161
              self.account["username"] = acct["name"]
 
162
              saved = self.dialog.on_edit_account_save()
 
163
          else:
 
164
            print "Failed"
 
165
 
 
166
      if saved:
 
167
        message_label.set_text(_("Successful"))
 
168
        self.dialog.infobar.set_message_type(Gtk.MessageType.INFO)
 
169
 
 
170
      self.ui.get_object("hbox_facebook_auth").hide()
 
171
      self.ui.get_object("fb_auth_done_label").set_label(_("%s has been authorized by Facebook") % str(self.account["username"]))
 
172
      self.ui.get_object("hbox_facebook_auth_done").show()
 
173
      if self.dialog.ui and self.account.has_key("id") and not saved:
 
174
        self.dialog.ui.get_object("vbox_save").show()
 
175
      elif self.dialog.ui and not saved:
 
176
        self.dialog.ui.get_object("vbox_create").show()
 
177
 
 
178
      self.ui.get_object("vbox1").show()
 
179
      self.ui.get_object("vbox_advanced").show()
 
180
      self.window.resize(*self.winsize)
 
181
      self.dialog.select_account ()
 
182
 
 
183
    if title.get_title() == "Failure":
 
184
      self.dialog.infobar.set_message_type(Gtk.MessageType.ERROR)
 
185
      message_label = Gtk.Label (_("Authorization failed. Please try again."))
 
186
      message_label.set_use_markup(True)
 
187
      message_label.set_ellipsize(Pango.EllipsizeMode.END)
 
188
      self.dialog.infobar_content_area.add(message_label)
 
189
      self.dialog.infobar.show_all()
 
190
 
 
191
      self.ui.get_object("vbox1").show()
 
192
      self.ui.get_object("vbox_advanced").show()
 
193
      self.scroll.destroy()
 
194
      self.window.resize(*self.winsize)
 
195
      self.dialog.select_account ()
 
196