OSDN Git Service

Modified gtk threads lock
authorHirotaka Kawata <hktechno@hotmail.com>
Wed, 23 Jun 2010 19:15:27 +0000 (04:15 +0900)
committerHirotaka Kawata <hktechno@hotmail.com>
Wed, 23 Jun 2010 19:15:27 +0000 (04:15 +0900)
iconstore.py
main.py
timeline.py
twitterapi.py

index a8e2f64..98a75f3 100644 (file)
@@ -51,10 +51,10 @@ class NewIcon(threading.Thread):
         icoldr = gtk.gdk.PixbufLoader()
         icoldr.write(ico)
         icopix = icoldr.get_pixbuf()
-        try:
-            icoldr.close()
-        except:
-            icopix = None
+        
+        try: icoldr.close()
+        except: icopix = None
+        
         return icopix
     
     def run(self):
@@ -83,12 +83,12 @@ class NewIcon(threading.Thread):
         self.icons[self.user.id] = icopix
         
         # Icon Refresh
-        gtk.gdk.threads_enter()
         for store, n in self.stores:
             i = store.get_iter_first()
             while i:
                 uid = store.get_value(i, n)
                 if uid == self.user.id:
+                    gtk.gdk.threads_enter()
                     store.set_value(i, 0, icopix)
+                    gtk.gdk.threads_leave()
                 i = store.iter_next(i)
-        gtk.gdk.threads_leave()
diff --git a/main.py b/main.py
index 2770d9b..aa7d20e 100644 (file)
--- a/main.py
+++ b/main.py
@@ -31,6 +31,9 @@ class Main:
         gtk.gdk.threads_init()
         gobject.threads_init()
         
+        # Twitter class instance
+        self.twitter = twitterapi(keys, maxn)        
+        
         # GtkBuilder instance
         builder = gtk.Builder()
         self.builder = builder
@@ -45,12 +48,11 @@ class Main:
         
         # init icon store
         self.icons = IconStore(iconmode)
-
+        
         # set tools
         self.twtools = twittertools.TwitterTools()
         
-        init = threading.Thread(target=self.initialize, args=(keys, maxn))
-        init.start()
+        self.initialize(keys, maxn)
     
     def main(self):
         gtk.gdk.threads_enter()
@@ -65,32 +67,25 @@ class Main:
             print >>sys.stderr, "[Warning] Allocation not defined"        
 
         window.show_all()
-        
+
         # Start gtk main loop
         gtk.main()
         gtk.gdk.threads_leave()
     
-    # Initialize Twitter API and Tabs (in another thread)
+    # Initialize Tabs (in another thread)
     def initialize(self, keys, maxn):
-        gtk.gdk.threads_enter()
-        # Twitter class instance
-        self.twitter = twitterapi(keys, maxn)
-        
-        # Set statusbar (Show API Remaining)
-        self.label_apilimit = gtk.Label()
-        
-        sbar = self.builder.get_object("statusbar1")
-        sbar.pack_start(
-            self.label_apilimit, expand = False, padding = 10)
-        sbar.show_all()
-        
         # Set Status Views
         for i in (("Home", "home_timeline", 30),
                   ("Mentions", "mentions", 300)):
             # create new timeline and tab view
             self.new_timeline(*i)
-            # insert little delay
-            time.sleep(random.random())
+
+        # Set statusbar (Show API Remaining)
+        self.label_apilimit = gtk.Label()
+        sbar = self.builder.get_object("statusbar1")
+        sbar.pack_start(
+            self.label_apilimit, expand = False, padding = 10)
+        sbar.show_all()        
         
         # Users tab append
         users = UserSelection()
@@ -99,10 +94,9 @@ class Main:
         self.icons.add_store(users.store, 1)
         users.set_userdict(self.twitter.users, self.icons)
         self.new_tab(users, "Users")
-
+        
         notebook = self.builder.get_object("notebook1")        
         notebook.set_current_page(0)
-        gtk.gdk.threads_leave()
     
     # Window close event
     def close(self, widget):
@@ -185,7 +179,7 @@ class Main:
     
     
     ########################################
-    # Original Events    
+    # Original Events
     
     # status added event
     def on_status_added(self, i):
index e9d012a..e09ab0a 100644 (file)
@@ -12,6 +12,7 @@ import twittertools
 
 import time
 import webbrowser
+import threading
 
 class timeline:
     def __init__(self, api, icons, iconmode = True):
@@ -126,57 +127,33 @@ class timeline:
         it = self.store.get_iter(path[0])
         sid = self.store.get_value(it, 2)
         return self.twitter.statuses[sid]
-    
-    # Color status
-    def color_status(self, status = None):
-        myname = self.twitter.myname
-        myid = self.twitter.me.id if self.twitter.me != None else -1
+
+    # Replace & -> &amp;
+    def _replace_amp(self, string):
+        amp = string.find('&')
+        if amp == -1: return string
         
-        # if not set target status
-        if status == None:
-            status = self.get_selected_status()
+        entity_match = self.noent_amp.finditer(string)
         
-        i = self.store.get_iter_first()
-        while i:
-            bg = None   
-            
-            id = self.store.get_value(i, 2)
-            s = self.twitter.statuses[id]
-            u = s.user
-            
-            if u.id == myid:
-                # My status (Blue)
-                bg = "#CCCCFF"
-            elif s.in_reply_to_user_id == myid or \
-                    s.text.find("@%s" % myname) != -1:
-                # Reply to me (Red)
-                bg = "#FFCCCC"
-            
-            if status:
-                if s.id == status.in_reply_to_status_id:
-                    # Reply to (Orange)
-                    bg = "#FFCC99"
-                elif u.id == status.in_reply_to_user_id:
-                    # Reply to other (Yellow)
-                    bg = "#FFFFCC"
-                elif u.id == status.user.id:
-                    # Selected user (Green)
-                    bg = "#CCFFCC"
-            
-            self.store.set_value(i, 4, bg)
-            i = self.store.iter_next(i)
+        for i, e in enumerate(entity_match):
+            string = "%s&amp;%s" % (
+                string[:e.start() + (4 * i)],
+                string[e.start() + (4 * i) + 1:])
+        
+        return string    
     
+    ########################################
+    # Execute in Background Thread Methods
+
     # Prepend new statuses
     def prepend_new_statuses(self, new_ids):
         # Auto scroll lock if adjustment changed manually
         vadj = self.scrwin.get_vadjustment()
         self.vadj_lock = True if vadj.value != 0.0 else False
         
-        gtk.gdk.threads_enter()
         # Insert New Status
         for i in new_ids:
             self.add_status(i)
-        gtk.gdk.threads_leave()
         
         self.color_status()
     
@@ -190,7 +167,7 @@ class timeline:
         text = self._replace_amp(text)
         
         if status.user.id in self.twitter.followers:
-            # Bold screen_name if follwer
+            # Bold screen_name if follower
             tmpl = "<b>%s</b>\n%s"
         else:
             # or gray
@@ -201,28 +178,63 @@ class timeline:
             status.user.screen_name, text)
         
         # New Status Prepend to Liststore (Add row)
+        gtk.gdk.threads_enter()
+        
         self.store.prepend(
             (self.icons.get(status.user),
              message,
              long(status.id), long(status.user.id),
              background))
-        
         self.on_status_added(i)
-    
-    # Replace & -> &amp;
-    def _replace_amp(self, string):
-        amp = string.find('&')
-        if amp == -1: return string
         
-        entity_match = self.noent_amp.finditer(string)
+        gtk.gdk.threads_leave()
+    
+    # Color status
+    def color_status(self, status = None):
+        t = threading.Thread(target=self.color_status_in_thread, args=(status,))
+        t.start()
+    
+    def color_status_in_thread(self, status = None):
+        myname = self.twitter.myname
+        myid = self.twitter.me.id if self.twitter.me != None else -1
         
-        for i, e in enumerate(entity_match):
-            string = "%s&amp;%s" % (
-                string[:e.start() + (4 * i)],
-                string[e.start() + (4 * i) + 1:])
+        # if not set target status
+        if status == None:
+            status = self.get_selected_status()
         
-        return string
-
+        i = self.store.get_iter_first()
+        while i:
+            bg = None   
+            
+            id = self.store.get_value(i, 2)
+            s = self.twitter.statuses[id]
+            u = s.user
+            
+            if u.id == myid:
+                # My status (Blue)
+                bg = "#CCCCFF"
+            elif s.in_reply_to_user_id == myid or \
+                    s.text.find("@%s" % myname) != -1:
+                # Reply to me (Red)
+                bg = "#FFCCCC"
+            
+            if status:
+                if s.id == status.in_reply_to_status_id:
+                    # Reply to (Orange)
+                    bg = "#FFCC99"
+                elif u.id == status.in_reply_to_user_id:
+                    # Reply to other (Yellow)
+                    bg = "#FFFFCC"
+                elif u.id == status.user.id:
+                    # Selected user (Green)
+                    bg = "#CCFFCC"
+            
+            gtk.gdk.threads_enter()
+            self.store.set_value(i, 4, bg)
+            gtk.gdk.threads_leave()
+            
+            i = self.store.iter_next(i)
+    
     # dummy events and methods
     def on_status_added(self, *args, **kwargs): pass
     def on_status_selection_changed(self, *args, **kwargs): pass
index a0ad79f..616e759 100644 (file)
@@ -23,10 +23,17 @@ class twitterapi():
         
         self.maxn = maxn
         self.my_name = keys[-1]
-        
-        # Get followers
+
         self.followers = set()
+        self.following = set()
+
+        t = threading.Thread(target=self.get_following_followers)
+        t.start()
+    
+    def get_following_followers(self):
+        # Get followers
         self.followers.update([int(i) for i in self.api.followers_ids()])
+        self.following.update([int(i) for i in self.api.friends_ids()])
     
     def create_timeline(self, func, interval, args = (), kwargs = {}):
         # Add New Timeline Thread