~ubuntu-branches/ubuntu/trusty/gwibber/trusty

« back to all changes in this revision

Viewing changes to .pc/lp_975437.patch/gwibber/microblog/plugins/foursquare/gtk/foursquare/__init__.py

  • Committer: Package Import Robot
  • Author(s): Ken VanDine
  • Date: 2012-04-12 14:51:08 UTC
  • Revision ID: package-import@ubuntu.com-20120412145108-ib1ok9gr71gj59cc
Tags: 3.4.0-0ubuntu4
* debian/patches/lp_975437.patch
  - foursquare: Don't crash on unicode characters in a users 
    fullname, thanks to Raza Sayed (LP: #975437)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from gi.repository import GLib, Gtk, Pango, WebKit
 
2
import urllib, urllib2, json, urlparse, uuid
 
3
from oauth import oauth
 
4
 
 
5
from gwibber.microblog.util import resources
 
6
from gwibber.microblog.util.keyring import get_from_keyring
 
7
import gettext
 
8
from gettext import gettext as _
 
9
if hasattr(gettext, 'bind_textdomain_codeset'):
 
10
    gettext.bind_textdomain_codeset('gwibber','UTF-8')
 
11
gettext.textdomain('gwibber')
 
12
 
 
13
GLib.threads_init()
 
14
 
 
15
sigmeth = oauth.OAuthSignatureMethod_HMAC_SHA1()
 
16
 
 
17
CLIENT_ID = "BA0GOA0K3PTRS1KUJ5TTZ1P3GDRH3VJEEXY4N44ROPUJYKPW"
 
18
CLIENT_SECRET = "TS1LGTEZSJATQG22K5SXNASF021KPXI5LJTBQOVTX4JGOTYH"
 
19
REDIRECT_URI = "http://gwibber.com/0/auth.html"
 
20
 
 
21
class AccountWidget(Gtk.VBox):
 
22
  """AccountWidget: A widget that provides a user interface for configuring foursquare accounts in Gwibber
 
23
  """
 
24
  
 
25
  def __init__(self, account=None, dialog=None):
 
26
    """Creates the account pane for configuring Foursquare accounts"""
 
27
    Gtk.VBox.__init__( self, False, 20 )
 
28
    self.ui = Gtk.Builder()
 
29
    self.ui.set_translation_domain ("gwibber")
 
30
    self.ui.add_from_file (resources.get_ui_asset("gwibber-accounts-foursquare.ui"))
 
31
    self.ui.connect_signals(self)
 
32
    self.vbox_settings = self.ui.get_object("vbox_settings")
 
33
    self.pack_start(self.vbox_settings, False, False, 0)
 
34
    self.show_all()
 
35
 
 
36
    self.account = account or {}
 
37
    self.dialog = dialog
 
38
    self.window = dialog.dialog
 
39
    has_secret_key = False
 
40
    if self.account.has_key("id"):
 
41
        has_secret_key = get_from_keyring(self.account['id'],
 
42
                                          'secret_token') is not None
 
43
 
 
44
    try:
 
45
      if self.account.has_key("access_token") and self.account.has_key("secret_token") and self.account.has_key("username") and has_secret_key and not self.dialog.condition:
 
46
        self.ui.get_object("hbox_foursquare_auth").hide()
 
47
        self.ui.get_object("foursquare_auth_done_label").set_label(_("%s has been authorized by Foursquare") % self.account["username"])
 
48
        self.ui.get_object("hbox_foursquare_auth_done").show()
 
49
      else:
 
50
        self.ui.get_object("hbox_foursquare_auth_done").hide()
 
51
        if self.dialog.ui:
 
52
          self.dialog.ui.get_object('vbox_create').hide()
 
53
    except:
 
54
      self.ui.get_object("hbox_foursquare_auth_done").hide()
 
55
      if self.dialog.ui:
 
56
        self.dialog.ui.get_object("vbox_create").hide()
 
57
 
 
58
  def console_message_cb (self, *args):
 
59
    return True
 
60
 
 
61
  def on_foursquare_auth_clicked(self, widget, data=None):
 
62
    self.winsize = self.window.get_size()
 
63
 
 
64
    web = WebKit.WebView()
 
65
    web.get_settings().set_property("enable-plugins", False)
 
66
    web.get_settings().set_property("enable-developer-extras", False)
 
67
    web.connect("console-message", self.console_message_cb)
 
68
    web.load_html_string(_("<p>Please wait...</p>"), "file:///")
 
69
    
 
70
    url = 'https://foursquare.com/oauth2/authenticate?client_id=' + CLIENT_ID + '&response_type=code&display=touch&redirect_uri='+ REDIRECT_URI
 
71
 
 
72
    web.load_uri(url)
 
73
    web.connect("title-changed", self.on_foursquare_auth_title_change)
 
74
 
 
75
    self.scroll = Gtk.ScrolledWindow()
 
76
    self.scroll.set_size_request(500, 400)
 
77
    self.scroll.add(web)
 
78
 
 
79
    self.pack_start(self.scroll, True, True, 0)
 
80
    self.show_all()
 
81
 
 
82
    self.ui.get_object("vbox1").hide()
 
83
    self.ui.get_object("vbox_advanced").hide()
 
84
    self.dialog.infobar.set_message_type(Gtk.MessageType.INFO)
 
85
 
 
86
  def on_foursquare_auth_title_change(self, web=None, title=None, data=None):
 
87
    saved = False
 
88
    if hasattr(self.dialog, "infobar_content_area"):
 
89
      for child in self.dialog.infobar_content_area.get_children(): child.destroy()
 
90
    self.dialog.infobar_content_area = self.dialog.infobar.get_content_area()
 
91
    self.dialog.infobar_content_area.show()
 
92
 
 
93
    if title.get_title() == "Success":
 
94
      message_label = Gtk.Label(_("Verifying"))
 
95
      message_label.set_use_markup(True)
 
96
      message_label.set_ellipsize(Pango.EllipsizeMode.END)
 
97
      self.dialog.infobar_content_area.add(message_label)
 
98
      self.dialog.infobar.show_all()
 
99
      self.scroll.destroy()
 
100
      
 
101
      #Get the code from the callback uri (it's formatted as http://gwibber.com/0/auth.html/?code=CODE)
 
102
      url = web.get_main_frame().get_uri()
 
103
      data = urlparse.parse_qs(url.split("?", 1)[1])
 
104
      self.code = data["code"][0]
 
105
      
 
106
      self.ui.get_object("vbox1").show()
 
107
      self.ui.get_object("vbox_advanced").show()
 
108
 
 
109
      #Exchange the code for an access token
 
110
      url = "https://foursquare.com/oauth2/access_token?client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&grant_type=authorization_code&redirect_uri=" + REDIRECT_URI + "&code=" + self.code
 
111
      self.access_token = json.load(urllib2.urlopen(url))['access_token']
 
112
      self.account["access_token"] = self.access_token
 
113
      self.account["secret_token"] = self.access_token
 
114
      
 
115
      #Make a request with our new token for the user's own data
 
116
      url = "https://api.foursquare.com/v2/users/self?oauth_token=" + self.access_token
 
117
      data = json.load(urllib2.urlopen(url))
 
118
      fullname = ""
 
119
      if data["response"]["user"].has_key("firstName"):
 
120
         fullname += data["response"]["user"]["firstName"] + " "
 
121
      if data["response"]["user"].has_key("lastName"):
 
122
        fullname += data["response"]["user"]["lastName"]
 
123
      self.account["username"] = fullname
 
124
      self.account["user_id"] = data["response"]["user"]["id"]
 
125
 
 
126
      if isinstance(data, dict):
 
127
        data = data["response"]["user"]
 
128
        if data.has_key("id"):
 
129
          saved = self.dialog.on_edit_account_save()
 
130
        else:
 
131
          print "Failed"
 
132
          self.dialog.infobar.set_message_type(Gtk.MessageType.ERROR)
 
133
          message_label.set_text(_("Authorization failed. Please try again.")) 
 
134
      else:
 
135
        print "Failed"
 
136
        self.dialog.infobar.set_message_type(Gtk.MessageType.ERROR)
 
137
        message_label.set_text(_("Authorization failed. Please try again."))
 
138
 
 
139
      if saved: 
 
140
        message_label.set_text(_("Successful"))
 
141
        self.dialog.infobar.set_message_type(Gtk.MessageType.INFO)
 
142
        #self.dialog.infobar.hide()
 
143
 
 
144
      self.ui.get_object("hbox_foursquare_auth").hide()
 
145
      self.ui.get_object("foursquare_auth_done_label").set_label(_("%s has been authorized by Foursquare") % str(self.account["username"]))
 
146
      self.ui.get_object("hbox_foursquare_auth_done").show()
 
147
      if self.dialog.ui and self.account.has_key("id") and not saved:
 
148
        self.dialog.ui.get_object("vbox_save").show()
 
149
      elif self.dialog.ui and not saved:
 
150
        self.dialog.ui.get_object("vbox_create").show()
 
151
 
 
152
      self.window.resize(*self.winsize)
 
153
 
 
154
    if title.get_title() == "Failure":
 
155
      self.dialog.infobar.set_message_type(Gtk.MessageType.ERROR)
 
156
      message_label = Gtk.Label(_("Authorization failed. Please try again."))
 
157
      message_label.set_use_markup(True)
 
158
      message_label.set_ellipsize(Pango.EllipsizeMode.END)
 
159
      self.dialog.infobar_content_area.add(message_label)
 
160
      self.dialog.infobar.show_all()
 
161
 
 
162
      self.ui.get_object("vbox1").show()
 
163
      self.ui.get_object("vbox_advanced").show()
 
164
      self.scroll.destroy()
 
165
      self.window.resize(*self.winsize)
 
166