2
# Copyright (C) 2010 Canonical Ltd
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.
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.
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/>.
16
# Copyright (C) 2010 Ken VanDine <ken.vandine@canonical.com>
18
# facebook widgets for Gwibber
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
32
from gwibber.microblog.util.custom import *
36
import json, urlparse, uuid
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')
46
APP_KEY = "71b85c6d8cb5bbb9f1a3f8bbdcdd4b05"
49
class AccountWidget(Gtk.VBox):
50
"""AccountWidget: A widget that provides a user interface for configuring facebook accounts in Gwibber
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)
64
self.account = account
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
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()
80
self.ui.get_object("hbox_facebook_auth_done").hide()
82
self.dialog.ui.get_object('vbox_create').hide()
84
self.ui.get_object("hbox_facebook_auth_done").hide()
86
self.dialog.ui.get_object("vbox_create").hide()
88
def console_message_cb (self, *args):
91
def on_facebook_auth_clicked(self, widget, data=None):
92
self.winsize = self.window.get_size()
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:///")
100
url = urllib.urlencode({
101
"client_id": FB_APP_KEY,
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",
107
web.load_uri("https://graph.facebook.com/oauth/authorize?" + url)
108
web.connect("title-changed", self.on_facebook_auth_title_change)
110
self.scroll = Gtk.ScrolledWindow()
113
self.scroll.set_size_request(450, 340)
115
self.pack_start(self.scroll, True, True, 0)
117
self.dialog.infobar.hide()
118
self.ui.get_object("vbox1").hide()
119
self.ui.get_object("vbox_advanced").hide()
121
def on_facebook_auth_title_change(self, web=None, title=None, data=None):
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()
128
url = web.get_main_frame().get_uri()
129
if title.get_title() == "Success":
131
self.account["access_token"] = str(urlparse.parse_qs(url.split("#", 1)[1])["access_token"][0])
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 ()
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()
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()
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()
167
message_label.set_text(_("Successful"))
168
self.dialog.infobar.set_message_type(Gtk.MessageType.INFO)
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()
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 ()
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()
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 ()