From cc99e36ed3ed4d52d88974b9df3f6eca91e887c4 Mon Sep 17 00:00:00 2001 From: Anna Date: Tue, 18 May 2010 17:41:32 -0400 Subject: [PATCH] Made posting an update a threaded action --- apithreads.py | 27 +++++++++++++++++++++++++++ mytwitter.py | 28 ++++++++++++++++------------ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/apithreads.py b/apithreads.py index fbe49d9..66c7492 100644 --- a/apithreads.py +++ b/apithreads.py @@ -260,6 +260,29 @@ class GetUserLists(ApiThread): +class PostUpdate(ApiThread): + def __init__(self, api, update, reply_id): + ApiThread.__init__(self, api) + self.sig_proxy = SigProxy() + + self.update = update + self.reply_id = reply_id + + + def run(self): + try: + with self.api.lock: + self.api.PostUpdate(self.update, in_reply_to_status_id=self.reply_id) + success = True + except (HTTPError, URLError): + success = False + + self.sig_proxy.emit('update-posted', success) + +### End class PostUpdate + + + class SigProxy(gtk.Alignment): """ This little class exists just so that we can have a gtk class in our @@ -276,6 +299,10 @@ gobject.signal_new("lists-ready", SigProxy, gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,gobject.TYPE_PYOBJECT)) +gobject.signal_new("update-posted", SigProxy, + gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)) + # We use these classes to emulate a Status object when we need # one to be built out of something else. diff --git a/mytwitter.py b/mytwitter.py index 4191d7e..d4420bb 100755 --- a/mytwitter.py +++ b/mytwitter.py @@ -182,19 +182,13 @@ class MyTwitter(): def update_status(self): reply_id = self.reply_id text = self.update_entry.get_text() - self.update_entry.set_text("") - with self.api.lock: - try: - self.api.PostUpdate(text, in_reply_to_status_id=reply_id) - except HTTPError,URLError: - self.update_status_bar('Failed to post tweet') - self.reply_id = None - return + thread = apithreads.PostUpdate(self.api, text, reply_id) + thread.sig_proxy.connect('update-posted', self.on_update_posted) + self.update_entry.set_sensitive(False) + self.update_status_bar('Posting...') + thread.start() - self.reply_id = None - self.update_status_bar('Tweet Posted') - def update_status_callback(self, widget): self.update_status() @@ -483,7 +477,17 @@ class MyTwitter(): def on_resize(self, widget, event): self.db['width'] = event.width self.db['height'] = event.height - + + + def on_update_posted(self, widget, success): + if success: + self.reply_id = None + self.update_entry.set_text("") + self.update_status_bar('Tweet Posted') + else: + self.update_status_bar('Failed to post tweet') + + self.update_entry.set_sensitive(True) ### end class MyTwitter