Attachment 'TwitterStock_Version2.py'
Download 1 import logging, dbus
2 import hippo
3 from bigboard.stock import Stock
4 import bigboard.accounts_dialog as accounts_dialog
5 import bigboard.libbig.gutil as gutil
6
7 _logger = logging.getLogger('bigboard.stocks.TwitterStock')
8
9 LOGIN_TO_TWITTER_STRING = "Login to Twitter"
10
11 TWITTER_KIND = "twitter"
12
13 class TwitterStock(Stock):
14
15 def __init__(self, *args, **kwargs):
16 Stock.__init__(self, *args, **kwargs)
17 self.__box = hippo.CanvasBox(orientation=hippo.ORIENTATION_VERTICAL)
18
19 self.__login_button = hippo.CanvasButton(text=LOGIN_TO_TWITTER_STRING)
20 self.__login_button.connect('activated', lambda button: self.__open_login_dialog())
21 self.__box.append(self.__login_button)
22
23 # even though the account system can theoretically return multiple Twitter accounts, this stock
24 # only supports one -- the last one that was added
25 self.__twitter_account = None
26 self.__twitter_account_changed_signal_match = None
27
28 try:
29 onlineaccounts_proxy = dbus.SessionBus().get_object('org.gnome.OnlineAccounts', '/onlineaccounts')
30 except dbus.DBusException, e:
31 _logger.error("onlineaccounts DBus service not available, can't get twitter accounts")
32 return
33
34 all_twitter_account_paths = onlineaccounts_proxy.GetEnabledAccountsWithKinds([TWITTER_KIND])
35 for a_path in all_twitter_account_paths:
36 self.__on_account_added(a_path)
37
38 self.__connections = gutil.DisconnectSet()
39
40 id = onlineaccounts_proxy.connect_to_signal('AccountEnabled', self.__on_account_added)
41 self.__connections.add(onlineaccounts_proxy, id)
42 id = onlineaccounts_proxy.connect_to_signal('AccountDisabled', self.__on_account_removed)
43 self.__connections.add(onlineaccounts_proxy, id)
44
45 def _on_delisted(self):
46 if self.__twitter_account:
47 self.__on_account_removed(self.__twitter_account.GetObjectPath())
48 self.__connections.disconnect_all()
49
50 def get_content(self, size):
51 return self.__box
52
53 def __on_account_added(self, acct_path):
54 try:
55 _logger.debug("acct_path is %s" % acct_path)
56 acct = dbus.SessionBus().get_object('org.gnome.OnlineAccounts', acct_path)
57 except dbus.DBusException, e:
58 _logger.error("onlineaccount for path %s was not found" % acct_path)
59 return
60
61 if acct.GetKind() != TWITTER_KIND:
62 return
63
64 _logger.debug("in __on_account_added for %s %s" % (str(acct), acct.GetUsername()))
65
66 if self.__twitter_account:
67 self.__on_account_removed(self.__twitter_account.GetObjectPath())
68
69 self.__twitter_account = acct
70 self.__twitter_account_changed_signal_match = \
71 self.__twitter_account.connect_to_signal('Changed', self.__on_twitter_account_changed)
72 self.__on_twitter_account_changed()
73
74 def __on_account_removed(self, acct_path):
75 _logger.debug("in __on_account_removed")
76 if self.__twitter_account and self.__twitter_account.GetObjectPath() == acct_path:
77 _logger.debug("in __on_account_removed for %s", acct_path)
78 self.__twitter_account_changed_signal_match.remove()
79 self.__twitter_account_changed_signal_match = None
80 self.__twitter_account = None
81 self.__box.remove_all()
82 self.__box.append(self.__login_button)
83
84 def __on_twitter_account_changed(self):
85 _logger.debug("will change stuff")
86 self.__box.remove_all()
87 hello_message = hippo.CanvasText(text="Hello Twitter User " + self.__twitter_account.GetUsername() , \
88 size_mode=hippo.CANVAS_SIZE_WRAP_WORD)
89 self.__box.append(hello_message)
90 if self.__twitter_account.GetPassword() == "":
91 self.__box.append(self.__login_button)
92
93 def __open_login_dialog(self):
94 accounts_dialog.open_dialog(["twitter"])
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.