in reply to text is now a clickable button that loads a single pane... still some formatting work to deal with there

This commit is contained in:
Anna 2010-04-13 17:32:04 -04:00
parent 290d553c3e
commit 46cf4d51f1

View File

@ -2,7 +2,11 @@
# #
# Custom twitter client... mostly for learning Python # Custom twitter client... mostly for learning Python
import sys, twitter, ConfigParser, os, datetime, dateutil.tz, gtk, gtk.glade, gobject, re, subprocess import sys, ConfigParser, os, re, subprocess
import datetime, dateutil.tz
import twitter
import gtk, gtk.glade, gobject
from urllib2 import HTTPError
class MyTwitter(): class MyTwitter():
@ -150,6 +154,10 @@ class MyTwitter():
self.api.PostRetweet(widget.id) self.api.PostRetweet(widget.id)
def on_reply_to(self, widget, data):
self.add_to_notebook(data['name'], data['id'])
def on_view_selected(self, event, name): def on_view_selected(self, event, name):
# If it already exists, don't add it, just switch to it # If it already exists, don't add it, just switch to it
for i in range(self.tweet_notebook.get_n_pages()): for i in range(self.tweet_notebook.get_n_pages()):
@ -178,7 +186,10 @@ class MyTwitter():
# This code modified from create_custom_tab in: # This code modified from create_custom_tab in:
# http://www.daa.com.au/pipermail/pygtk/2006-April/012216.html # http://www.daa.com.au/pipermail/pygtk/2006-April/012216.html
def add_to_notebook(self, name, single_tweet=None): def add_to_notebook(self, name, single_tweet=None):
new_pane = TweetPane(name, self, single_tweet) try:
new_pane = TweetPane(name, self, single_tweet)
except HTTPError:
return
self.tweet_notebook.append_page(new_pane, new_pane.get_tab_label()) self.tweet_notebook.append_page(new_pane, new_pane.get_tab_label())
new_pane.get_tab_label().connect('close_clicked', self.remove_view, name) new_pane.get_tab_label().connect('close_clicked', self.remove_view, name)
self.tweet_notebook.set_current_page(-1) # switch to the new pane self.tweet_notebook.set_current_page(-1) # switch to the new pane
@ -232,6 +243,10 @@ class TweetPane(gtk.ScrolledWindow):
self.tweets = [] self.tweets = []
self.num_entries = self.mytwitter.num_entries
if self.is_single_tweet():
self.num_entries = 1
self.init_widgets() self.init_widgets()
@ -242,14 +257,13 @@ class TweetPane(gtk.ScrolledWindow):
viewport = gtk.Viewport() viewport = gtk.Viewport()
# Build us some labels... # Build us some labels...
num_entries = self.mytwitter.num_entries for i in range(0, self.num_entries):
if self.is_single_tweet():
num_entries = 1
for i in range(0, num_entries):
self.tweets.append(TweetBox()) self.tweets.append(TweetBox())
tweet_box.pack_start(self.tweets[i]) tweet_box.pack_start(self.tweets[i])
self.tweets[i].connect('reply', self.mytwitter.on_reply) self.tweets[i].connect('reply', self.mytwitter.on_reply)
self.tweets[i].connect('retweet', self.mytwitter.on_retweet) self.tweets[i].connect('retweet', self.mytwitter.on_retweet)
self.tweets[i].connect('in-reply-to', self.mytwitter.on_reply_to)
viewport.add(tweet_box) viewport.add(tweet_box)
@ -284,7 +298,7 @@ class TweetPane(gtk.ScrolledWindow):
# Keep count of the new tweets for posting a status message # Keep count of the new tweets for posting a status message
self.num_new_tweets = 0 self.num_new_tweets = 0
for i in range(0, self.mytwitter.num_entries): for i in range(0, self.num_entries):
read = True read = True
if i < len(statuses): if i < len(statuses):
if statuses[i].id > self.last_tweet_read: if statuses[i].id > self.last_tweet_read:
@ -370,6 +384,8 @@ class TweetBox(gtk.VBox):
self.screen_name = None self.screen_name = None
self.id = None self.id = None
self.in_reply_to_id = None
self.in_reply_to_screen_name = None
self.init_widgets() self.init_widgets()
@ -397,7 +413,7 @@ class TweetBox(gtk.VBox):
self.pack_start(self.text_eb) self.pack_start(self.text_eb)
# Set the text's properties # Set the text's properties
text_align.set_padding(2, 10, 3, 0) text_align.set_padding(2, 5, 10, 5)
self.text.set_alignment(0.0, 0.0) self.text.set_alignment(0.0, 0.0)
self.text.set_selectable(True) self.text.set_selectable(True)
self.text.set_line_wrap(True) self.text.set_line_wrap(True)
@ -406,17 +422,23 @@ class TweetBox(gtk.VBox):
self.text.connect('button-press-event', self.on_mouse_clicked) self.text.connect('button-press-event', self.on_mouse_clicked)
# Build the buttons # Build the buttons
button_box_align = gtk.Alignment()
button_box_align.set_padding(0, 15, 0, 0)
button_box = gtk.HBox() button_box = gtk.HBox()
self.pack_start(button_box) self.pack_start(button_box)
self.reply_to_button = gtk.Label("") self.reply_to_button = gtk.Button("")
self.reply_to_button.set_relief(gtk.RELIEF_NONE)
button_box.pack_start(self.reply_to_button, expand=False) button_box.pack_start(self.reply_to_button, expand=False)
self.reply_to_button.connect("clicked", self.on_in_reply_to_clicked)
reply_button = gtk.Button("Reply") reply_button = gtk.Button("Reply")
reply_button.set_relief(gtk.RELIEF_HALF)
button_box.pack_end(reply_button, expand=False) button_box.pack_end(reply_button, expand=False)
reply_button.connect("clicked", self.on_reply_clicked) reply_button.connect("clicked", self.on_reply_clicked)
retweet_button = gtk.Button("Retweet") retweet_button = gtk.Button("Retweet")
retweet_button.set_relief(gtk.RELIEF_HALF)
button_box.pack_end(retweet_button, expand=False) button_box.pack_end(retweet_button, expand=False)
retweet_button.connect("clicked", self.on_retweet_clicked) retweet_button.connect("clicked", self.on_retweet_clicked)
@ -433,6 +455,8 @@ class TweetBox(gtk.VBox):
# Get user's data for retweeting / replying # Get user's data for retweeting / replying
self.screen_name = user.screen_name self.screen_name = user.screen_name
self.id = status.id self.id = status.id
self.in_reply_to_id = status.in_reply_to_status_id
self.in_reply_to_screen_name = status.in_reply_to_screen_name
# ... and a formatted timestamp # ... and a formatted timestamp
timestamp = datetime.datetime.strptime(status.created_at, "%a %b %d %H:%M:%S +0000 %Y") timestamp = datetime.datetime.strptime(status.created_at, "%a %b %d %H:%M:%S +0000 %Y")
@ -450,8 +474,8 @@ class TweetBox(gtk.VBox):
self.text.set_markup(new_text) self.text.set_markup(new_text)
# If this is in reply to something, set appropriate label # If this is in reply to something, set appropriate label
if status.in_reply_to_screen_name: if self.in_reply_to_screen_name:
self.reply_to_button.set_label('in reply to ' + status.in_reply_to_screen_name) self.reply_to_button.set_label('in reply to ' + self.in_reply_to_screen_name)
@ -480,6 +504,10 @@ class TweetBox(gtk.VBox):
self.emit('retweet') self.emit('retweet')
def on_in_reply_to_clicked(self, widget):
self.emit('in-reply-to', {'id': self.in_reply_to_id, 'name': self.in_reply_to_screen_name})
def on_mouse_clicked(self, widget, event): def on_mouse_clicked(self, widget, event):
if event.button == 1: if event.button == 1:
self.set_read(True) self.set_read(True)
@ -585,6 +613,9 @@ gobject.signal_new("reply", TweetBox,
gobject.signal_new("retweet", TweetBox, gobject.signal_new("retweet", TweetBox,
gobject.SIGNAL_RUN_LAST, gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE, ()) gobject.TYPE_NONE, ())
gobject.signal_new("in-reply-to", TweetBox,
gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,))
my_twitter = MyTwitter() my_twitter = MyTwitter()
gtk.main() gtk.main()