1
import gtk, pango, webkit, gnomekeyring
2
import urllib, urllib2, json, urlparse, uuid
3
from oauth import oauth
5
from gtk import Builder
6
from gwibber.microblog.util import resources
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-service-sina')
14
gtk.gdk.threads_init()
16
sigmeth = oauth.OAuthSignatureMethod_HMAC_SHA1()
18
class AccountWidget(gtk.VBox):
19
"""AccountWidget: A widget that provides a user interface for configuring sina accounts in Gwibber
22
def __init__(self, account=None, dialog=None):
23
"""Creates the account pane for configuring Sina accounts"""
24
gtk.VBox.__init__( self, False, 20 )
25
self.ui = gtk.Builder()
26
self.ui.set_translation_domain ("gwibber")
27
self.ui.add_from_file (resources.get_ui_asset("gwibber-accounts-sina.ui"))
28
self.ui.connect_signals(self)
29
self.vbox_settings = self.ui.get_object("vbox_settings")
30
self.pack_start(self.vbox_settings, False, False)
33
self.account = account or {}
36
if self.account.has_key("id"):
38
value = gnomekeyring.find_items_sync(gnomekeyring.ITEM_GENERIC_SECRET, {"id": str("%s/%s" % (self.account["id"], "secret_token"))})[0].secret
39
except gnomekeyring.NoMatchError:
40
has_secret_key = False
43
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:
44
self.ui.get_object("hbox_sina_auth").hide()
45
self.ui.get_object("sina_auth_done_label").set_label(_("%s has been authorized by Sina") % self.account["username"])
46
self.ui.get_object("hbox_sina_auth_done").show()
48
self.ui.get_object("hbox_sina_auth_done").hide()
50
self.dialog.ui.get_object('vbox_create').hide()
52
self.ui.get_object("hbox_sina_auth_done").hide()
54
self.dialog.ui.get_object("vbox_create").hide()
57
def on_sina_auth_clicked(self, widget, data=None):
58
self.winsize = self.window.get_size()
60
web = webkit.WebView()
61
web.get_settings().set_property("enable-plugins", False)
62
web.load_html_string(_("<p>Please wait...</p>"), "file:///")
64
self.consumer = oauth.OAuthConsumer(*sina.utils.get_sina_keys())
66
request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, http_method="POST",
67
callback="http://gwibber.com/0/auth.html",
68
http_url="http://api.t.sina.com.cn/oauth/request_token")
70
request.sign_request(sigmeth, self.consumer, token=None)
72
tokendata = urllib2.urlopen(request.http_url, request.to_postdata()).read()
73
self.token = oauth.OAuthToken.from_string(tokendata)
75
url = "http://api.t.sina.com.cn/oauth/authorize?oauth_token=" + self.token.key
78
web.set_size_request(550, 400)
79
web.connect("title-changed", self.on_sina_auth_title_change)
81
self.scroll = gtk.ScrolledWindow()
84
self.pack_start(self.scroll, True, True, 0)
87
self.ui.get_object("vbox1").hide()
88
self.ui.get_object("vbox_advanced").hide()
89
self.dialog.infobar.set_message_type(gtk.MESSAGE_INFO)
91
def on_sina_auth_title_change(self, web=None, title=None, data=None):
93
if title.get_title() == "Success":
95
if hasattr(self.dialog, "infobar_content_area"):
96
for child in self.dialog.infobar_content_area.get_children(): child.destroy()
97
self.dialog.infobar_content_area = self.dialog.infobar.get_content_area()
98
self.dialog.infobar_content_area.show()
99
self.dialog.infobar.show()
101
message_label = gtk.Label(_("Verifying"))
102
message_label.set_use_markup(True)
103
message_label.set_ellipsize(pango.ELLIPSIZE_END)
104
self.dialog.infobar_content_area.add(message_label)
105
self.dialog.infobar.show_all()
107
url = web.get_main_frame().get_uri()
108
data = urlparse.parse_qs(url.split("?", 1)[1])
110
self.ui.get_object("vbox1").show()
111
self.ui.get_object("vbox_advanced").show()
113
token = data["oauth_token"][0]
114
verifier = data["oauth_verifier"][0]
116
request = oauth.OAuthRequest.from_consumer_and_token(
117
self.consumer, self.token, http_method="POST",
118
http_url="http://api.t.sina.com.cn/oauth/access_token",
119
parameters={"oauth_verifier": str(verifier)})
120
request.sign_request(sigmeth, self.consumer, self.token)
122
tokendata = urllib2.urlopen(request.http_url, request.to_postdata()).read()
123
data = urlparse.parse_qs(tokendata)
125
atok = oauth.OAuthToken.from_string(tokendata)
127
self.account["access_token"] = data["oauth_token"][0]
128
self.account["secret_token"] = data["oauth_token_secret"][0]
130
apireq = oauth.OAuthRequest.from_consumer_and_token(
133
http_url="http://api.t.sina.com.cn/account/verify_credentials.json", parameters=None)
135
apireq.sign_request(sigmeth, self.consumer, atok)
137
account_data = json.loads(urllib2.urlopen(apireq.to_url()).read())
139
self.account["username"] = account_data["screen_name"]
140
self.account["user_id"] = account_data["id"]
142
if isinstance(account_data, dict):
143
if account_data.has_key("id"):
144
saved = self.dialog.on_edit_account_save()
147
self.dialog.infobar.set_message_type(gtk.MESSAGE_ERROR)
148
message_label.set_text(_("Authorization failed. Please try again."))
151
self.dialog.infobar.set_message_type(gtk.MESSAGE_ERROR)
152
message_label.set_text(_("Authorization failed. Please try again."))
155
message_label.set_text(_("Successful"))
156
self.dialog.infobar.set_message_type(gtk.MESSAGE_INFO)
157
#self.dialog.infobar.hide()
159
self.ui.get_object("hbox_sina_auth").hide()
160
self.ui.get_object("sina_auth_done_label").set_label(_("%s has been authorized by Sina") % str(self.account["username"]))
161
self.ui.get_object("hbox_sina_auth_done").show()
162
if self.dialog.ui and self.account.has_key("id") and not saved:
163
self.dialog.ui.get_object("vbox_save").show()
164
elif self.dialog.ui and not saved:
165
self.dialog.ui.get_object("vbox_create").show()
167
self.window.resize(*self.winsize)
169
if title.get_title() == "Failure":
171
self.dialog.infobar.set_message_type(gtk.MESSAGE_ERROR)
172
message_label.set_text(_("Authorization failed. Please try again."))
173
self.dialog.infobar.show_all()
175
self.ui.get_object("vbox1").show()
176
self.ui.get_object("vbox_advanced").show()
177
self.window.resize(*self.winsize)